CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
SvgWriter.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Iterator.hpp"
11#include "MeshVertex.hpp"
12#include "PathVertex.hpp"
13#include "ShapeVertex.hpp"
14#include "Triangle.hpp"
16
20
21#include <algorithm>
22#include <map>
23#include <sstream>
24
26
28 class SvgWriter {
29 private:
30 class BaseElem {
31 public:
32 double z_;
33
35
36 BaseElem(double z, const Vector3& color);
37
38 virtual ~BaseElem();
39
40 // Writes this svg element to a stream.
41 virtual void stream(std::ostream&) const = 0;
42 };
43
44 class VertexElem : public BaseElem {
45 public:
47
48 VertexElem(const Vector3& p, const Vector3& color);
49
50 virtual void stream(std::ostream& os) const override;
51 };
52
53 class LineElem : public BaseElem {
54 public:
56
57 LineElem(const Vector3& p1, const Vector3& p2, const Vector3& color);
58
59 virtual void stream(std::ostream& os) const override;
60 };
61
62 class TriangleElem : public BaseElem {
63 public:
65
66 TriangleElem(const Vector3& p1, const Vector3& p2, const Vector3& p3, const Vector3& color);
67
68 virtual void stream(std::ostream& os) const override;
69 };
70
71 Vector3 project(const Vector3& p) const;
72
73 Vector3 normalToColor(const Vector3& normal) const;
74
75 Vector2i size_;
76
77 Matrix4 viewMatrix_;
78 Matrix4 projMatrix_;
81 Vector2i viewportSize_;
82
84
86
88
89 public:
92 SvgWriter(int width, int height);
93
95 void modelView(const Matrix4& matrix);
96
101 void perspective(double fovy, double aspect, double zNear, double zFar);
102
107 void ortho(double left, double right, double bottom, double top);
108
110 void viewport(int x, int y, int width, int height);
111
113 void cullface(bool cullface);
114
116 void writePoint(const Vector3& p, const Vector3& color = {0.0, 0.0, 0.0});
117
119 void writeLine(const Vector3& p1, const Vector3& p2, const Vector3& color = {0.0, 0.0, 0.0});
120
122 void writeTriangle(const Vector3& p1, const Vector3& p2, const Vector3& p3, const Vector3& color);
123
125 void writeTriangle(const Vector3& p1, const Vector3& p2, const Vector3& p3);
126
128 template<typename Shape> void writeShape(const Shape& shape, bool writeVertices = false, bool writeAxis = false) {
129 Vector<ShapeVertex> vertices{};
130 for(const auto& vertex : shape.vertices()) {
131 vertices.push_back(vertex);
132 }
133
134 for(auto e : shape.edges()) {
135 auto p1 = Vector3{vertices[e.vertices[0]].position, 0.0};
136 auto p2 = Vector3{vertices[e.vertices[1]].position, 0.0};
137
138 writeLine(p1, p2, {0.5, 0.5, 0.5});
139 }
140
141 if(writeAxis) {
142 for(auto v : vertices) {
143 auto p1 = Vector3{v.position, 0.0};
144 auto p2 = Vector3{v.position + 0.1 * v.tangent, 0.0};
145 auto p3 = Vector3{v.position + 0.1 * v.normal(), 0.0};
146
147 writeLine(p1, p2, {0.0, 1.0, 0.0});
148 writeLine(p1, p3, {1.0, 0.0, 0.0});
149 }
150 }
151
152 if(writeVertices) {
153 for(auto v : shape.vertices()) {
154 writePoint(Vector3{v.position, 0.0});
155 }
156 }
157 }
158
161 template<typename Path> void writePath(const Path& path, const bool writeVertices = false, const bool writeAxis = false) {
162 Vector<PathVertex> vertices{};
163 for(const auto& temp : path.vertices()) {
164 vertices.push_back(temp);
165 }
166
167 if(writeAxis) {
168 for(const auto& v : path.vertices()) {
169 writeLine(v.position, v.position + 0.1 * v.tangent, {0.0, 0.0, 1.0});
170 writeLine(v.position, v.position + 0.1 * v.normal, {1.0, 0.0, 0.0});
171 writeLine(v.position, v.position + 0.1 * v.binormal(), {0.0, 1.0, 0.0});
172 }
173 }
174
175 if(writeVertices) {
176 for(const auto& v : path.vertices()) {
177 writePoint(v.position + 0.001 * v.normal);
178 }
179 }
180
181 for(const auto& e : path.edges()) {
182 writeLine(vertices[e.vertices[0]].position, vertices[e.vertices[1]].position);
183 }
184 }
185
187 template<typename Mesh> void writeMesh(const Mesh& mesh, const bool writeVertices = false, const bool writeNormals = false) {
188 Vector<MeshVertex> vertices{};
189 for(const MeshVertex& vertex : mesh.vertices()) {
190 vertices.push_back(vertex);
191 }
192
193 for(Triangle t : mesh.triangles()) {
194 writeTriangle(vertices[t.vertices[0]].position, vertices[t.vertices[1]].position, vertices[t.vertices[2]].position);
195 }
196
197 if(writeVertices) {
198 for(const auto& v : vertices) {
199 writePoint(v.position);
200 }
201 }
202
203 // Normals
204 if(writeNormals) {
205 for(const auto& v : vertices) {
206 writeLine(v.position, v.position + 0.1 * v.normal, {0.0, 0.0, 1.0});
207 }
208 }
209 }
210
212 std::string str() const;
213 };
214
215} // namespace CeresEngine::MeshGenerator
Definition MeshVertex.hpp:14
BaseElem(double z, const Vector3 &color)
double z_
Definition SvgWriter.hpp:32
Vector3 color_
Definition SvgWriter.hpp:34
virtual void stream(std::ostream &) const =0
virtual void stream(std::ostream &os) const override
Vector3 p1_
Definition SvgWriter.hpp:55
Vector3 p2_
Definition SvgWriter.hpp:55
LineElem(const Vector3 &p1, const Vector3 &p2, const Vector3 &color)
virtual void stream(std::ostream &os) const override
Array< Vector3, 3 > p_
Definition SvgWriter.hpp:64
TriangleElem(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3, const Vector3 &color)
virtual void stream(std::ostream &os) const override
VertexElem(const Vector3 &p, const Vector3 &color)
Vector3 p_
Definition SvgWriter.hpp:46
A simple svg writer class for generating preview and debug images.
Definition SvgWriter.hpp:28
void writeLine(const Vector3 &p1, const Vector3 &p2, const Vector3 &color={0.0, 0.0, 0.0})
Write one line.
void viewport(int x, int y, int width, int height)
Sets the viewport. Default fills the whole image.
void writeShape(const Shape &shape, bool writeVertices=false, bool writeAxis=false)
Write all shaped edges and optionally vertices, tangents and normals.
Definition SvgWriter.hpp:128
Vector3 normalToColor(const Vector3 &normal) const
Vector3 project(const Vector3 &p) const
Matrix4 projMatrix_
Definition SvgWriter.hpp:78
Vector2i size_
Definition SvgWriter.hpp:75
void writePath(const Path &path, const bool writeVertices=false, const bool writeAxis=false)
Write all path edges as lines and optionally vertices, tangents, normals and binormals.
Definition SvgWriter.hpp:161
Matrix4 viewMatrix_
Definition SvgWriter.hpp:77
Vector2i viewportSize_
Definition SvgWriter.hpp:81
Vector3 lightDir_
Definition SvgWriter.hpp:83
bool cullface_
Definition SvgWriter.hpp:85
Vector2i viewportOrigin_
Definition SvgWriter.hpp:80
Vector< UPtr< BaseElem > > elems_
Definition SvgWriter.hpp:87
void writeTriangle(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3, const Vector3 &color)
Write one triangle.
std::string str() const
Generates svg xml from the data written so far.
void ortho(double left, double right, double bottom, double top)
Sets the projection mode to orthographic projection.
SvgWriter(int width, int height)
void writeTriangle(const Vector3 &p1, const Vector3 &p2, const Vector3 &p3)
Write one triangle with color automatically calculated from light.
void perspective(double fovy, double aspect, double zNear, double zFar)
Sets the projection mode to perspective projection.
Matrix4 viewProjMatrix_
Definition SvgWriter.hpp:79
void modelView(const Matrix4 &matrix)
Sets the model view matrix. Default is the identity matrix.
void writePoint(const Vector3 &p, const Vector3 &color={0.0, 0.0, 0.0})
Write one point. Drawn as a circle.
void cullface(bool cullface)
Sets if backfacing triangles should be culled. Default is true.
void writeMesh(const Mesh &mesh, const bool writeVertices=false, const bool writeNormals=false)
Write all triangles from a mesh.
Definition SvgWriter.hpp:187
Definition Triangle.hpp:14
A base class for all mesh implementations.
Definition Mesh.hpp:112
A type that describes a conjunction of shapes that can be filled and stroked.
Definition Shape.hpp:198
Definition AnyGenerator.hpp:12
std::vector< T, ScopedAllocatorAdaptor< StdAllocator< T, RawAllocator > > > Vector
Vector is a sequence container that encapsulates dynamic size arrays.
Definition Vector.hpp:17
std::array< T, N > Array
Array is a container that encapsulates fixed size arrays.
Definition Array.hpp:17
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25