source: trunk/sys/TinyGL/src/zgl.h @ 50

Last change on this file since 50 was 1, checked in by alain, 8 years ago

First import

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