version 2.8
zgl.h
1 #ifndef _tgl_zgl_h_
2 #define _tgl_zgl_h_
3 
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <math.h>
7 #include <GL/gl.h>
8 #include "zbuffer.h"
9 #include "zmath.h"
10 #include "zfeatures.h"
11 
12 #define DEBUG
13 /* #define NDEBUG */
14 
15 enum {
16 
17 #define ADD_OP(a,b,c) OP_ ## a ,
18 
19 #include "opinfo.h"
20 
21 };
22 
23 /* initially # of allocated GLVertexes (will grow when necessary) */
24 #define POLYGON_MAX_VERTEX 16
25 
26 /* Max # of specular light pow buffers */
27 #define MAX_SPECULAR_BUFFERS 8
28 /* # of entries in specular buffer */
29 #define SPECULAR_BUFFER_SIZE 1024
30 /* specular buffer granularity */
31 #define SPECULAR_BUFFER_RESOLUTION 1024
32 
33 
34 #define MAX_MODELVIEW_STACK_DEPTH 32
35 #define MAX_PROJECTION_STACK_DEPTH 8
36 #define MAX_TEXTURE_STACK_DEPTH 8
37 #define MAX_NAME_STACK_DEPTH 64
38 #define MAX_TEXTURE_LEVELS 11
39 #define MAX_LIGHTS 16
40 
41 #define VERTEX_HASH_SIZE 1031
42 
43 #define MAX_DISPLAY_LISTS 1024
44 #define OP_BUFFER_MAX_SIZE 512
45 
46 #define TGL_OFFSET_FILL 0x1
47 #define TGL_OFFSET_LINE 0x2
48 #define TGL_OFFSET_POINT 0x4
49 
50 typedef struct GLSpecBuf {
51  int shininess_i;
52  int last_used;
53  float buf[SPECULAR_BUFFER_SIZE+1];
54  struct GLSpecBuf *next;
55 } GLSpecBuf;
56 
57 typedef struct GLLight {
58  V4 ambient;
59  V4 diffuse;
60  V4 specular;
61  V4 position;
62  V3 spot_direction;
63  float spot_exponent;
64  float spot_cutoff;
65  float attenuation[3];
66  /* precomputed values */
67  float cos_spot_cutoff;
68  V3 norm_spot_direction;
69  V3 norm_position;
70  /* we use a linked list to know which are the enabled lights */
71  int enabled;
72  struct GLLight *next,*prev;
73 } GLLight;
74 
75 typedef struct GLMaterial {
76  V4 emission;
77  V4 ambient;
78  V4 diffuse;
79  V4 specular;
80  float shininess;
81 
82  /* computed values */
83  int shininess_i;
84  int do_specular;
85 } GLMaterial;
86 
87 
88 typedef struct GLViewport {
89  int xmin,ymin,xsize,ysize;
90  V3 scale;
91  V3 trans;
92  int updated;
93 } GLViewport;
94 
95 typedef union {
96  int op;
97  float f;
98  int i;
99  unsigned int ui;
100  void *p;
101 } GLParam;
102 
103 typedef struct GLParamBuffer {
104  GLParam ops[OP_BUFFER_MAX_SIZE];
105  struct GLParamBuffer *next;
106 } GLParamBuffer;
107 
108 typedef struct GLList {
109  GLParamBuffer *first_op_buffer;
110  /* TODO: extensions for an hash table or a better allocating scheme */
111 } GLList;
112 
113 typedef struct GLVertex {
114  int edge_flag;
115  V3 normal;
116  V4 coord;
117  V4 tex_coord;
118  V4 color;
119 
120  /* computed values */
121  V4 ec; /* eye coordinates */
122  V4 pc; /* coordinates in the normalized volume */
123  int clip_code; /* clip code */
124  ZBufferPoint zp; /* integer coordinates for the rasterization */
125 } GLVertex;
126 
127 typedef struct GLImage {
128  void *pixmap;
129  int xsize,ysize;
130 } GLImage;
131 
132 /* textures */
133 
134 #define TEXTURE_HASH_TABLE_SIZE 256
135 
136 typedef struct GLTexture {
137  GLImage images[MAX_TEXTURE_LEVELS];
138  int handle;
139  struct GLTexture *next,*prev;
140 } GLTexture;
141 
142 
143 /* shared state */
144 
145 typedef struct GLSharedState {
146  GLList **lists;
147  GLTexture **texture_hash_table;
148 } GLSharedState;
149 
150 struct GLContext;
151 
152 typedef void (*gl_draw_triangle_func)(struct GLContext *c,
153  GLVertex *p0,GLVertex *p1,GLVertex *p2);
154 
155 /* display context */
156 
157 typedef struct GLContext {
158  /* Z buffer */
159  ZBuffer *zb;
160 
161  /* lights */
162  GLLight lights[MAX_LIGHTS];
163  GLLight *first_light;
164  V4 ambient_light_model;
165  int local_light_model;
166  int lighting_enabled;
167  int light_model_two_side;
168 
169  /* materials */
170  GLMaterial materials[2];
171  int color_material_enabled;
172  int current_color_material_mode;
173  int current_color_material_type;
174 
175  /* textures */
176  GLTexture *current_texture;
177  int texture_2d_enabled;
178 
179  /* shared state */
180  GLSharedState shared_state;
181 
182  /* current list */
183  GLParamBuffer *current_op_buffer;
184  int current_op_buffer_index;
185  int exec_flag,compile_flag,print_flag;
186 
187  /* matrix */
188 
189  int matrix_mode;
190  M4 *matrix_stack[3];
191  M4 *matrix_stack_ptr[3];
192  int matrix_stack_depth_max[3];
193 
194  M4 matrix_model_view_inv;
195  M4 matrix_model_projection;
196  int matrix_model_projection_updated;
197  int matrix_model_projection_no_w_transform;
198  int apply_texture_matrix;
199 
200  /* viewport */
201  GLViewport viewport;
202 
203  /* current state */
204  int polygon_mode_back;
205  int polygon_mode_front;
206 
207  int current_front_face;
208  int current_shade_model;
209  int current_cull_face;
210  int cull_face_enabled;
211  int normalize_enabled;
212  gl_draw_triangle_func draw_triangle_front,draw_triangle_back;
213 
214  /* selection */
215  int render_mode;
216  unsigned int *select_buffer;
217  int select_size;
218  unsigned int *select_ptr,*select_hit;
219  int select_overflow;
220  int select_hits;
221 
222  /* names */
223  unsigned int name_stack[MAX_NAME_STACK_DEPTH];
224  int name_stack_size;
225 
226  /* clear */
227  float clear_depth;
228  V4 clear_color;
229 
230  /* current vertex state */
231  V4 current_color;
232  unsigned int longcurrent_color[3]; /* precomputed integer color */
233  V4 current_normal;
234  V4 current_tex_coord;
235  int current_edge_flag;
236 
237  /* glBegin / glEnd */
238  int in_begin;
239  int begin_type;
240  int vertex_n,vertex_cnt;
241  int vertex_max;
242  GLVertex *vertex;
243 
244  /* opengl 1.1 arrays */
245  float *vertex_array;
246  int vertex_array_size;
247  int vertex_array_stride;
248  float *normal_array;
249  int normal_array_stride;
250  float *color_array;
251  int color_array_size;
252  int color_array_stride;
253  float *texcoord_array;
254  int texcoord_array_size;
255  int texcoord_array_stride;
256  int client_states;
257 
258  /* opengl 1.1 polygon offset */
259  float offset_factor;
260  float offset_units;
261  int offset_states;
262 
263  /* specular buffer. could probably be shared between contexts,
264  but that wouldn't be 100% thread safe */
265  GLSpecBuf *specbuf_first;
266  int specbuf_used_counter;
267  int specbuf_num_buffers;
268 
269  /* opaque structure for user's use */
270  void *opaque;
271  /* resize viewport function */
272  int (*gl_resize_viewport)(struct GLContext *c,int *xsize,int *ysize);
273 
274  /* depth test */
275  int depth_test;
276 } GLContext;
277 
278 extern GLContext *gl_ctx;
279 
280 void gl_add_op(GLParam *p);
281 
282 /* clip.c */
283 void gl_transform_to_viewport(GLContext *c,GLVertex *v);
284 void gl_draw_triangle(GLContext *c,GLVertex *p0,GLVertex *p1,GLVertex *p2);
285 void gl_draw_line(GLContext *c,GLVertex *p0,GLVertex *p1);
286 void gl_draw_point(GLContext *c,GLVertex *p0);
287 
288 void gl_draw_triangle_point(GLContext *c,
289  GLVertex *p0,GLVertex *p1,GLVertex *p2);
290 void gl_draw_triangle_line(GLContext *c,
291  GLVertex *p0,GLVertex *p1,GLVertex *p2);
292 void gl_draw_triangle_fill(GLContext *c,
293  GLVertex *p0,GLVertex *p1,GLVertex *p2);
294 void gl_draw_triangle_select(GLContext *c,
295  GLVertex *p0,GLVertex *p1,GLVertex *p2);
296 
297 /* matrix.c */
298 void gl_print_matrix(const float *m);
299 /*
300 void glopLoadIdentity(GLContext *c,GLParam *p);
301 void glopTranslate(GLContext *c,GLParam *p);*/
302 
303 /* light.c */
304 void gl_add_select(GLContext *c,unsigned int zmin,unsigned int zmax);
305 void gl_enable_disable_light(GLContext *c,int light,int v);
306 void gl_shade_vertex(GLContext *c,GLVertex *v);
307 
308 void glInitTextures(GLContext *c);
309 void glEndTextures(GLContext *c);
310 GLTexture *alloc_texture(GLContext *c,int h);
311 
312 /* image_util.c */
313 void gl_convertRGB_to_5R6G5B(unsigned short *pixmap,unsigned char *rgb,
314  int xsize,int ysize);
315 void gl_convertRGB_to_8A8R8G8B(unsigned int *pixmap, unsigned char *rgb,
316  int xsize, int ysize);
317 void gl_resizeImage(unsigned char *dest,int xsize_dest,int ysize_dest,
318  unsigned char *src,int xsize_src,int ysize_src);
319 void gl_resizeImageNoInterpolate(unsigned char *dest,int xsize_dest,int ysize_dest,
320  unsigned char *src,int xsize_src,int ysize_src);
321 
322 GLContext *gl_get_context(void);
323 
324 void gl_fatal_error(char *format, ...);
325 void gl_assert(int test);
326 
327 
328 /* specular buffer "api" */
329 GLSpecBuf *specbuf_get_buffer(GLContext *c, const int shininess_i,
330  const float shininess);
331 
332 #ifdef __BEOS__
333 void dprintf(const char *, ...);
334 
335 #else /* !BEOS */
336 
337 #ifdef DEBUG
338 
339 #define dprintf(format, args...) \
340  fprintf(stderr,"In '%s': " format "\n",__FUNCTION__, ##args);
341 
342 #else
343 
344 #define dprintf(format, args...)
345 
346 #endif
347 #endif /* !BEOS */
348 
349 /* glopXXX functions */
350 
351 #define ADD_OP(a,b,c) void glop ## a (GLContext *,GLParam *);
352 #include "opinfo.h"
353 
354 /* this clip epsilon is needed to avoid some rounding errors after
355  several clipping stages */
356 
357 #define CLIP_EPSILON (1E-5)
358 
359 static inline int gl_clipcode(float x,float y,float z,float w1)
360 {
361  float w;
362 
363  w=w1 * (1.0 + CLIP_EPSILON);
364  return (x<-w) |
365  ((x>w)<<1) |
366  ((y<-w)<<2) |
367  ((y>w)<<3) |
368  ((z<-w)<<4) |
369  ((z>w)<<5) ;
370 }
371 
372 #endif /* _tgl_zgl_h_ */