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.
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.
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
19
/**********************************************
21
* Copyright (C) 2001-2003 Bertrand 'blam' LAMY
22
**********************************************/
25
//#include <SDL/SDL.h>
27
// TO DO is all needed ?
32
#include "atmosphere.h"
39
#include "watercube.h"
45
extern int engine_option;
46
extern P3_renderer* renderer;
47
extern int* lights_gl_activated;
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;
59
P3_renderer* P3_renderer_new (void) {
60
P3_renderer* r = (P3_renderer*) malloc (sizeof (P3_renderer));
61
r->root_object = 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 ();
85
void P3_renderer_dealloc (P3_renderer* r) {
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]);
108
P3_frustum* P3_renderer_get_frustum (P3_instance* inst) {
110
return renderer->r_frustum;
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));
116
return (P3_frustum*) (renderer->frustums->content + inst->frustum_data);
121
P3_frustum* P3_renderer_get_frustum (P3_coordsys* csys) {
123
return renderer->r_frustum;
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;
129
return renderer->c_frustum;
134
P3_context* P3_renderer_get_context (void) {
137
n = renderer->nb_contexts;
138
(renderer->nb_contexts)++;
139
if (n < renderer->max_contexts) {
140
rc = renderer->contexts[n];
142
rc->atmosphere = NULL;
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)++;
148
// TO DO simple array and not pointer array ?
149
// in that case I need to stock index and not pointer...
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;
161
void P3_renderer_active_context_over (P3_context* old, P3_context* ctxt) {
165
// TO DO active / inactive light only if necessary ?
167
/* inactive previous 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;
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); }
184
/* active new context */
186
if (ctxt->atmosphere != NULL && (old == NULL || ctxt->atmosphere != old->atmosphere)) {
187
P3_atmosphere_render (ctxt->atmosphere);
190
for (i = 0; i < list->nb; i++) {
191
P3_light_render ((P3_light*) P3_list_get (list, i), NULL);
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);
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);
221
void P3_renderer_reset (void) {
224
renderer->nb_contexts = 0;
225
renderer->r_atmosphere = NULL;
226
P3_disable_all_lights ();
227
// TO DO why ? usefull ?
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);
235
renderer->top_lights->nb = 0;
236
renderer->all_lights->nb = 0;
238
for (i = 0; i < renderer->worlds_made->nb; i++) {
239
((P3_world*) P3_list_get (renderer->worlds_made, i))->option -= P3_WORLD_BATCHED;
241
renderer->worlds_made->nb = 0;
242
renderer->portals->nb = 0;
243
renderer->watercubes->nb = 0;
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;
255
void P3_renderer_render_specials (P3_chunk* list) {
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);
271
void P3_renderer_render_list (P3_chunk* list) {
272
P3_renderer_obj* robj;
275
while (list->nb < max) {
277
//printf("new object on list at %i\n", list->nb);
279
robj = (P3_renderer_obj*) (list->content + list->nb);
281
if (robj->ctx != renderer->c_context) {
282
P3_renderer_active_context_over (renderer->c_context, robj->ctx);
283
renderer->c_context = robj->ctx;
286
if (robj->ins != NULL) glLoadMatrixf (robj->ins->render_matrix);
287
renderer->c_instance = robj->ins;
289
renderer->data->nb = robj->data;
291
//printf ("rendering a %i (option %i)\n", robj->obj->class->id, robj->obj->option);
293
// TO DO NEW at the end
294
list->nb += sizeof (P3_renderer_obj);
296
robj->obj->class->render (robj->obj, robj->ins);
300
void P3_renderer_render (void) {
308
//printf("--- renderer start\n");
310
renderer->frustums = P3_get_chunk ();
312
/*==========================*
313
* RENDERING STEP 1 : BATCH *
314
*==========================*/
316
ctxt = P3_renderer_get_context ();
317
renderer->c_context = ctxt;
319
if (renderer->root_object->class->batch != 0)
320
renderer->root_object->class->batch (renderer->root_object, (P3_instance*) renderer->c_camera);
322
/*===========================*
323
* RENDERING STEP 2 : RENDER *
324
*===========================*/
326
if (renderer->r_atmosphere != NULL) {
327
P3_atmosphere_clear (renderer->r_atmosphere);
329
P3_clear_screen (NULL);
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);
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);
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);
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;
370
if (w->atmosphere != NULL && w->atmosphere->option & P3_ATMOSPHERE_FOG) {
371
P3_portal_draw_fog (portal, w->atmosphere);
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));
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 ();
392
P3_drop_chunk (renderer->frustums);
394
//printf("time for renderer : %i\n", SDL_GetTicks() - t);
396
//printf("renderer end\n");
400
void P3_renderer_batch (P3_chunk* list, void* object, P3_instance* instance, int data) {
401
P3_renderer_obj* robj;
403
i = P3_chunk_register (list, sizeof (P3_renderer_obj));
405
//printf("add object on list at %i\n", i);
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;
415
void P3_renderer_add (void* obj, P3_instance* ins) {
416
P3_renderer_batch (renderer->opaque, obj, ins, renderer->data->nb);
419
void P3_renderer_add_alpha (void* obj, P3_instance* ins) {
420
P3_renderer_batch (renderer->alpha, obj, ins, renderer->data->nb);