添加ElementRenderer的单元测试

dev-VirtualTexture
wuyize 2023-02-08 16:34:16 +08:00
parent b562ff5053
commit ed3f3fc74a
12 changed files with 135 additions and 16 deletions

View File

@ -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;

View File

@ -392,7 +392,7 @@ GLuint Renderer::Model::loadPainting(std::string path)
vector<std::shared_ptr<ElementStyle>> style = {
std::make_shared<ElementStyleFillDemo>(),
std::make_shared<ElementStyleStrokeDemo>()
std::make_shared<ElementStyleStrokeDemo>(0.02)
};
vector<std::shared_ptr<Element>> element = {

View File

@ -1,3 +1,13 @@
#include "BaseStyle.h"
using namespace Renderer;
using namespace Renderer;
bool Renderer::Material::operator==(const Material& m) const
{
return color == m.color && metallic == m.metallic && roughness == m.roughness;
}
std::pair<glm::vec4, glm::vec2> Renderer::Material::toVec() const
{
return { glm::vec4(color.redF(), color.greenF(), color.blueF(), color.alphaF()), glm::vec2(metallic, roughness)};
}

View File

@ -30,4 +30,13 @@ namespace Renderer
std::shared_ptr<TransformStyle> transform;
std::shared_ptr<MaterialStyle> material;
};
struct Material
{
QColor color;
float metallic;
float roughness;
bool operator==(const Material&) const;
std::pair<glm::vec4, glm::vec2> toVec() const;
};
}

View File

@ -10,8 +10,13 @@ std::vector<BaseStyle> Renderer::ElementStyleFillDemo::toBaseStyles() const
std::make_shared<MaterialStyleFill>(std::make_shared<FillPlain>(QColor(0, 255, 0), 0, 0.8))) };
}
Renderer::ElementStyleStrokeDemo::ElementStyleStrokeDemo(float width)
: width(width)
{
}
std::vector<BaseStyle> Renderer::ElementStyleStrokeDemo::toBaseStyles() const
{
return { BaseStyle(std::make_shared<TransformStyle>(),
std::make_shared<MaterialStyleStroke>(0.02, StrokeType::kBothSides, StrokeEndType::kRound, std::make_shared<StrokePlain>(QColor(0, 0, 255), 0, 0.8))) };
std::make_shared<MaterialStyleStroke>(width, StrokeType::kLeftSide, StrokeEndType::kRound, std::make_shared<StrokePlain>(QColor(0, 0, 255), 0, 0.8))) };
}

View File

@ -26,6 +26,9 @@ namespace Renderer
class ElementStyleStrokeDemo : public ElementStyle
{
public:
ElementStyleStrokeDemo(float width);
virtual std::vector<BaseStyle> toBaseStyles() const override;
protected:
float width;
};
}

View File

@ -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<GLfloat> 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<const StrokePlain&>(m).color
&& metallic == static_cast<const StrokePlain&>(m).metallic
&& roughness == static_cast<const StrokePlain&>(m).roughness;
&& material == static_cast<const StrokePlain&>(m).material;
}
Renderer::MaterialStyleStroke::MaterialStyleStroke(float width, StrokeType strokeType, StrokeEndType endType, std::shared_ptr<MaterialStroke> materialStroke)
@ -40,6 +38,10 @@ std::vector<GLfloat> Renderer::MaterialStyleStroke::encoded() const
{
std::vector<GLfloat> 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;
}

View File

@ -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<GLfloat> 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<GLfloat> 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
{

View File

@ -0,0 +1,26 @@
#include "CppUnitTest.h"
#include <QGuiApplication>
#include <QtWidgets/QApplication>
#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();
}
};
}

View File

@ -0,0 +1,36 @@
#pragma once
#include <QOpenGLFunctions>
#include <QOpenGLWidget>
#include <QPainter>
#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 {};
};
}

View File

@ -105,6 +105,12 @@
</ItemDefinitionGroup>
<ItemGroup>
<QtUic Include="UnitTest.ui" />
<ClCompile Include="ElementRendererTest.cpp">
<DynamicSource Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">input</DynamicSource>
<QtMocFileName Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).moc</QtMocFileName>
<DynamicSource Condition="'$(Configuration)|$(Platform)'=='Release|x64'">input</DynamicSource>
<QtMocFileName Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).moc</QtMocFileName>
</ClCompile>
<ClCompile Include="UnitTest.cpp" />
</ItemGroup>
<ItemGroup>
@ -116,6 +122,9 @@
<Value>$(QtDllPath)</Value>
</T4ParameterValues>
</ItemGroup>
<ItemGroup>
<QtMoc Include="ElementRendererTest.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">
<Import Project="$(QtMsBuild)\qt.targets" />

View File

@ -33,4 +33,14 @@
<ItemGroup>
<None Include="Qt.runsettings.tt" />
</ItemGroup>
<ItemGroup>
<QtMoc Include="ElementRendererTest.h">
<Filter>Header Files</Filter>
</QtMoc>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ElementRendererTest.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>