source: trunk/sys/TinyGL/src/init.c @ 50

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

First import

File size: 4.0 KB
Line 
1#include "zgl.h"
2
3GLContext *gl_ctx;
4
5
6void initSharedState(GLContext *c)
7{
8  GLSharedState *s=&c->shared_state;
9  s->lists=gl_zalloc(sizeof(GLList *) * MAX_DISPLAY_LISTS);
10  s->texture_hash_table=
11      gl_zalloc(sizeof(GLTexture *) * TEXTURE_HASH_TABLE_SIZE);
12
13  alloc_texture(c,0);
14}
15
16void endSharedState(GLContext *c)
17{
18  GLSharedState *s=&c->shared_state;
19  int i;
20
21  for(i=0;i<MAX_DISPLAY_LISTS;i++) {
22    /* TODO */
23  }
24  gl_free(s->lists);
25
26  gl_free(s->texture_hash_table);
27}
28
29
30void glInit(void *zbuffer1)
31{
32  ZBuffer *zbuffer=(ZBuffer *)zbuffer1;
33  GLContext *c;
34  GLViewport *v;
35  int i;
36
37  c=gl_zalloc(sizeof(GLContext));
38  gl_ctx=c;
39
40  c->zb=zbuffer;
41
42  /* allocate GLVertex array */
43  c->vertex_max = POLYGON_MAX_VERTEX;
44  c->vertex = gl_malloc(POLYGON_MAX_VERTEX*sizeof(GLVertex));
45 
46  /* viewport */
47  v=&c->viewport;
48  v->xmin=0;
49  v->ymin=0;
50  v->xsize=zbuffer->xsize;
51  v->ysize=zbuffer->ysize;
52  v->updated=1;
53
54  /* shared state */
55  initSharedState(c);
56
57  /* lists */
58
59  c->exec_flag=1;
60  c->compile_flag=0;
61  c->print_flag=0;
62
63  c->in_begin=0;
64
65  /* lights */
66  for(i=0;i<MAX_LIGHTS;i++) {
67    GLLight *l=&c->lights[i];
68    l->ambient=gl_V4_New(0,0,0,1);
69    l->diffuse=gl_V4_New(1,1,1,1);
70    l->specular=gl_V4_New(1,1,1,1);
71    l->position=gl_V4_New(0,0,1,0);
72    l->norm_position=gl_V3_New(0,0,1);
73    l->spot_direction=gl_V3_New(0,0,-1);
74    l->norm_spot_direction=gl_V3_New(0,0,-1);
75    l->spot_exponent=0;
76    l->spot_cutoff=180;
77    l->attenuation[0]=1;
78    l->attenuation[1]=0;
79    l->attenuation[2]=0;
80    l->enabled=0;
81  }
82  c->first_light=NULL;
83  c->ambient_light_model=gl_V4_New(0.2,0.2,0.2,1);
84  c->local_light_model=0;
85  c->lighting_enabled=0;
86  c->light_model_two_side = 0;
87
88  /* default materials */
89  for(i=0;i<2;i++) {
90    GLMaterial *m=&c->materials[i];
91    m->emission=gl_V4_New(0,0,0,1);
92    m->ambient=gl_V4_New(0.2,0.2,0.2,1);
93    m->diffuse=gl_V4_New(0.8,0.8,0.8,1);
94    m->specular=gl_V4_New(0,0,0,1);
95    m->shininess=0;
96  }
97  c->current_color_material_mode=GL_FRONT_AND_BACK;
98  c->current_color_material_type=GL_AMBIENT_AND_DIFFUSE;
99  c->color_material_enabled=0;
100
101  /* textures */
102  glInitTextures(c);
103
104  /* default state */
105  c->current_color.X=1.0;
106  c->current_color.Y=1.0;
107  c->current_color.Z=1.0;
108  c->current_color.W=1.0;
109  c->longcurrent_color[0] = 65535;
110  c->longcurrent_color[1] = 65535;
111  c->longcurrent_color[2] = 65535;
112
113  c->current_normal.X=1.0;
114  c->current_normal.Y=0.0;
115  c->current_normal.Z=0.0;
116  c->current_normal.W=0.0;
117
118  c->current_edge_flag=1;
119 
120  c->current_tex_coord.X=0;
121  c->current_tex_coord.Y=0;
122  c->current_tex_coord.Z=0;
123  c->current_tex_coord.W=1;
124
125  c->polygon_mode_front=GL_FILL;
126  c->polygon_mode_back=GL_FILL;
127
128  c->current_front_face=0; /* 0 = GL_CCW  1 = GL_CW */
129  c->current_cull_face=GL_BACK;
130  c->current_shade_model=GL_SMOOTH;
131  c->cull_face_enabled=0;
132 
133  /* clear */
134  c->clear_color.v[0]=0;
135  c->clear_color.v[1]=0;
136  c->clear_color.v[2]=0;
137  c->clear_color.v[3]=0;
138  c->clear_depth=0;
139
140  /* selection */
141  c->render_mode=GL_RENDER;
142  c->select_buffer=NULL;
143  c->name_stack_size=0;
144
145  /* matrix */
146  c->matrix_mode=0;
147 
148  c->matrix_stack_depth_max[0]=MAX_MODELVIEW_STACK_DEPTH;
149  c->matrix_stack_depth_max[1]=MAX_PROJECTION_STACK_DEPTH;
150  c->matrix_stack_depth_max[2]=MAX_TEXTURE_STACK_DEPTH;
151
152  for(i=0;i<3;i++) {
153    c->matrix_stack[i]=gl_zalloc(c->matrix_stack_depth_max[i] * sizeof(M4));
154    c->matrix_stack_ptr[i]=c->matrix_stack[i];
155  }
156
157  glMatrixMode(GL_PROJECTION);
158  glLoadIdentity();
159  glMatrixMode(GL_TEXTURE);
160  glLoadIdentity();
161  glMatrixMode(GL_MODELVIEW);
162  glLoadIdentity();
163
164  c->matrix_model_projection_updated=1;
165
166  /* opengl 1.1 arrays */
167  c->client_states = 0;
168 
169  /* opengl 1.1 polygon offset */
170  c->offset_states = 0;
171 
172  /* clear the resize callback function pointer */
173  c->gl_resize_viewport = NULL;
174 
175  /* specular buffer */
176  c->specbuf_first = NULL;
177  c->specbuf_used_counter = 0;
178  c->specbuf_num_buffers = 0;
179
180  /* depth test */
181  c->depth_test = 0;
182}
183
184void glClose(void)
185{
186  GLContext *c=gl_get_context();
187  endSharedState(c);
188  gl_free(c);
189}
Note: See TracBrowser for help on using the repository browser.