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

« back to all changes in this revision

Viewing changes to c_src/mesh.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
 
 * mesh.c
21
 
 * Copyright (C) 2001-2003 Bertrand 'blam' LAMY
22
 
 **********************************************/
23
 
 
24
 
#include "p3_base.h"
25
 
#include "math3d.h"
26
 
#include "util.h"
27
 
#include "raypick.h"
28
 
#include "material.h"
29
 
#include "frustum.h"
30
 
#include "coordsys.h"
31
 
#include "renderer.h"
32
 
#include "light.h"
33
 
#include "fx.h"
34
 
#include "watercube.h"
35
 
#include "shadow.h"
36
 
#include "mesh.h"
37
 
 
38
 
extern GLfloat white[4];
39
 
extern GLfloat black[4];
40
 
extern P3_renderer* renderer;
41
 
extern int engine_option;
42
 
extern P3_material* current_material;
43
 
extern P3_material* light_shader;
44
 
extern int shadow_display_list;
45
 
 
46
 
 
47
 
P3_class P3_class_mesh = {
48
 
  P3_ID_MESH,
49
 
  (batch_func)     P3_mesh_batch,
50
 
  (render_func)    P3_mesh_render,
51
 
  (shadow_func)    P3_mesh_shadow,
52
 
  (raypick_func)   P3_mesh_raypick,
53
 
  (raypick_b_func) P3_mesh_raypick_b,
54
 
};
55
 
 
56
 
 
57
 
P3_mesh* P3_mesh_new (P3_mesh* mesh) {
58
 
  if (mesh == NULL) mesh = (P3_mesh*) malloc (sizeof (P3_mesh));
59
 
  mesh->class = &P3_class_mesh;
60
 
  P3_xmesh_initialize ((P3_xmesh*) mesh);
61
 
  mesh->xtra1 = NULL;
62
 
  mesh->xtra2 = NULL;
63
 
  return mesh;
64
 
}
65
 
 
66
 
static void P3_mesh_face_list_dealloc (P3_mesh_face_list* flist) {
67
 
  free (flist->faces);
68
 
  free (flist);
69
 
}
70
 
 
71
 
static void P3_mesh_display_list_dealloc (P3_mesh_display_list* dlist) {
72
 
  int i;
73
 
  if (engine_option & P3_GL_INITED) {
74
 
    for (i = 0; i < dlist->nb_list_opaque + dlist->nb_list_alpha; i++) {
75
 
      glDeleteLists ((dlist->dlists + i)->dlist, 1);
76
 
    }
77
 
  }
78
 
  free (dlist->dlists);
79
 
  free (dlist);
80
 
}
81
 
 
82
 
static void P3_mesh_free_xtra1 (P3_mesh* mesh) {
83
 
  if (mesh->xtra1 != NULL) {
84
 
    if (mesh->option & P3_MESH_TREE) {
85
 
      P3_xnode_dealloc ((P3_xnode*) mesh->xtra1);
86
 
      mesh->option -= P3_MESH_TREE;
87
 
    } else if (mesh->option & P3_MESH_CELL_SHADING) {
88
 
      /* shader is not decrefed cause it belongs to the mesh->materials array */
89
 
//      P3_mesh_face_list_dealloc ((P3_mesh_face_list*) mesh->xtra1);
90
 
      mesh->option -= P3_MESH_CELL_SHADING;
91
 
//      mesh->option -= P3_MESH_FACE_LIST;
92
 
      mesh->option -= P3_MESH_HAS_SPHERE;
93
 
    } else if (mesh->option & P3_MESH_DISPLAY_LISTS) {
94
 
      P3_mesh_display_list_dealloc ((P3_mesh_display_list*) mesh->xtra1);
95
 
      mesh->option -= P3_MESH_DISPLAY_LISTS;
96
 
    }
97
 
  }
98
 
  mesh->xtra1 = NULL;
99
 
}
100
 
 
101
 
static void P3_mesh_free_xtra2 (P3_mesh* mesh) {
102
 
  if (mesh->xtra2 != NULL) {
103
 
    if (mesh->option & P3_MESH_FACE_LIST) {
104
 
      P3_mesh_face_list_dealloc ((P3_mesh_face_list*) mesh->xtra2);
105
 
      mesh->option -= P3_MESH_FACE_LIST;
106
 
      mesh->option -= P3_MESH_HAS_SPHERE;
107
 
    } else if (mesh->option & P3_MESH_HAS_SPHERE) {
108
 
      free (mesh->xtra2);
109
 
      mesh->option -= P3_MESH_HAS_SPHERE;
110
 
    }
111
 
  }
112
 
  mesh->xtra2 = NULL;
113
 
}
114
 
 
115
 
void P3_mesh_dealloc (P3_mesh* mesh) {
116
 
  P3_mesh_free_xtra1 (mesh);
117
 
  P3_mesh_free_xtra2 (mesh);
118
 
  P3_xmesh_free_data ((P3_xmesh*) mesh);
119
 
}
120
 
 
121
 
void P3_mesh_check_fx (P3_mesh* mesh) {
122
 
  GLfloat* color;
123
 
  int i;
124
 
  /* need fx */
125
 
  P3_fx_init ();
126
 
  if (mesh->option & P3_MESH_DISPLAY_LISTS) P3_mesh_free_xtra1 (mesh);
127
 
  if (!(mesh->option & P3_MESH_VERTEX_OPTIONS)) {
128
 
    mesh->vertex_options = (char*) calloc (mesh->nb_vertices, sizeof (char));
129
 
    mesh->option |= P3_MESH_VERTEX_OPTIONS;
130
 
  }
131
 
  if (!(mesh->option & P3_MESH_WARFOGS)) {
132
 
    mesh->vertex_warfogs = (GLfloat**) malloc (mesh->nb_vertices * sizeof (GLfloat*));
133
 
    mesh->option |= P3_MESH_WARFOGS;
134
 
    if (mesh->option & P3_MESH_DIFFUSES) {
135
 
      for (i = 0; i < mesh->nb_vertices; i++) mesh->vertex_warfogs[i] = mesh->vertex_diffuses[i];
136
 
    } else {
137
 
      if (mesh->nb_values >= 4) {
138
 
        color = mesh->values;
139
 
      } else {
140
 
        i = P3_xmesh_register_value ((P3_xmesh*) mesh, white, 4);
141
 
        color = mesh->values + i;
142
 
      }
143
 
      for (i = 0; i < mesh->nb_vertices; i++) mesh->vertex_warfogs[i] = color;
144
 
    }
145
 
  }
146
 
}
147
 
 
148
 
 
149
 
/*============+
150
 
 | RAYPICKING |
151
 
 +============*/
152
 
 
153
 
void P3_mesh_raypick (P3_mesh* mesh, P3_raypick_data* data, P3_raypickable* parent) {
154
 
  GLfloat* raydata;
155
 
  raydata = P3_raypickable_get_raypick_data (parent, data);
156
 
  if (mesh->option & P3_MESH_TREE) {
157
 
    P3_xmesh_node_raypick ((P3_xmesh*) mesh, (P3_xnode*) mesh->xtra1, raydata, data, parent);
158
 
  } else {
159
 
    if (mesh->option & P3_MESH_HAS_SPHERE &&
160
 
        P3_sphere_raypick (raydata, (GLfloat*) mesh->xtra2) == P3_FALSE) {
161
 
      return;
162
 
    }
163
 
    if (mesh->option & P3_MESH_FACE_LIST) {
164
 
      P3_mesh_face_list* flist = (P3_mesh_face_list*) mesh->xtra2;
165
 
      int i;
166
 
      for (i = 0; i < flist->nb_faces; i++) {
167
 
        P3_xmesh_face_raypick ((P3_xmesh*) mesh, flist->faces[i], raydata, data, parent);
168
 
      }
169
 
    } else {
170
 
      P3_xmesh_raypick ((P3_xmesh*) mesh, raydata, data, parent);
171
 
    }
172
 
  }
173
 
}
174
 
 
175
 
int P3_mesh_raypick_b (P3_mesh* mesh, P3_raypick_data* data, P3_raypickable* parent) {
176
 
  GLfloat* raydata;
177
 
  raydata = P3_raypickable_get_raypick_data (parent, data);
178
 
  if (mesh->option & P3_MESH_TREE) {
179
 
    return P3_xmesh_node_raypick_b ((P3_xmesh*) mesh, (P3_xnode*) mesh->xtra1, raydata, data);
180
 
  } else {
181
 
    if (mesh->option & P3_MESH_HAS_SPHERE &&
182
 
        P3_sphere_raypick (raydata, (GLfloat*) mesh->xtra2) == P3_FALSE) {
183
 
      return P3_FALSE;
184
 
    }
185
 
    if (mesh->option & P3_MESH_FACE_LIST) {
186
 
      P3_mesh_face_list* flist = (P3_mesh_face_list*) mesh->xtra2;
187
 
      int i;
188
 
      for (i = 0; i < flist->nb_faces; i++) {
189
 
        if (P3_xmesh_face_raypick_b ((P3_xmesh*) mesh, flist->faces[i], raydata, data) == P3_TRUE) {
190
 
          return P3_TRUE;
191
 
        }
192
 
      }
193
 
    } else {
194
 
      return P3_xmesh_raypick_b ((P3_xmesh*) mesh, raydata, data);
195
 
    }
196
 
  }
197
 
  return P3_FALSE;
198
 
}
199
 
 
200
 
 
201
 
/*===================+
202
 
 | XTRA CONSTRUCTORS |
203
 
 +===================*/
204
 
 
205
 
static void P3_mesh_create_faces_list (P3_mesh_face_list* flist, P3_mesh* mesh) {
206
 
  P3_xface* face;
207
 
  int i;
208
 
  flist->nb_faces = 0;
209
 
  face = (P3_xface*) mesh->faces;
210
 
  while ((void*) face < mesh->faces + mesh->faces_size) {
211
 
    face = ((void*) face) + P3_xmesh_face_size ((P3_xmesh*) mesh, face);
212
 
    flist->nb_faces++;
213
 
  }
214
 
  flist->faces = (P3_xface**) malloc (flist->nb_faces * sizeof (P3_xface*));
215
 
  i = 0;
216
 
  face = (P3_xface*) mesh->faces;
217
 
  while ((void*) face < mesh->faces + mesh->faces_size) {
218
 
    flist->faces[i] = face;
219
 
    face = ((void*) face) + P3_xmesh_face_size ((P3_xmesh*) mesh, face);
220
 
    i++;
221
 
  }
222
 
}
223
 
 
224
 
static void P3_mesh_display_list_init (P3_mesh* mesh) {
225
 
  P3_mesh_display_list* dlists;
226
 
  P3_display_list* dlist;
227
 
  P3_xface* face;
228
 
  int i;
229
 
  dlists = (P3_mesh_display_list*) mesh->xtra1;
230
 
  P3_xmesh_option_activate (mesh->option);
231
 
  for (i = 0; i < dlists->nb_list_opaque + dlists->nb_list_alpha; i++) {
232
 
    dlist = dlists->dlists + i;
233
 
    dlist->dlist = glGenLists (1);
234
 
    P3_material_activate (dlist->material);
235
 
    P3_xface_option_activate (dlist->option);
236
 
    glNewList (dlist->dlist, GL_COMPILE);
237
 
    if (dlist->option & P3_FACE_TRIANGLE) {
238
 
      glBegin (GL_TRIANGLES);
239
 
    } else if (dlist->option & P3_FACE_QUAD) {
240
 
      glBegin (GL_QUADS);
241
 
    }
242
 
    face = mesh->faces;
243
 
    while ((void*) face < mesh->faces + mesh->faces_size) {
244
 
      if ((face->option & P3_DISPLAY_LIST_OPTIONS) == dlist->option && face->pack->material == dlist->material) {
245
 
        if (face->option & P3_FACE_QUAD) P3_xmesh_quad_render     ((P3_xmesh*) mesh, face);
246
 
        else                             P3_xmesh_triangle_render ((P3_xmesh*) mesh, face);
247
 
      }
248
 
      face = ((void*) face) + P3_xmesh_face_size ((P3_xmesh*) mesh, face);
249
 
    }
250
 
    glEnd ();
251
 
    glEndList ();
252
 
    P3_xface_option_inactivate (dlist->option);
253
 
  }
254
 
  P3_xmesh_option_inactivate (mesh->option);
255
 
  mesh->option |= P3_MESH_INITED;
256
 
}
257
 
 
258
 
void P3_mesh_build_tree (P3_mesh* mesh) {
259
 
  P3_mesh_free_xtra1 (mesh);
260
 
  mesh->xtra1 = P3_xmesh_build_tree ((P3_xmesh*) mesh);
261
 
  mesh->option |= P3_MESH_TREE;
262
 
}
263
 
 
264
 
void P3_mesh_optimize_tree (P3_mesh* mesh, GLfloat collapse, int mode, GLfloat param) {
265
 
  if (mesh->option & P3_MESH_TREE)
266
 
    P3_xmesh_optimize_tree ((P3_xnode*) mesh->xtra1, collapse, mode, param);
267
 
}
268
 
 
269
 
void P3_mesh_set_xtra1_display_lists (P3_mesh* mesh) {
270
 
  P3_mesh_display_list* displist;
271
 
  P3_display_list* dl;
272
 
  P3_xface* face;
273
 
  void* max;
274
 
  int nb, i, k;
275
 
  P3_mesh_free_xtra1 (mesh);
276
 
  displist = (P3_mesh_display_list*) malloc (sizeof (P3_mesh_display_list));
277
 
  displist->nb_list_opaque = 0;
278
 
  displist->nb_list_alpha = 0;
279
 
  nb = 0;
280
 
  displist->dlists = NULL;
281
 
  for (k = 0; k < 2; k++) {
282
 
    face = (P3_xface*) mesh->faces;
283
 
    max = mesh->faces + mesh->faces_size;
284
 
    while ((void*) face < max) {
285
 
      if ((face->option & P3_FACE_ALPHA && k == 1) || (!(face->option & P3_FACE_ALPHA) && k == 0)) {
286
 
        for (i = 0; i < nb; i++) {
287
 
          dl = displist->dlists + i;
288
 
          if (dl->material == face->pack->material && dl->option == (face->option & P3_DISPLAY_LIST_OPTIONS)) {
289
 
            i = -1;
290
 
            break;
291
 
          }
292
 
        }
293
 
        if (i != -1) {
294
 
          displist->dlists = (P3_display_list*) realloc (displist->dlists, (nb + 1) * sizeof (P3_display_list));
295
 
          dl = displist->dlists + nb;
296
 
          dl->material = face->pack->material;
297
 
          dl->option = face->option & P3_DISPLAY_LIST_OPTIONS;
298
 
          if (dl->option & P3_FACE_ALPHA) (displist->nb_list_alpha)++;
299
 
          else                            (displist->nb_list_opaque)++;
300
 
          nb++;
301
 
        }
302
 
      }
303
 
      face = ((void*) face) + P3_xmesh_face_size ((P3_xmesh*) mesh, face);
304
 
    }
305
 
  }
306
 
  mesh->xtra1 = displist;
307
 
  mesh->option |= P3_MESH_DISPLAY_LISTS;
308
 
}
309
 
 
310
 
void P3_mesh_set_xtra1_cell_shading (P3_mesh* mesh, P3_material* shader, GLfloat* color, GLfloat line_width_factor) {
311
 
  P3_mesh_cell_shading* cshade;
312
 
  P3_mesh_free_xtra1 (mesh);
313
 
  cshade = (P3_mesh_cell_shading*) malloc (sizeof (P3_mesh_cell_shading));
314
 
  cshade->shader = shader;
315
 
  if (shader != NULL) P3_xmesh_register_material ((P3_xmesh*) mesh, shader);
316
 
  memcpy (cshade->line_color, color, 4 * sizeof (GLfloat));
317
 
  cshade->line_width_factor = line_width_factor;
318
 
  mesh->option |= P3_MESH_CELL_SHADING;
319
 
  mesh->xtra1 = cshade;
320
 
}
321
 
 
322
 
void P3_mesh_set_xtra2_sphere (P3_mesh* mesh) {
323
 
  P3_mesh_free_xtra2 (mesh);
324
 
  mesh->xtra2 = malloc (4 * sizeof (GLfloat));
325
 
  if (mesh->nb_coords > 0) {
326
 
    P3_sphere_from_points ((GLfloat*)  mesh->xtra2, mesh->coords, mesh->nb_coords);
327
 
  }
328
 
  mesh->option |= P3_MESH_HAS_SPHERE;
329
 
}
330
 
 
331
 
void P3_mesh_set_xtra2_face_list (P3_mesh* mesh) {
332
 
  P3_mesh_face_list* flist;
333
 
  flist = (P3_mesh_face_list*) malloc (sizeof (P3_mesh_face_list));
334
 
  P3_sphere_from_points (flist->sphere, mesh->coords, mesh->nb_coords);
335
 
  P3_mesh_create_faces_list (flist, mesh);
336
 
  P3_mesh_free_xtra2 (mesh);
337
 
  mesh->xtra2 = flist;
338
 
  mesh->option |= P3_MESH_FACE_LIST;
339
 
  mesh->option |= P3_MESH_HAS_SPHERE;
340
 
}
341
 
 
342
 
 
343
 
/*===========+
344
 
 | RENDERING |
345
 
 +===========*/
346
 
 
347
 
void P3_mesh_batch_outline (P3_mesh* mesh, P3_instance* inst, P3_frustum* frustum) {
348
 
 
349
 
// TO DO computation + rendering can be done during rendering ???
350
 
 
351
 
  P3_chain* face_batched;
352
 
  P3_chain* faces;
353
 
  P3_chain* max;
354
 
  GLfloat* plane;
355
 
  P3_xface* face;
356
 
  P3_xface** neighbors;
357
 
  P3_xface* n;
358
 
  GLfloat d;
359
 
  int nbv;
360
 
  int k;
361
 
  int nb = 0;
362
 
  if (((P3_mesh_cell_shading*) mesh->xtra1)->line_width_factor <= 0.0) {
363
 
    /* no outline to draw */
364
 
    return;
365
 
  }
366
 
  /* compute distance between camera and mesh to adjust outline width */
367
 
  if (mesh->option & P3_MESH_HAS_SPHERE) {
368
 
    d = P3_sphere_distance_point ((GLfloat*) mesh->xtra2, frustum->position);
369
 
    if (d < 1.0) 
370
 
      d = ((P3_mesh_cell_shading*) mesh->xtra1)->line_width_factor;
371
 
    else
372
 
      d = ((P3_mesh_cell_shading*) mesh->xtra1)->line_width_factor / d;
373
 
//      d = ((P3_mesh_cell_shading*) mesh->xtra1)->line_width_factor * 0.5 * (1.0 + 1.0 / d);
374
 
  } else {
375
 
    d = 2.0;
376
 
  }
377
 
  /* batch in secondpass */
378
 
  P3_chunk_add_float (renderer->data, d);
379
 
  /* compute line to draw */
380
 
  max = (P3_chain*) (renderer->faces->content + renderer->faces->nb);
381
 
  face_batched = (P3_chain*) (renderer->faces->content + renderer->face_start);
382
 
  faces = face_batched;
383
 
  while (faces < max) {
384
 
    face = (P3_xface*) faces->data;
385
 
    plane = face->normal;
386
 
    if (plane[0] * frustum->position[0] + plane[1] * frustum->position[1] + plane[2] * frustum->position[2] + plane[3] > 0.0)
387
 
      face->option |= P3_FACE_FRONT;
388
 
    else
389
 
      face->option |= P3_FACE_BACK;
390
 
    faces++;
391
 
  }
392
 
  /* find edges */    
393
 
  faces = face_batched;
394
 
  while (faces < max) {
395
 
    face = (P3_xface*) faces->data;
396
 
    if (face->option & P3_FACE_FRONT) {
397
 
      if (face->option & P3_FACE_QUAD) {
398
 
        neighbors = (P3_xface**) (&(face->v4) + 1);
399
 
        nbv = 4;
400
 
      } else {
401
 
        neighbors = (P3_xface**) (&(face->v4));
402
 
        nbv = 3;
403
 
      }
404
 
      /* test if neighbors are back */
405
 
      for (k = 0; k < nbv; k++) {
406
 
        n = neighbors[k];
407
 
        if (n == NULL || n->option & P3_FACE_BACK) {
408
 
          /* add edge k = vertices k and k + 1 */
409
 
          P3_chunk_add_int (renderer->data, (&(face->v1))[k]);
410
 
          if (k < nbv - 1) {
411
 
            P3_chunk_add_int (renderer->data, (&(face->v1))[k + 1]);
412
 
          } else {
413
 
            P3_chunk_add_int (renderer->data, face->v1);
414
 
          }
415
 
          nb++;
416
 
        }
417
 
      }
418
 
    }
419
 
    faces++;
420
 
  }
421
 
  P3_chunk_add_int (renderer->data, -1);
422
 
  /* remove face added option */
423
 
  faces = face_batched;
424
 
  while (faces < max) {
425
 
    ((P3_xface*) faces->data)->option &= ~(P3_FACE_FRONT | P3_FACE_BACK);
426
 
    faces++;
427
 
  }   
428
 
}
429
 
 
430
 
void P3_mesh_batch (P3_mesh* mesh, P3_instance* inst) {
431
 
  P3_frustum* frustum = P3_renderer_get_frustum (inst);
432
 
  if (mesh->option & P3_MESH_HAS_SPHERE && 
433
 
      P3_sphere_in_frustum (frustum, (GLfloat*) mesh->xtra2) == P3_FALSE) { 
434
 
    return; 
435
 
  }
436
 
  if (mesh->option & P3_MESH_DISPLAY_LISTS) {
437
 
    P3_mesh_display_list* dlist = (P3_mesh_display_list*) mesh->xtra1;
438
 
    if (dlist->nb_list_opaque > 0) P3_renderer_batch (renderer->opaque, mesh, inst, -1);
439
 
    if (dlist->nb_list_alpha  > 0) P3_renderer_batch (renderer->alpha,  mesh, inst, -1);
440
 
  } else {
441
 
    P3_xmesh_batch_start (inst);
442
 
    /* batch each face */
443
 
    if (mesh->option & P3_MESH_TREE) {
444
 
      P3_xmesh_node_batch ((P3_xmesh*) mesh, (P3_xnode*) mesh->xtra1, frustum);
445
 
    } else if (mesh->option & P3_MESH_FACE_LIST) {
446
 
      P3_mesh_face_list* flist = (P3_mesh_face_list*) mesh->xtra2;
447
 
      int i;
448
 
      for (i = 0; i < flist->nb_faces; i++) {
449
 
        P3_xmesh_face_batch ((P3_xmesh*) mesh, flist->faces[i]);
450
 
      }
451
 
    } else {
452
 
      void* face = mesh->faces;
453
 
      void* max  = mesh->faces + mesh->faces_size;
454
 
      while (face < max) {
455
 
        P3_xmesh_face_batch ((P3_xmesh*) mesh, (P3_xface*) face);
456
 
        face += P3_xmesh_face_size ((P3_xmesh*) mesh, face);
457
 
      }
458
 
    }
459
 
    P3_xmesh_batch_end ();
460
 
    if (mesh->option & P3_MESH_CELL_SHADING) {
461
 
      P3_renderer_batch (renderer->secondpass, mesh, inst, renderer->data->nb);
462
 
      P3_mesh_batch_outline (mesh, inst, frustum);
463
 
    }
464
 
  }
465
 
}
466
 
 
467
 
static void P3_mesh_prepare_cell_shading_shades (P3_mesh* mesh, GLfloat* shades, P3_list* lights) {
468
 
  P3_light* light;
469
 
  GLfloat* ptr1;
470
 
  GLfloat* ptr2;
471
 
  GLfloat v[3];
472
 
  GLfloat tmp;
473
 
  int i, j, k;
474
 
  if (mesh->option & P3_MESH_VERTEX_NORMALS) {
475
 
    /* 1 shade per vnormal */
476
 
    for (i = 0; i < lights->nb; i++) {
477
 
      light = (P3_light*) P3_list_get (lights, i);
478
 
      if (light->option & P3_LIGHT_DIRECTIONAL) {
479
 
        /* directional light */
480
 
        ptr1 = mesh->vnormals;
481
 
        for (j = 0; j < mesh->nb_vnormals; j++) {
482
 
          tmp = - P3_vector_dot_product (ptr1, light->data);
483
 
          if (tmp > 0.0) shades[j] += tmp;
484
 
          ptr1 += 3;
485
 
        }
486
 
      } else {
487
 
        /* positional light */
488
 
        ptr1 = mesh->vnormals;
489
 
        for (j = 0; j < mesh->nb_vnormals; j++) {
490
 
          /* search the coord that correspond to this vertex normal */
491
 
          for (k = 0; k < mesh->nb_vertices; k++) {
492
 
            if (mesh->vertex_normals[k] == ptr1) break;
493
 
          }
494
 
          ptr2 = mesh->vertex_coords[k];
495
 
          v[0] = light->data[0] - ptr2[0];
496
 
          v[1] = light->data[1] - ptr2[1];
497
 
          v[2] = light->data[2] - ptr2[2];
498
 
          P3_vector_normalize (v);
499
 
          tmp = P3_vector_dot_product (ptr1, v);
500
 
          if (tmp > 0.0) shades[j] += tmp;
501
 
          ptr1 += 3;
502
 
        }
503
 
      }
504
 
    }
505
 
  } else {
506
 
    /* 1 shade per coord */
507
 
    for (i = 0; i < lights->nb; i++) {
508
 
      light = (P3_light*) P3_list_get (lights, i);
509
 
      if (light->option & P3_LIGHT_DIRECTIONAL) {
510
 
        /* directional light */
511
 
        ptr1 = mesh->vnormals;
512
 
        for (j = 0; j < mesh->nb_vnormals; j++) {
513
 
          tmp = - P3_vector_dot_product (ptr1, light->data);
514
 
          if (tmp > 0.0) shades[j] += tmp;
515
 
          ptr1 += 3;
516
 
        }
517
 
      } else {
518
 
        /* positional light */
519
 
        ptr1 = mesh->vnormals;
520
 
        ptr2 = mesh->coords;
521
 
        for (j = 0; j < mesh->nb_vnormals; j++) {
522
 
          v[0] = light->data[0] - ptr2[0];
523
 
          v[1] = light->data[1] - ptr2[1];
524
 
          v[2] = light->data[2] - ptr2[2];
525
 
          P3_vector_normalize (v);
526
 
          tmp = P3_vector_dot_product (ptr1, v);
527
 
          if (tmp > 0.0) shades[j] += tmp; 
528
 
          ptr1 += 3;
529
 
          ptr2 += 3;
530
 
        }
531
 
      }
532
 
    }
533
 
  }
534
 
}
535
 
 
536
 
void P3_mesh_prepare_cell_shading (P3_mesh* mesh, P3_instance* inst, GLfloat* shades) {
537
 
  int n;
538
 
  P3_light_list_cast_into (renderer->top_lights, (P3_coordsys*) inst);
539
 
  P3_light_list_cast_into (renderer->c_context->lights, (P3_coordsys*) inst);
540
 
  if (mesh->nb_vnormals > 0) {
541
 
    for (n = 0; n < mesh->nb_vnormals; n++) shades[n] = 0.0;
542
 
    P3_mesh_prepare_cell_shading_shades (mesh, shades, renderer->top_lights);
543
 
    P3_mesh_prepare_cell_shading_shades (mesh, shades, renderer->c_context->lights);
544
 
    /* clip shade texcoord values */
545
 
    for (n = 0; n < mesh->nb_vnormals; n++) {
546
 
      /* do not clip with interval [0, 1] because smooth magnification of texture
547
 
       * causes visual bugs
548
 
       */
549
 
      if (shades[n] > 0.95) shades[n] = 0.95;
550
 
      if (shades[n] < 0.05) shades[n] = 0.05;
551
 
    }
552
 
  }
553
 
}
554
 
 
555
 
void P3_mesh_render_outline (P3_mesh* mesh) {
556
 
  void* data;
557
 
  int* ptr;
558
 
  int i;
559
 
  data = renderer->data->content + renderer->data->nb;
560
 
  glLineWidth (*((GLfloat*) data));
561
 
  glColor4fv (((P3_mesh_cell_shading*) mesh->xtra1)->line_color);
562
 
  glDisable (GL_LIGHTING);
563
 
  glDepthFunc (GL_LEQUAL);
564
 
  P3_material_inactivate (current_material);
565
 
  current_material = NULL;
566
 
  ptr = (int*) (data + sizeof (GLfloat));
567
 
  if (renderer->colors == NULL) {
568
 
    glBegin (GL_LINES);
569
 
    while (1) { 
570
 
      i = *ptr;
571
 
      if (i == -1) break;
572
 
      glVertex3fv (mesh->vertex_coords[i]);
573
 
      ptr++;
574
 
    }
575
 
    glEnd ();
576
 
  } else {
577
 
    GLfloat* color_ptr = ((P3_mesh_cell_shading*) mesh->xtra1)->line_color;
578
 
    GLfloat alpha = color_ptr[3];
579
 
    glEnable (GL_BLEND);
580
 
    glBegin (GL_LINES);
581
 
    while (1) { 
582
 
      i = *ptr;
583
 
      if (i == -1) break;
584
 
      color_ptr[3] = renderer->colors[i][3];
585
 
      glColor4fv (color_ptr);
586
 
      glVertex3fv (mesh->vertex_coords[i]);
587
 
      ptr++;
588
 
    }
589
 
    glEnd ();
590
 
    color_ptr[3] = alpha;
591
 
    glDisable (GL_BLEND);
592
 
  }
593
 
  glEnable (GL_LIGHTING);
594
 
  glDepthFunc (GL_LESS);
595
 
  glColor4fv (white);
596
 
}
597
 
 
598
 
void P3_mesh_render (P3_mesh* mesh, P3_instance* inst) {
599
 
  int i;
600
 
  P3_xmesh_option_activate (mesh->option);
601
 
  if (mesh->option & P3_MESH_WARFOGS) {
602
 
    renderer->colors = mesh->vertex_warfogs;
603
 
  } else if (mesh->option & P3_MESH_DIFFUSES) {
604
 
    renderer->colors = mesh->vertex_diffuses;
605
 
  } else {
606
 
    renderer->colors = NULL;
607
 
  }
608
 
  if (mesh->option & P3_MESH_DISPLAY_LISTS) {
609
 
    P3_mesh_display_list* displist = (P3_mesh_display_list*) mesh->xtra1;
610
 
    P3_display_list* dlists;
611
 
    P3_display_list* dlist;
612
 
    int nb;
613
 
    if (!(mesh->option & P3_MESH_INITED)) P3_mesh_display_list_init (mesh);
614
 
    if (renderer->state == P3_RENDERER_STATE_OPAQUE) {
615
 
      dlists = displist->dlists;
616
 
      nb = displist->nb_list_opaque;
617
 
    } else {
618
 
      dlists = displist->dlists + displist->nb_list_opaque;
619
 
      nb = displist->nb_list_alpha;
620
 
    }
621
 
    for (i = 0; i < nb; i++) {
622
 
      dlist = dlists + i;
623
 
      P3_xface_option_activate (dlist->option);
624
 
      P3_material_activate (dlist->material);
625
 
      glCallList (dlist->dlist);
626
 
      P3_xface_option_inactivate (dlist->option);
627
 
    }
628
 
  } else {
629
 
    if (mesh->option & P3_MESH_CELL_SHADING) {
630
 
      if (renderer->state == P3_RENDERER_STATE_SECONDPASS) {
631
 
        P3_mesh_render_outline (mesh);
632
 
      } else {
633
 
        P3_chunk* chunk;
634
 
        GLfloat* shades;
635
 
        chunk = P3_get_chunk ();
636
 
        P3_chunk_register (chunk, mesh->nb_vnormals * sizeof (GLfloat));
637
 
        shades = (GLfloat*) chunk->content;
638
 
        P3_mesh_prepare_cell_shading (mesh, inst, shades);
639
 
        /* active shader */
640
 
        light_shader = ((P3_mesh_cell_shading*) mesh->xtra1)->shader;
641
 
        P3_xmesh_pack_render_cellshaded ((P3_xmesh*) mesh, shades);
642
 
        P3_drop_chunk (chunk);
643
 
      }
644
 
    } else {
645
 
      P3_xmesh_pack_render ((P3_xmesh*) mesh);
646
 
    }
647
 
  }
648
 
  P3_xmesh_option_inactivate (mesh->option);
649
 
}
650
 
 
651
 
 
652
 
/*========+
653
 
 | SHADOW |
654
 
 +========*/
655
 
 
656
 
int P3_mesh_shadow (P3_mesh* mesh, P3_instance* inst, P3_light* light) {
657
 
 
658
 
// TO DO assume mesh has a face list
659
 
 
660
 
  P3_mesh_face_list* fl = (P3_mesh_face_list*) mesh->xtra2;
661
 
  P3_frustum* frustum;
662
 
  P3_xface** neighbors;
663
 
  P3_xface* face;
664
 
  GLfloat* coord_ptr;
665
 
  GLfloat coord[4];
666
 
  GLfloat cone[9];
667
 
  GLfloat b = renderer->c_camera->back;
668
 
  int nbv, i, k;
669
 
  if (!(mesh->option & P3_MESH_SHADOW_CAST)) return P3_FALSE;
670
 
  /* tag all faces front or back */
671
 
  P3_light_cast_into (light, (P3_coordsys*) inst);
672
 
  if (light->option & P3_LIGHT_DIRECTIONAL) {
673
 
 
674
 
printf ("-->\n");
675
 
 
676
 
    P3_cone_from_sphere_and_vector (cone, (GLfloat*) mesh->xtra2, light->data, b);
677
 
    for (i = 0; i < fl->nb_faces; i++) {
678
 
      face = fl->faces[i];
679
 
      if (mesh->option & P3_MESH_VERTEX_OPTIONS) {
680
 
        if (mesh->vertex_options[face->v1] & P3_VERTEX_INVISIBLE &&
681
 
            mesh->vertex_options[face->v2] & P3_VERTEX_INVISIBLE &&
682
 
            mesh->vertex_options[face->v3] & P3_VERTEX_INVISIBLE &&
683
 
            (face->option & P3_FACE_TRIANGLE || mesh->vertex_options[face->v4] & P3_VERTEX_INVISIBLE)) {
684
 
          continue;
685
 
        }
686
 
      }
687
 
      /* consider double sided faces as the others faces. that's not 100% working
688
 
       * but it's the best I can do
689
 
       */
690
 
      if (P3_vector_dot_product (light->data, face->normal) >= 0.0) {
691
 
        face->option |= P3_FACE_BACK;
692
 
 
693
 
printf ("back\n");
694
 
 
695
 
      } else {
696
 
        face->option |= P3_FACE_FRONT;
697
 
 
698
 
printf ("front\n");
699
 
 
700
 
      }
701
 
    }
702
 
  } else {
703
 
    if (P3_cone_from_sphere_and_origin (cone, (GLfloat*) mesh->xtra2, light->data, b) == P3_FALSE) return P3_FALSE;
704
 
    for (i = 0; i < fl->nb_faces; i++) {
705
 
      face = fl->faces[i];
706
 
      if (mesh->option & P3_MESH_VERTEX_OPTIONS) {
707
 
        if (mesh->vertex_options[face->v1] & P3_VERTEX_INVISIBLE &&
708
 
            mesh->vertex_options[face->v2] & P3_VERTEX_INVISIBLE &&
709
 
            mesh->vertex_options[face->v3] & P3_VERTEX_INVISIBLE &&
710
 
            (face->option & P3_FACE_TRIANGLE || mesh->vertex_options[face->v4] & P3_VERTEX_INVISIBLE)) {
711
 
          continue;
712
 
        }
713
 
      }
714
 
      if (light->data[0] * face->normal[0] + light->data[1] * face->normal[1] + light->data[2] * face->normal[2] + face->normal[3] > 0.0) {
715
 
        face->option |= P3_FACE_FRONT;
716
 
      } else {
717
 
        face->option |= P3_FACE_BACK;
718
 
      }
719
 
    }
720
 
  }
721
 
 
722
 
  /* draw shadow volume 1rst step */
723
 
  glStencilFunc (GL_ALWAYS, 1, 0xFFFFFFFF);
724
 
  glFrontFace (GL_CW);
725
 
  glStencilOp (GL_KEEP, GL_KEEP, GL_INCR);
726
 
  glLoadMatrixf (inst->render_matrix);
727
 
  glNewList (shadow_display_list, GL_COMPILE_AND_EXECUTE);
728
 
  glBegin (GL_QUADS);
729
 
  /* test if camera is inside the shadow */
730
 
  frustum = P3_renderer_get_frustum (inst);
731
 
  coord[0] = 0.5 * (frustum->points[0] + frustum->points[6]);
732
 
  coord[1] = 0.5 * (frustum->points[1] + frustum->points[7]);
733
 
  coord[2] = 0.5 * (frustum->points[2] + frustum->points[8]);
734
 
  coord[3] = P3_point_distance_to (coord, frustum->points);
735
 
  if (P3_sphere_is_in_cone (coord, cone) == P3_TRUE) {
736
 
    /* camera is inside the shadow => special case 
737
 
     * we must draw the intersection of the shadow volume with the camera front plane
738
 
     * by chance we already have functions to do such thing in the watercube ;)
739
 
     */
740
 
    P3_watercube_underwater* underwater;
741
 
    P3_chunk* chunk = P3_get_chunk ();
742
 
    GLfloat* coord_ptr2;
743
 
    GLfloat* m1;
744
 
    GLfloat* m2;
745
 
    GLfloat coord2[3];
746
 
    GLfloat coord3[3];
747
 
    GLfloat coord4[3];
748
 
    m1 = P3_coordsys_get_root_matrix ((P3_coordsys*) inst);
749
 
    m2 = P3_coordsys_get_inverted_root_matrix ((P3_coordsys*) renderer->c_camera);
750
 
    /* find edges and draw shadow volume */
751
 
    for (i = 0; i < fl->nb_faces; i++) {
752
 
      face = fl->faces[i];
753
 
      if (face->option & P3_FACE_BACK) {
754
 
        /* test if neighbors are front */
755
 
        if (face->option & P3_FACE_QUAD) { 
756
 
          neighbors = (P3_xface**) (&(face->v4) + 1);
757
 
          nbv = 4;
758
 
        } else {
759
 
          neighbors = (P3_xface**) (&(face->v4));
760
 
          nbv = 3;
761
 
        }
762
 
        for (k = 0; k < nbv; k++) {
763
 
          if (neighbors[k] == NULL || neighbors[k]->option & P3_FACE_FRONT) {
764
 
 
765
 
// TO DO avoid pushing 2 vertices with the same coord
766
 
 
767
 
            /* add edge k = vertices k and k + 1 */
768
 
            coord_ptr = mesh->vertex_coords[(&(face->v1))[k]];
769
 
            glVertex3fv (coord_ptr);
770
 
            /* push coord far away */
771
 
            if (light->option & P3_LIGHT_DIRECTIONAL) {
772
 
              coord[0] = coord_ptr[0] + b * light->data[0];
773
 
              coord[1] = coord_ptr[1] + b * light->data[1];
774
 
              coord[2] = coord_ptr[2] + b * light->data[2];
775
 
              glVertex3fv (coord);
776
 
            } else {
777
 
              coord[0] = coord_ptr[0] - light->data[0];
778
 
              coord[1] = coord_ptr[1] - light->data[1];
779
 
              coord[2] = coord_ptr[2] - light->data[2];
780
 
              P3_vector_normalize (coord);
781
 
              coord[0] = coord_ptr[0] + b * coord[0];
782
 
              coord[1] = coord_ptr[1] + b * coord[1];
783
 
              coord[2] = coord_ptr[2] + b * coord[2];
784
 
              glVertex3fv (coord);
785
 
            }
786
 
            if (k < nbv - 1) {
787
 
              coord_ptr2 = mesh->vertex_coords[(&(face->v1))[k + 1]];
788
 
            } else {
789
 
              coord_ptr2 = mesh->vertex_coords[face->v1];
790
 
            }
791
 
            /* push coord far away */
792
 
            if (light->option & P3_LIGHT_DIRECTIONAL) {
793
 
              coord2[0] = coord_ptr2[0] + b * light->data[0];
794
 
              coord2[1] = coord_ptr2[1] + b * light->data[1];
795
 
              coord2[2] = coord_ptr2[2] + b * light->data[2];
796
 
              glVertex3fv (coord2);
797
 
            } else {
798
 
              coord2[0] = coord_ptr2[0] - light->data[0];
799
 
              coord2[1] = coord_ptr2[1] - light->data[1];
800
 
              coord2[2] = coord_ptr2[2] - light->data[2];
801
 
              P3_vector_normalize (coord2);
802
 
              coord2[0] = coord_ptr2[0] + b * coord2[0];
803
 
              coord2[1] = coord_ptr2[1] + b * coord2[1];
804
 
              coord2[2] = coord_ptr2[2] + b * coord2[2];
805
 
              glVertex3fv (coord2);
806
 
            }
807
 
            glVertex3fv (coord_ptr2);
808
 
            /* convert point to camera coordsys */
809
 
            P3_point_by_matrix_copy (coord3, coord_ptr,  m1);
810
 
            P3_point_by_matrix_copy (coord4, coord_ptr2, m1);
811
 
            P3_point_by_matrix (coord,  m1);
812
 
            P3_point_by_matrix (coord2, m1);
813
 
            P3_point_by_matrix (coord,  m2);
814
 
            P3_point_by_matrix (coord2, m2);
815
 
            P3_point_by_matrix (coord3, m2);
816
 
            P3_point_by_matrix (coord4, m2);
817
 
            P3_watercube_underwater_intersect_face (chunk, coord3, coord, coord2);
818
 
            P3_watercube_underwater_intersect_face (chunk, coord2, coord4, coord3);
819
 
          }
820
 
        }
821
 
      }
822
 
    }
823
 
    glEnd ();
824
 
    glEndList ();
825
 
    glDisable (GL_CULL_FACE);
826
 
    glDisable (GL_DEPTH_TEST);
827
 
    glLoadIdentity ();
828
 
    if (chunk->nb == 0) {
829
 
      /* 2 cases: front plane entirely inside the shadow or entirely outside
830
 
       * use a raypicking to be sure
831
 
       */
832
 
      P3_raypick_data rdata;
833
 
      GLfloat f;
834
 
      rdata.option = P3_RAYPICK_HALF_LINE;
835
 
      rdata.raypicked = P3_get_list ();
836
 
      rdata.raypick_data = P3_get_chunk ();
837
 
      if (light->option & P3_LIGHT_DIRECTIONAL) {
838
 
        rdata.root_data[0] = renderer->r_frustum->position[0];
839
 
        rdata.root_data[1] = renderer->r_frustum->position[1];
840
 
        rdata.root_data[2] = renderer->r_frustum->position[2];
841
 
        coord_ptr = P3_coordsys_get_root_matrix ((P3_coordsys*) light);
842
 
        rdata.root_data[6] = renderer->c_camera->back;
843
 
        rdata.root_data[3] = rdata.root_data[0] + rdata.root_data[6] * coord_ptr[ 8];
844
 
        rdata.root_data[4] = rdata.root_data[1] + rdata.root_data[6] * coord_ptr[ 9];
845
 
        rdata.root_data[5] = rdata.root_data[2] + rdata.root_data[6] * coord_ptr[10];
846
 
      } else {
847
 
        coord_ptr = P3_coordsys_get_root_matrix ((P3_coordsys*) light);
848
 
        memcpy (rdata.root_data, coord_ptr + 12, 3 * sizeof (GLfloat));
849
 
        rdata.root_data[3] = renderer->r_frustum->position[0] - rdata.root_data[0];
850
 
        rdata.root_data[4] = renderer->r_frustum->position[1] - rdata.root_data[1];
851
 
        rdata.root_data[5] = renderer->r_frustum->position[2] - rdata.root_data[2];
852
 
        rdata.root_data[6] = P3_vector_length (rdata.root_data + 3);
853
 
        f = 1.0 / rdata.root_data[6];
854
 
        rdata.root_data[3] *= f;
855
 
        rdata.root_data[4] *= f;
856
 
        rdata.root_data[5] *= f;
857
 
      }
858
 
      if (P3_mesh_raypick_b (mesh, &rdata, (P3_raypickable*) inst) == P3_TRUE) {
859
 
        /* all the screen must be shadowed
860
 
         * that's cool I have the coordinate of the edge of the screen in the gl vertex array ;)
861
 
         */
862
 
        glDrawArrays (GL_QUADS, 0, 4);
863
 
      }
864
 
      for (i = 0; i < rdata.raypicked->nb; i++) {
865
 
        ((P3_raypickable*) P3_list_get (rdata.raypicked, i))->raypick_data = -1;
866
 
      }
867
 
      P3_drop_list  (rdata.raypicked);
868
 
      P3_drop_chunk (rdata.raypick_data);
869
 
    } else if (chunk->nb > 0) {
870
 
      underwater = P3_watercube_underwater_join_segments ((GLfloat*) chunk->content, chunk->nb / (3 * sizeof (GLfloat)));
871
 
      P3_watercube_underwater_draw_segments (underwater);
872
 
      free (underwater->points);
873
 
      free (underwater->seq_sizes);
874
 
      free (underwater);
875
 
    }
876
 
    P3_drop_chunk (chunk);
877
 
    glEnable (GL_CULL_FACE);
878
 
    glEnable (GL_DEPTH_TEST);
879
 
    /* draw shadow volume 2nd step */
880
 
    glLoadMatrixf (inst->render_matrix);
881
 
    glFrontFace (GL_CCW);
882
 
    glStencilFunc (GL_ALWAYS, 0, 0xFFFFFFFF);
883
 
    glStencilOp (GL_KEEP, GL_KEEP, GL_DECR);
884
 
    glCallList (shadow_display_list);
885
 
  } else {
886
 
    /* find edges and draw shadow volume */
887
 
    for (i = 0; i < fl->nb_faces; i++) {
888
 
      face = fl->faces[i];
889
 
      if (face->option & P3_FACE_BACK) {
890
 
        /* test if neighbors are front */
891
 
        if (face->option & P3_FACE_QUAD) { 
892
 
          neighbors = (P3_xface**) (&(face->v4) + 1);
893
 
          nbv = 4;
894
 
        } else {
895
 
          neighbors = (P3_xface**) (&(face->v4));
896
 
          nbv = 3;
897
 
        }
898
 
        for (k = 0; k < nbv; k++) {
899
 
          if (neighbors[k] == NULL || neighbors[k]->option & P3_FACE_FRONT) {
900
 
 
901
 
// TO DO avoid pushing 2 vertices with the same coord
902
 
 
903
 
            /* add edge k = vertices k and k + 1 */
904
 
            coord_ptr = mesh->vertex_coords[(&(face->v1))[k]];
905
 
            glVertex3fv (coord_ptr);
906
 
            /* push coord far away */
907
 
            if (light->option & P3_LIGHT_DIRECTIONAL) {
908
 
              glVertex3f (coord_ptr[0] + b * light->data[0], coord_ptr[1] + b * light->data[1], coord_ptr[2] + b * light->data[2]);
909
 
            } else {
910
 
              coord[0] = coord_ptr[0] - light->data[0];
911
 
              coord[1] = coord_ptr[1] - light->data[1];
912
 
              coord[2] = coord_ptr[2] - light->data[2];
913
 
              P3_vector_normalize (coord);
914
 
              glVertex3f (coord_ptr[0] + b * coord[0], coord_ptr[1] + b * coord[1], coord_ptr[2] + b * coord[2]);
915
 
            }
916
 
            if (k < nbv - 1) {
917
 
              coord_ptr = mesh->vertex_coords[(&(face->v1))[k + 1]];
918
 
            } else {
919
 
              coord_ptr = mesh->vertex_coords[face->v1];
920
 
            }
921
 
            /* push coord far away */
922
 
            if (light->option & P3_LIGHT_DIRECTIONAL) {
923
 
              glVertex3f (coord_ptr[0] + b * light->data[0], coord_ptr[1] + b * light->data[1], coord_ptr[2] + b * light->data[2]);
924
 
            } else {
925
 
              coord[0] = coord_ptr[0] - light->data[0];
926
 
              coord[1] = coord_ptr[1] - light->data[1];
927
 
              coord[2] = coord_ptr[2] - light->data[2];
928
 
              P3_vector_normalize (coord);
929
 
              glVertex3f (coord_ptr[0] + b * coord[0], coord_ptr[1] + b * coord[1], coord_ptr[2] + b * coord[2]);
930
 
            }
931
 
            glVertex3fv (coord_ptr);
932
 
          }
933
 
        }
934
 
      }
935
 
    }
936
 
    glEnd ();
937
 
    glEndList ();
938
 
    /* draw shadow volume 2nd step */
939
 
    glFrontFace (GL_CCW);
940
 
    glStencilFunc (GL_ALWAYS, 0, 0xFFFFFFFF);
941
 
    glStencilOp (GL_KEEP, GL_KEEP, GL_DECR);
942
 
    glCallList (shadow_display_list);
943
 
  }
944
 
  /* remove face tag */
945
 
  for (i = 0; i < fl->nb_faces; i++) fl->faces[i]->option &= ~(P3_FACE_FRONT | P3_FACE_BACK);
946
 
  return P3_TRUE;
947
 
}
948
 
 
949
 
 
950
 
/*==================+
951
 
 | SAVING / LOADING |
952
 
 +==================*/
953
 
 
954
 
static void P3_mesh_display_list_get_data (P3_mesh* mesh, P3_mesh_display_list* dlist, P3_chunk* chunk) {
955
 
  P3_display_list* dl;
956
 
  int i;
957
 
  P3_chunk_save_int (chunk, dlist->nb_list_opaque);
958
 
  P3_chunk_save_int (chunk, dlist->nb_list_alpha);
959
 
  for (i = 0; i < dlist->nb_list_opaque + dlist->nb_list_alpha; i++) {
960
 
    dl = dlist->dlists + i;
961
 
    P3_chunk_save_int (chunk, dl->option);
962
 
    P3_chunk_save_int (chunk, P3_xmesh_get_material_index ((P3_xmesh*) mesh, dl->material));
963
 
  }
964
 
}
965
 
 
966
 
static void P3_mesh_cell_shading_get_data (P3_mesh* mesh, P3_mesh_cell_shading* cshade, P3_chunk* chunk) {
967
 
  P3_chunk_save (chunk, cshade->line_color, 4 * sizeof (GLfloat));
968
 
  P3_chunk_save_float (chunk, cshade->line_width_factor);
969
 
  if (cshade->shader == NULL) {
970
 
    P3_chunk_save_int (chunk, -1);
971
 
  } else {
972
 
    P3_chunk_save_int (chunk, P3_xmesh_get_material_index ((P3_xmesh*) mesh, cshade->shader));
973
 
  }
974
 
}
975
 
 
976
 
void P3_mesh_get_data (P3_mesh* mesh, P3_chunk* chunk) {
977
 
  int i;
978
 
  /* option */
979
 
  i = mesh->option;
980
 
  if (i & P3_MESH_INITED) i -= P3_MESH_INITED;
981
 
  P3_chunk_save_int (chunk, i);
982
 
  /* xtra */
983
 
  if (mesh->option & P3_MESH_DISPLAY_LISTS) {
984
 
    P3_mesh_display_list_get_data (mesh, (P3_mesh_display_list*) mesh->xtra1, chunk);
985
 
  }
986
 
  if (mesh->option & P3_MESH_HAS_SPHERE) {
987
 
    P3_chunk_save (chunk, (GLfloat*) mesh->xtra2, 4 * sizeof (GLfloat));
988
 
  }
989
 
  if (mesh->option & P3_MESH_CELL_SHADING) {
990
 
    P3_mesh_cell_shading_get_data (mesh, (P3_mesh_cell_shading*) mesh->xtra1, chunk);
991
 
  }
992
 
  /* xmesh data */
993
 
  P3_xmesh_get_data ((P3_xmesh*) mesh, chunk);
994
 
  /* xtra */
995
 
  if (mesh->option & P3_MESH_TREE) {
996
 
    P3_xnode_get_data ((P3_xnode*) mesh->xtra1, (P3_xmesh*) mesh, chunk);
997
 
  }
998
 
}
999
 
 
1000
 
static P3_mesh_display_list* P3_mesh_display_list_set_data (P3_mesh* mesh, P3_chunk* chunk) {
1001
 
  P3_mesh_display_list* dlist;
1002
 
  P3_display_list* dl;
1003
 
  int nb;
1004
 
  int i;
1005
 
  int n;
1006
 
  dlist = (P3_mesh_display_list*) malloc (sizeof (P3_mesh_display_list));
1007
 
  dlist->nb_list_opaque     = P3_chunk_load_int (chunk);
1008
 
  dlist->nb_list_alpha      = P3_chunk_load_int (chunk);
1009
 
  nb = dlist->nb_list_opaque + dlist->nb_list_alpha;
1010
 
  dlist->dlists = (P3_display_list*) malloc (nb * sizeof (P3_display_list));
1011
 
  for (i = 0; i < nb; i++) {
1012
 
    dl = dlist->dlists + i;
1013
 
    dl->option   = P3_chunk_load_int (chunk);
1014
 
    n = P3_chunk_load_int (chunk);
1015
 
    if (n == -1) {
1016
 
      dl->material = NULL;
1017
 
    } else {
1018
 
      dl->material = mesh->materials[n];
1019
 
    }
1020
 
  }
1021
 
  return dlist;
1022
 
}
1023
 
 
1024
 
static void P3_mesh_cell_shading_set_data (P3_mesh* mesh, P3_mesh_cell_shading* cshade, P3_chunk* chunk) {
1025
 
  int n;
1026
 
  P3_chunk_load (chunk, cshade->line_color, 4 * sizeof (GLfloat));
1027
 
  cshade->line_width_factor = P3_chunk_load_float (chunk);
1028
 
  n = P3_chunk_load_int (chunk);
1029
 
  if (n == -1) {
1030
 
    cshade->shader = NULL;
1031
 
  } else {
1032
 
    cshade->shader = mesh->materials[n];
1033
 
  }
1034
 
}
1035
 
 
1036
 
void P3_mesh_set_data (P3_mesh* mesh, P3_chunk* chunk) {
1037
 
  /* option */
1038
 
  mesh->option = P3_chunk_load_int (chunk);
1039
 
  mesh->xtra1 = NULL;
1040
 
  mesh->xtra2 = NULL;
1041
 
  /* xtra */
1042
 
  if (mesh->option & P3_MESH_DISPLAY_LISTS) mesh->xtra1 = P3_mesh_display_list_set_data (mesh, chunk);
1043
 
  if (mesh->option & P3_MESH_FACE_LIST) {
1044
 
    P3_mesh_face_list* flist = (P3_mesh_face_list*) malloc (sizeof (P3_mesh_face_list));
1045
 
    P3_chunk_load (chunk, flist->sphere, 4 * sizeof (GLfloat));
1046
 
    mesh->xtra2 = flist;
1047
 
  } else if (mesh->option & P3_MESH_HAS_SPHERE) {
1048
 
    GLfloat* sphere = (GLfloat*) malloc (4 * sizeof (GLfloat));
1049
 
    P3_chunk_load (chunk, sphere, 4 * sizeof (GLfloat));
1050
 
    mesh->xtra2 = sphere;
1051
 
  }
1052
 
  if (mesh->option & P3_MESH_CELL_SHADING) {
1053
 
    P3_mesh_cell_shading* cshade = (P3_mesh_cell_shading*) malloc (sizeof (P3_mesh_cell_shading));
1054
 
    P3_mesh_cell_shading_set_data (mesh, cshade, chunk);
1055
 
    mesh->xtra1 = cshade;
1056
 
  }
1057
 
  /* xmesh data */
1058
 
  P3_xmesh_set_data ((P3_xmesh*) mesh, chunk);
1059
 
  /* xtra */
1060
 
  if (mesh->option & P3_MESH_TREE) mesh->xtra1 = P3_xnode_set_data ((P3_xmesh*) mesh, chunk);
1061
 
  /* xtra initialization */
1062
 
  if (mesh->option & P3_MESH_FACE_LIST) P3_mesh_create_faces_list ((P3_mesh_face_list*) mesh->xtra2, mesh);
1063
 
}
1064