From ed3f3fc74a070f70df8666bc695c1970b2cf14c8 Mon Sep 17 00:00:00 2001 From: wuyize Date: Wed, 8 Feb 2023 16:34:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0ElementRenderer=E7=9A=84?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Shaders/painting.comp | 5 +-- .../src/Renderer/Model.cpp | 2 +- .../src/Renderer/Painting/BaseStyle.cpp | 12 ++++++- .../src/Renderer/Painting/BaseStyle.h | 9 +++++ .../src/Renderer/Painting/ElementStyle.cpp | 7 +++- .../src/Renderer/Painting/ElementStyle.h | 3 ++ .../Renderer/Painting/MaterialStyleStroke.cpp | 14 ++++---- .../Renderer/Painting/MaterialStyleStroke.h | 18 +++++++--- UnitTest/ElementRendererTest.cpp | 26 ++++++++++++++ UnitTest/ElementRendererTest.h | 36 +++++++++++++++++++ UnitTest/UnitTest.vcxproj | 9 +++++ UnitTest/UnitTest.vcxproj.filters | 10 ++++++ 12 files changed, 135 insertions(+), 16 deletions(-) create mode 100644 UnitTest/ElementRendererTest.cpp create mode 100644 UnitTest/ElementRendererTest.h diff --git a/ArchitectureColoredPainting/Shaders/painting.comp b/ArchitectureColoredPainting/Shaders/painting.comp index 46b31cc..896d544 100644 --- a/ArchitectureColoredPainting/Shaders/painting.comp +++ b/ArchitectureColoredPainting/Shaders/painting.comp @@ -1034,8 +1034,9 @@ bool drawElement(uint elementIndex, vec2 localUV, out vec3 color, out vec2 metal uint contourIndex = linesOffset + leftChild - 0x80000000; float minDistance = 1e38; uint lineCount = elementIndexs[contourIndex]; - //float lineType = elementData[styleIndex+4]; - float lineType = 2; + vec4 styleHead = unpackUnorm4x8(floatBitsToUint(elementData[styleIndex+1])); + float lineType = floor(styleHead.b*10); + //float lineType = 2; vec2 p3Last = vec2(1e38); vec2 p2Last = vec2(1e38); int debugBegin = 0; diff --git a/ArchitectureColoredPainting/src/Renderer/Model.cpp b/ArchitectureColoredPainting/src/Renderer/Model.cpp index b33f599..2dde17f 100644 --- a/ArchitectureColoredPainting/src/Renderer/Model.cpp +++ b/ArchitectureColoredPainting/src/Renderer/Model.cpp @@ -392,7 +392,7 @@ GLuint Renderer::Model::loadPainting(std::string path) vector> style = { std::make_shared(), - std::make_shared() + std::make_shared(0.02) }; vector> element = { diff --git a/ArchitectureColoredPainting/src/Renderer/Painting/BaseStyle.cpp b/ArchitectureColoredPainting/src/Renderer/Painting/BaseStyle.cpp index f690a9c..f1b2088 100644 --- a/ArchitectureColoredPainting/src/Renderer/Painting/BaseStyle.cpp +++ b/ArchitectureColoredPainting/src/Renderer/Painting/BaseStyle.cpp @@ -1,3 +1,13 @@ #include "BaseStyle.h" -using namespace Renderer; \ No newline at end of file +using namespace Renderer; + +bool Renderer::Material::operator==(const Material& m) const +{ + return color == m.color && metallic == m.metallic && roughness == m.roughness; +} + +std::pair Renderer::Material::toVec() const +{ + return { glm::vec4(color.redF(), color.greenF(), color.blueF(), color.alphaF()), glm::vec2(metallic, roughness)}; +} diff --git a/ArchitectureColoredPainting/src/Renderer/Painting/BaseStyle.h b/ArchitectureColoredPainting/src/Renderer/Painting/BaseStyle.h index b02a657..e478faf 100644 --- a/ArchitectureColoredPainting/src/Renderer/Painting/BaseStyle.h +++ b/ArchitectureColoredPainting/src/Renderer/Painting/BaseStyle.h @@ -30,4 +30,13 @@ namespace Renderer std::shared_ptr transform; std::shared_ptr material; }; + + struct Material + { + QColor color; + float metallic; + float roughness; + bool operator==(const Material&) const; + std::pair toVec() const; + }; } diff --git a/ArchitectureColoredPainting/src/Renderer/Painting/ElementStyle.cpp b/ArchitectureColoredPainting/src/Renderer/Painting/ElementStyle.cpp index e65ce5f..4cafdcd 100644 --- a/ArchitectureColoredPainting/src/Renderer/Painting/ElementStyle.cpp +++ b/ArchitectureColoredPainting/src/Renderer/Painting/ElementStyle.cpp @@ -10,8 +10,13 @@ std::vector Renderer::ElementStyleFillDemo::toBaseStyles() const std::make_shared(std::make_shared(QColor(0, 255, 0), 0, 0.8))) }; } +Renderer::ElementStyleStrokeDemo::ElementStyleStrokeDemo(float width) + : width(width) +{ +} + std::vector Renderer::ElementStyleStrokeDemo::toBaseStyles() const { return { BaseStyle(std::make_shared(), - std::make_shared(0.02, StrokeType::kBothSides, StrokeEndType::kRound, std::make_shared(QColor(0, 0, 255), 0, 0.8))) }; + std::make_shared(width, StrokeType::kLeftSide, StrokeEndType::kRound, std::make_shared(QColor(0, 0, 255), 0, 0.8))) }; } diff --git a/ArchitectureColoredPainting/src/Renderer/Painting/ElementStyle.h b/ArchitectureColoredPainting/src/Renderer/Painting/ElementStyle.h index 53cb306..1cbcea9 100644 --- a/ArchitectureColoredPainting/src/Renderer/Painting/ElementStyle.h +++ b/ArchitectureColoredPainting/src/Renderer/Painting/ElementStyle.h @@ -26,6 +26,9 @@ namespace Renderer class ElementStyleStrokeDemo : public ElementStyle { public: + ElementStyleStrokeDemo(float width); virtual std::vector toBaseStyles() const override; + protected: + float width; }; } diff --git a/ArchitectureColoredPainting/src/Renderer/Painting/MaterialStyleStroke.cpp b/ArchitectureColoredPainting/src/Renderer/Painting/MaterialStyleStroke.cpp index 4948314..f9c9301 100644 --- a/ArchitectureColoredPainting/src/Renderer/Painting/MaterialStyleStroke.cpp +++ b/ArchitectureColoredPainting/src/Renderer/Painting/MaterialStyleStroke.cpp @@ -3,7 +3,7 @@ using namespace Renderer; Renderer::StrokePlain::StrokePlain(QColor color, float metallic, float roughness) - : color(color), metallic(metallic), roughness(roughness) + : material{ color,metallic,roughness } { } @@ -14,16 +14,14 @@ MaterialStrokeType Renderer::StrokePlain::type() const std::vector Renderer::StrokePlain::encoded() const { - return { glm::uintBitsToFloat(glm::packUnorm4x8(glm::vec4(metallic, roughness, 0, 0))), - glm::uintBitsToFloat(glm::packUnorm4x8(glm::vec4(color.redF(), color.greenF(), color.blueF(), color.alphaF()))) }; + return { glm::uintBitsToFloat(glm::packUnorm4x8(glm::vec4(material.toVec().second, 0.f, 0.f))), + glm::uintBitsToFloat(glm::packUnorm4x8(material.toVec().first)) }; } bool Renderer::StrokePlain::operator==(const MaterialStroke& m) const { return type() == m.type() - && color == static_cast(m).color - && metallic == static_cast(m).metallic - && roughness == static_cast(m).roughness; + && material == static_cast(m).material; } Renderer::MaterialStyleStroke::MaterialStyleStroke(float width, StrokeType strokeType, StrokeEndType endType, std::shared_ptr materialStroke) @@ -40,6 +38,10 @@ std::vector Renderer::MaterialStyleStroke::encoded() const { std::vector v = { width }; auto encoded = materialStroke->encoded(); + glm::vec4 head = glm::unpackUnorm4x8(glm::floatBitsToUint(encoded[0])); + head.b = (float)strokeType / 10. + (float)endType / 100.; + head.a = 1; /// ิคม๔ + encoded[0] = glm::uintBitsToFloat(glm::packUnorm4x8(head)); v.insert(v.end(), encoded.begin(), encoded.end()); return v; } diff --git a/ArchitectureColoredPainting/src/Renderer/Painting/MaterialStyleStroke.h b/ArchitectureColoredPainting/src/Renderer/Painting/MaterialStyleStroke.h index b9b8fbd..ef22b81 100644 --- a/ArchitectureColoredPainting/src/Renderer/Painting/MaterialStyleStroke.h +++ b/ArchitectureColoredPainting/src/Renderer/Painting/MaterialStyleStroke.h @@ -3,7 +3,7 @@ namespace Renderer { - enum class MaterialStrokeType { kPlain }; + enum class MaterialStrokeType { kPlain, kLinearGradient, kRadialGradient}; class MaterialStroke { @@ -21,14 +21,22 @@ namespace Renderer virtual std::vector encoded() const override; virtual bool operator==(const MaterialStroke&) const override; - QColor color; - float metallic; - float roughness; + Material material; }; + class StrokeRadialGradient : public MaterialStroke + { + public: + StrokeRadialGradient(QColor color, float metallic, float roughness); + virtual MaterialStrokeType type() const override; + virtual std::vector encoded() const override; + virtual bool operator==(const MaterialStroke&) const override; + + //std::map< + }; enum class StrokeType { kBothSides = 2, kLeftSide = 1, kRightSide = 0 }; - enum class StrokeEndType { kRound }; + enum class StrokeEndType { kRound = 0 }; class MaterialStyleStroke : public MaterialStyle { diff --git a/UnitTest/ElementRendererTest.cpp b/UnitTest/ElementRendererTest.cpp new file mode 100644 index 0000000..9b4f0f8 --- /dev/null +++ b/UnitTest/ElementRendererTest.cpp @@ -0,0 +1,26 @@ +#include "CppUnitTest.h" +#include +#include +#include "ElementRendererTest.h" + +using namespace Microsoft::VisualStudio::CppUnitTestFramework; + +namespace UnitTest +{ + TEST_CLASS(ElementRendererTest) + { + public: + TEST_METHOD(TestMethod1) + { + QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); + char arg[] = ""; + char* argv[] = { arg }; + int argc = 1; + QApplication a(argc, argv); + TestGLWidget w; + w.show(); + a.exec(); + } + }; +} diff --git a/UnitTest/ElementRendererTest.h b/UnitTest/ElementRendererTest.h new file mode 100644 index 0000000..1e95b8d --- /dev/null +++ b/UnitTest/ElementRendererTest.h @@ -0,0 +1,36 @@ +#pragma once +#include +#include +#include +#include "Renderer/Preview/ElementRenderer.h" +#include "Editor/ThirdPartyLib/qquick/qquicksvgparser_p.h" +#include "Renderer/Painting/MaterialStyleStroke.h" +#include "Renderer/Painting/ElementStyle.h" + +namespace UnitTest +{ + class TestGLWidget : public QOpenGLWidget, protected QOpenGLFunctions + { + Q_OBJECT + private: + Renderer::ElementRenderer renderer; + public: + TestGLWidget(QWidget* parent = nullptr) : QOpenGLWidget(parent), renderer(this) {}; + void initializeGL() override + { + initializeOpenGLFunctions(); + renderer.initialize(); + }; + void paintGL() override + { + glClearColor(1.0, 1.0, 1.0, 1.0); + glClear(GL_COLOR_BUFFER_BIT); + QPainterPath path; + QQuickSvgParser::parsePathDataFast("M100,100C-.5,100,0,100.5,0,0L40,.07C40,59.5,39.5,60,100,60Z", path); + auto [img, pos] = renderer.drawElement(path, Renderer::ElementStyleStrokeDemo(2), 1, false); + QPainter painter(this); + painter.drawImage(pos, img); + }; + void resizeGL(int w, int h) override {}; + }; +} \ No newline at end of file diff --git a/UnitTest/UnitTest.vcxproj b/UnitTest/UnitTest.vcxproj index b7ba805..2e19c30 100644 --- a/UnitTest/UnitTest.vcxproj +++ b/UnitTest/UnitTest.vcxproj @@ -105,6 +105,12 @@ + + input + %(Filename).moc + input + %(Filename).moc + @@ -116,6 +122,9 @@ $(QtDllPath) + + + diff --git a/UnitTest/UnitTest.vcxproj.filters b/UnitTest/UnitTest.vcxproj.filters index 3d88512..7460244 100644 --- a/UnitTest/UnitTest.vcxproj.filters +++ b/UnitTest/UnitTest.vcxproj.filters @@ -33,4 +33,14 @@ + + + Header Files + + + + + Source Files + + \ No newline at end of file