This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathzgl.h
473 lines (368 loc) · 10.1 KB
/
zgl.h
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#ifndef _tgl_zgl_h_
#define _tgl_zgl_h_
#ifndef NDEBUG
#define NDEBUG
#endif
#include "../include/GL/gl.h"
#include "../include/zbuffer.h"
#include "../include/zfeatures.h"
#include "zmath.h"
#include <math.h>
#include <stdlib.h>
#include <string.h>
#ifndef M_PI
#define M_PI 3.1415926535897932384626433832795
#endif
enum {
#define ADD_OP(a, b, c) OP_##a,
#include "opinfo.h"
};
#if TGL_FEATURE_GL_POLYGON == 1
#define POLYGON_MAX_VERTEX 16
#else
#define POLYGON_MAX_VERTEX 4
#endif
/* Max # of specular light pow buffers */
#define MAX_SPECULAR_BUFFERS 32
/* # of entries in specular buffer */
#define SPECULAR_BUFFER_SIZE 512
/* specular buffer granularity */
#define MAX_MODELVIEW_STACK_DEPTH 32
#define MAX_PROJECTION_STACK_DEPTH 8
#define MAX_TEXTURE_STACK_DEPTH 8
#define MAX_NAME_STACK_DEPTH 16
#define MAX_TEXTURE_LEVELS 1
#define MAX_LIGHTS 16
#define VERTEX_ARRAY 0x0001
#define COLOR_ARRAY 0x0002
#define NORMAL_ARRAY 0x0004
#define TEXCOORD_ARRAY 0x0008
#define MAX_DISPLAY_LISTS 16384
#define OP_BUFFER_MAX_SIZE 4096
#define TGL_OFFSET_FILL 0x1
#define TGL_OFFSET_LINE 0x2
#define TGL_OFFSET_POINT 0x4
typedef struct GLSpecBuf {
GLint shininess_i;
GLint last_used;
GLfloat buf[SPECULAR_BUFFER_SIZE + 1];
struct GLSpecBuf* next;
} GLSpecBuf;
typedef struct GLLight {
V4 ambient;
V4 diffuse;
V4 specular;
V4 position;
V3 spot_direction;
V3 norm_spot_direction;
V3 norm_position;
GLfloat spot_exponent;
GLfloat spot_cutoff;
GLfloat attenuation[3];
/* precomputed values */
GLfloat cos_spot_cutoff;
/* we use a linked list to know which are the enabled lights */
struct GLLight *next, *prev;
GLubyte enabled;
} GLLight;
typedef struct GLMaterial {
V4 emission;
V4 ambient;
V4 diffuse;
V4 specular;
GLfloat shininess;
/* computed values */
GLint shininess_i;
GLint do_specular;
} GLMaterial;
typedef struct GLViewport {
V3 scale;
V3 trans;
GLint xmin, ymin, xsize, ysize;
} GLViewport;
typedef union {
GLint op;
GLfloat f;
GLint i;
GLuint ui;
void* p;
} GLParam;
typedef struct GLParamBuffer {
GLParam ops[OP_BUFFER_MAX_SIZE];
struct GLParamBuffer* next;
} GLParamBuffer;
typedef struct GLList {
GLParamBuffer* first_op_buffer;
/* TODO: extensions for an hash table or a better allocating scheme */
} GLList;
typedef struct GLVertex {
V3 normal;
V4 coord;
V4 tex_coord;
V4 color;
/* computed values */
V4 ec; /* eye coordinates */
V4 pc; /* coordinates in the normalized volume */
ZBufferPoint zp; /* GLinteger coordinates for the rasterization */
GLint clip_code; /* clip code */
GLint edge_flag;
} GLVertex;
typedef struct GLImage {
PIXEL pixmap[TGL_FEATURE_TEXTURE_DIM * TGL_FEATURE_TEXTURE_DIM];
GLint xsize, ysize;
} GLImage;
/* textures */
#define TEXTURE_HASH_TABLE_SIZE 256
#define TEXTURE_HASH_TABLE_MASK 255
typedef struct GLTexture {
GLImage images[MAX_TEXTURE_LEVELS];
struct GLTexture *next, *prev;
GLint handle;
} GLTexture;
/* buffers */
#define MAX_BUFFERS 2048
typedef struct GLBuffer {
void* data;
GLuint size;
} GLBuffer;
/* shared state */
typedef struct GLSharedState {
GLList** lists;
GLTexture** texture_hash_table;
GLBuffer** buffers;
} GLSharedState;
struct GLContext;
typedef void (*gl_draw_triangle_func)(GLVertex* p0, GLVertex* p1, GLVertex* p2);
/* display context */
typedef struct GLContext {
/* lights */
GLLight lights[MAX_LIGHTS];
/* viewport */
GLViewport viewport;
GLMaterial materials[2];
GLVertex vertex[POLYGON_MAX_VERTEX];
M4 matrix_model_view_inv;
M4 matrix_model_projection;
V4 ambient_light_model;
V4 clear_color;
V4 current_color;
V4 current_normal;
V4 current_tex_coord;
V4 rasterpos;
/*Pointers.*/
/* shared state */
GLSharedState shared_state;
ZBuffer* zb;
GLLight* first_light;
GLTexture* current_texture;
GLParamBuffer* current_op_buffer;
M4* matrix_stack[3];
M4* matrix_stack_ptr[3];
gl_draw_triangle_func draw_triangle_front, draw_triangle_back;
/* resize viewport function */
GLint (*gl_resize_viewport)(GLint* xsize, GLint* ysize);
GLfloat* texcoord_array;
GLfloat* vertex_array;
GLfloat* normal_array;
GLfloat* color_array;
#if TGL_FEATURE_ALT_RENDERMODES == 1
GLfloat* feedback_buffer;
GLuint* select_buffer;
GLuint *select_ptr, *select_hit;
GLfloat* feedback_ptr;
#endif
GLint local_light_model;
GLint lighting_enabled;
GLint light_model_two_side;
/* materials */
GLint color_material_enabled;
GLint current_color_material_mode;
GLint current_color_material_type;
/* textures */
GLint texture_2d_enabled;
/* current list */
GLint current_op_buffer_index;
GLint exec_flag, compile_flag, print_flag;
GLuint listbase;
/* matrix */
GLint matrix_mode;
GLint matrix_stack_depth_max[3];
GLint matrix_model_projection_updated;
GLint matrix_model_projection_no_w_transform;
GLint apply_texture_matrix;
/* current state */
GLint polygon_mode_back;
GLint polygon_mode_front;
GLint current_front_face;
GLint current_shade_model;
GLint current_cull_face;
GLint cull_face_enabled;
GLint normalize_enabled;
/* selection */
#if TGL_FEATURE_ALT_RENDERMODES == 1
GLint render_mode;
GLint select_size;
GLint select_overflow;
GLint select_hits;
#endif
/* glDrawBuffer, glRenderBuffer */
GLenum drawbuffer;
GLenum readbuffer;
/* feedback */
#if TGL_FEATURE_ALT_RENDERMODES == 1
GLuint feedback_size;
GLint feedback_hits;
GLubyte feedback_overflow;
GLenum feedback_type;
/* names */
GLuint name_stack[MAX_NAME_STACK_DEPTH];
GLint name_stack_size;
#endif
/* clear */
GLfloat clear_depth;
GLint current_edge_flag;
/* glBegin / glEnd */
GLint in_begin;
GLint begin_type;
GLint vertex_n, vertex_cnt;
/* opengl 1.1 arrays */
GLint vertex_array_size;
GLint vertex_array_stride;
GLint normal_array_stride;
GLint color_array_size;
GLint color_array_stride;
GLint texcoord_array_size;
GLint texcoord_array_stride;
GLint client_states;
/* opengl 1.1 polygon offset */
GLfloat offset_factor;
GLfloat offset_units;
GLint offset_states;
/* opengl blending */
/* specular buffer. could probably be shared between contexts,
but that wouldn't be 100% thread safe */
#if TGL_FEATURE_SPECULAR_BUFFERS == 1
GLSpecBuf* specbuf_first;
GLint specbuf_used_counter;
GLint specbuf_num_buffers;
#endif
GLint zEnableSpecular;
/* raster position */
GLint rasterpos_zz;
GLfloat pzoomx, pzoomy;
GLVertex rastervertex;
/* text */
GLTEXTSIZE textsize;
/* buffers */
GLint boundarraybuffer;
GLint boundvertexbuffer;
GLint boundnormalbuffer;
GLint boundcolorbuffer;
GLint boundtexcoordbuffer;
GLubyte rasterposvalid;
#if TGL_FEATURE_ERROR_CHECK == 1
GLenum error_flag;
#endif
} GLContext;
extern GLContext gl_ctx;
static GLContext* gl_get_context(void) { return &gl_ctx; }
extern void (*op_table_func[])(GLParam*);
extern GLint op_table_size[];
extern void gl_compile_op(GLParam* p);
static void gl_add_op(GLParam* p) {
GLContext* c = gl_get_context();
#if TGL_FEATURE_ERROR_CHECK == 1
#include "error_check.h"
#endif
GLint op;
op = p[0].op;
if (c->exec_flag) {
op_table_func[op](p);
#if TGL_FEATURE_ERROR_CHECK == 1
#include "error_check.h"
#endif
}
if (c->compile_flag) {
gl_compile_op(p);
#if TGL_FEATURE_ERROR_CHECK == 1
#include "error_check.h"
#endif
}
}
/* select.c */
void gl_add_select(GLuint zmin, GLuint zmax);
void gl_add_feedback(GLfloat token, GLVertex* v1, GLVertex* v2, GLVertex* v3, GLfloat passthrough_token_value);
/* clip.c */
#define CLIP_EPSILON (1E-5)
static GLint gl_clipcode(GLfloat x, GLfloat y, GLfloat z, GLfloat w1) {
GLfloat w;
w = w1 * (1.0 + CLIP_EPSILON);
return (x < -w) | ((x > w) << 1) | ((y < -w) << 2) | ((y > w) << 3) | ((z < -w) << 4) | ((z > w) << 5);
}
#define CLIP_XMIN (1 << 0)
#define CLIP_XMAX (1 << 1)
#define CLIP_YMIN (1 << 2)
#define CLIP_YMAX (1 << 3)
#define CLIP_ZMIN (1 << 4)
#define CLIP_ZMAX (1 << 5)
static GLfloat clampf(GLfloat a, GLfloat min, GLfloat max) {
if (a < min)
return min;
else if (a > max)
return max;
else
return a;
}
/* triangle */
/*
* Clipping
*/
/* We clip the segment [a,b] against the 6 planes of the normal volume.
* We compute the point 'c' of GLintersection and the value of the parameter 't'
* of the GLintersection if x=a+t(b-a).
*/
void gl_draw_triangle(GLVertex* p0, GLVertex* p1, GLVertex* p2);
void gl_draw_line(GLVertex* p0, GLVertex* p1);
void gl_draw_point(GLVertex* p0);
void gl_draw_triangle_point(GLVertex* p0, GLVertex* p1, GLVertex* p2);
void gl_draw_triangle_line(GLVertex* p0, GLVertex* p1, GLVertex* p2);
void gl_draw_triangle_fill(GLVertex* p0, GLVertex* p1, GLVertex* p2);
void gl_draw_triangle_select(GLVertex* p0, GLVertex* p1, GLVertex* p2);
void gl_draw_triangle_feedback(GLVertex* p0, GLVertex* p1, GLVertex* p2);
/* matrix.c */
void gl_print_matrix(const GLfloat* m);
/*
void glopLoadIdentity(GLParam *p);
void glopTranslate(GLParam *p);*/
/* light.c */
void gl_enable_disable_light(GLint light, GLint v);
void gl_shade_vertex(GLVertex* v);
void glInitTextures();
void glEndTextures();
GLTexture* alloc_texture(GLint h);
/* image_util.c */
void gl_convertRGB_to_5R6G5B(GLushort* pixmap, GLubyte* rgb, GLint xsize, GLint ysize);
void gl_convertRGB_to_8A8R8G8B(GLuint* pixmap, GLubyte* rgb, GLint xsize, GLint ysize);
void gl_resizeImage(GLubyte* dest, GLint xsize_dest, GLint ysize_dest, GLubyte* src, GLint xsize_src, GLint ysize_src);
void gl_resizeImageNoInterpolate(GLubyte* dest, GLint xsize_dest, GLint ysize_dest, GLubyte* src, GLint xsize_src, GLint ysize_src);
void gl_fatal_error(char* format, ...);
/* specular buffer "api" */
GLSpecBuf* specbuf_get_buffer(const GLint shininess_i, const GLfloat shininess);
/* glopXXX functions */
#define ADD_OP(a, b, c) void glop##a(GLParam*);
#include "opinfo.h"
/* this clip epsilon is needed to avoid some rounding errors after
several clipping stages */
static void gl_eval_viewport() {
GLContext* c = gl_get_context();
GLViewport* v;
GLfloat zsize = (1 << (ZB_Z_BITS + ZB_POINT_Z_FRAC_BITS));
v = &c->viewport;
v->trans.X = ((v->xsize - 0.5) / 2.0) + v->xmin;
v->trans.Y = ((v->ysize - 0.5) / 2.0) + v->ymin;
v->trans.Z = ((zsize - 0.5) / 2.0) + ((1 << ZB_POINT_Z_FRAC_BITS)) / 2;
v->scale.X = (v->xsize - 0.5) / 2.0;
v->scale.Y = -(v->ysize - 0.5) / 2.0;
v->scale.Z = -((zsize - 0.5) / 2.0);
}
#endif /* _tgl_zgl_h_ */