1 /***********************************************************************
5 Gestion des light sous OpenGL
11 - We can't take in account GL_QUADRATIC_ATTENUATION.
13 - Position des lumieres. Il faut faire le glLight(GL_POSITION) apres la
14 gestion des matrices de transformation (GL_MODELVIEW).
16 - Se mefier de GL_POSITION pour les lumieres directionelle. GL_POSITION
17 indique bien la position ou se trouve la lumiere. Comme on a une direction
18 comme valeur il faut l'inverser.
23 - La variable lightOn permet d'optimiser le changement d'etat des lumieres.
24 Ceci permet d'eviter d'appeler glIsEnable(GL_LIGHTING). Il faut bien sur
25 que l'on utilise les 2 points d'entree LightOn() et LightOff().
26 L'init de la variable est faite par call_func_redraw_all_structs (OpenGl_funcs.c)
29 HISTORIQUE DES MODIFICATIONS :
30 --------------------------------
31 20-06-97 : PCT ; creation
32 30-06-96 : FMN ; Integration
33 18-07-96 : FMN, PCT ; Correction indice, ajout IsLightOn()
34 25-07-97 : CAL ; Portage NT (include OpenGl_tgl_all.h)
35 07-10-97 : FMN ; Simplification WNT
37 ************************************************************************/
39 /*----------------------------------------------------------------------*/
44 #include <OpenGl_tgl_all.hxx>
52 #include <OpenGl_LightBox.hxx>
53 #include <OpenGl_Memory.hxx>
55 /*----------------------------------------------------------------------*/
63 #define NO_PRINT_DEBUG
65 #define GROW_SIZE_WKS 10
66 #define GROW_SIZE_LIGHT 8
68 /*----------------------------------------------------------------------*/
76 IMPLEMENT_MEMORY_OPERATORS
84 TEL_LIGHT_DATA *lights;
85 IMPLEMENT_MEMORY_OPERATORS
89 /*----------------------------------------------------------------------*/
94 static TEL_LIGHT_WKS *wks = NULL;
95 static int wks_count = 0;
96 static int wks_size = 0;
98 static GLfloat default_amb[4] = { 0.0, 0.0, 0.0, 1.0 };
99 static GLfloat default_sptdir[3] = { 0.0, 0.0, -1.0 };
100 static GLfloat default_sptexpo = 0.0;
101 static GLfloat default_sptcutoff = 180.0;
103 static GLboolean lightOn = GL_FALSE;
105 /*----------------------------------------------------------------------*/
110 /*-----------------------------------------------------------------*/
114 static void bind_light(TEL_LIGHT *lptr, int *gl_lid)
117 GLfloat data_diffu[4];
119 GLfloat data_sptdir[3];
120 GLfloat data_sptexpo;
121 GLfloat data_sptcutoff;
122 GLfloat data_constantattenuation;
123 GLfloat data_linearattenuation;
127 /* on n'a droit qu'a 8 lights avec OpenGL... */
128 if (*gl_lid > GL_LIGHT7) return;
131 /* la light est une headlight ? */
134 glGetIntegerv(GL_MATRIX_MODE, &cur_matrix);
135 glMatrixMode(GL_MODELVIEW);
141 /* set la light en fonction de son type */
145 data_amb[0] = lptr->col.rgb[0];
146 data_amb[1] = lptr->col.rgb[1];
147 data_amb[2] = lptr->col.rgb[2];
150 /*------------------------- Ambient ---------------------------*/
152 * The GL_AMBIENT parameter refers to RGBA intensity of the ambient
155 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, data_amb);
158 printf("OpenGL_LightBox::bind_light:LightAmbient \n");
159 printf("\tGL_LIGHT_MODEL_AMBIENT %f %f %f \n", data_amb[0], data_amb[1], data_amb[2]);
164 case TLightDirectional:
165 data_diffu[0] = lptr->col.rgb[0];
166 data_diffu[1] = lptr->col.rgb[1];
167 data_diffu[2] = lptr->col.rgb[2];
170 /*------------------------- Direction ---------------------------*/
171 /* From Open GL Programming Rev 1 Guide Chapt 6 :
172 Lighting The Mathematics of Lighting ( p 168 )
174 Directional Light Source ( Infinite ) :
175 if the last parameter of GL_POSITION , w , is zero, the
176 corresponding light source is a Directional one.
178 GL_SPOT_CUTOFF a 180 signifie que ce n'est pas un spot.
179 To create a realistic effect, set the GL_SPECULAR parameter
180 to the same value as the GL_DIFFUSE.
183 data_pos[0] = -lptr->dir[0];
184 data_pos[1] = -lptr->dir[1];
185 data_pos[2] = -lptr->dir[2];
188 glLightfv(*gl_lid, GL_AMBIENT, default_amb);
189 glLightfv(*gl_lid, GL_DIFFUSE, data_diffu);
190 glLightfv(*gl_lid, GL_SPECULAR, data_diffu);
192 glLightfv(*gl_lid, GL_POSITION, data_pos);
193 glLightfv(*gl_lid, GL_SPOT_DIRECTION, default_sptdir);
194 glLightf(*gl_lid, GL_SPOT_EXPONENT, default_sptexpo);
195 glLightf(*gl_lid, GL_SPOT_CUTOFF, default_sptcutoff);
198 printf("OpenGL_LightBox::bind_light:LightDirection \n");
199 printf("\tGL_AMBIENT %f %f %f \n", default_amb[0], default_amb[1], default_amb[2]);
200 printf("\tGL_DIFFUSE %f %f %f \n", data_diffu[0], data_diffu[1], data_diffu[2]);
201 printf("\tGL_SPECULAR %f %f %f \n", data_diffu[0], data_diffu[1], data_diffu[2]);
202 printf("\tGL_POSITION %f %f %f \n", data_pos[0], data_pos[1], data_pos[2]);
207 case TLightPositional:
208 data_diffu[0] = lptr->col.rgb[0];
209 data_diffu[1] = lptr->col.rgb[1];
210 data_diffu[2] = lptr->col.rgb[2];
213 /*------------------------- Position -----------------------------*/
214 /* From Open GL Programming Rev 1 Guide Chapt 6 :
215 Lighting The Mathematics of Lighting ( p 168 )
216 Positional Light Source :
217 if the last parameter of GL_POSITION , w , is nonzero,
218 the corresponding light source is a Positional one.
220 GL_SPOT_CUTOFF a 180 signifie que ce n'est pas un spot.
222 To create a realistic effect, set the GL_SPECULAR parameter
223 to the same value as the GL_DIFFUSE.
226 data_pos[0] = lptr->pos[0];
227 data_pos[1] = lptr->pos[1];
228 data_pos[2] = lptr->pos[2];
231 data_constantattenuation = lptr->atten[0];
232 data_linearattenuation = lptr->atten[1];
234 glLightfv(*gl_lid, GL_AMBIENT, default_amb);
235 glLightfv(*gl_lid, GL_DIFFUSE, data_diffu);
236 glLightfv(*gl_lid, GL_SPECULAR, data_diffu);
238 glLightfv(*gl_lid, GL_POSITION, data_pos);
239 glLightfv(*gl_lid, GL_SPOT_DIRECTION, default_sptdir);
240 glLightf(*gl_lid, GL_SPOT_EXPONENT, default_sptexpo);
241 glLightf(*gl_lid, GL_SPOT_CUTOFF, default_sptcutoff);
242 glLightf(*gl_lid, GL_CONSTANT_ATTENUATION, data_constantattenuation);
243 glLightf(*gl_lid, GL_LINEAR_ATTENUATION, data_linearattenuation);
244 glLightf(*gl_lid, GL_QUADRATIC_ATTENUATION, 0.0);
247 printf("OpenGL_LightBox::bind_light:LightPosition \n");
248 printf("\tGL_AMBIENT %f %f %f \n", default_amb[0], default_amb[1], default_amb[2]);
249 printf("\tGL_DIFFUSE %f %f %f \n", data_diffu[0], data_diffu[1], data_diffu[2]);
250 printf("\tGL_SPECULAR %f %f %f \n", data_diffu[0], data_diffu[1], data_diffu[2]);
251 printf("\tGL_POSITION %f %f %f \n", data_pos[0], data_pos[1], data_pos[2]);
257 data_diffu[0] = lptr->col.rgb[0];
258 data_diffu[1] = lptr->col.rgb[1];
259 data_diffu[2] = lptr->col.rgb[2];
262 data_pos[0] = lptr->pos[0];
263 data_pos[1] = lptr->pos[1];
264 data_pos[2] = lptr->pos[2];
267 data_sptdir[0] = lptr->dir[0];
268 data_sptdir[1] = lptr->dir[1];
269 data_sptdir[2] = lptr->dir[2];
271 data_sptexpo = ( float )lptr->shine * 128.0F;
272 data_sptcutoff = ( float )(lptr->angle * 180.0F)/( float )M_PI;
274 data_constantattenuation = lptr->atten[0];
275 data_linearattenuation = lptr->atten[1];
277 glLightfv(*gl_lid, GL_AMBIENT, default_amb);
278 glLightfv(*gl_lid, GL_DIFFUSE, data_diffu);
279 glLightfv(*gl_lid, GL_SPECULAR, data_diffu);
281 glLightfv(*gl_lid, GL_POSITION, data_pos);
282 glLightfv(*gl_lid, GL_SPOT_DIRECTION, data_sptdir);
283 glLightf(*gl_lid, GL_SPOT_EXPONENT, data_sptexpo);
284 glLightf(*gl_lid, GL_SPOT_CUTOFF, data_sptcutoff);
285 glLightf(*gl_lid, GL_CONSTANT_ATTENUATION, data_constantattenuation);
286 glLightf(*gl_lid, GL_LINEAR_ATTENUATION, data_linearattenuation);
287 glLightf(*gl_lid, GL_QUADRATIC_ATTENUATION, 0.0);
290 printf("OpenGL_LightBox::bind_light:LightSpot \n");
291 printf("\tGL_AMBIENT %f %f %f \n", default_amb[0], default_amb[1], default_amb[2]);
292 printf("\tGL_DIFFUSE %f %f %f \n", data_diffu[0], data_diffu[1], data_diffu[2]);
293 printf("\tGL_SPECULAR %f %f %f \n", data_diffu[0], data_diffu[1], data_diffu[2]);
294 printf("\tGL_POSITION %f %f %f \n", data_pos[0], data_pos[1], data_pos[2]);
295 printf("\tGL_SPOT_DIRECTION %f %f %f \n", data_sptdir[0], data_sptdir[1], data_sptdir[2]);
296 printf("\tGL_SPOT_EXPONENT %f \n", data_sptexpo);
297 printf("\tGL_SPOT_CUTOFF %f \n", data_sptcutoff);
298 printf("\tGL_CONSTANT_ATTENUATION %f \n", data_constantattenuation);
299 printf("\tGL_LINEAR_ATTENUATION %f \n", data_linearattenuation);
305 if (lptr->type != TLightAmbient)
308 printf("OpenGL_LightBox::bind_light:glEnable %d \n", *gl_lid);
315 /* si la light etait une headlight alors restaure la matrice precedente */
319 glMatrixMode(cur_matrix);
324 /*-----------------------------------------------------------------*/
326 * recherche de la liste de lampe d'une wks, creation d'une liste si non existante
328 static int find_wks(Tint WksID, int alloc)
332 /* recherche la wks dans la liste si elle existe */
333 for (i=0; i<wks_count; i++)
334 if (wks[i].wks == WksID)
337 if (!alloc) return -1;
339 /* la wks n'existe pas => on fait de la place si yen a plus */
340 if (wks_count == wks_size )
342 wks_size += GROW_SIZE_WKS;
344 wks = new TEL_LIGHT_WKS[wks_size];
346 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)
347 wks = (TEL_LIGHT_WKS*)realloc(wks, wks_size*sizeof(TEL_LIGHT_WKS));
349 wks = cmn_resizemem<TEL_LIGHT_WKS>(wks, wks_size);
356 wks[wks_count].wks = WksID;
357 wks[wks_count].lights = NULL;
358 wks[wks_count].lights_size = 0;
359 wks[wks_count].lights_count = 0;
364 /*-----------------------------------------------------------------*/
366 * recherche une lampe d'une wks, creation d'une lampe si elle n'existe pas
368 static int find_light(int WksIdx, Tint LightID, int alloc)
371 TEL_LIGHT_DATA *lights;
374 /* recherche la light dans la liste de la wks */
375 lights = wks[WksIdx].lights;
376 for (i=0; i<wks[WksIdx].lights_count; i++)
377 if (lights[i].LightID == LightID)
380 if (!alloc) return -1;
382 /* la lampe n'existe pas => on cree une lampe */
383 if (wks[WksIdx].lights_count == wks[WksIdx].lights_size)
385 wks[WksIdx].lights_size += GROW_SIZE_LIGHT;
386 if (!wks[WksIdx].lights)
387 wks[WksIdx].lights = new TEL_LIGHT_DATA[wks[WksIdx].lights_size];
390 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)
391 (TEL_LIGHT_DATA*)realloc( wks[WksIdx].lights,
392 wks[WksIdx].lights_size*sizeof(TEL_LIGHT_DATA) );
394 cmn_resizemem<TEL_LIGHT_DATA>( wks[WksIdx].lights,
395 wks[WksIdx].lights_size );
397 if (!wks[WksIdx].lights)
401 return wks[WksIdx].lights_count++;
405 /*----------------------------------------------------------------------*/
407 * Fonctions publiques
411 /*-----------------------------------------------------------------*/
413 * Ajout d'une lumiere dans la Wks
415 TStatus AddLight(Tint WksID, Tint LightID, tel_light light)
421 printf("AddLight %d dans wks %d [wds_count=%d]\n", LightID, WksID, wks_count);
424 /* obtient le numero de la liste de lights de la wks */
425 wks_entry = find_wks(WksID, 1);
426 if (wks_entry == -1) return TFailure;
428 /* recherche le numero de la lampe si elle existe */
429 light_entry = find_light(wks_entry, LightID, 1);
430 if (light_entry == -1) return TFailure;
432 /* met a jour la light */
433 wks[wks_entry].lights[light_entry].LightID = LightID;
434 wks[wks_entry].lights[light_entry].light = *light;
437 printf("ajout ok\n");
443 /*-----------------------------------------------------------------*/
445 * Maj des lumieres de la Wks
447 TStatus UpdateLight(Tint WksID)
455 printf("UpdateLight %d\n", WksID);
458 /* vire toutes les lights des le depart avant une re-init complete */
459 for (i=GL_LIGHT0; i<=GL_LIGHT7; i++)
461 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, default_amb);
463 /* recherche la liste de light de la wks */
464 wks_entry = find_wks(WksID, 0);
465 if (wks_entry == -1) return TFailure;
468 printf("*** Update: nb = %d\n", wks[wks_entry].lights_count);
473 for (i=0; i<wks[wks_entry].lights_count; i++)
476 printf("binding light %d\n", i);
478 bind_light(&wks[wks_entry].lights[i].light, &gl_lid);
481 if (wks[wks_entry].lights_count > 0) LightOn();
484 printf("update ok\n");
491 /*-----------------------------------------------------------------*/
493 * Remove une lumiere de la Wks
495 TStatus RemoveLight(Tint WksID, Tint LightID)
501 /* recherche de la wks */
502 wks_entry = find_wks(WksID, 0);
503 if (wks_entry == -1) return TFailure;
505 /* recherche de la light */
506 light_entry = find_light(wks_entry, LightID, 0);
507 if (light_entry == -1) return TFailure;
509 /* retire la light */
510 memcpy(&wks[wks_entry].lights[light_entry],
511 &wks[wks_entry].lights[light_entry+1],
512 (wks[wks_entry].lights_count - light_entry - 1)*sizeof(TEL_LIGHT_DATA));
513 wks[wks_entry].lights_count--;
516 printf("RemoveLight %d dans wks %d [wds_count=%d]\n", LightID, WksID, wks_count);
523 /*-----------------------------------------------------------------*/
525 * Remove des lumieres de la Wks
527 TStatus RemoveWksLight(Tint WksID)
531 /* recherche de la wks */
532 wks_entry = find_wks(WksID, 0);
533 if (wks_entry == -1) return TFailure;
535 /* destruction de toute la wks */
536 free(wks[wks_entry].lights);
537 memcpy(&wks[wks_entry],
539 (wks_count - wks_entry - 1)*sizeof(TEL_LIGHT_WKS));
546 /*-----------------------------------------------------------------*/
548 * Reset de toutes les lights d'une Wks
550 TStatus ResetWksLight(Tint WksID)
554 /* recherche de la wks */
555 wks_entry = find_wks(WksID, 0);
556 if (wks_entry == -1) return TFailure;
558 /* destruction de toutes les lights */
559 wks[wks_entry].lights_count = 0;
565 /*-----------------------------------------------------------------*/
573 printf("LightOn(): lighting already enabled!");
575 printf("LightOn() succeeded");
577 glEnable(GL_LIGHTING);
581 /*-----------------------------------------------------------------*/
589 printf("LightOff(): lighting already disabled!");
591 printf("LightOff() succeeded");
593 glDisable(GL_LIGHTING);
595 /*-----------------------------------------------------------------*/
598 * IsEnable des lights
601 GLboolean IsLightOn(void)
603 return glIsEnabled(GL_LIGHTING);
605 /*-----------------------------------------------------------------*/