TinyGL 0.4.1 for MinGW
zgl.h
Go to the documentation of this file.
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 <assert.h>
8#include <string.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;
56 struct GLSpecBuf *next;
58
59typedef struct GLLight {
67 float attenuation[3];
68 /* precomputed values */
72 /* we use a linked list to know which are the enabled lights */
74 struct GLLight *next,*prev;
76
77typedef struct GLMaterial {
82 float shininess;
83
84 /* computed values */
88
89
90typedef struct 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 {
109
110typedef struct GLList {
112 /* TODO: extensions for an hash table or a better allocating scheme */
114
115typedef struct GLVertex {
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 */
128
129typedef struct GLImage {
130 void *pixmap;
133
134/* textures */
135
136#define TEXTURE_HASH_TABLE_SIZE 256
137
138typedef struct GLTexture {
143
144
145/* shared state */
146
147typedef struct 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 */
162
163 /* lights */
170
171 /* materials */
176
177 /* textures */
180
181 /* shared state */
183
184 /* current list */
188
189 /* matrix */
190
195
201
202 /* viewport */
204
205 /* current state */
208
215
216 /* selection */
218 unsigned int *select_buffer;
220 unsigned int *select_ptr,*select_hit;
223
224 /* names */
227
228 /* clear */
231
232 /* current vertex state */
234 unsigned int longcurrent_color[3]; /* precomputed integer color */
238
239 /* glBegin / glEnd */
245
246 /* opengl 1.1 arrays */
259
260 /* opengl 1.1 polygon offset */
264
265 /* specular buffer. could probably be shared between contexts,
266 but that wouldn't be 100% thread safe */
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 */
279
280extern GLContext *gl_ctx;
281
282void gl_add_op(GLParam *p);
283
284/* clip.c */
287void gl_draw_line(GLContext *c,GLVertex *p0,GLVertex *p1);
288void gl_draw_point(GLContext *c,GLVertex *p0);
289
291 GLVertex *p0,GLVertex *p1,GLVertex *p2);
293 GLVertex *p0,GLVertex *p1,GLVertex *p2);
295 GLVertex *p0,GLVertex *p1,GLVertex *p2);
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);
309
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
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_ */
Definition: zgl.h:159
int select_hits
Definition: zgl.h:222
float offset_units
Definition: zgl.h:262
int print_flag
Definition: zgl.h:187
int render_mode
Definition: zgl.h:217
GLMaterial materials[2]
Definition: zgl.h:172
float * texcoord_array
Definition: zgl.h:255
float offset_factor
Definition: zgl.h:261
int specbuf_num_buffers
Definition: zgl.h:269
float * normal_array
Definition: zgl.h:250
int texcoord_array_stride
Definition: zgl.h:257
GLLight * first_light
Definition: zgl.h:165
V4 current_tex_coord
Definition: zgl.h:236
M4 * matrix_stack[3]
Definition: zgl.h:192
int cull_face_enabled
Definition: zgl.h:212
int current_edge_flag
Definition: zgl.h:237
int matrix_model_projection_updated
Definition: zgl.h:198
float * vertex_array
Definition: zgl.h:247
unsigned int * select_ptr
Definition: zgl.h:220
int in_begin
Definition: zgl.h:240
V4 ambient_light_model
Definition: zgl.h:166
int begin_type
Definition: zgl.h:241
int matrix_mode
Definition: zgl.h:191
int polygon_mode_back
Definition: zgl.h:206
int color_material_enabled
Definition: zgl.h:173
GLSharedState shared_state
Definition: zgl.h:182
int current_front_face
Definition: zgl.h:209
int depth_test
Definition: zgl.h:277
int matrix_model_projection_no_w_transform
Definition: zgl.h:199
M4 * matrix_stack_ptr[3]
Definition: zgl.h:193
int select_size
Definition: zgl.h:219
int vertex_max
Definition: zgl.h:243
int texcoord_array_size
Definition: zgl.h:256
GLVertex * vertex
Definition: zgl.h:244
int matrix_stack_depth_max[3]
Definition: zgl.h:194
int vertex_n
Definition: zgl.h:242
int vertex_array_size
Definition: zgl.h:248
int current_color_material_mode
Definition: zgl.h:174
int current_color_material_type
Definition: zgl.h:175
V4 clear_color
Definition: zgl.h:230
int normalize_enabled
Definition: zgl.h:213
int current_op_buffer_index
Definition: zgl.h:186
M4 matrix_model_projection
Definition: zgl.h:197
int color_array_stride
Definition: zgl.h:254
GLSpecBuf * specbuf_first
Definition: zgl.h:267
float clear_depth
Definition: zgl.h:229
GLLight lights[MAX_LIGHTS]
Definition: zgl.h:164
ZBuffer * zb
Definition: zgl.h:161
gl_draw_triangle_func draw_triangle_back
Definition: zgl.h:214
int light_model_two_side
Definition: zgl.h:169
int compile_flag
Definition: zgl.h:187
int exec_flag
Definition: zgl.h:187
GLParamBuffer * current_op_buffer
Definition: zgl.h:185
unsigned int * select_buffer
Definition: zgl.h:218
int apply_texture_matrix
Definition: zgl.h:200
void * opaque
Definition: zgl.h:272
int name_stack_size
Definition: zgl.h:226
GLViewport viewport
Definition: zgl.h:203
int lighting_enabled
Definition: zgl.h:168
V4 current_normal
Definition: zgl.h:235
int current_shade_model
Definition: zgl.h:210
int normal_array_stride
Definition: zgl.h:251
unsigned int longcurrent_color[3]
Definition: zgl.h:234
int texture_2d_enabled
Definition: zgl.h:179
unsigned int name_stack[MAX_NAME_STACK_DEPTH]
Definition: zgl.h:225
unsigned int * select_hit
Definition: zgl.h:220
int local_light_model
Definition: zgl.h:167
gl_draw_triangle_func draw_triangle_front
Definition: zgl.h:214
int polygon_mode_front
Definition: zgl.h:207
M4 matrix_model_view_inv
Definition: zgl.h:196
int offset_states
Definition: zgl.h:263
int vertex_array_stride
Definition: zgl.h:249
GLTexture * current_texture
Definition: zgl.h:178
float * color_array
Definition: zgl.h:252
int current_cull_face
Definition: zgl.h:211
V4 current_color
Definition: zgl.h:233
int color_array_size
Definition: zgl.h:253
int specbuf_used_counter
Definition: zgl.h:268
int client_states
Definition: zgl.h:258
int vertex_cnt
Definition: zgl.h:242
int(* gl_resize_viewport)(struct GLContext *c, int *xsize, int *ysize)
Definition: zgl.h:274
int select_overflow
Definition: zgl.h:221
Definition: zgl.h:129
void * pixmap
Definition: zgl.h:130
int ysize
Definition: zgl.h:131
int xsize
Definition: zgl.h:131
Definition: zgl.h:59
int enabled
Definition: zgl.h:73
float cos_spot_cutoff
Definition: zgl.h:69
V4 ambient
Definition: zgl.h:60
float spot_exponent
Definition: zgl.h:65
struct GLLight * next
Definition: zgl.h:74
float spot_cutoff
Definition: zgl.h:66
V3 norm_position
Definition: zgl.h:71
V4 specular
Definition: zgl.h:62
V3 norm_spot_direction
Definition: zgl.h:70
struct GLLight * prev
Definition: zgl.h:74
V4 position
Definition: zgl.h:63
V3 spot_direction
Definition: zgl.h:64
float attenuation[3]
Definition: zgl.h:67
V4 diffuse
Definition: zgl.h:61
Definition: zgl.h:110
GLParamBuffer * first_op_buffer
Definition: zgl.h:111
Definition: zgl.h:77
V4 emission
Definition: zgl.h:78
V4 ambient
Definition: zgl.h:79
V4 specular
Definition: zgl.h:81
int shininess_i
Definition: zgl.h:85
float shininess
Definition: zgl.h:82
int do_specular
Definition: zgl.h:86
V4 diffuse
Definition: zgl.h:80
GLParam ops[OP_BUFFER_MAX_SIZE]
Definition: zgl.h:106
struct GLParamBuffer * next
Definition: zgl.h:107
GLList ** lists
Definition: zgl.h:148
GLTexture ** texture_hash_table
Definition: zgl.h:149
float buf[SPECULAR_BUFFER_SIZE+1]
Definition: specbuf.h:14
int last_used
Definition: specbuf.h:13
int shininess_i
Definition: specbuf.h:12
struct GLSpecBuf * next
Definition: specbuf.h:15
Definition: zgl.h:138
int handle
Definition: zgl.h:140
GLImage images[MAX_TEXTURE_LEVELS]
Definition: zgl.h:139
struct GLTexture * next
Definition: zgl.h:141
struct GLTexture * prev
Definition: zgl.h:141
Definition: zgl.h:115
ZBufferPoint zp
Definition: zgl.h:126
int edge_flag
Definition: zgl.h:116
V4 color
Definition: zgl.h:120
V4 coord
Definition: zgl.h:118
int clip_code
Definition: zgl.h:125
V4 ec
Definition: zgl.h:123
V4 tex_coord
Definition: zgl.h:119
V3 normal
Definition: zgl.h:117
V4 pc
Definition: zgl.h:124
Definition: zgl.h:90
V3 trans
Definition: zgl.h:93
int xmin
Definition: zgl.h:91
int ymin
Definition: zgl.h:91
int ysize
Definition: zgl.h:91
int updated
Definition: zgl.h:94
int xsize
Definition: zgl.h:91
V3 scale
Definition: zgl.h:92
Definition: zmath.h:6
Definition: zmath.h:24
Definition: zmath.h:28
Definition: zgl.h:97
void * p
Definition: zgl.h:102
int op
Definition: zgl.h:98
unsigned int ui
Definition: zgl.h:101
int i
Definition: zgl.h:100
float f
Definition: zgl.h:99
GLSpecBuf * specbuf_get_buffer(GLContext *c, const int shininess_i, const float shininess)
Definition: specbuf.c:19
struct GLContext GLContext
struct GLMaterial GLMaterial
void gl_enable_disable_light(GLContext *c, int light, int v)
Definition: light.c:164
struct GLSharedState GLSharedState
GLContext * gl_get_context(void)
Definition: list.c:25
void gl_shade_vertex(GLContext *c, GLVertex *v)
Definition: light.c:181
#define dprintf(format, args...)
Definition: zgl.h:340
void glInitTextures(GLContext *c)
Definition: texture.c:62
struct GLVertex GLVertex
GLContext * gl_ctx
Definition: init.c:3
#define SPECULAR_BUFFER_SIZE
Definition: zgl.h:31
struct GLViewport GLViewport
void gl_draw_triangle(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2)
Definition: clip.c:241
#define MAX_TEXTURE_LEVELS
Definition: zgl.h:40
#define MAX_LIGHTS
Definition: zgl.h:41
void gl_convertRGB_to_8A8R8G8B(unsigned int *pixmap, unsigned char *rgb, int xsize, int ysize)
Definition: image_util.c:21
void gl_draw_point(GLContext *c, GLVertex *p0)
Definition: clip.c:65
#define MAX_NAME_STACK_DEPTH
Definition: zgl.h:39
void gl_draw_triangle_fill(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2)
Definition: clip.c:383
struct GLLight GLLight
void gl_resizeImage(unsigned char *dest, int xsize_dest, int ysize_dest, unsigned char *src, int xsize_src, int ysize_src)
Definition: image_util.c:54
void gl_convertRGB_to_5R6G5B(unsigned short *pixmap, unsigned char *rgb, int xsize, int ysize)
Definition: image_util.c:7
struct GLParamBuffer GLParamBuffer
void glEndTextures(GLContext *c)
GLTexture * alloc_texture(GLContext *c, int h)
Definition: texture.c:43
void gl_transform_to_viewport(GLContext *c, GLVertex *v)
Definition: clip.c:13
void gl_add_select(GLContext *c, unsigned int zmin, unsigned int zmax)
Definition: select.c:88
void gl_draw_line(GLContext *c, GLVertex *p0, GLVertex *p1)
Definition: clip.c:112
#define CLIP_EPSILON
Definition: zgl.h:358
#define OP_BUFFER_MAX_SIZE
Definition: zgl.h:46
struct GLSpecBuf GLSpecBuf
void gl_print_matrix(const float *m)
Definition: matrix.c:3
void gl_draw_triangle_point(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2)
Definition: clip.c:435
void(* gl_draw_triangle_func)(struct GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2)
Definition: zgl.h:154
void gl_add_op(GLParam *p)
Definition: list.c:137
struct GLList GLList
struct GLTexture GLTexture
void gl_fatal_error(char *format,...)
Definition: error.c:4
void gl_draw_triangle_select(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2)
Definition: clip.c:373
struct GLImage GLImage
void gl_resizeImageNoInterpolate(unsigned char *dest, int xsize_dest, int ysize_dest, unsigned char *src, int xsize_src, int ysize_src)
Definition: image_util.c:105
void gl_draw_triangle_line(GLContext *c, GLVertex *p0, GLVertex *p1, GLVertex *p2)
Definition: clip.c:418