1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
/* $Id: gl.c 61 2006-11-09 15:31:12Z mmmaddd $ */
/*
G3DViewer - 3D object viewer
Copyright (C) 2005, 2006 Markus Dahms <mad@automagically.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <glib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <g3d/types.h>
#if 0
#include "main.h"
#endif
#include "gl.h"
#include "trackball.h"
#if DEBUG > 1
#define TIMING
#endif
static int _initialized = 0;
#ifdef TIMING
static GTimer *timer = NULL;
static gulong avg_msec = 0;
#endif
GLuint evil;
void gl_init(void)
{
#if DEBUG > 1
g_printerr("init OpenGL\n");
#endif
GLfloat light0_pos[4] = { -50.0, 50.0, 0.0, 0.0 };
GLfloat light0_col[4] = { 0.6, 0.6, 0.6, 1.0 };
GLfloat light1_pos[4] = { 50.0, 50.0, 0.0, 0.0 };
GLfloat light1_col[4] = { 0.4, 0.4, 0.4, 1.0 };
GLfloat ambient_lc[4] = { 0.35, 0.35, 0.35, 1.0 };
#if 0
glEnable(GL_CULL_FACE);
#endif
/* transparency and blending */
#if 0
glAlphaFunc(GL_GREATER, 0.1);
#endif
glEnable(GL_ALPHA_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glDepthFunc(GL_LEQUAL);
glEnable(GL_DEPTH_TEST);
#if 0
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
#endif
#if 0
glDisable(GL_DITHER);
#endif
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient_lc);
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
glLightfv(GL_LIGHT0, GL_POSITION, light0_pos);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_col);
glLightfv(GL_LIGHT1, GL_POSITION, light1_pos);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_col);
glLightfv(GL_LIGHT1, GL_SPECULAR, light1_col);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_LIGHTING);
/* colors and materials */
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
/* texture stuff */
glEnable(GL_TEXTURE_2D);
#ifdef TIMING
timer = g_timer_new();
#endif
}
void gl_set_twoside(gboolean twoside)
{
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, twoside ? 1 : 0);
glColorMaterial(
twoside ? GL_FRONT_AND_BACK : GL_FRONT,
GL_AMBIENT_AND_DIFFUSE);
}
void gl_set_textures(gboolean textures)
{
if(textures)
glEnable(GL_TEXTURE_2D);
else
{
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
}
/* GHFunc */
void gl_load_texture(gpointer key, gpointer value, gpointer data)
{
G3DImage *image = (G3DImage *)value;
gint32 env;
#if 0
/* predefined - update object->_tex_images else... */
glGenTextures(1, &(image->gl_texid));
#endif
#if DEBUG > 0
g_print("gl: loading texture '%s' (%dx%dx%d) - id %d\n",
image->name ? image->name : "(null)",
image->width, image->height, image->depth,
image->tex_id);
#endif
glBindTexture(GL_TEXTURE_2D, image->tex_id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST);
switch(image->tex_env)
{
case G3D_TEXENV_BLEND: env = GL_BLEND; break;
case G3D_TEXENV_MODULATE: env = GL_MODULATE; break;
case G3D_TEXENV_DECAL: env = GL_DECAL; break;
case G3D_TEXENV_REPLACE: env = GL_REPLACE; break;
default: env = GL_BLEND; break;
}
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, env);
glTexImage2D(
GL_TEXTURE_2D /* target */,
0 /* level */,
GL_RGBA /* internalFormat */,
image->width /* width */,
image->height /* height */,
0 /* border */,
GL_RGBA /* format */,
GL_UNSIGNED_BYTE /* type */,
image->pixeldata /* pixels */);
gluBuild2DMipmaps(
GL_TEXTURE_2D,
GL_RGBA,
image->width,
image->height,
GL_RGBA,
GL_UNSIGNED_BYTE,
image->pixeldata);
}
void gl_update_material(gint32 glflags, G3DMaterial *material)
{
GLenum facetype;
GLfloat normspec[4] = { 0.0, 0.0, 0.0, 1.0 };
// g_return_if_fail(material != NULL);
if(glflags & G3D_FLAG_GL_ALLTWOSIDE)
facetype = GL_FRONT_AND_BACK;
else
facetype = GL_FRONT;
glColor4f(
material->r,
material->g,
material->b,
material->a);
return;
if(glflags & G3D_FLAG_GL_SPECULAR)
glMaterialfv(facetype, GL_SPECULAR, material->specular);
else
glMaterialfv(facetype, GL_SPECULAR, normspec);
if(glflags & G3D_FLAG_GL_SHININESS)
glMaterialf(facetype, GL_SHININESS, material->shininess * 10);
else
glMaterialf(facetype, GL_SHININESS, 0.0);
}
static void gl_draw_face(gint32 glflags, G3DObject *object, gint32 i,
G3DFloat min_a, G3DFloat max_a, gboolean *dont_render, gboolean *init)
{
static G3DMaterial *prev_material = NULL;
static guint32 prev_texid = 0;
gint32 j;
if(*init)
{
prev_material = NULL;
prev_texid = 0;
*init = FALSE;
}
/* material check */
if(prev_material != object->_materials[i])
{
if((object->_materials[i]->a < min_a) ||
(object->_materials[i]->a >= max_a))
{
*dont_render = TRUE;
return;
}
*dont_render = FALSE;
glEnd();
gl_update_material(glflags, object->_materials[i]);
glBegin(GL_TRIANGLES);
prev_material = object->_materials[i];
prev_texid = 0;
}
if(*dont_render) return;
/* texture stuff */
if((glflags & G3D_FLAG_GL_TEXTURES) &&
(object->_flags[i] & G3D_FLAG_FAC_TEXMAP))
{
/* if texture has changed update to new texture */
if(object->_tex_images[i] != prev_texid)
{
prev_texid = object->_tex_images[i];
glEnd();
glBindTexture(GL_TEXTURE_2D, prev_texid);
glBegin(GL_TRIANGLES);
#if DEBUG > 5
g_print("gl: binding to texture id %d\n", prev_texid);
#endif
}
}
/* draw triangles */
for(j = 0; j < 3; j ++)
{
if((glflags & G3D_FLAG_GL_TEXTURES) &&
(object->_flags[i] & G3D_FLAG_FAC_TEXMAP))
{
glTexCoord2f(
object->_tex_coords[(i * 3 + j) * 2 + 0],
object->_tex_coords[(i * 3 + j) * 2 + 1]);
#if DEBUG > 5
g_print("gl: setting texture coords: %f, %f\n",
object->_tex_coords[(i * 3 + j) * 2 + 0],
object->_tex_coords[(i * 3 + j) * 2 + 1]);
#endif
}
glNormal3f(
object->_normals[(i*3+j)*3+0],
object->_normals[(i*3+j)*3+1],
object->_normals[(i*3+j)*3+2]);
glVertex3f(
object->vertex_data[object->_indices[i*3+j]*3+0],
object->vertex_data[object->_indices[i*3+j]*3+1],
object->vertex_data[object->_indices[i*3+j]*3+2]);
} /* 1 .. 3 */
}
static void gl_draw_objects(gint32 glflags, GSList *objects,
G3DFloat min_a, G3DFloat max_a)
{
GSList *olist;
int i;
G3DObject *object;
gboolean dont_render;
gboolean init = TRUE;
olist = objects;
while(olist != NULL)
{
object = (G3DObject *)olist->data;
olist = olist->next;
dont_render = FALSE;
/* don't render invisible objects */
if(object->hide) continue;
g_return_if_fail(object != NULL);
#if DEBUG > 3
g_printerr("name: %s {", object->name);
#endif
#if DEBUG > 2
g_printerr("new object\n");
#endif
glPushMatrix();
if(object->transformation)
{
#if G3D_FLOAT_IS_DOUBLE
glMultMatrixd(object->transformation->matrix);
#else
glMultMatrixf(object->transformation->matrix);
#endif
}
glBegin(GL_TRIANGLES);
for(i = 0; i < object->_num_faces; i ++)
{
gl_draw_face(glflags, object, i, min_a, max_a,
&dont_render, &init);
} /* all faces */
glEnd();
/* handle sub-objects */
gl_draw_objects(glflags, object->objects, min_a, max_a);
glPopMatrix();
} /* while olist != NULL */
}
void gl_draw_model(G3DModel *model)
{
G3DFloat f;
gint32 glflags =
/* G3D_FLAG_GL_SPECULAR | */
G3D_FLAG_GL_SHININESS |
G3D_FLAG_GL_ALLTWOSIDE |
G3D_FLAG_GL_TEXTURES;
/* draw all objects */
for(f = 1.0; f >= 0.0; f -= 0.2)
gl_draw_objects(glflags, model->objects, f, f + 0.2);
}
void gl_draw_model_with_flags(gint32 glflags, G3DModel *model)
{
G3DFloat f;
/* draw all objects */
for(f = 1.0; f >= 0.0; f -= 0.2)
gl_draw_objects(glflags, model->objects, f, f + 0.2);
}
void gl_draw(gint32 glflags, G3DFloat zoom, G3DFloat aspect, G3DFloat *bgcolor,
G3DFloat *quat, G3DModel *model)
{
#if G3D_FLOAT_IS_DOUBLE
GLdouble m[4][4];
#else
GLfloat m[4][4];
#endif
static gchar *previous_name = NULL;
static gint32 previous_glflags = -1;
static gint32 dlist = -1;
GLenum error;
// G3DFloat f;
if(! _initialized)
{
gl_init();
_initialized = 1;
}
/* draw scene... */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(zoom, aspect, 1, 100);
glMatrixMode(GL_MODELVIEW);
glClearColor(
bgcolor[0],
bgcolor[1],
bgcolor[2],
bgcolor[3]);
glClearDepth(1.0);
glClearIndex(0.3);
glClear(
GL_COLOR_BUFFER_BIT |
GL_DEPTH_BUFFER_BIT |
GL_ACCUM_BUFFER_BIT |
GL_STENCIL_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -30);
build_rotmatrix(m, quat);
#if G3D_FLOAT_IS_DOUBLE
glMultMatrixd(&m[0][0]);
#else
glMultMatrixf(&m[0][0]);
#endif
/* reset texture */
glBindTexture (GL_TEXTURE_2D, 0);
if(model == NULL)
return;
#ifdef TIMING
g_timer_start(timer);
#endif
/* FIXME: better detection of new model */
if((dlist < 0) || (glflags != previous_glflags) ||
(previous_name == NULL) || strcmp(previous_name, model->filename))
{
#if DEBUG > 0
g_printerr("[gl] creating new display list\n");
#endif
/* create and execute display list */
if(dlist >= 0)
glDeleteLists(dlist, 1);
dlist = glGenLists(1);
glNewList(dlist, GL_COMPILE);
/* draw all objects */
// for(f = 1.0; f >= 0.0; f -= 0.2)
// gl_draw_objects(glflags, model->objects, f, f + 0.2);
gl_draw_model_with_flags(glflags, model);
glEndList();
if(previous_name) g_free(previous_name);
previous_name = g_strdup(model->filename);
previous_glflags = glflags;
}
/* execute display list */
glCallList(dlist);
error = glGetError();
if(error != GL_NO_ERROR)
g_printerr("[gl] E: %d\n", error);
#ifdef TIMING /* get time to draw one frame to compare algorithms */
g_timer_stop(timer);
if(avg_msec == 0)
{
gulong msec;
gdouble sec;
sec = g_timer_elapsed(timer, &msec);
avg_msec = (gulong)sec * 1000000 + msec;
}
else
{
gulong msec, add;
gdouble sec;
sec = g_timer_elapsed(timer, &msec);
add = (gulong)sec * 1000000 + msec;
avg_msec = (avg_msec + add) / 2;
}
g_printerr("average time to render frame: %lu µs\n", avg_msec);
#endif
#if DEBUG > 3
g_printerr("gl.c: drawn...\n");
#endif
}
int texture_load_all_textures(G3DModel *model)
{
if(model == NULL) return EXIT_FAILURE;
if(model->tex_images != NULL)
g_hash_table_foreach(model->tex_images, gl_load_texture, NULL);
return EXIT_SUCCESS;
}
|