Panda3D
zgl.h
1 #ifndef _tgl_zgl_h_
2 #define _tgl_zgl_h_
3 
4 #include "dtoolbase.h"
5 
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <math.h>
9 #include <assert.h>
10 #include "zbuffer.h"
11 #include "zmath.h"
12 #include "zfeatures.h"
13 
14 /* initially # of allocated GLVertexes (will grow when necessary) */
15 #define POLYGON_MAX_VERTEX 16
16 
17 /* Max # of specular light pow buffers */
18 #define MAX_SPECULAR_BUFFERS 8
19 /* # of entries in specular buffer */
20 #define SPECULAR_BUFFER_SIZE 1024
21 /* specular buffer granularity */
22 #define SPECULAR_BUFFER_RESOLUTION 1024
23 
24 
25 #define MAX_MODELVIEW_STACK_DEPTH 32
26 #define MAX_PROJECTION_STACK_DEPTH 8
27 #define MAX_TEXTURE_STACK_DEPTH 8
28 #define MAX_NAME_STACK_DEPTH 64
29 #define MAX_TEXTURE_LEVELS 11
30 #define MAX_LIGHTS 16
31 
32 #define VERTEX_HASH_SIZE 1031
33 
34 #define MAX_DISPLAY_LISTS 1024
35 #define OP_BUFFER_MAX_SIZE 512
36 
37 #define TGL_OFFSET_FILL 0x1
38 #define TGL_OFFSET_LINE 0x2
39 #define TGL_OFFSET_POINT 0x4
40 
41 typedef struct GLSpecBuf {
42  int shininess_i;
43  int last_used;
44  PN_stdfloat buf[SPECULAR_BUFFER_SIZE+1];
45  struct GLSpecBuf *next;
46 } GLSpecBuf;
47 
48 typedef struct GLLight {
49  V4 ambient;
50  V4 diffuse;
51  V4 specular;
52  V4 position;
53  V3 spot_direction;
54  PN_stdfloat spot_exponent;
55  PN_stdfloat spot_cutoff;
56  PN_stdfloat attenuation[3];
57  /* precomputed values */
58  PN_stdfloat cos_spot_cutoff;
59  V3 norm_spot_direction;
60  V3 norm_position;
61  struct GLLight *next;
62 } GLLight;
63 
64 typedef struct GLMaterial {
65  V4 emission;
66  V4 ambient;
67  V4 diffuse;
68  V4 specular;
69  PN_stdfloat shininess;
70 
71  /* computed values */
72  int shininess_i;
73  int do_specular;
74 } GLMaterial;
75 
76 
77 typedef struct GLViewport {
78  int xmin,ymin,xsize,ysize;
79  V3 scale;
80  V3 trans;
81  int updated;
82 } GLViewport;
83 
84 typedef struct GLScissor {
85  PN_stdfloat left, right, bottom, top;
86 } GLScissor;
87 
88 typedef union {
89  int op;
90  PN_stdfloat f;
91  int i;
92  unsigned int ui;
93  void *p;
94 } GLParam;
95 
96 typedef struct GLParamBuffer {
97  GLParam ops[OP_BUFFER_MAX_SIZE];
98  struct GLParamBuffer *next;
100 
101 typedef struct GLList {
102  GLParamBuffer *first_op_buffer;
103  /* TODO: extensions for an hash table or a better allocating scheme */
104 } GLList;
105 
106 typedef struct GLVertex {
107  int edge_flag;
108  V3 normal;
109  V4 coord;
110  V2 tex_coord[MAX_TEXTURE_STAGES];
111  V4 color;
112 
113  /* computed values */
114  V4 ec; /* eye coordinates */
115  V4 pc; /* coordinates in the normalized volume */
116  int clip_code; /* clip code */
117  ZBufferPoint zp; /* integer coordinates for the rasterization */
118 } GLVertex;
119 
120 /* textures */
121 
122 /* The combination of all mipmap levels: one complete texture. */
123 typedef struct GLTexture {
124  ZTextureLevel levels[MAX_MIPMAP_LEVELS];
125  int num_levels;
126  int xsize, ysize;
127  int s_max, t_max;
128  V4 border_color;
129 
130  void *allocated_buffer;
131  int total_bytecount;
132 } GLTexture;
133 
134 struct GLContext;
135 
136 typedef void (*gl_draw_triangle_func)(struct GLContext *c,
137  GLVertex *p0,GLVertex *p1,GLVertex *p2);
138 
139 /* display context */
140 
141 typedef struct GLContext {
142  /* Z buffer */
143  ZBuffer *zb;
144 
145  /* lights */
146  GLLight lights[MAX_LIGHTS];
147  GLLight *first_light;
148  V4 ambient_light_model;
149  int local_light_model;
150  int lighting_enabled;
151  int light_model_two_side;
152 
153  /* materials */
154  GLMaterial materials[2];
155 
156  /* textures */
157  GLTexture *current_textures[MAX_TEXTURE_STAGES];
158  int num_textures_enabled;
159 
160  /* matrix */
161  M4 matrix_projection;
162  M4 matrix_model_view;
163  M4 matrix_model_view_inv;
164  M4 matrix_model_projection;
165  int matrix_model_projection_updated;
166  int matrix_model_projection_no_w_transform;
167  int apply_texture_matrix;
168 
169  /* viewport */
170  GLViewport viewport;
171  GLScissor scissor;
172 
173  /* current state */
174  int smooth_shade_model;
175  int cull_face_enabled;
176  int cull_clockwise;
177  int normalize_enabled;
178  PN_stdfloat normal_scale;
179 
180  gl_draw_triangle_func draw_triangle_front,draw_triangle_back;
181  ZB_fillTriangleFunc zb_fill_tri;
182 
183  /* current vertex state */
184  V4 current_color;
185  V4 current_normal;
186 
187  /* depth test */
188  int depth_test;
189  int zbias;
190  bool has_zrange;
191  double zmin, zrange;
192 
193  /* specular buffer. could probably be shared between contexts,
194  but that wouldn't be 100% thread safe */
195  GLSpecBuf *specbuf_first;
196  int specbuf_used_counter;
197  int specbuf_num_buffers;
198 } GLContext;
199 
200 /* init.c */
201 void glInit(GLContext *c, ZBuffer *zbuffer);
202 void glClose(GLContext *c);
203 
204 /* clip.c */
205 void gl_transform_to_viewport(GLContext *c,GLVertex *v);
206 void gl_draw_triangle(GLContext *c,GLVertex *p0,GLVertex *p1,GLVertex *p2);
207 void gl_draw_line(GLContext *c,GLVertex *p0,GLVertex *p1);
208 void gl_draw_point(GLContext *c,GLVertex *p0);
209 
210 void gl_draw_triangle_point(GLContext *c,
211  GLVertex *p0,GLVertex *p1,GLVertex *p2);
212 void gl_draw_triangle_line(GLContext *c,
213  GLVertex *p0,GLVertex *p1,GLVertex *p2);
214 void gl_draw_triangle_fill(GLContext *c,
215  GLVertex *p0,GLVertex *p1,GLVertex *p2);
216 
217 /* light.c */
218 void gl_enable_disable_light(GLContext *c,int light,int v);
219 void gl_shade_vertex(GLContext *c,GLVertex *v);
220 
221 /* vertex.c */
222 void gl_eval_viewport(GLContext *c);
223 void gl_vertex_transform(GLContext * c, GLVertex * v);
224 
225 /* image_util.c */
226 void gl_convertRGB_to_5R6G5B(unsigned short *pixmap,unsigned char *rgb,
227  int xsize,int ysize);
228 void gl_convertRGB_to_8A8R8G8B(unsigned int *pixmap, unsigned char *rgb,
229  int xsize, int ysize);
230 void gl_resizeImage(unsigned char *dest,int xsize_dest,int ysize_dest,
231  unsigned char *src,int xsize_src,int ysize_src);
232 void gl_resizeImageNoInterpolate(unsigned char *dest,int xsize_dest,int ysize_dest,
233  unsigned char *src,int xsize_src,int ysize_src);
234 
235 GLContext *gl_get_context(void);
236 
237 void gl_fatal_error(const char *format, ...);
238 
239 
240 /* specular buffer "api" */
241 GLSpecBuf *specbuf_get_buffer(GLContext *c, const int shininess_i,
242  const PN_stdfloat shininess);
243 
244 /* this clip epsilon is needed to avoid some rounding errors after
245  several clipping stages */
246 
247 #define CLIP_EPSILON (1E-5f)
248 
249 static inline int gl_clipcode(PN_stdfloat x,PN_stdfloat y,PN_stdfloat z,PN_stdfloat w1)
250 {
251  PN_stdfloat w;
252 
253  w = w1 * (1.0f + CLIP_EPSILON);
254  return (x<-w) |
255  ((x>w)<<1) |
256  ((y<-w)<<2) |
257  ((y>w)<<3) |
258  ((z<-w)<<4) |
259  ((z>w)<<5) ;
260 }
261 
262 #endif /* _tgl_zgl_h_ */
Definition: zmath.h:22
Definition: zmath.h:18
Definition: zmath.h:6
Definition: zgl.h:141
Definition: zgl.h:88
Definition: zmath.h:26
Definition: zgl.h:77
Definition: zgl.h:48
Definition: zgl.h:106
Definition: zgl.h:123
Definition: zgl.h:84
Definition: zgl.h:64
Definition: zgl.h:101
Definition: zgl.h:41