/
tesselator.c
319 lines (269 loc) · 9.91 KB
/
tesselator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
Copyright (c) 2017-2020 ByteBit
This file is part of BetterSpades.
BetterSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BetterSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BetterSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "tesselator.h"
static size_t vertex_type_size(enum tesselator_vertex_type type) {
switch(type) {
case VERTEX_INT: return sizeof(int16_t);
case VERTEX_FLOAT: return sizeof(float);
default: return 0;
}
}
void tesselator_create(struct tesselator* t, enum tesselator_vertex_type type, int has_normal) {
t->quad_count = 0;
t->quad_space = 128;
t->vertices = NULL;
t->colors = NULL;
t->vertex_type = type;
t->has_normal = has_normal;
#ifdef TESSELATE_QUADS
t->vertices = malloc(t->quad_space * vertex_type_size(t->vertex_type) * 3 * 4);
CHECK_ALLOCATION_ERROR(t->vertices)
t->colors = malloc(t->quad_space * sizeof(uint32_t) * 4);
CHECK_ALLOCATION_ERROR(t->colors)
if(t->has_normal) {
t->normals = malloc(t->quad_space * sizeof(int8_t) * 3 * 4);
CHECK_ALLOCATION_ERROR(t->normals)
} else {
t->normals = NULL;
}
#endif
#ifdef TESSELATE_TRIANGLES
t->vertices = malloc(t->quad_space * vertex_type_size(t->vertex_type) * 3 * 6);
CHECK_ALLOCATION_ERROR(t->vertices)
t->colors = malloc(t->quad_space * sizeof(uint32_t) * 6);
CHECK_ALLOCATION_ERROR(t->colors)
if(t->has_normal) {
t->normals = malloc(t->quad_space * sizeof(int8_t) * 3 * 6);
CHECK_ALLOCATION_ERROR(t->normals)
} else {
t->normals = NULL;
}
#endif
}
void tesselator_clear(struct tesselator* t) {
t->quad_count = 0;
}
void tesselator_free(struct tesselator* t) {
if(t->vertices) {
free(t->vertices);
t->vertices = NULL;
}
if(t->colors) {
free(t->colors);
t->colors = NULL;
}
if(t->normals) {
free(t->normals);
t->normals = NULL;
}
}
void tesselator_draw(struct tesselator* t, int with_color) {
glEnableClientState(GL_VERTEX_ARRAY);
if(t->has_normal) {
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_BYTE, 0, t->normals);
}
switch(t->vertex_type) {
case VERTEX_INT: glVertexPointer(3, GL_SHORT, 0, t->vertices); break;
case VERTEX_FLOAT: glVertexPointer(3, GL_FLOAT, 0, t->vertices); break;
}
if(with_color) {
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, t->colors);
}
#ifdef TESSELATE_QUADS
glDrawArrays(GL_QUADS, 0, t->quad_count * 4);
#endif
#ifdef TESSELATE_TRIANGLES
glDrawArrays(GL_TRIANGLES, 0, t->quad_count * 6);
#endif
if(with_color) {
glDisableClientState(GL_COLOR_ARRAY);
}
glDisableClientState(GL_VERTEX_ARRAY);
if(t->has_normal) {
glDisableClientState(GL_NORMAL_ARRAY);
}
}
void tesselator_glx(struct tesselator* t, struct glx_displaylist* x) {
#ifdef TESSELATE_QUADS
switch(t->vertex_type) {
case VERTEX_INT:
glx_displaylist_update(x, t->quad_count * 4, GLX_DISPLAYLIST_NORMAL, t->colors, t->vertices, t->normals);
break;
case VERTEX_FLOAT:
glx_displaylist_update(x, t->quad_count * 4, GLX_DISPLAYLIST_ENHANCED, t->colors, t->vertices, t->normals);
break;
}
#endif
#ifdef TESSELATE_TRIANGLES
switch(t->vertex_type) {
case VERTEX_INT:
glx_displaylist_update(x, t->quad_count * 6, GLX_DISPLAYLIST_NORMAL, t->colors, t->vertices, t->normals);
break;
case VERTEX_FLOAT:
glx_displaylist_update(x, t->quad_count * 6, GLX_DISPLAYLIST_ENHANCED, t->colors, t->vertices, t->normals);
break;
}
#endif
}
void tesselator_set_color(struct tesselator* t, uint32_t color) {
t->color = color;
}
void tesselator_set_normal(struct tesselator* t, int8_t x, int8_t y, int8_t z) {
t->normal[0] = x;
t->normal[1] = y;
t->normal[2] = z;
}
static void tesselator_check_space(struct tesselator* t) {
if(t->quad_count >= t->quad_space) {
t->quad_space *= 2;
#ifdef TESSELATE_QUADS
t->vertices = realloc(t->vertices, t->quad_space * vertex_type_size(t->vertex_type) * 3 * 4);
CHECK_ALLOCATION_ERROR(t->vertices)
t->colors = realloc(t->colors, t->quad_space * sizeof(uint32_t) * 4);
CHECK_ALLOCATION_ERROR(t->colors)
if(t->has_normal) {
t->normals = realloc(t->normals, t->quad_space * sizeof(int8_t) * 3 * 4);
CHECK_ALLOCATION_ERROR(t->normals)
}
#endif
#ifdef TESSELATE_TRIANGLES
t->vertices = realloc(t->vertices, t->quad_space * vertex_type_size(t->vertex_type) * 3 * 6);
CHECK_ALLOCATION_ERROR(t->vertices)
t->colors = realloc(t->colors, t->quad_space * sizeof(uint32_t) * 6);
CHECK_ALLOCATION_ERROR(t->colors)
if(t->has_normal) {
t->normals = realloc(t->normals, t->quad_space * sizeof(int8_t) * 3 * 6);
CHECK_ALLOCATION_ERROR(t->normals)
}
#endif
}
}
static void tesselator_emit_color(struct tesselator* t, uint32_t* colors) {
#ifdef TESSELATE_QUADS
memcpy(t->colors + t->quad_count * 4, colors, sizeof(uint32_t) * 4);
#endif
#ifdef TESSELATE_TRIANGLES
memcpy(t->colors + t->quad_count * 6, colors, sizeof(uint32_t) * 3);
t->colors[t->quad_count * 6 + 3] = colors[0];
memcpy(t->colors + t->quad_count * 6 + 4, colors + 2, sizeof(uint32_t) * 2);
#endif
}
static void tesselator_emit_normals(struct tesselator* t, int8_t* normals) {
if(t->has_normal) {
#ifdef TESSELATE_QUADS
memcpy(t->normals + t->quad_count * 3 * 4, normals, sizeof(int8_t) * 3 * 4);
#endif
#ifdef TESSELATE_TRIANGLES
memcpy(t->normals + t->quad_count * 3 * 6 + 3 * 0, normals, sizeof(int8_t) * 3 * 3);
memcpy(t->normals + t->quad_count * 3 * 6 + 3 * 3, normals + 3 * 0, sizeof(int8_t) * 3);
memcpy(t->normals + t->quad_count * 3 * 6 + 3 * 4, normals + 3 * 2, sizeof(int8_t) * 3 * 2);
#endif
}
}
void tesselator_addi(struct tesselator* t, int16_t* coords, uint32_t* colors, int8_t* normals) {
tesselator_check_space(t);
tesselator_emit_color(t, colors);
tesselator_emit_normals(t, normals);
#ifdef TESSELATE_QUADS
memcpy(((int16_t*)t->vertices) + t->quad_count * 3 * 4, coords, sizeof(int16_t) * 3 * 4);
#endif
#ifdef TESSELATE_TRIANGLES
memcpy(((int16_t*)t->vertices) + t->quad_count * 3 * 6 + 3 * 0, coords, sizeof(int16_t) * 3 * 3);
memcpy(((int16_t*)t->vertices) + t->quad_count * 3 * 6 + 3 * 3, coords + 3 * 0, sizeof(int16_t) * 3);
memcpy(((int16_t*)t->vertices) + t->quad_count * 3 * 6 + 3 * 4, coords + 3 * 2, sizeof(int16_t) * 3 * 2);
#endif
t->quad_count++;
}
void tesselator_addf(struct tesselator* t, float* coords, uint32_t* colors, int8_t* normals) {
tesselator_check_space(t);
tesselator_emit_color(t, colors);
tesselator_emit_normals(t, normals);
#ifdef TESSELATE_QUADS
memcpy(((float*)t->vertices) + t->quad_count * 3 * 4, coords, sizeof(float) * 3 * 4);
#endif
#ifdef TESSELATE_TRIANGLES
memcpy(((float*)t->vertices) + t->quad_count * 3 * 6 + 3 * 0, coords, sizeof(float) * 3 * 3);
memcpy(((float*)t->vertices) + t->quad_count * 3 * 6 + 3 * 3, coords + 3 * 0, sizeof(float) * 3);
memcpy(((float*)t->vertices) + t->quad_count * 3 * 6 + 3 * 4, coords + 3 * 2, sizeof(float) * 3 * 2);
#endif
t->quad_count++;
}
void tesselator_addi_simple(struct tesselator* t, int16_t* coords) {
tesselator_addi(t, coords, (uint32_t[]) {t->color, t->color, t->color, t->color},
t->has_normal ? (int8_t[]) {t->normal[0], t->normal[1], t->normal[2], t->normal[0], t->normal[1],
t->normal[2], t->normal[0], t->normal[1], t->normal[2], t->normal[0],
t->normal[1], t->normal[2]} :
NULL);
}
void tesselator_addf_simple(struct tesselator* t, float* coords) {
tesselator_addf(t, coords, (uint32_t[]) {t->color, t->color, t->color, t->color},
t->has_normal ? (int8_t[]) {t->normal[0], t->normal[1], t->normal[2], t->normal[0], t->normal[1],
t->normal[2], t->normal[0], t->normal[1], t->normal[2], t->normal[0],
t->normal[1], t->normal[2]} :
NULL);
}
void tesselator_addi_cube_face(struct tesselator* t, enum tesselator_cube_face face, int16_t x, int16_t y, int16_t z) {
switch(face) {
case CUBE_FACE_Z_N:
tesselator_addi_simple(t, (int16_t[]) {x, y, z, x, y + 1, z, x + 1, y + 1, z, x + 1, y, z});
break;
case CUBE_FACE_Z_P:
tesselator_addi_simple(t, (int16_t[]) {x, y, z + 1, x + 1, y, z + 1, x + 1, y + 1, z + 1, x, y + 1, z + 1});
break;
case CUBE_FACE_X_N:
tesselator_addi_simple(t, (int16_t[]) {x, y, z, x, y, z + 1, x, y + 1, z + 1, x, y + 1, z});
break;
case CUBE_FACE_X_P:
tesselator_addi_simple(t, (int16_t[]) {x + 1, y, z, x + 1, y + 1, z, x + 1, y + 1, z + 1, x + 1, y, z + 1});
break;
case CUBE_FACE_Y_P:
tesselator_addi_simple(t, (int16_t[]) {x, y + 1, z, x, y + 1, z + 1, x + 1, y + 1, z + 1, x + 1, y + 1, z});
break;
case CUBE_FACE_Y_N:
tesselator_addi_simple(t, (int16_t[]) {x, y, z, x + 1, y, z, x + 1, y, z + 1, x, y, z + 1});
break;
}
}
void tesselator_addf_cube_face(struct tesselator* t, enum tesselator_cube_face face, float x, float y, float z,
float sz) {
switch(face) {
case CUBE_FACE_Z_N:
tesselator_addf_simple(t, (float[]) {x, y, z, x, y + sz, z, x + sz, y + sz, z, x + sz, y, z});
break;
case CUBE_FACE_Z_P:
tesselator_addf_simple(
t, (float[]) {x, y, z + sz, x + sz, y, z + sz, x + sz, y + sz, z + sz, x, y + sz, z + sz});
break;
case CUBE_FACE_X_N:
tesselator_addf_simple(t, (float[]) {x, y, z, x, y, z + sz, x, y + sz, z + sz, x, y + sz, z});
break;
case CUBE_FACE_X_P:
tesselator_addf_simple(
t, (float[]) {x + sz, y, z, x + sz, y + sz, z, x + sz, y + sz, z + sz, x + sz, y, z + sz});
break;
case CUBE_FACE_Y_P:
tesselator_addf_simple(
t, (float[]) {x, y + sz, z, x, y + sz, z + sz, x + sz, y + sz, z + sz, x + sz, y + sz, z});
break;
case CUBE_FACE_Y_N:
tesselator_addf_simple(t, (float[]) {x, y, z, x + sz, y, z, x + sz, y, z + sz, x, y, z + sz});
break;
}
}