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
69 # define M_PI 3.14159265358979323846
72 /*----------------------------------------------------------------------*/
80 IMPLEMENT_MEMORY_OPERATORS
88 TEL_LIGHT_DATA *lights;
89 IMPLEMENT_MEMORY_OPERATORS
93 /*----------------------------------------------------------------------*/
98 static TEL_LIGHT_WKS *wks = NULL;
99 static int wks_count = 0;
100 static int wks_size = 0;
102 static GLfloat default_amb[4] = { 0.0, 0.0, 0.0, 1.0 };
103 static GLfloat default_sptdir[3] = { 0.0, 0.0, -1.0 };
104 static GLfloat default_sptexpo = 0.0;
105 static GLfloat default_sptcutoff = 180.0;
107 static GLboolean lightOn = GL_FALSE;
109 /*----------------------------------------------------------------------*/
114 /*-----------------------------------------------------------------*/
118 static void bind_light(TEL_LIGHT *lptr, int *gl_lid)
121 GLfloat data_diffu[4];
123 GLfloat data_sptdir[3];
124 GLfloat data_sptexpo;
125 GLfloat data_sptcutoff;
126 GLfloat data_constantattenuation;
127 GLfloat data_linearattenuation;
131 /* on n'a droit qu'a 8 lights avec OpenGL... */
132 if (*gl_lid > GL_LIGHT7) return;
135 /* la light est une headlight ? */
138 glGetIntegerv(GL_MATRIX_MODE, &cur_matrix);
139 glMatrixMode(GL_MODELVIEW);
145 /* set la light en fonction de son type */
149 data_amb[0] = lptr->col.rgb[0];
150 data_amb[1] = lptr->col.rgb[1];
151 data_amb[2] = lptr->col.rgb[2];
154 /*------------------------- Ambient ---------------------------*/
156 * The GL_AMBIENT parameter refers to RGBA intensity of the ambient
159 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, data_amb);
162 printf("OpenGL_LightBox::bind_light:LightAmbient \n");
163 printf("\tGL_LIGHT_MODEL_AMBIENT %f %f %f \n", data_amb[0], data_amb[1], data_amb[2]);
168 case TLightDirectional:
169 data_diffu[0] = lptr->col.rgb[0];
170 data_diffu[1] = lptr->col.rgb[1];
171 data_diffu[2] = lptr->col.rgb[2];
174 /*------------------------- Direction ---------------------------*/
175 /* From Open GL Programming Rev 1 Guide Chapt 6 :
176 Lighting The Mathematics of Lighting ( p 168 )
178 Directional Light Source ( Infinite ) :
179 if the last parameter of GL_POSITION , w , is zero, the
180 corresponding light source is a Directional one.
182 GL_SPOT_CUTOFF a 180 signifie que ce n'est pas un spot.
183 To create a realistic effect, set the GL_SPECULAR parameter
184 to the same value as the GL_DIFFUSE.
187 data_pos[0] = -lptr->dir[0];
188 data_pos[1] = -lptr->dir[1];
189 data_pos[2] = -lptr->dir[2];
192 glLightfv(*gl_lid, GL_AMBIENT, default_amb);
193 glLightfv(*gl_lid, GL_DIFFUSE, data_diffu);
194 glLightfv(*gl_lid, GL_SPECULAR, data_diffu);
196 glLightfv(*gl_lid, GL_POSITION, data_pos);
197 glLightfv(*gl_lid, GL_SPOT_DIRECTION, default_sptdir);
198 glLightf(*gl_lid, GL_SPOT_EXPONENT, default_sptexpo);
199 glLightf(*gl_lid, GL_SPOT_CUTOFF, default_sptcutoff);
202 printf("OpenGL_LightBox::bind_light:LightDirection \n");
203 printf("\tGL_AMBIENT %f %f %f \n", default_amb[0], default_amb[1], default_amb[2]);
204 printf("\tGL_DIFFUSE %f %f %f \n", data_diffu[0], data_diffu[1], data_diffu[2]);
205 printf("\tGL_SPECULAR %f %f %f \n", data_diffu[0], data_diffu[1], data_diffu[2]);
206 printf("\tGL_POSITION %f %f %f \n", data_pos[0], data_pos[1], data_pos[2]);
211 case TLightPositional:
212 data_diffu[0] = lptr->col.rgb[0];
213 data_diffu[1] = lptr->col.rgb[1];
214 data_diffu[2] = lptr->col.rgb[2];
217 /*------------------------- Position -----------------------------*/
218 /* From Open GL Programming Rev 1 Guide Chapt 6 :
219 Lighting The Mathematics of Lighting ( p 168 )
220 Positional Light Source :
221 if the last parameter of GL_POSITION , w , is nonzero,
222 the corresponding light source is a Positional one.
224 GL_SPOT_CUTOFF a 180 signifie que ce n'est pas un spot.
226 To create a realistic effect, set the GL_SPECULAR parameter
227 to the same value as the GL_DIFFUSE.
230 data_pos[0] = lptr->pos[0];
231 data_pos[1] = lptr->pos[1];
232 data_pos[2] = lptr->pos[2];
235 data_constantattenuation = lptr->atten[0];
236 data_linearattenuation = lptr->atten[1];
238 glLightfv(*gl_lid, GL_AMBIENT, default_amb);
239 glLightfv(*gl_lid, GL_DIFFUSE, data_diffu);
240 glLightfv(*gl_lid, GL_SPECULAR, data_diffu);
242 glLightfv(*gl_lid, GL_POSITION, data_pos);
243 glLightfv(*gl_lid, GL_SPOT_DIRECTION, default_sptdir);
244 glLightf(*gl_lid, GL_SPOT_EXPONENT, default_sptexpo);
245 glLightf(*gl_lid, GL_SPOT_CUTOFF, default_sptcutoff);
246 glLightf(*gl_lid, GL_CONSTANT_ATTENUATION, data_constantattenuation);
247 glLightf(*gl_lid, GL_LINEAR_ATTENUATION, data_linearattenuation);
248 glLightf(*gl_lid, GL_QUADRATIC_ATTENUATION, 0.0);
251 printf("OpenGL_LightBox::bind_light:LightPosition \n");
252 printf("\tGL_AMBIENT %f %f %f \n", default_amb[0], default_amb[1], default_amb[2]);
253 printf("\tGL_DIFFUSE %f %f %f \n", data_diffu[0], data_diffu[1], data_diffu[2]);
254 printf("\tGL_SPECULAR %f %f %f \n", data_diffu[0], data_diffu[1], data_diffu[2]);
255 printf("\tGL_POSITION %f %f %f \n", data_pos[0], data_pos[1], data_pos[2]);
261 data_diffu[0] = lptr->col.rgb[0];
262 data_diffu[1] = lptr->col.rgb[1];
263 data_diffu[2] = lptr->col.rgb[2];
266 data_pos[0] = lptr->pos[0];
267 data_pos[1] = lptr->pos[1];
268 data_pos[2] = lptr->pos[2];
271 data_sptdir[0] = lptr->dir[0];
272 data_sptdir[1] = lptr->dir[1];
273 data_sptdir[2] = lptr->dir[2];
275 data_sptexpo = ( float )lptr->shine * 128.0F;
276 data_sptcutoff = ( float )(lptr->angle * 180.0F)/( float )M_PI;
278 data_constantattenuation = lptr->atten[0];
279 data_linearattenuation = lptr->atten[1];
281 glLightfv(*gl_lid, GL_AMBIENT, default_amb);
282 glLightfv(*gl_lid, GL_DIFFUSE, data_diffu);
283 glLightfv(*gl_lid, GL_SPECULAR, data_diffu);
285 glLightfv(*gl_lid, GL_POSITION, data_pos);
286 glLightfv(*gl_lid, GL_SPOT_DIRECTION, data_sptdir);
287 glLightf(*gl_lid, GL_SPOT_EXPONENT, data_sptexpo);
288 glLightf(*gl_lid, GL_SPOT_CUTOFF, data_sptcutoff);
289 glLightf(*gl_lid, GL_CONSTANT_ATTENUATION, data_constantattenuation);
290 glLightf(*gl_lid, GL_LINEAR_ATTENUATION, data_linearattenuation);
291 glLightf(*gl_lid, GL_QUADRATIC_ATTENUATION, 0.0);
294 printf("OpenGL_LightBox::bind_light:LightSpot \n");
295 printf("\tGL_AMBIENT %f %f %f \n", default_amb[0], default_amb[1], default_amb[2]);
296 printf("\tGL_DIFFUSE %f %f %f \n", data_diffu[0], data_diffu[1], data_diffu[2]);
297 printf("\tGL_SPECULAR %f %f %f \n", data_diffu[0], data_diffu[1], data_diffu[2]);
298 printf("\tGL_POSITION %f %f %f \n", data_pos[0], data_pos[1], data_pos[2]);
299 printf("\tGL_SPOT_DIRECTION %f %f %f \n", data_sptdir[0], data_sptdir[1], data_sptdir[2]);
300 printf("\tGL_SPOT_EXPONENT %f \n", data_sptexpo);
301 printf("\tGL_SPOT_CUTOFF %f \n", data_sptcutoff);
302 printf("\tGL_CONSTANT_ATTENUATION %f \n", data_constantattenuation);
303 printf("\tGL_LINEAR_ATTENUATION %f \n", data_linearattenuation);
309 if (lptr->type != TLightAmbient)
312 printf("OpenGL_LightBox::bind_light:glEnable %d \n", *gl_lid);
319 /* si la light etait une headlight alors restaure la matrice precedente */
323 glMatrixMode(cur_matrix);
328 /*-----------------------------------------------------------------*/
330 * recherche de la liste de lampe d'une wks, creation d'une liste si non existante
332 static int find_wks(Tint WksID, int alloc)
336 /* recherche la wks dans la liste si elle existe */
337 for (i=0; i<wks_count; i++)
338 if (wks[i].wks == WksID)
341 if (!alloc) return -1;
343 /* la wks n'existe pas => on fait de la place si yen a plus */
344 if (wks_count == wks_size )
346 wks_size += GROW_SIZE_WKS;
348 wks = new TEL_LIGHT_WKS[wks_size];
350 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)
351 wks = (TEL_LIGHT_WKS*)realloc(wks, wks_size*sizeof(TEL_LIGHT_WKS));
353 wks = cmn_resizemem<TEL_LIGHT_WKS>(wks, wks_size);
360 wks[wks_count].wks = WksID;
361 wks[wks_count].lights = NULL;
362 wks[wks_count].lights_size = 0;
363 wks[wks_count].lights_count = 0;
368 /*-----------------------------------------------------------------*/
370 * recherche une lampe d'une wks, creation d'une lampe si elle n'existe pas
372 static int find_light(int WksIdx, Tint LightID, int alloc)
375 TEL_LIGHT_DATA *lights;
378 /* recherche la light dans la liste de la wks */
379 lights = wks[WksIdx].lights;
380 for (i=0; i<wks[WksIdx].lights_count; i++)
381 if (lights[i].LightID == LightID)
384 if (!alloc) return -1;
386 /* la lampe n'existe pas => on cree une lampe */
387 if (wks[WksIdx].lights_count == wks[WksIdx].lights_size)
389 wks[WksIdx].lights_size += GROW_SIZE_LIGHT;
390 if (!wks[WksIdx].lights)
391 wks[WksIdx].lights = new TEL_LIGHT_DATA[wks[WksIdx].lights_size];
394 #if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x530)
395 (TEL_LIGHT_DATA*)realloc( wks[WksIdx].lights,
396 wks[WksIdx].lights_size*sizeof(TEL_LIGHT_DATA) );
398 cmn_resizemem<TEL_LIGHT_DATA>( wks[WksIdx].lights,
399 wks[WksIdx].lights_size );
401 if (!wks[WksIdx].lights)
405 return wks[WksIdx].lights_count++;
409 /*----------------------------------------------------------------------*/
411 * Fonctions publiques
415 /*-----------------------------------------------------------------*/
417 * Ajout d'une lumiere dans la Wks
419 TStatus AddLight(Tint WksID, Tint LightID, tel_light light)
425 printf("AddLight %d dans wks %d [wds_count=%d]\n", LightID, WksID, wks_count);
428 /* obtient le numero de la liste de lights de la wks */
429 wks_entry = find_wks(WksID, 1);
430 if (wks_entry == -1) return TFailure;
432 /* recherche le numero de la lampe si elle existe */
433 light_entry = find_light(wks_entry, LightID, 1);
434 if (light_entry == -1) return TFailure;
436 /* met a jour la light */
437 wks[wks_entry].lights[light_entry].LightID = LightID;
438 wks[wks_entry].lights[light_entry].light = *light;
441 printf("ajout ok\n");
447 /*-----------------------------------------------------------------*/
449 * Maj des lumieres de la Wks
451 TStatus UpdateLight(Tint WksID)
459 printf("UpdateLight %d\n", WksID);
462 /* vire toutes les lights des le depart avant une re-init complete */
463 for (i=GL_LIGHT0; i<=GL_LIGHT7; i++)
465 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, default_amb);
467 /* recherche la liste de light de la wks */
468 wks_entry = find_wks(WksID, 0);
469 if (wks_entry == -1) return TFailure;
472 printf("*** Update: nb = %d\n", wks[wks_entry].lights_count);
477 for (i=0; i<wks[wks_entry].lights_count; i++)
480 printf("binding light %d\n", i);
482 bind_light(&wks[wks_entry].lights[i].light, &gl_lid);
485 if (wks[wks_entry].lights_count > 0) LightOn();
488 printf("update ok\n");
495 /*-----------------------------------------------------------------*/
497 * Remove une lumiere de la Wks
499 TStatus RemoveLight(Tint WksID, Tint LightID)
505 /* recherche de la wks */
506 wks_entry = find_wks(WksID, 0);
507 if (wks_entry == -1) return TFailure;
509 /* recherche de la light */
510 light_entry = find_light(wks_entry, LightID, 0);
511 if (light_entry == -1) return TFailure;
513 /* retire la light */
514 memcpy(&wks[wks_entry].lights[light_entry],
515 &wks[wks_entry].lights[light_entry+1],
516 (wks[wks_entry].lights_count - light_entry - 1)*sizeof(TEL_LIGHT_DATA));
517 wks[wks_entry].lights_count--;
520 printf("RemoveLight %d dans wks %d [wds_count=%d]\n", LightID, WksID, wks_count);
527 /*-----------------------------------------------------------------*/
529 * Remove des lumieres de la Wks
531 TStatus RemoveWksLight(Tint WksID)
535 /* recherche de la wks */
536 wks_entry = find_wks(WksID, 0);
537 if (wks_entry == -1) return TFailure;
539 /* destruction de toute la wks */
540 free(wks[wks_entry].lights);
541 memcpy(&wks[wks_entry],
543 (wks_count - wks_entry - 1)*sizeof(TEL_LIGHT_WKS));
550 /*-----------------------------------------------------------------*/
552 * Reset de toutes les lights d'une Wks
554 TStatus ResetWksLight(Tint WksID)
558 /* recherche de la wks */
559 wks_entry = find_wks(WksID, 0);
560 if (wks_entry == -1) return TFailure;
562 /* destruction de toutes les lights */
563 wks[wks_entry].lights_count = 0;
569 /*-----------------------------------------------------------------*/
577 printf("LightOn(): lighting already enabled!");
579 printf("LightOn() succeeded");
581 glEnable(GL_LIGHTING);
585 /*-----------------------------------------------------------------*/
593 printf("LightOff(): lighting already disabled!");
595 printf("LightOff() succeeded");
597 glDisable(GL_LIGHTING);
599 /*-----------------------------------------------------------------*/
602 * IsEnable des lights
605 GLboolean IsLightOn(void)
607 return glIsEnabled(GL_LIGHTING);
609 /*-----------------------------------------------------------------*/