Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
glimBatch.cpp
Go to the documentation of this file.
1/*
2** GLIM - OpenGL Immediate Mode
3** Copyright Jan Krassnigg (Jan@Krassnigg.de)
4** For more details, see the included Readme.txt.
5*/
6
7
8
9#include "glimBatch.h"
10
14
15namespace NS_GLIM
16{
18 {
19 Clear (true, 64 * 3, 1);
20 }
21
22 void GLIM_BATCH::Clear (const bool reserveBuffers, const unsigned int vertexCount, const unsigned int attributeCount)
23 {
25 m_Data.Reset (reserveBuffers, vertexCount, attributeCount);
26 }
27
29 {
31 return false;
32 }
33
34 GLIM_CHECK (m_Data.m_State == GLIM_BATCH_STATE::STATE_FINISHED_BATCH, "GLIM_BATCH::RenderBatch: This function can only be called after a batch has been created.");
35
36 return true;
37 }
38
39 void GLIM_BATCH::BeginBatch (bool reserveBuffers, unsigned int vertexCount, unsigned int attributeCount)
40 {
41 GLIM_CHECK (m_Data.m_State == GLIM_BATCH_STATE::STATE_EMPTY || m_Data.m_State == GLIM_BATCH_STATE::STATE_FINISHED_BATCH, "GLIM_BATCH::BeginBatch: This function cannot be called again before EndBatch has been called.");
42
43 // clear all previous data
44 Clear (reserveBuffers, vertexCount, attributeCount);
45
46 // start an entirely new batch
48 }
49
51 {
52 // if the state is STATE_BEGINNING_BATCH, than no Begin/End call has been made => created an empty batch, which is ok
53 GLIM_CHECK (m_Data.m_State == GLIM_BATCH_STATE::STATE_END_PRIMITIVE || m_Data.m_State == GLIM_BATCH_STATE::STATE_BEGINNING_BATCH, "GLIM_BATCH::EndBatch: This function must be called after a call to \"End\".");
54
55 // mark this batch as finished
57
58 return m_Data;
59 }
60
61 void GLIM_BATCH::Begin (GLIM_ENUM eType) noexcept
62 {
63 // if the state is STATE_BEGINNING_BATCH, than no Begin/End call has been made yet
64 // if it is STATE_END_PRIMITIVE then a previous Begin/End call has been made
65 GLIM_CHECK (m_Data.m_State == GLIM_BATCH_STATE::STATE_END_PRIMITIVE || m_Data.m_State == GLIM_BATCH_STATE::STATE_BEGINNING_BATCH, "GLIM_BATCH::Begin: This function must be called after a call to \"BeginBatch\" or after a \"Begin\"/\"End\"-pair.");
66
68 m_PrimitiveType = eType;
69 m_uiPrimitiveVertex = 0;
70 m_uiPrimitiveFirstIndex = (unsigned int) m_Data.m_PositionData.size () / 3; // three floats per vertex
71
72 switch (m_PrimitiveType)
73 {
82 // Life is good.
83 break;
84
87
89
90 GLIM_CHECK (false, "GLIM_BATCH::Begin: The given primitive-type is currently not supported.");
91 break;
92
93 default:
94 GLIM_CHECK (false, "GLIM_BATCH::Begin: The given primitive-type is unknown.");
95 return;
96 }
97 }
98
100 {
101 GLIM_CHECK (m_Data.m_State == GLIM_BATCH_STATE::STATE_BEGIN_PRIMITIVE, "GLIM_BATCH::End: This function can only be called after a call to \"Begin\".");
102
104
105 switch (m_PrimitiveType)
106 {
108 {
109 GLIM_CHECK (m_uiPrimitiveVertex % 3 == 0, "GLIM_BATCH::End: You did not finish constructing the last triangle.");
110 }
111 break;
112
114 {
116 }
117 break;
118
120 {
121 GLIM_CHECK (m_uiPrimitiveVertex >= 4, "GLIM_BATCH::End: You did not finish constructing the triangle fan. At least 4 vertices are required.");
122
123 // add the very first vertex index
125 // add the previous vertex index
127
128 // add the second vertex index
130
133
136 }
137 break;
138
140 {
141 GLIM_CHECK (m_uiPrimitiveVertex % 4 == 0, "GLIM_BATCH::End: You did not finish constructing the last Quad.");
142 }
143 break;
144
146 {
148 }
149 break;
150
152 {
153 // nothing to do
154 }
155 break;
156
158 {
159 GLIM_CHECK (m_uiPrimitiveVertex % 2 == 0, "GLIM_BATCH::End: You did not finish constructing the last Line.");
160 }
161 break;
162
164 {
165 GLIM_CHECK (m_uiPrimitiveVertex > 1, "GLIM_BATCH::End: You did not finish constructing the Line-strip.");
166 }
167 break;
168
170 {
171 GLIM_CHECK (m_uiPrimitiveVertex > 1, "GLIM_BATCH::End: You did not finish constructing the Line-Loop.");
172
175 }
176 break;
177
179 {
180 GLIM_CHECK (m_uiPrimitiveVertex == 0 || m_uiPrimitiveVertex >= 3, "GLIM_BATCH::End: You did not finish constructing the last Polygon.");
181
184 }
185 break;
186
187 default:
188 GLIM_CHECK (false, "GLIM_BATCH::End: The given primitive-type is unknown.");
189 }
190 }
191
192 void GLIM_BATCH::Vertex (float x, float y, float z)
193 {
194 GLIM_CHECK (m_Data.m_State == GLIM_BATCH_STATE::STATE_BEGIN_PRIMITIVE, "GLIM_BATCH::Vertex: This function can only be called after a call to \"Begin\".");
195
196 switch (m_PrimitiveType)
197 {
199 {
201 const unsigned int uiIndex = m_Data.AddVertex (x, y, z);
202 m_Data.m_IndexBuffer_Triangles.push_back (uiIndex);
203
204 if (m_uiPrimitiveVertex > 1)
205 {
207 m_Data.m_IndexBuffer_Wireframe.push_back (uiIndex);
208 }
209
210 if (m_uiPrimitiveVertex == 3)
211 {
213
214 m_Data.m_IndexBuffer_Wireframe.push_back (uiIndex);
216 }
217
218 // if this is the first vertex of any quad, store that index
219 if (m_uiPrimitiveVertex == 1)
220 m_uiPrimitiveFirstIndex = uiIndex;
221
222 m_uiPrimitiveLastIndex = uiIndex;
223 }
224 break;
225
227 {
229 }
230 break;
231
233 {
235
236 // first 3 vertices are simply added; at the fourth we use two cached vertices to construct a new triangle
237 if (m_uiPrimitiveVertex >= 4)
238 {
239 // add the very first vertex index
241 // add the previous vertex index
243
246 }
247
248 const unsigned int uiIndex = m_Data.AddVertex (x, y, z);
249 m_Data.m_IndexBuffer_Triangles.push_back (uiIndex);
250
251 if (m_uiPrimitiveVertex > 1)
252 {
254 m_Data.m_IndexBuffer_Wireframe.push_back (uiIndex);
255 }
256
257 m_uiPrimitiveLastIndex = uiIndex;
258
259 }
260 break;
261
263 {
265
266 // first 3 vertices are simply added, at the fourth we use two cached vertices to construct a new triangle
267 if (m_uiPrimitiveVertex == 4)
268 {
269 // add the very first vertex index
271 // add the previous vertex index
273 }
274
275 const unsigned int uiIndex = m_Data.AddVertex (x, y, z);
276 m_Data.m_IndexBuffer_Triangles.push_back (uiIndex);
277
278 if (m_uiPrimitiveVertex > 1)
279 {
281 m_Data.m_IndexBuffer_Wireframe.push_back (uiIndex);
282 }
283
284 if (m_uiPrimitiveVertex == 4)
285 {
286 // reset the vertex counter
288
289 m_Data.m_IndexBuffer_Wireframe.push_back (uiIndex);
291 }
292
293 // if this is the first vertex of any quad, store that index
294 if (m_uiPrimitiveVertex == 1)
295 m_uiPrimitiveFirstIndex = uiIndex;
296
297 m_uiPrimitiveLastIndex = uiIndex;
298 }
299 break;
300
302 {
304 }
305 break;
306
308 {
309 const unsigned int uiIndex = m_Data.AddVertex (x, y, z);
310 m_Data.m_IndexBuffer_Points.push_back (uiIndex);
311 }
312 break;
313
315 {
317 m_Data.m_IndexBuffer_Lines.push_back (m_Data.AddVertex(x, y, z));
318 }
319 break;
320
323 {
325
326 // very first vertex, just store it, but don't create a line yet
327 if (m_uiPrimitiveVertex == 1)
328 {
330 break;
331 }
332
333 // push the previous vertex into the index-buffer
335
336 // store the current vertex in the vertex-array
338 // push the current vertex into the index buffer
340 }
341 break;
342
344 {
346
347 // first 3 vertices are simply added, at the fourth we use two cached vertices to construct a new triangle
348 if (m_uiPrimitiveVertex >= 4)
349 {
350 // add the very first vertex index
352 // add the previous vertex index
354 }
355
356 const unsigned int uiIndex = m_Data.AddVertex (x, y, z);
357 m_Data.m_IndexBuffer_Triangles.push_back (uiIndex);
358
359 if (m_uiPrimitiveVertex > 1)
360 {
362 m_Data.m_IndexBuffer_Wireframe.push_back (uiIndex);
363 }
364
365 m_uiPrimitiveLastIndex = uiIndex;
366 }
367 break;
368
369 default:
370 GLIM_CHECK (false, "GLIM_BATCH::Vertex: The given primitive-type is unknown.");
371 return;
372 }
373 }
374}
void Vertex(float x, float y, float z=0.0f)
Specifies a new vertex of a primitive.
Definition: glimBatch.cpp:192
void BeginBatch(bool reserveBuffers=true, unsigned int vertexCount=64 *3, unsigned int attributeCount=1)
Definition: glimBatch.cpp:39
glimBatchData & EndBatch(void) noexcept
Ends defining the batch. After this call "RenderBatch" can be called to actually render it.
Definition: glimBatch.cpp:50
unsigned int m_uiPrimitiveFirstIndex
Definition: glimBatch.h:95
void Begin(GLIM_ENUM eType) noexcept
Begins gathering information about the given type of primitives.
Definition: glimBatch.cpp:61
glimBatchData m_Data
Definition: glimBatch.h:99
void End(void)
Ends gathering information about the primitives.
Definition: glimBatch.cpp:99
unsigned int m_uiPrimitiveLastIndex
Definition: glimBatch.h:97
GLIM_ENUM m_PrimitiveType
Definition: glimBatch.h:90
unsigned int m_uiPrimitiveVertex
Definition: glimBatch.h:93
bool PrepareRender()
Renders n instances of the batch that has been defined previously.
Definition: glimBatch.cpp:28
void Clear(bool reserveBuffers, unsigned int vertexCount, unsigned int attributeCount)
Deletes all data associated with this object.
Definition: glimBatch.cpp:22
void GLIM_CHECK(bool bCondition, const char *szErrorMsg) noexcept
Assert Macro used internally.
Definition: Declarations.h:66
GLIM_ENUM
The enum holding all important GLIM configuration values.
Definition: Declarations.h:17
@ GLIM_LINES
Can be passed to GLIM::Begin.
@ GLIM_LINE_STRIP
Can be passed to GLIM::Begin.
@ GLIM_QUADS
Can be passed to GLIM::Begin.
@ GLIM_POLYGON
Can be passed to GLIM::Begin.
@ GLIM_TRIANGLE_STRIP
Can be passed to GLIM::Begin (not yet implemented)
@ GLIM_POINTS
Can be passed to GLIM::Begin.
@ GLIM_LINE_LOOP
Can be passed to GLIM::Begin.
@ GLIM_TRIANGLE_FAN
Can be passed to GLIM::Begin.
@ GLIM_TRIANGLES
Can be passed to GLIM::Begin.
@ GLIM_QUAD_STRIP
Can be passed to GLIM::Begin (not yet implemented)
Divide::vector< unsigned int > m_IndexBuffer_Triangles
Definition: glimBatchData.h:82
void Reset(bool reserveBuffers=false, unsigned int vertexCount=64 *3, unsigned int attributeCount=1)
unsigned int AddVertex(float x, float y, float z)
Divide::vector< unsigned int > m_IndexBuffer_Lines
Definition: glimBatchData.h:80
Divide::vector< unsigned int > m_IndexBuffer_Wireframe
Definition: glimBatchData.h:84
Divide::vector< unsigned int > m_IndexBuffer_Points
Definition: glimBatchData.h:78
GLIM_BATCH_STATE m_State
Definition: glimBatchData.h:69