qxLib
geom.h
Go to the documentation of this file.
1 /**
2 
3  @file geom.h
4  @author Khrapov
5  @date 4.04.2021
6  @copyright © Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 #pragma once
10 
11 #include <qx/render/draw_mode.h>
12 #include <qx/typedefs.h>
13 
14 #include <glm/geometric.hpp>
15 #include <glm/vec3.hpp>
16 
17 #include <array>
18 #include <cmath>
19 #include <numbers>
20 #include <span>
21 #include <unordered_map>
22 #include <vector>
23 
24 namespace qx
25 {
26 
27 struct vertex
28 {
29  glm::vec3 pos = glm::vec3(0.f);
30  glm::vec3 normal = glm::vec3(0.f);
31  glm::vec3 tangent = glm::vec3(0.f);
32 };
33 
34 using index_type = u32;
35 using vertices = std::vector<vertex>;
36 using indices = std::vector<index_type>;
37 using indices_view = std::span<const index_type>;
38 
39 struct geometry
40 {
41  vertices geomVertices; //!< vertices array
42  indices geomIndices; //!< indices array
43  glm::vec3 offset = glm::vec3(0.f); //!< offset of the center
44  draw_mode eDrawMode = draw_mode::triangles_list; //!< draw mode
45 };
46 
47 namespace shape_indices
48 {
49 
50 constexpr std::array<index_type, 60> icosahedron { {
51  1, 4, 0, 4, 9, 0, 4, 5, 9, 8, 5, 4, 1, 8, 4, 1, 10, 8, 10, 3, 8, 8, 3, 5, 3, 2, 5, 3, 7, 2,
52  3, 10, 7, 10, 6, 7, 6, 11, 7, 6, 0, 11, 6, 1, 0, 10, 1, 6, 11, 0, 9, 2, 11, 9, 5, 2, 9, 11, 2, 7,
53 } };
54 
55 constexpr std::array<index_type, 36> parallelogram = {
56  3, 1, 0, 2, 1, 3, 2, 5, 1, 6, 5, 2, 6, 4, 5, 7, 4, 6, 7, 0, 4, 3, 0, 7, 7, 2, 3, 6, 2, 7, 0, 5, 4, 1, 5, 0,
57 };
58 
59 constexpr std::array<index_type, 6> rect = { 0, 1, 3, 1, 2, 3 };
60 
61 } // namespace shape_indices
62 
63 /**
64  @brief Create tangent vector for a given normal
65  @param normal - normal vector
66  @retval - tangent vector
67 **/
68 glm::vec3 create_tangent(const glm::vec3& normal);
69 
70 /**
71  @brief Transform triangle indices to line indices
72  @param triangles - triangle indices
73  @retval - line indices
74 **/
75 indices transform_triangle_indices_to_lines(indices_view triangles);
76 
77 /**
78  @brief Create parallelogram geometry
79  @param fSide1 - side 1 length
80  @param fSide2 - side 2 length
81  @param fSide3 - side 3 length
82  @param bFlatNormals - if true, each vertex will have independent normal
83  @retval - parallelogram geometry
84 **/
85 geometry create_parallelogram(float fSide1, float fSide2, float fSide3, bool bFlatNormals = true);
86 
87 /**
88  @brief Create parallelogram geometry for lines drawing
89  @param fSide1 - side 1 length
90  @param fSide2 - side 2 length
91  @param fSide3 - side 3 length
92  @retval - parallelogram geometry
93 **/
94 geometry create_parallelogram_lines(float fSide1, float fSide2, float fSide3);
95 
96 /**
97  @brief Create cube geometry
98  @param fSide - cube side length
99  @param bFlatNormals - if true, each vertex will have independent normal
100  @retval - cube geometry
101 **/
102 geometry create_cube(float fSide, bool bFlatNormals = true);
103 
104 /**
105  @brief Create cube geometry for lines drawing
106  @param fSide - cube side length
107  @retval - cube geometry
108 **/
109 geometry create_cube_lines(float fSide);
110 
111 /**
112  @brief Create icosahedron geometry
113  @param fRadius - icosahedron radius
114  @param bFlatNormals - if true, each vertex will have independent normal
115  @retval - icosahedron geometry
116 **/
117 geometry create_icosahedron(float fRadius, bool bFlatNormals = true);
118 
119 /**
120  @brief Create icosahedron geometry for lines drawing
121  @param fRadius - icosahedron radius
122  @retval - icosahedron geometry
123 **/
124 geometry create_icosahedron_lines(float fRadius);
125 
126 /**
127  @brief Create icosphere geometry
128  @param fRadius - icosphere radius
129  @param nDivides - num of divides of icosahedron (bigger is higher precision)
130  @param bFlatNormals - if true, each vertex will have independent normal
131  @retval - icosphere geometry
132 **/
133 geometry create_icosphere(float fRadius, size_t nDivides, bool bFlatNormals = false);
134 
135 /**
136  @brief Create icosphere geometry for lines drawing
137  @param fRadius - icosphere radius
138  @param nDivides - num of divides of icosahedron (bigger is higher precision)
139  @retval - icosphere geometry
140 **/
141 geometry create_icosphere_lines(float fRadius, size_t nDivides);
142 
143 /**
144  @brief Create rectangle geometry
145  @param fWidth - rectangle width
146  @param fHeight - rectangle height
147  @retval - rectangle geometry
148 **/
149 geometry create_rect(float fWidth, float fHeight);
150 
151 /**
152  @brief Create rectangle geometry for lines drawing
153  @param fWidth - rectangle width
154  @param fHeight - rectangle height
155  @retval - rectangle geometry
156 **/
157 geometry create_rect_lines(float fWidth, float fHeight);
158 
159 /**
160  @brief Create square geometry
161  @param fSideLength - square side length
162  @retval - square geometry
163 **/
164 geometry create_square(float fSideLength);
165 
166 /**
167  @brief Create square geometry for lines drawing
168  @param fSideLength - square side length
169  @retval - square geometry
170 **/
171 geometry create_square_lines(float fSideLength);
172 
173 /**
174  @brief Create ellipse geometry
175  @param fHorRadius - ellipse horizontal radius
176  @param fVertRadius - ellipse vertical radius
177  @param nSides - number of lines in ellipse edge, must be > 3
178  the greater value, the smoother еру edge
179  @retval - ellipse geometry
180 **/
181 geometry create_ellipse(float fHorRadius, float fVertRadius, size_t nSides);
182 
183 /**
184  @brief Create ellipse geometry for lines drawing
185  @param fHorRadius - ellipse horizontal radius
186  @param fVertRadius - ellipse vertical radius
187  @param nSides - number of lines in ellipse edge, must be > 3
188  the greater value, the smoother еру edge
189  @retval - ellipse geometry
190 **/
191 geometry create_ellipse_lines(float fHorRadius, float fVertRadius, size_t nSides);
192 
193 /**
194  @brief Create circle geometry for lines drawing
195  @param fRadius - circle radius
196  @param nSides - number of lines in ellipse edge, must be > 3
197  the greater value, the smoother еру edge
198  @retval - circle geometry
199 **/
200 geometry create_circle(float fRadius, size_t nSides);
201 
202 /**
203  @brief Create circle geometry for lines drawing
204  @param fRadius - circle radius
205  @param nSides - number of lines in ellipse edge, must be > 3
206  the greater value, the smoother еру edge
207  @retval - circle geometry
208 **/
209 geometry create_circle_lines(float fRadius, size_t nSides);
210 
211 } // namespace qx
212 
213 #include <qx/render/geom.inl>
Rectangle class.
geometry create_cube(float fSide, bool bFlatNormals=true)
Create cube geometry.
Definition: geom.inl:165
geometry create_circle_lines(float fRadius, size_t nSides)
Create circle geometry for lines drawing.
Definition: geom.inl:363
geometry create_ellipse(float fHorRadius, float fVertRadius, size_t nSides)
Create ellipse geometry.
Definition: geom.inl:322
geometry create_icosahedron(float fRadius, bool bFlatNormals=true)
Create icosahedron geometry.
Definition: geom.inl:179
geometry create_square_lines(float fSideLength)
Create square geometry for lines drawing.
Definition: geom.inl:317
geometry create_parallelogram_lines(float fSide1, float fSide2, float fSide3)
Create parallelogram geometry for lines drawing.
Definition: geom.inl:156
geometry create_icosphere_lines(float fRadius, size_t nDivides)
Create icosphere geometry for lines drawing.
Definition: geom.inl:270
geometry create_ellipse_lines(float fHorRadius, float fVertRadius, size_t nSides)
Create ellipse geometry for lines drawing.
Definition: geom.inl:350
glm::vec3 create_tangent(const glm::vec3 &normal)
Create tangent vector for a given normal.
Definition: geom.inl:125
geometry create_parallelogram(float fSide1, float fSide2, float fSide3, bool bFlatNormals=true)
Create parallelogram geometry.
Definition: geom.inl:151
geometry create_square(float fSideLength)
Create square geometry.
Definition: geom.inl:312
geometry create_cube_lines(float fSide)
Create cube geometry for lines drawing.
Definition: geom.inl:170
indices transform_triangle_indices_to_lines(indices_view triangles)
Transform triangle indices to line indices.
Definition: geom.inl:132
geometry create_icosahedron_lines(float fRadius)
Create icosahedron geometry for lines drawing.
Definition: geom.inl:188
geometry create_circle(float fRadius, size_t nSides)
Create circle geometry for lines drawing.
Definition: geom.inl:358
geometry create_icosphere(float fRadius, size_t nDivides, bool bFlatNormals=false)
Create icosphere geometry.
Definition: geom.inl:197
geometry create_rect(float fWidth, float fHeight)
Create rectangle geometry.
Definition: geom.inl:279
geometry create_rect_lines(float fWidth, float fHeight)
Create rectangle geometry for lines drawing.
Definition: geom.inl:293
indices geomIndices
indices array
Definition: geom.h:42
vertices geomVertices
vertices array
Definition: geom.h:41
glm::vec3 offset
offset of the center
Definition: geom.h:43
draw_mode eDrawMode
draw mode
Definition: geom.h:44
uint32_t u32
0 .. 18 446 744 073 709 551 615
Definition: typedefs.h:24