Panda3D
|
00001 #ifndef _tgl_zgl_h_ 00002 #define _tgl_zgl_h_ 00003 00004 #include <stdlib.h> 00005 #include <stdio.h> 00006 #include <math.h> 00007 #include <assert.h> 00008 #include "zbuffer.h" 00009 #include "zmath.h" 00010 #include "zfeatures.h" 00011 00012 /* initially # of allocated GLVertexes (will grow when necessary) */ 00013 #define POLYGON_MAX_VERTEX 16 00014 00015 /* Max # of specular light pow buffers */ 00016 #define MAX_SPECULAR_BUFFERS 8 00017 /* # of entries in specular buffer */ 00018 #define SPECULAR_BUFFER_SIZE 1024 00019 /* specular buffer granularity */ 00020 #define SPECULAR_BUFFER_RESOLUTION 1024 00021 00022 00023 #define MAX_MODELVIEW_STACK_DEPTH 32 00024 #define MAX_PROJECTION_STACK_DEPTH 8 00025 #define MAX_TEXTURE_STACK_DEPTH 8 00026 #define MAX_NAME_STACK_DEPTH 64 00027 #define MAX_TEXTURE_LEVELS 11 00028 #define MAX_LIGHTS 16 00029 00030 #define VERTEX_HASH_SIZE 1031 00031 00032 #define MAX_DISPLAY_LISTS 1024 00033 #define OP_BUFFER_MAX_SIZE 512 00034 00035 #define TGL_OFFSET_FILL 0x1 00036 #define TGL_OFFSET_LINE 0x2 00037 #define TGL_OFFSET_POINT 0x4 00038 00039 typedef struct GLSpecBuf { 00040 int shininess_i; 00041 int last_used; 00042 PN_stdfloat buf[SPECULAR_BUFFER_SIZE+1]; 00043 struct GLSpecBuf *next; 00044 } GLSpecBuf; 00045 00046 typedef struct GLLight { 00047 V4 ambient; 00048 V4 diffuse; 00049 V4 specular; 00050 V4 position; 00051 V3 spot_direction; 00052 PN_stdfloat spot_exponent; 00053 PN_stdfloat spot_cutoff; 00054 PN_stdfloat attenuation[3]; 00055 /* precomputed values */ 00056 PN_stdfloat cos_spot_cutoff; 00057 V3 norm_spot_direction; 00058 V3 norm_position; 00059 struct GLLight *next; 00060 } GLLight; 00061 00062 typedef struct GLMaterial { 00063 V4 emission; 00064 V4 ambient; 00065 V4 diffuse; 00066 V4 specular; 00067 PN_stdfloat shininess; 00068 00069 /* computed values */ 00070 int shininess_i; 00071 int do_specular; 00072 } GLMaterial; 00073 00074 00075 typedef struct GLViewport { 00076 int xmin,ymin,xsize,ysize; 00077 V3 scale; 00078 V3 trans; 00079 int updated; 00080 } GLViewport; 00081 00082 typedef struct GLScissor { 00083 PN_stdfloat left, right, bottom, top; 00084 } GLScissor; 00085 00086 typedef union { 00087 int op; 00088 PN_stdfloat f; 00089 int i; 00090 unsigned int ui; 00091 void *p; 00092 } GLParam; 00093 00094 typedef struct GLParamBuffer { 00095 GLParam ops[OP_BUFFER_MAX_SIZE]; 00096 struct GLParamBuffer *next; 00097 } GLParamBuffer; 00098 00099 typedef struct GLList { 00100 GLParamBuffer *first_op_buffer; 00101 /* TODO: extensions for an hash table or a better allocating scheme */ 00102 } GLList; 00103 00104 typedef struct GLVertex { 00105 int edge_flag; 00106 V3 normal; 00107 V4 coord; 00108 V2 tex_coord[MAX_TEXTURE_STAGES]; 00109 V4 color; 00110 00111 /* computed values */ 00112 V4 ec; /* eye coordinates */ 00113 V4 pc; /* coordinates in the normalized volume */ 00114 int clip_code; /* clip code */ 00115 ZBufferPoint zp; /* integer coordinates for the rasterization */ 00116 } GLVertex; 00117 00118 /* textures */ 00119 00120 /* The combination of all mipmap levels: one complete texture. */ 00121 typedef struct GLTexture { 00122 ZTextureLevel levels[MAX_MIPMAP_LEVELS]; 00123 int num_levels; 00124 int xsize, ysize; 00125 int s_max, t_max; 00126 V4 border_color; 00127 00128 void *allocated_buffer; 00129 int total_bytecount; 00130 } GLTexture; 00131 00132 struct GLContext; 00133 00134 typedef void (*gl_draw_triangle_func)(struct GLContext *c, 00135 GLVertex *p0,GLVertex *p1,GLVertex *p2); 00136 00137 /* display context */ 00138 00139 typedef struct GLContext { 00140 /* Z buffer */ 00141 ZBuffer *zb; 00142 00143 /* lights */ 00144 GLLight lights[MAX_LIGHTS]; 00145 GLLight *first_light; 00146 V4 ambient_light_model; 00147 int local_light_model; 00148 int lighting_enabled; 00149 int light_model_two_side; 00150 00151 /* materials */ 00152 GLMaterial materials[2]; 00153 00154 /* textures */ 00155 GLTexture *current_textures[MAX_TEXTURE_STAGES]; 00156 int num_textures_enabled; 00157 00158 /* matrix */ 00159 M4 matrix_projection; 00160 M4 matrix_model_view; 00161 M4 matrix_model_view_inv; 00162 M4 matrix_model_projection; 00163 int matrix_model_projection_updated; 00164 int matrix_model_projection_no_w_transform; 00165 int apply_texture_matrix; 00166 00167 /* viewport */ 00168 GLViewport viewport; 00169 GLScissor scissor; 00170 00171 /* current state */ 00172 int smooth_shade_model; 00173 int cull_face_enabled; 00174 int cull_clockwise; 00175 int normalize_enabled; 00176 PN_stdfloat normal_scale; 00177 00178 gl_draw_triangle_func draw_triangle_front,draw_triangle_back; 00179 ZB_fillTriangleFunc zb_fill_tri; 00180 00181 /* current vertex state */ 00182 V4 current_color; 00183 V4 current_normal; 00184 00185 /* depth test */ 00186 int depth_test; 00187 int zbias; 00188 00189 /* specular buffer. could probably be shared between contexts, 00190 but that wouldn't be 100% thread safe */ 00191 GLSpecBuf *specbuf_first; 00192 int specbuf_used_counter; 00193 int specbuf_num_buffers; 00194 } GLContext; 00195 00196 /* init.c */ 00197 void glInit(GLContext *c, ZBuffer *zbuffer); 00198 void glClose(GLContext *c); 00199 00200 /* clip.c */ 00201 void gl_transform_to_viewport(GLContext *c,GLVertex *v); 00202 void gl_draw_triangle(GLContext *c,GLVertex *p0,GLVertex *p1,GLVertex *p2); 00203 void gl_draw_line(GLContext *c,GLVertex *p0,GLVertex *p1); 00204 void gl_draw_point(GLContext *c,GLVertex *p0); 00205 00206 void gl_draw_triangle_point(GLContext *c, 00207 GLVertex *p0,GLVertex *p1,GLVertex *p2); 00208 void gl_draw_triangle_line(GLContext *c, 00209 GLVertex *p0,GLVertex *p1,GLVertex *p2); 00210 void gl_draw_triangle_fill(GLContext *c, 00211 GLVertex *p0,GLVertex *p1,GLVertex *p2); 00212 00213 /* light.c */ 00214 void gl_enable_disable_light(GLContext *c,int light,int v); 00215 void gl_shade_vertex(GLContext *c,GLVertex *v); 00216 00217 /* vertex.c */ 00218 void gl_eval_viewport(GLContext *c); 00219 void gl_vertex_transform(GLContext * c, GLVertex * v); 00220 00221 /* image_util.c */ 00222 void gl_convertRGB_to_5R6G5B(unsigned short *pixmap,unsigned char *rgb, 00223 int xsize,int ysize); 00224 void gl_convertRGB_to_8A8R8G8B(unsigned int *pixmap, unsigned char *rgb, 00225 int xsize, int ysize); 00226 void gl_resizeImage(unsigned char *dest,int xsize_dest,int ysize_dest, 00227 unsigned char *src,int xsize_src,int ysize_src); 00228 void gl_resizeImageNoInterpolate(unsigned char *dest,int xsize_dest,int ysize_dest, 00229 unsigned char *src,int xsize_src,int ysize_src); 00230 00231 GLContext *gl_get_context(void); 00232 00233 void gl_fatal_error(const char *format, ...); 00234 00235 00236 /* specular buffer "api" */ 00237 GLSpecBuf *specbuf_get_buffer(GLContext *c, const int shininess_i, 00238 const PN_stdfloat shininess); 00239 00240 /* this clip epsilon is needed to avoid some rounding errors after 00241 several clipping stages */ 00242 00243 #define CLIP_EPSILON (1E-5f) 00244 00245 static inline int gl_clipcode(PN_stdfloat x,PN_stdfloat y,PN_stdfloat z,PN_stdfloat w1) 00246 { 00247 PN_stdfloat w; 00248 00249 w = w1 * (1.0f + CLIP_EPSILON); 00250 return (x<-w) | 00251 ((x>w)<<1) | 00252 ((y<-w)<<2) | 00253 ((y>w)<<3) | 00254 ((z<-w)<<4) | 00255 ((z>w)<<5) ; 00256 } 00257 00258 #endif /* _tgl_zgl_h_ */