~jtaylor/ubuntu/oneiric/soya/fix-780305

« back to all changes in this revision

Viewing changes to c_src/renderer.c

  • Committer: Bazaar Package Importer
  • Author(s): Marc Dequènes (Duck)
  • Date: 2005-01-30 09:55:06 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050130095506-f21p6v6cgaobhn5j
Tags: 0.9.2-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * P3
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License as published by
6
 
 * the Free Software Foundation; either version 2 of the License, or
7
 
 * (at your option) any later version.
8
 
 *
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License
15
 
 * along with this program; if not, write to the Free Software
16
 
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 */
18
 
 
19
 
/**********************************************
20
 
 * renderer.c
21
 
 * Copyright (C) 2001-2003 Bertrand 'blam' LAMY
22
 
 **********************************************/
23
 
 
24
 
// HACK
25
 
//#include <SDL/SDL.h>
26
 
 
27
 
// TO DO is all needed ?
28
 
#include "p3_base.h"
29
 
#include "util.h"
30
 
#include "gladd.h"
31
 
#include "math3d.h"
32
 
#include "atmosphere.h"
33
 
#include "light.h"
34
 
#include "frustum.h"
35
 
#include "coordsys.h"
36
 
#include "camera.h"
37
 
#include "portal.h"
38
 
#include "material.h"
39
 
#include "watercube.h"
40
 
#include "fx.h"
41
 
#include "shadow.h"
42
 
//#include "bsp.h"
43
 
#include "renderer.h"
44
 
 
45
 
extern int engine_option;
46
 
extern P3_renderer* renderer;
47
 
extern int* lights_gl_activated;
48
 
extern int  maxlights;
49
 
//extern P3_bsp* bsp;
50
 
extern GLfloat black[4];
51
 
extern GLfloat white[4];
52
 
extern P3_material* current_material;
53
 
extern float delta_time;
54
 
extern int shadow_display_list;
55
 
 
56
 
 
57
 
/* - RENDERER - */
58
 
 
59
 
P3_renderer* P3_renderer_new (void) {
60
 
  P3_renderer* r = (P3_renderer*) malloc (sizeof (P3_renderer));
61
 
  r->root_object = NULL;
62
 
  r->c_camera = NULL;
63
 
  r->c_context = NULL;
64
 
  r->nb_contexts = 0;
65
 
  r->max_contexts = 0;
66
 
  r->contexts = NULL;
67
 
  r->r_atmosphere = NULL;
68
 
  r->c_frustum_coordsys = NULL;
69
 
  r->c_frustum = (P3_frustum*) malloc (sizeof (P3_frustum));
70
 
  r->r_frustum = (P3_frustum*) malloc (sizeof (P3_frustum));
71
 
  r->top_lights  = P3_list_new (0);
72
 
  r->all_lights  = P3_list_new (2);
73
 
  r->worlds_made = P3_list_new (0);
74
 
  r->portals     = P3_list_new (0);
75
 
  r->watercubes  = P3_list_new (0);
76
 
  r->opaque     = P3_chunk_new ();
77
 
  r->secondpass = P3_chunk_new ();
78
 
  r->alpha      = P3_chunk_new ();
79
 
  r->specials   = P3_chunk_new ();
80
 
  r->data       = P3_chunk_new ();
81
 
  r->faces      = P3_chunk_new ();
82
 
  return r;
83
 
}
84
 
 
85
 
void P3_renderer_dealloc (P3_renderer* r) {
86
 
  int i;
87
 
  P3_chunk_dealloc (r->opaque);
88
 
  P3_chunk_dealloc (r->alpha);
89
 
  P3_chunk_dealloc (r->secondpass);
90
 
  P3_chunk_dealloc (r->specials);
91
 
  P3_chunk_dealloc (r->data);
92
 
  P3_chunk_dealloc (r->faces);
93
 
  P3_list_dealloc (r->top_lights);
94
 
  P3_list_dealloc (r->all_lights);
95
 
  P3_list_dealloc (r->worlds_made);
96
 
  P3_list_dealloc (r->portals);
97
 
  P3_list_dealloc (r->watercubes);
98
 
  P3_frustum_dealloc (r->c_frustum);
99
 
  P3_frustum_dealloc (r->r_frustum);
100
 
  for (i = 0; i < r->max_contexts; i++) {
101
 
    P3_list_dealloc (r->contexts[i]->lights);
102
 
    free (r->contexts[i]);
103
 
  }
104
 
  free (r->contexts);
105
 
  free (r);
106
 
}
107
 
 
108
 
P3_frustum* P3_renderer_get_frustum (P3_instance* inst) {
109
 
  if (inst == NULL) {
110
 
    return renderer->r_frustum;
111
 
  } else {
112
 
    if (inst->frustum_data == -1) {
113
 
      inst->frustum_data = P3_chunk_register (renderer->frustums, sizeof (P3_frustum));
114
 
      P3_frustum_by_matrix ((P3_frustum*) (renderer->frustums->content + inst->frustum_data), renderer->r_frustum, P3_coordsys_get_inverted_root_matrix ((P3_coordsys*) inst));
115
 
    }
116
 
    return (P3_frustum*) (renderer->frustums->content + inst->frustum_data);
117
 
  }
118
 
}
119
 
 
120
 
/*
121
 
P3_frustum* P3_renderer_get_frustum (P3_coordsys* csys) {
122
 
  if (csys == NULL) {
123
 
    return renderer->r_frustum;
124
 
  } else {
125
 
    if (renderer->c_frustum_coordsys != csys) {
126
 
      P3_frustum_by_matrix (renderer->c_frustum, renderer->r_frustum, P3_coordsys_get_inverted_root_matrix (csys));
127
 
      renderer->c_frustum_coordsys = csys;
128
 
    }
129
 
    return renderer->c_frustum;
130
 
  }
131
 
}
132
 
*/
133
 
 
134
 
P3_context* P3_renderer_get_context (void) {
135
 
  P3_context* rc;
136
 
  int n;
137
 
  n = renderer->nb_contexts;
138
 
  (renderer->nb_contexts)++;
139
 
  if (n < renderer->max_contexts) {
140
 
    rc = renderer->contexts[n];
141
 
    rc->lights->nb = 0;
142
 
    rc->atmosphere = NULL;
143
 
    rc->portal = NULL;
144
 
  } else {
145
 
    /* I don't think contexts need a geometrical growth... the 1rst render will be slower but it doesn't matter */
146
 
    (renderer->max_contexts)++;
147
 
 
148
 
// TO DO simple array and not pointer array ?
149
 
// in that case I need to stock index and not pointer...
150
 
 
151
 
    renderer->contexts = (P3_context**) realloc (renderer->contexts, renderer->max_contexts * sizeof (P3_context*));
152
 
    rc = (P3_context*) malloc (sizeof (P3_context));
153
 
    renderer->contexts[n] = rc;
154
 
    rc->lights = P3_list_new (2);
155
 
    rc->atmosphere = NULL;
156
 
    rc->portal = NULL;
157
 
  }
158
 
  return rc;
159
 
}
160
 
 
161
 
void P3_renderer_active_context_over (P3_context* old, P3_context* ctxt) {
162
 
  int i;
163
 
  P3_list* list;
164
 
  P3_light* light;
165
 
// TO DO active / inactive light only if necessary ?
166
 
  if (old != NULL) {
167
 
    /* inactive previous lights */
168
 
    list = old->lights;
169
 
    for (i = 0; i < list->nb; i++) {
170
 
      light = (P3_light*) P3_list_get (list, i);
171
 
      if (light->id != -1) {
172
 
        glDisable (GL_LIGHT0 + light->id);
173
 
        lights_gl_activated[light->id] = P3_FALSE;
174
 
      }
175
 
    }
176
 
    if (old->portal != NULL && old->portal->option & (P3_PORTAL_USE_4_CLIP_PLANES | P3_PORTAL_USE_5_CLIP_PLANES)) {
177
 
      glDisable (GL_CLIP_PLANE0);
178
 
      glDisable (GL_CLIP_PLANE1);
179
 
      glDisable (GL_CLIP_PLANE2);
180
 
      glDisable (GL_CLIP_PLANE3);
181
 
      if (old->portal->option & P3_PORTAL_USE_5_CLIP_PLANES) { glDisable (GL_CLIP_PLANE4); }
182
 
    }
183
 
  }
184
 
  /* active new context */
185
 
  if (ctxt != NULL) {
186
 
    if (ctxt->atmosphere != NULL && (old == NULL || ctxt->atmosphere != old->atmosphere)) {
187
 
      P3_atmosphere_render (ctxt->atmosphere);
188
 
    }
189
 
    list = ctxt->lights;
190
 
    for (i = 0; i < list->nb; i++) {
191
 
      P3_light_render ((P3_light*) P3_list_get (list, i), NULL);
192
 
    }
193
 
  }
194
 
  /* re-active top level lights that may have been inactivated due to too many lights */
195
 
  list = renderer->top_lights;
196
 
  for (i = 0; i < list->nb; i++) {
197
 
    light = (P3_light*) P3_list_get (list, i);
198
 
    if (light->id == -1) {
199
 
      P3_light_render (light, NULL);
200
 
    }
201
 
  }
202
 
  /* active portal clip plane */
203
 
  if (ctxt != NULL && ctxt->portal != NULL && ctxt->portal->option & (P3_PORTAL_USE_4_CLIP_PLANES | P3_PORTAL_USE_5_CLIP_PLANES)) {
204
 
    P3_portal* portal = ctxt->portal;
205
 
    glLoadIdentity ();  /* the clip planes must be defined in the camera coordsys... so identity */
206
 
    glClipPlane (GL_CLIP_PLANE0, portal->equation);
207
 
    glClipPlane (GL_CLIP_PLANE1, portal->equation + 4);
208
 
    glClipPlane (GL_CLIP_PLANE2, portal->equation + 8);
209
 
    glClipPlane (GL_CLIP_PLANE3, portal->equation + 12);
210
 
    glEnable (GL_CLIP_PLANE0);
211
 
    glEnable (GL_CLIP_PLANE1);
212
 
    glEnable (GL_CLIP_PLANE2);
213
 
    glEnable (GL_CLIP_PLANE3);
214
 
    if (portal->option & P3_PORTAL_USE_5_CLIP_PLANES) { 
215
 
      glClipPlane (GL_CLIP_PLANE4, portal->equation + 16);
216
 
      glEnable (GL_CLIP_PLANE4); 
217
 
    }
218
 
  }
219
 
}
220
 
 
221
 
void P3_renderer_reset (void) {
222
 
  int i;
223
 
  /* reset renderer */
224
 
  renderer->nb_contexts = 0;
225
 
  renderer->r_atmosphere = NULL;
226
 
  P3_disable_all_lights ();
227
 
// TO DO why ? usefull ?
228
 
/*
229
 
  glDisable (GL_CLIP_PLANE0);
230
 
  glDisable (GL_CLIP_PLANE1);
231
 
  glDisable (GL_CLIP_PLANE2);
232
 
  glDisable (GL_CLIP_PLANE3);
233
 
  glDisable (GL_CLIP_PLANE4);
234
 
*/
235
 
  renderer->top_lights->nb  = 0;
236
 
  renderer->all_lights->nb  = 0;
237
 
 
238
 
  for (i = 0; i < renderer->worlds_made->nb; i++) {
239
 
    ((P3_world*) P3_list_get (renderer->worlds_made, i))->option -= P3_WORLD_BATCHED;
240
 
  }
241
 
  renderer->worlds_made->nb = 0;
242
 
  renderer->portals->nb = 0;
243
 
  renderer->watercubes->nb = 0;
244
 
 
245
 
  renderer->data->nb = 0;
246
 
  renderer->faces->nb = 0;
247
 
  renderer->opaque->nb = 0;
248
 
  renderer->secondpass->nb = 0;
249
 
  renderer->alpha->nb = 0;
250
 
  renderer->specials->nb = 0;
251
 
  delta_time = 0.0;
252
 
}
253
 
 
254
 
/*
255
 
void P3_renderer_render_specials (P3_chunk* list) {
256
 
  P3_any_object* obj;
257
 
  int max = list->nb;
258
 
  list->nb = 0;
259
 
  while (list->nb < max) {
260
 
    obj = *((P3_any_object**) (list->content + list->nb));
261
 
    list->nb += sizeof (P3_any_object*);
262
 
    switch (obj->class->id) {
263
 
    case P3_ID_WATERCUBE:
264
 
      P3_watercube_render ((P3_watercube*) obj);
265
 
      break;
266
 
    }
267
 
  }
268
 
}
269
 
*/
270
 
 
271
 
void P3_renderer_render_list (P3_chunk* list) {
272
 
  P3_renderer_obj* robj;
273
 
  int max = list->nb;
274
 
  list->nb = 0;
275
 
  while (list->nb < max) {
276
 
 
277
 
//printf("new object on list at %i\n", list->nb);
278
 
 
279
 
    robj = (P3_renderer_obj*) (list->content + list->nb);
280
 
    /* context */
281
 
    if (robj->ctx != renderer->c_context) {
282
 
      P3_renderer_active_context_over (renderer->c_context, robj->ctx);
283
 
      renderer->c_context = robj->ctx;
284
 
    }
285
 
    /* instance */
286
 
    if (robj->ins != NULL) glLoadMatrixf (robj->ins->render_matrix);
287
 
    renderer->c_instance = robj->ins;
288
 
    /* data */
289
 
    renderer->data->nb = robj->data;
290
 
 
291
 
//printf ("rendering a %i (option %i)\n", robj->obj->class->id, robj->obj->option);
292
 
 
293
 
// TO DO NEW at the end
294
 
    list->nb += sizeof (P3_renderer_obj);
295
 
    /* object */
296
 
    robj->obj->class->render (robj->obj, robj->ins);
297
 
  }
298
 
}
299
 
 
300
 
void P3_renderer_render (void) {
301
 
  P3_context* ctxt;
302
 
  P3_portal* portal;
303
 
  P3_world* w;
304
 
  P3_list* list;
305
 
  P3_light* light;
306
 
  int i;
307
 
 
308
 
//printf("--- renderer start\n");
309
 
 
310
 
  renderer->frustums = P3_get_chunk ();
311
 
 
312
 
  /*==========================*
313
 
   * RENDERING STEP 1 : BATCH *
314
 
   *==========================*/
315
 
 
316
 
  ctxt = P3_renderer_get_context ();
317
 
  renderer->c_context = ctxt;
318
 
 
319
 
  if (renderer->root_object->class->batch != 0) 
320
 
    renderer->root_object->class->batch (renderer->root_object, (P3_instance*) renderer->c_camera);
321
 
  
322
 
  /*===========================*
323
 
   * RENDERING STEP 2 : RENDER *
324
 
   *===========================*/
325
 
  /* clear */
326
 
  if (renderer->r_atmosphere != NULL) {
327
 
    P3_atmosphere_clear (renderer->r_atmosphere);
328
 
  } else {
329
 
    P3_clear_screen (NULL);
330
 
  }
331
 
  /* draw the atmosphere of the worlds that are seen through portals */
332
 
  for (i = renderer->portals->nb - 1; i >= 0; i--) {
333
 
    portal = (P3_portal*) P3_list_get (renderer->portals, i);
334
 
    if (portal->option & P3_PORTAL_BOUND_ATMOSPHERE) {
335
 
      P3_atmosphere_clear_part (portal);
336
 
    }
337
 
  }
338
 
  /* activate top lights */
339
 
  list = renderer->top_lights;
340
 
  for (i = 0; i < list->nb; i++) {
341
 
    P3_light_render ((P3_light*) P3_list_get (list, i), NULL);
342
 
  }
343
 
  /* current context have been changed during batching -> reinitialize */
344
 
  renderer->c_context = NULL;
345
 
  /* render collected objects */
346
 
  /* draw opaque stuff first */
347
 
  renderer->state = P3_RENDERER_STATE_OPAQUE;
348
 
  P3_renderer_render_list (renderer->opaque);
349
 
  /* then draw secondpass */
350
 
  renderer->state = P3_RENDERER_STATE_SECONDPASS;
351
 
  P3_renderer_render_list (renderer->secondpass);
352
 
  /* finally draw alpha */
353
 
  renderer->state = P3_RENDERER_STATE_ALPHA;
354
 
  glDepthMask (GL_FALSE);
355
 
  glEnable (GL_BLEND);
356
 
  P3_renderer_render_list (renderer->alpha);
357
 
  /* reset a part of the renderer here cause it's needed for rendering the portal fog */
358
 
  P3_material_activate (NULL);
359
 
  P3_renderer_active_context_over (renderer->c_context, NULL);
360
 
  renderer->c_context = NULL;
361
 
  /* draw fog for portal if necessary */
362
 
  for (i = renderer->portals->nb - 1; i >= 0; i--) {
363
 
    portal = (P3_portal*) P3_list_get (renderer->portals, i);
364
 
    if (portal->option & P3_PORTAL_BOUND_ATMOSPHERE) {
365
 
      /* get root atmosphere */
366
 
      w = (P3_world*) portal->parent;
367
 
      while (w != NULL && w->atmosphere == NULL) {
368
 
        w = (P3_world*) w->parent;
369
 
      }
370
 
      if (w->atmosphere != NULL && w->atmosphere->option & P3_ATMOSPHERE_FOG) {
371
 
        P3_portal_draw_fog (portal, w->atmosphere);
372
 
      }
373
 
    }
374
 
  }
375
 
  /* render shadow */
376
 
  if (engine_option & P3_SHADOWS) P3_render_shadows ();
377
 
  /* render specials: objects that are not shadowed (sprite, particules) */
378
 
  renderer->state = P3_RENDERER_STATE_SPECIAL;
379
 
  P3_renderer_render_list (renderer->specials);
380
 
  /* render watercube */
381
 
  for (i = 0; i < renderer->watercubes->nb; i++) {
382
 
    P3_watercube_render ((P3_watercube*) P3_list_get (renderer->watercubes, i));
383
 
  }
384
 
  /* end of OpenGL rendering */
385
 
  glDepthMask (GL_TRUE);
386
 
  glDisable (GL_BLEND);
387
 
//  P3_renderer_render_specials (renderer->specials);
388
 
  if (engine_option & P3_FX_INITED) P3_fx_advance_time ();
389
 
  /* auto-reset renderer */
390
 
  P3_renderer_reset ();
391
 
 
392
 
  P3_drop_chunk (renderer->frustums);
393
 
 
394
 
//printf("time for renderer : %i\n", SDL_GetTicks() - t);
395
 
 
396
 
//printf("renderer end\n");
397
 
 
398
 
}
399
 
 
400
 
void P3_renderer_batch (P3_chunk* list, void* object, P3_instance* instance, int data) {
401
 
  P3_renderer_obj* robj;
402
 
  int i;
403
 
  i = P3_chunk_register (list, sizeof (P3_renderer_obj));
404
 
 
405
 
//printf("add object on list at %i\n", i);
406
 
 
407
 
  robj = (P3_renderer_obj*) (list->content + i);
408
 
  robj->obj = (P3_any_object*) object;
409
 
  robj->ins = instance;
410
 
  robj->ctx = renderer->c_context;
411
 
  robj->data = data;
412
 
}
413
 
 
414
 
/*
415
 
void P3_renderer_add (void* obj, P3_instance* ins) {
416
 
  P3_renderer_batch (renderer->opaque, obj, ins, renderer->data->nb);
417
 
}
418
 
 
419
 
void P3_renderer_add_alpha (void* obj, P3_instance* ins) {
420
 
  P3_renderer_batch (renderer->alpha, obj, ins, renderer->data->nb);
421
 
}
422
 
*/
423
 
 
424