Skip to content

Commit 30d2c34

Browse files
committed
Use nglDrawArray in Chunk
1 parent b5b5fb9 commit 30d2c34

File tree

3 files changed

+10
-168
lines changed

3 files changed

+10
-168
lines changed

chunk.cpp

Lines changed: 5 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ static constexpr bool inBounds(int x, int y, int z)
2727
return x >= 0 && y >= 0 && z >= 0 && x < Chunk::SIZE && y < Chunk::SIZE && z < Chunk::SIZE;
2828
}
2929

30-
int Chunk::getPosition(int x, int y, int z)
30+
unsigned int Chunk::getPosition(int x, int y, int z)
3131
{
3232
if(pos_indices[x][y][z] == -1)
3333
{
@@ -155,143 +155,13 @@ void Chunk::buildGeometry()
155155

156156
std::fill(sides_rendered[0][0] + 0, sides_rendered[SIZE - 1][SIZE - 1] + SIZE, 0);
157157

158-
positions_transformed.resize(positions.size());
159-
positions_perspective.resize(positions.size());
158+
positions_processed.resize(positions.size());
160159

161160
render_dirty = false;
162161

163162
debug("Done!\n");
164163
}
165164

166-
#define MAKE_VERTEX(pos, iver) { (pos).x, (pos).y, (pos).z, iver.u, iver.v, iver.c }
167-
168-
VERTEX Chunk::perspective(const IndexedVertex &v, VECTOR3 &transformed)
169-
{
170-
std::pair<VECTOR3, bool> &p = positions_perspective[v.pos];
171-
if(!p.second)
172-
{
173-
VERTEX ver = MAKE_VERTEX(transformed, v);
174-
nglPerspective(&ver);
175-
p.first = { ver.x, ver.y, ver.z };
176-
p.second = true;
177-
178-
return ver;
179-
}
180-
181-
return MAKE_VERTEX(p.first, v);
182-
}
183-
184-
bool Chunk::drawTriangle(const IndexedVertex &low, const IndexedVertex &middle, const IndexedVertex &high, bool backface_culling)
185-
{
186-
VECTOR3 pos_low = positions_transformed[low.pos], pos_middle = positions_transformed[middle.pos], pos_high = positions_transformed[high.pos];
187-
188-
#ifndef Z_CLIPPING
189-
if(pos_low.z < GLFix(CLIP_PLANE) || pos_middle.z < GLFix(CLIP_PLANE) || pos_high.z < GLFix(CLIP_PLANE))
190-
return true;
191-
192-
VERTEX low_p = perspective(low, pos_low), middle_p = perspective(middle, pos_middle), high_p = perspective(high, pos_high);
193-
194-
if(backface_culling && nglIsBackface(&low_p, &middle_p, &high_p))
195-
return false;
196-
197-
nglDrawTriangleZClipped(&low_p, &middle_p, &high_p);
198-
199-
return true;
200-
#else
201-
202-
VERTEX invisible[3];
203-
IndexedVertex visible[3];
204-
VECTOR3 *pos_visible[3];
205-
int count_invisible = -1, count_visible = -1;
206-
207-
if(pos_low.z < GLFix(CLIP_PLANE))
208-
invisible[++count_invisible] = MAKE_VERTEX(pos_low, low);
209-
else
210-
{
211-
visible[++count_visible] = low;
212-
pos_visible[count_visible] = &pos_low;
213-
}
214-
215-
if(pos_middle.z < GLFix(CLIP_PLANE))
216-
invisible[++count_invisible] = MAKE_VERTEX(pos_middle, middle);
217-
else
218-
{
219-
visible[++count_visible] = middle;
220-
pos_visible[count_visible] = &pos_middle;
221-
}
222-
223-
if(pos_high.z < GLFix(CLIP_PLANE))
224-
invisible[++count_invisible] = MAKE_VERTEX(pos_high, high);
225-
else
226-
{
227-
visible[++count_visible] = high;
228-
pos_visible[count_visible] = &pos_high;
229-
}
230-
231-
//Interpolated vertices
232-
VERTEX v1, v2;
233-
234-
//Temporary vertices
235-
VERTEX t0, t1;
236-
237-
switch(count_visible)
238-
{
239-
case -1:
240-
return true;
241-
242-
case 0:
243-
t0 = MAKE_VERTEX(*pos_visible[0], visible[0]);
244-
245-
nglInterpolateVertexZ(&invisible[0], &t0, &v1);
246-
nglInterpolateVertexZ(&invisible[1], &t0, &v2);
247-
248-
t0 = perspective(visible[0], *pos_visible[0]);
249-
nglPerspective(&v1);
250-
nglPerspective(&v2);
251-
252-
if(backface_culling && nglIsBackface(&t0, &v1, &v2))
253-
return false;
254-
255-
nglDrawTriangleZClipped(&t0, &v1, &v2);
256-
return true;
257-
258-
case 1:
259-
t0 = MAKE_VERTEX(*pos_visible[0], visible[0]);
260-
t1 = MAKE_VERTEX(*pos_visible[1], visible[1]);
261-
262-
nglInterpolateVertexZ(&t0, &invisible[0], &v1);
263-
nglInterpolateVertexZ(&t1, &invisible[0], &v2);
264-
265-
t0 = perspective(visible[0], *pos_visible[0]);
266-
t1 = perspective(visible[1], *pos_visible[1]);
267-
nglPerspective(&v1);
268-
269-
//TODO: Hack: This doesn't work as expected
270-
/*if(backface_culling && nglIsBackface(&t0, &t1, &v1))
271-
return false;*/
272-
273-
nglPerspective(&v2);
274-
nglDrawTriangleZClipped(&t0, &t1, &v1);
275-
nglDrawTriangleZClipped(&t1, &v1, &v2);
276-
return true;
277-
278-
case 2:
279-
invisible[0] = perspective(low, pos_low);
280-
invisible[1] = perspective(middle, pos_middle);
281-
invisible[2] = perspective(high, pos_high);
282-
283-
if(backface_culling && nglIsBackface(&invisible[0], &invisible[1], &invisible[2]))
284-
return false;
285-
286-
nglDrawTriangleZClipped(&invisible[0], &invisible[1], &invisible[2]);
287-
return true;
288-
289-
default:
290-
return true;
291-
}
292-
#endif
293-
}
294-
295165
static bool behindClip(const VERTEX &v1)
296166
{
297167
return transformation->data[2][0]*v1.x + transformation->data[2][1]*v1.y + transformation->data[2][2]*v1.z + transformation->data[2][3] <= GLFix(CLIP_PLANE);
@@ -374,37 +244,14 @@ void Chunk::render()
374244
&& v13.y >= SCREEN_HEIGHT && v14.y >= SCREEN_HEIGHT && v15.y >= SCREEN_HEIGHT && v16.y >= SCREEN_HEIGHT)
375245
return;
376246

377-
std::fill(positions_perspective.begin(), positions_perspective.end(), std::make_pair<VECTOR3, bool>({0, 0, 0}, false));
378-
379-
for(unsigned int i = 0; i < positions.size(); i++)
380-
nglMultMatVectRes(transformation, &positions[i], &positions_transformed[i]);
381-
382247
nglForceColor(true);
383-
const IndexedVertex *v = vertices_color.data();
384-
for(unsigned int i = 0; i < vertices_color.size(); i += 4, v += 4)
385-
{
386-
if(drawTriangle(v[0], v[1], v[2], true))
387-
drawTriangle(v[2], v[3], v[0], false);
388-
}
248+
nglDrawArray(vertices_color.data(), vertices_color.size(), positions.data(), positions.size(), positions_processed.data(), GL_QUADS, true);
389249
nglForceColor(false);
390250

391-
//Same, but with textures
392-
v = vertices.data();
393-
for(unsigned int i = 0; i < vertices.size(); i += 4, v += 4)
394-
{
395-
if(drawTriangle(v[0], v[1], v[2], (v[0].c & TEXTURE_DRAW_BACKFACE) == 0))
396-
drawTriangle(v[2], v[3], v[0], false);
397-
}
251+
nglDrawArray(vertices.data(), vertices.size(), positions.data(), positions.size(), positions_processed.data(), GL_QUADS, false);
398252

399-
//Now do the same again, but with a different texture bound
400253
glBindTexture(terrain_quad);
401-
402-
v = vertices_quad.data();
403-
for(unsigned int i = 0; i < vertices_quad.size(); i += 4, v += 4)
404-
{
405-
if(drawTriangle(v[0], v[1], v[2], (v[0].c & TEXTURE_DRAW_BACKFACE) == 0))
406-
drawTriangle(v[2], v[3], v[0], false);
407-
}
254+
nglDrawArray(vertices_quad.data(), vertices_quad.size(), positions.data(), positions.size(), positions_processed.data(), GL_QUADS, false);
408255

409256
glBindTexture(terrain_current);
410257

chunk.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,12 @@
66
#include <tuple>
77

88
#include "gl.h"
9+
#include "gldrawarray.h"
910
#include "terrain.h"
1011
#include "aabb.h"
1112

1213
class World;
1314

14-
struct IndexedVertex {
15-
int pos;
16-
GLFix u, v;
17-
COLOR c;
18-
};
19-
2015
class Chunk
2116
{
2217
public:
@@ -71,7 +66,7 @@ class Chunk
7166
void makeTree(unsigned int x, unsigned int y, unsigned int z);
7267

7368
//Data
74-
int getPosition(int x, int y, int z);
69+
unsigned int getPosition(int x, int y, int z);
7570

7671
//Rendering
7772
void geometrySpecialBlock(BLOCK_WDATA block, unsigned int x, unsigned int y, unsigned int z, BLOCK_SIDE side);
@@ -88,8 +83,8 @@ class Chunk
8883
bool render_dirty = true;
8984
int pos_indices[SIZE + 1][SIZE + 1][SIZE + 1];
9085
BLOCK_SIDE_BITFIELD sides_rendered[SIZE][SIZE][SIZE] = {}; //It could be that other chunks already rendered parts of our blocks
91-
std::vector<VECTOR3> positions, positions_transformed;
92-
std::vector<std::pair<VECTOR3, bool>> positions_perspective;
86+
std::vector<VECTOR3> positions;
87+
std::vector<ProcessedPosition> positions_processed;
9388
std::vector<IndexedVertex> vertices, vertices_quad, vertices_color;
9489
std::vector<VERTEX> vertices_unaligned; //The optimized drawing with indices doesn't work with unaligned positions
9590
int tick_counter = 1; //1 to trigger a tick the next frame

0 commit comments

Comments
 (0)