1#ifndef GENERATOR_SVG_HPP
2#define GENERATOR_SVG_HPP
33 BaseElem(
double z,
const gml::dvec3& color);
38 virtual void stream(std::ostream&)
const = 0;
41 class VertexElem :
public BaseElem
46 VertexElem(
const gml::dvec3& p,
const gml::dvec3& color);
48 virtual void stream(std::ostream& os)
const override;
51 class LineElem :
public BaseElem
56 LineElem(
const gml::dvec3& p1,
const gml::dvec3& p2,
const gml::dvec3& color);
58 virtual void stream(std::ostream& os)
const override;
61 class TriangleElem :
public BaseElem
64 std::array<gml::dvec3, 3> p_;
66 TriangleElem(
const gml::dvec3& p1,
const gml::dvec3& p2,
const gml::dvec3& p3,
const gml::dvec3& color);
68 virtual void stream(std::ostream& os)
const override;
71 gml::dvec3 project(
const gml::dvec3& p)
const;
73 gml::dvec3 normalToColor(
const gml::dvec3&
normal)
const;
77 gml::dmat4 viewMatrix_;
78 gml::dmat4 projMatrix_;
79 gml::dmat4 viewProjMatrix_;
80 gml::ivec2 viewportOrigin_;
81 gml::ivec2 viewportSize_;
87 mutable std::vector<std::unique_ptr<BaseElem>> elems_;
101 void perspective(
double fovy,
double aspect,
double zNear,
double zFar);
107 void ortho(
double left,
double right,
double bottom,
double top);
110 void viewport(
int x,
int y,
int width,
int height);
116 void writePoint(
const gml::dvec3& p,
const gml::dvec3& color = {0.0, 0.0, 0.0});
119 void writeLine(
const gml::dvec3& p1,
const gml::dvec3& p2,
const gml::dvec3& color = {0.0, 0.0, 0.0});
122 void writeTriangle(
const gml::dvec3& p1,
const gml::dvec3& p2,
const gml::dvec3& p3,
const gml::dvec3& color);
125 void writeTriangle(
const gml::dvec3& p1,
const gml::dvec3& p2,
const gml::dvec3& p3);
128 template<
typename shape_t>
129 void writeShape(
const shape_t& shape,
bool writeVertices =
false,
bool writeAxis =
false)
131 std::vector<shape_vertex_t> vertices{};
132 for(
const auto& vertex : shape.vertices())
134 vertices.push_back(vertex);
137 for(
auto e : shape.edges())
139 auto p1 = gml::dvec3{vertices[e.vertices[0]].position, 0.0};
140 auto p2 = gml::dvec3{vertices[e.vertices[1]].position, 0.0};
147 for(
auto v : vertices)
149 auto p1 = gml::dvec3{v.position, 0.0};
150 auto p2 = gml::dvec3{v.position + 0.1 * v.tangent, 0.0};
151 auto p3 = gml::dvec3{v.position + 0.1 * v.normal(), 0.0};
160 for(
auto v : shape.vertices())
169 template<
typename Path>
170 void writePath(
const Path& path,
bool writeVertices =
false,
bool writeAxis =
false)
172 std::vector<path_vertex_t> vertices{};
173 for(
const auto& temp : path.vertices())
175 vertices.push_back(temp);
180 for(
const auto& v : path.vertices())
182 writeLine(v.position, v.position + 0.1 * v.tangent, {0.0, 0.0, 1.0});
183 writeLine(v.position, v.position + 0.1 * v.normal, {1.0, 0.0, 0.0});
184 writeLine(v.position, v.position + 0.1 * v.binormal(), {0.0, 1.0, 0.0});
190 for(
const auto& v : path.vertices())
196 for(
const auto& e : path.edges())
198 writeLine(vertices[e.vertices[0]].position, vertices[e.vertices[1]].position);
203 template<
typename Mesh>
204 void writeMesh(
const Mesh& mesh,
bool writeVertices =
false,
bool writeNormals =
false)
206 std::vector<mesh_vertex_t> vertices{};
209 vertices.push_back(vertex);
215 vertices[t.vertices[1]].position,
216 vertices[t.vertices[2]].position);
221 for(
const auto& v : vertices)
230 for(
const auto& v : vertices)
232 writeLine(v.position, v.position + 0.1 * v.normal, {0.0, 0.0, 1.0});
238 std::string
str()
const;
A simple svg writer class for generating preview and debug images.
void viewport(int x, int y, int width, int height)
Sets the viewport. Default fills the whole image.
void writeShape(const shape_t &shape, bool writeVertices=false, bool writeAxis=false)
Write all shaped edges and optionally vertices, tangents and normals.
void modelView(const gml::dmat4 &matrix)
Sets the model view matrix. Default is the identity matrix.
void writeTriangle(const gml::dvec3 &p1, const gml::dvec3 &p2, const gml::dvec3 &p3, const gml::dvec3 &color)
Write one triangle.
svg_writer_t(int width, int height)
void writeLine(const gml::dvec3 &p1, const gml::dvec3 &p2, const gml::dvec3 &color={0.0, 0.0, 0.0})
Write one line.
void writeMesh(const Mesh &mesh, bool writeVertices=false, bool writeNormals=false)
Write all triangles from a mesh.
void perspective(double fovy, double aspect, double zNear, double zFar)
void ortho(double left, double right, double bottom, double top)
std::string str() const
Generates svg xml from the data written so far.
void writePoint(const gml::dvec3 &p, const gml::dvec3 &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 writePath(const Path &path, bool writeVertices=false, bool writeAxis=false)