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