Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

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