Small rendering cleanup.

This commit is contained in:
jmsgrogan 2023-01-24 08:57:16 +00:00
parent d269cc8482
commit fc44c4c623
13 changed files with 171 additions and 37 deletions

View file

@ -25,9 +25,9 @@ else()
if (CMAKE_COMPILER_IS_GNUCC) if (CMAKE_COMPILER_IS_GNUCC)
message(STATUS "Enabling code coverage") message(STATUS "Enabling code coverage")
set(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage") set(GCC_COVERAGE_COMPILE_FLAGS "-fprofile-arcs -ftest-coverage")
set(GCC_COVERAGE_LINK_FLAGS "-lgcov") set(GCC_COVERAGE_LINK_FLAGS "-lgcov -fprofile-arcs")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}") set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
endif() endif()
endif() endif()
endif() endif()

View file

@ -20,3 +20,21 @@ std::vector<Path> Directory::getFilesWithExtension(const Path& path, const std::
} }
return paths; return paths;
} }
void Directory::createIfNotExisting(const Path& path)
{
Path working_path;
if (std::filesystem::is_directory(path))
{
working_path = path;
}
else
{
working_path = path.parent_path();
}
if (!std::filesystem::exists(working_path))
{
std::filesystem::create_directories(working_path);
}
}

View file

@ -8,6 +8,7 @@ using Path = std::filesystem::path;
class Directory class Directory
{ {
public: public:
static void createIfNotExisting(const Path& path);
static std::vector<Path> getFilesWithExtension(const Path& path, const std::string& extension, bool recursive=false); static std::vector<Path> getFilesWithExtension(const Path& path, const std::string& extension, bool recursive=false);
}; };

View file

@ -4,6 +4,7 @@
#include "Win32WicInterface.h" #include "Win32WicInterface.h"
#include "Win32WicImage.h" #include "Win32WicImage.h"
#include "StringUtils.h" #include "StringUtils.h"
#include "Directory.h"
#include <wincodec.h> #include <wincodec.h>
#include <wrl.h> #include <wrl.h>
@ -16,6 +17,8 @@ Win32WicImageWriter::Win32WicImageWriter(ImgFormat format)
void Win32WicImageWriter::write(const Path& path, Image* image) void Win32WicImageWriter::write(const Path& path, Image* image)
{ {
Directory::createIfNotExisting(path);
Win32WicInterface wic_interface; Win32WicInterface wic_interface;
Microsoft::WRL::ComPtr<IWICStream> pStream; Microsoft::WRL::ComPtr<IWICStream> pStream;

View file

@ -57,6 +57,10 @@ void DirectX2dPainter::paint(SceneModel* model)
{ {
paintPath(model); paintPath(model);
} }
else if (model->getGeometry()->getType() == AbstractGeometricItem::Type::LINE)
{
paintLine(model);
}
} }
void DirectX2dPainter::paintRect(SceneModel* model) void DirectX2dPainter::paintRect(SceneModel* model)
@ -107,8 +111,6 @@ void DirectX2dPainter::paintRect(SceneModel* model)
void DirectX2dPainter::paintCircle(SceneModel* model) void DirectX2dPainter::paintCircle(SceneModel* model)
{ {
auto rt = mD2dInterface->getRenderTarget();
const auto loc = model->getTransform().getLocation(); const auto loc = model->getTransform().getLocation();
const auto scale_x = model->getTransform().getScaleX(); const auto scale_x = model->getTransform().getScaleX();
const auto scale_y = model->getTransform().getScaleY(); const auto scale_y = model->getTransform().getScaleY();
@ -120,6 +122,7 @@ void DirectX2dPainter::paintCircle(SceneModel* model)
D2D1_POINT_2F d2d_centre{ static_cast<float>(loc.getX()), static_cast<float>(loc.getY()) }; D2D1_POINT_2F d2d_centre{ static_cast<float>(loc.getX()), static_cast<float>(loc.getY()) };
D2D1_ELLIPSE ellipse{ d2d_centre, static_cast<float>(radius), static_cast<float>(radiusy) }; D2D1_ELLIPSE ellipse{ d2d_centre, static_cast<float>(radius), static_cast<float>(radiusy) };
auto rt = mD2dInterface->getRenderTarget();
if (model->hasFillColor()) if (model->hasFillColor())
{ {
mSolidBrush->SetColor(toD2dColor(model->getFillColor())); mSolidBrush->SetColor(toD2dColor(model->getFillColor()));
@ -137,6 +140,44 @@ D2D_POINT_2F DirectX2dPainter::toD2dPoint(const Point& point)
return D2D1::Point2F(static_cast<float>(point.getX()), static_cast<float>(point.getY())); return D2D1::Point2F(static_cast<float>(point.getX()), static_cast<float>(point.getY()));
} }
void DirectX2dPainter::paintLine(SceneModel* model)
{
Microsoft::WRL::ComPtr<ID2D1PathGeometry> path_geom;
mD2dInterface->getFactory()->CreatePathGeometry(&path_geom);
Microsoft::WRL::ComPtr<ID2D1GeometrySink> path_sink;
path_geom->Open(&path_sink);
auto line = dynamic_cast<Line*>(model->getGeometry());
path_sink->BeginFigure(toD2dPoint(line->getFirstPoint()), D2D1_FIGURE_BEGIN_FILLED);
onLine(line, path_sink.Get());
path_sink->EndFigure(D2D1_FIGURE_END_CLOSED);
path_sink->Close();
auto rt = mD2dInterface->getRenderTarget();
const auto loc = model->getTransform().getLocation();
D2D1_MATRIX_3X2_F translation = D2D1::Matrix3x2F::Translation(static_cast<float>(loc.getX()), static_cast<float>(loc.getY()));
rt->SetTransform(translation);
if (model->hasFillColor())
{
mSolidBrush->SetColor(toD2dColor(model->getFillColor()));
rt->FillGeometry(path_geom.Get(), mSolidBrush.Get());
}
if (model->hasOutlineColor())
{
mSolidBrush->SetColor(toD2dColor(model->getOutlineColor()));
rt->DrawGeometry(path_geom.Get(), mSolidBrush.Get(), 1.0f);
}
rt->SetTransform(D2D1::Matrix3x2F::Identity());
}
void DirectX2dPainter::paintPath(SceneModel* model) void DirectX2dPainter::paintPath(SceneModel* model)
{ {
Microsoft::WRL::ComPtr<ID2D1PathGeometry> path_geom; Microsoft::WRL::ComPtr<ID2D1PathGeometry> path_geom;
@ -185,6 +226,11 @@ void DirectX2dPainter::paintPath(SceneModel* model)
path_sink->Close(); path_sink->Close();
auto rt = mD2dInterface->getRenderTarget(); auto rt = mD2dInterface->getRenderTarget();
const auto loc = model->getTransform().getLocation();
D2D1_MATRIX_3X2_F translation = D2D1::Matrix3x2F::Translation(static_cast<float>(loc.getX()), static_cast<float>(loc.getY()));
rt->SetTransform(translation);
if (model->hasFillColor()) if (model->hasFillColor())
{ {
mSolidBrush->SetColor(toD2dColor(model->getFillColor())); mSolidBrush->SetColor(toD2dColor(model->getFillColor()));
@ -195,6 +241,8 @@ void DirectX2dPainter::paintPath(SceneModel* model)
mSolidBrush->SetColor(toD2dColor(model->getOutlineColor())); mSolidBrush->SetColor(toD2dColor(model->getOutlineColor()));
rt->DrawGeometry(path_geom.Get(), mSolidBrush.Get(), 1.0f); rt->DrawGeometry(path_geom.Get(), mSolidBrush.Get(), 1.0f);
} }
rt->SetTransform(D2D1::Matrix3x2F::Identity());
} }
void DirectX2dPainter::onArc(Curve* element, ID2D1GeometrySink* sink) void DirectX2dPainter::onArc(Curve* element, ID2D1GeometrySink* sink)

View file

@ -50,6 +50,8 @@ private:
void paintPath(SceneModel* model); void paintPath(SceneModel* model);
void paintLine(SceneModel* model);
static D2D1::ColorF toD2dColor(const Color& color); static D2D1::ColorF toD2dColor(const Color& color);
static D2D_POINT_2F toD2dPoint(const Point& point); static D2D_POINT_2F toD2dPoint(const Point& point);

View file

@ -50,6 +50,9 @@ list(APPEND visual_elements_LIB_INCLUDES
nodes/GeometryNode.cpp nodes/GeometryNode.cpp
nodes/AbstractVisualNode.h nodes/AbstractVisualNode.h
nodes/AbstractVisualNode.cpp nodes/AbstractVisualNode.cpp
Material.h
BasicMaterial.h
BasicMaterial.cpp
Texture.cpp Texture.cpp
) )

View file

View file

@ -1,6 +1,7 @@
add_subdirectory(test_utils) add_subdirectory(test_utils)
add_subdirectory(geometry) add_subdirectory(geometry)
add_subdirectory(graphics)
add_subdirectory(ui_controls) add_subdirectory(ui_controls)
file(COPY data/ DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_data) file(COPY data/ DESTINATION ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_data)
@ -12,7 +13,6 @@ set(TEST_MODULES
core core
database database
fonts fonts
graphics
image image
ipc ipc
network network

View file

@ -1,38 +1,40 @@
set(MODULE_NAME graphics)
set(PLATFORM_UNIT_TEST_FILES) set(PLATFORM_UNIT_TEST_FILES)
set(OpenGL_GL_PREFERENCE "GLVND") set(OpenGL_GL_PREFERENCE "GLVND")
find_package(OpenGL QUIET) find_package(OpenGL QUIET)
if (OpenGL_FOUND) if (OpenGL_FOUND)
set(PLATFORM_UNIT_TEST_FILES set(PLATFORM_UNIT_TEST_FILES
graphics/TestOpenGlRendering.cpp TestOpenGlRendering.cpp
) )
endif() endif()
if(WIN32) if(WIN32)
list(APPEND PLATFORM_UNIT_TEST_FILES
list(APPEND PLATFORM_UNIT_TEST_FILES TestD2DOffScreenRendering.cpp
graphics/TestD2DOffScreenRendering.cpp )
) set(UI_TEST_FILES
set(GRAPHICS_UI_TEST_FILES TestDirectXRendering.cpp
graphics/TestDirectXRendering.cpp TestD2DRendering.cpp
graphics/TestD2DRendering.cpp )
PARENT_SCOPE
)
endif() endif()
set(GRAPHICS_UNIT_TEST_DEPENDENCIES set(UNIT_TEST_FILES
graphics client TestRasterizer.cpp
PARENT_SCOPE
)
set(GRAPHICS_UI_TEST_DEPENDENCIES
graphics client
PARENT_SCOPE
)
set(GRAPHICS_UNIT_TEST_FILES
graphics/TestRasterizer.cpp
${PLATFORM_UNIT_TEST_FILES} ${PLATFORM_UNIT_TEST_FILES}
PARENT_SCOPE )
)
set(UNIT_TEST_TARGET_NAME ${MODULE_NAME}_unit_tests)
add_executable(${UNIT_TEST_TARGET_NAME} ${CMAKE_SOURCE_DIR}/test/test_runner.cpp ${UNIT_TEST_FILES})
target_link_libraries(${UNIT_TEST_TARGET_NAME} PUBLIC test_utils graphics client)
set_property(TARGET ${UNIT_TEST_TARGET_NAME} PROPERTY FOLDER test/${MODULE_NAME})
set(UI_TEST_TARGET_NAME ${MODULE_NAME}_ui_tests)
add_executable(${UI_TEST_TARGET_NAME} WIN32 ${CMAKE_SOURCE_DIR}/test/ui_test_runner.cpp ${UI_TEST_FILES})
target_link_libraries(${UI_TEST_TARGET_NAME} PUBLIC test_utils graphics client)
set_property(TARGET ${UI_TEST_TARGET_NAME} PROPERTY FOLDER test/${MODULE_NAME})

View file

@ -3,17 +3,74 @@
#include "TestRenderUtils.h" #include "TestRenderUtils.h"
#include "RectangleNode.h" #include "RectangleNode.h"
#include "CircleNode.h"
#include "LineNode.h"
#include "PathNode.h"
void addRect(const Point& loc, std::vector<std::unique_ptr<AbstractVisualNode> >& nodes, double radius = 0.0)
{
auto node = std::make_unique<RectangleNode>(loc, 150.0, 100.0);
node->setRadius(radius);
nodes.push_back(std::move(node));
}
void addCircle(const Point& loc, std::vector<std::unique_ptr<AbstractVisualNode> >& nodes, double minorRadius = 0.0)
{
const auto radius = 50.0;
auto centre_loc = loc;
centre_loc.move(0.0, radius);
auto node = std::make_unique<CircleNode>(centre_loc, radius);
if (minorRadius != 0.0)
{
node->setMinorRadius(minorRadius);
}
nodes.push_back(std::move(node));
}
void addLine(const Point& loc, std::vector<std::unique_ptr<AbstractVisualNode> >& nodes)
{
std::vector<Point> points = { Point(150.0, 100.0) };
auto node = std::make_unique<LineNode>(loc, points);
nodes.push_back(std::move(node));
}
void addPath(const Point& loc, const std::string& path, std::vector<std::unique_ptr<AbstractVisualNode> >& nodes)
{
auto node = std::make_unique<PathNode>(loc, path);
nodes.push_back(std::move(node));
}
TEST_CASE(TestD2dOffScreenRendering, "graphics") TEST_CASE(TestD2dOffScreenRendering, "graphics")
{ {
TestRenderer renderer(800, 800); TestRenderer renderer(800, 800);
auto rect = std::make_unique<RectangleNode>(Point(10, 10), 200.0, 200.0); std::vector<std::unique_ptr<AbstractVisualNode> > nodes;
auto loc = Point(10, 10);
addRect(loc, nodes);
loc.move(250, 0);
addCircle(loc, nodes);
loc.move(100, 0);
addLine(loc, nodes);
loc = Point(10, 150);
addRect(loc, nodes, 10.0);
loc.move(250, 0);
addCircle(loc, nodes, 75.0);
loc.move(100, 0);
addPath(loc, "M0 0 h150 v100 h-150Z", nodes);
auto scene = renderer.getScene(); auto scene = renderer.getScene();
//scene->setBackgroundColor(Color(100, 100, 0)); for (const auto& node : nodes)
scene->addNode(rect.get()); {
scene->addNode(node.get());
}
renderer.write(TestUtils::getTestOutputDir(__FILE__) / "out.png"); renderer.write(TestUtils::getTestOutputDir(__FILE__) / "out.png");
}; };