Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
gl_light.cpp File Reference
#include "gfxlib.h"
#include "gl_light.h"
#include <stack>
#include <assert.h>
#include "vs_globals.h"
#include "config_xml.h"

Go to the source code of this file.

Functions

void GFXPushGlobalEffects ()
 
GFXBOOL GFXPopGlobalEffects ()
 This function undoes the above function to restore the global lights after specialFX phase. More...
 
GFXBOOL GFXSetCutoff (const float ttcutoff)
 Sets the intensity cutoff before picked lights are ignored. More...
 
void GFXSetOptimalIntensity (const float intensity, const float saturate)
 Sets the optimal intensity given that Optimal number of lights is fulfilled. More...
 
GFXBOOL GFXSetOptimalNumLights (const int numLights)
 Sets number of lights graphics can handle optimally. More...
 
GFXBOOL GFXSetSeparateSpecularColor (const GFXBOOL spec)
 Sets the light model to have separate specular color (if available) More...
 
GFXBOOL GFXLightContextAmbient (const GFXColor &amb)
 Sets the ambient light value. More...
 
GFXBOOL GFXGetLightContextAmbient (GFXColor &amb)
 Gets the ambient light value. More...
 
GFXBOOL GFXCreateLight (int &light, const GFXLight &templatecopy, const bool global)
 
void GFXDeleteLight (const int light)
 Deletes a light, removing it from the light table if necessary. More...
 
GFXBOOL GFXSetLight (const int light, const enum LIGHT_TARGET lt, const GFXColor &color)
 Modifies the parameters of the given light. More...
 
GFXBOOL GFXEnableLight (int light)
 activates a light if not already activated More...
 
GFXBOOL GFXDisableLight (int light)
 Deactivates an active light. More...
 
static void SetupGLLightGlobals ()
 
void GFXCreateLightContext (int &con_number)
 creates a light context (relevant to a star system) to add lights to More...
 
void GFXDeleteLightContext (int con_number)
 Deletes a light context (hence a star system) More...
 
void GFXSetLightContext (const int con_number)
 Sets active light context (restores all gllights in such context) More...
 
void GFXDestroyAllLights ()
 

Variables

GLint GFX_MAX_LIGHTS = 8
 
GLint GFX_OPTIMAL_LIGHTS = 4
 
GFXBOOL GFXLIGHTING = GFXFALSE
 
int _currentContext = 0
 The curren tlight context. More...
 
vector< vector< gfx_light > > _local_lights_dat
 The light data _llights points to one of these. More...
 
vector< GFXColor_ambient_light
 The ambient lights that are around. More...
 
vector< gfx_light > * _llights = NULL
 The lights existing in a certain context. Points to _local_lights_dat. More...
 
OpenGLLightsGLLights = NULL
 currently stored GL lights! More...
 
static stack< bool * > GlobalEffects
 
static stack< bool * > GlobalEffectsFreelist
 
static stack< GFXColorGlobalEffectsAmbient
 

Function Documentation

GFXBOOL GFXCreateLight ( int light,
const GFXLight ,
const bool  global 
)

GFXCreateLight gets a light as a parameter. It sets light to have the index of the created light for modification in the future. If the light is global, it will always affect objects in space. if the light is not global, it is local and goes into a 3d hashtable to be picked at a later point on a per object basis.

Definition at line 272 of file gl_light.cpp.

References _llights.

Referenced by Stars::Draw(), BaseInterface::Room::BaseShip::Draw(), Mesh::DrawNow(), SetupCloakState(), and Atmosphere::Update().

273 {
274  for (light = 0; light < static_cast<int>(_llights->size()); light++)
275  if ( (*_llights)[light].Target() == -2 )
276  break;
277  if ( light == static_cast<int>(_llights->size()) )
278  _llights->push_back( gfx_light() );
279  return (*_llights)[light].Create( templatecopy, global );
280 }
void GFXCreateLightContext ( int con_number)

creates a light context (relevant to a star system) to add lights to

Definition at line 317 of file gl_light.cpp.

References _ambient_light, _currentContext, _local_lights_dat, GFXBOOL, GFXFALSE, GFXSetLightContext(), GFXTRUE, and SetupGLLightGlobals().

Referenced by GameStarSystem::GameStarSystem(), GFXInit(), and GameUniverse::StartGFX().

318 {
319  static GFXBOOL LightInit = GFXFALSE;
320  if (!LightInit) {
321  LightInit = GFXTRUE;
323  }
324  con_number = _local_lights_dat.size();
325  _currentContext = con_number;
326  _ambient_light.push_back( GFXColor( 0, 0, 0, 1 ) );
327  _local_lights_dat.push_back( vector< gfx_light > () );
328  GFXSetLightContext( con_number );
329 }
void GFXDeleteLight ( const int  light)

Deletes a light, removing it from the light table if necessary.

Definition at line 282 of file gl_light.cpp.

Referenced by Atmosphere::Draw(), Stars::Draw(), BaseInterface::Room::BaseShip::Draw(), Mesh::DrawNow(), and GamePlanet::Kill().

283 {
284  (*_llights)[light].Kill();
285 }
void GFXDeleteLightContext ( int  con_number)

Deletes a light context (hence a star system)

Definition at line 331 of file gl_light.cpp.

References _local_lights_dat.

332 {
333  _local_lights_dat[con_number] = vector< gfx_light > ();
334 }
void GFXDestroyAllLights ( )

Definition at line 365 of file gl_light.cpp.

References lighttable.

Referenced by GFXShutdown().

366 {
367  lighttable.Clear();
368  if (GLLights)
369  free( GLLights );
370 }
GFXBOOL GFXDisableLight ( int  light)

Deactivates an active light.

Definition at line 306 of file gl_light.cpp.

References _llights, GFXFALSE, and GFXTRUE.

Referenced by GamePlanet::DisableLights(), and Atmosphere::Draw().

307 {
308  assert( light >= 0 && light <= static_cast<int>(_llights->size()) );
309  if ( (*_llights)[light].Target() == -2 )
310  return GFXFALSE;
311  (*_llights)[light].Disable();
312  return GFXTRUE;
313 }
GFXBOOL GFXEnableLight ( int  light)

activates a light if not already activated

Definition at line 296 of file gl_light.cpp.

References _llights, GFXFALSE, and GFXTRUE.

Referenced by GamePlanet::EnableLights(), and Atmosphere::Update().

297 {
298  assert( light >= 0 && light <= static_cast<int>(_llights->size()) );
299  //return FALSE;
300  if ( (*_llights)[light].Target() == -2 )
301  return GFXFALSE;
302  (*_llights)[light].Enable();
303  return GFXTRUE;
304 }
GFXBOOL GFXGetLightContextAmbient ( GFXColor amb)

Gets the ambient light value.

Definition at line 264 of file gl_light.cpp.

References _ambient_light, _currentContext, GFXFALSE, and GFXTRUE.

Referenced by GameStarSystem::Draw(), SphereMesh::ProcessDrawQueue(), CityLights::ProcessDrawQueue(), Terrain::Render(), and Terrain::RenderAll().

265 {
266  if ( _currentContext >= static_cast<int>(_ambient_light.size()) )
267  return GFXFALSE;
269  return GFXTRUE;
270 }
GFXBOOL GFXLightContextAmbient ( const GFXColor amb)

Sets the ambient light value.

Definition at line 251 of file gl_light.cpp.

References _ambient_light, _currentContext, GFXColor::a, GFXColor::b, GFXColor::g, GFXFALSE, GFXTRUE, and GFXColor::r.

Referenced by GameStarSystem::Draw(), GFXPopGlobalEffects(), GFXPushGlobalEffects(), SphereMesh::ProcessDrawQueue(), CityLights::ProcessDrawQueue(), Terrain::Render(), Terrain::RenderAll(), and GameUniverse::StartGFX().

252 {
253  if ( _currentContext >= static_cast<int>(_ambient_light.size()) )
254  return GFXFALSE;
256  //(_ambient_light[_currentContext])[1]=amb.g;
257  //(_ambient_light[_currentContext])[2]=amb.b;
258  //(_ambient_light[_currentContext])[3]=amb.a;
259  float tmp[4] = {amb.r, amb.g, amb.b, amb.a};
260  glLightModelfv( GL_LIGHT_MODEL_AMBIENT, tmp );
261  return GFXTRUE;
262 }
GFXBOOL GFXPopGlobalEffects ( )

This function undoes the above function to restore the global lights after specialFX phase.

Definition at line 67 of file gl_light.cpp.

References GFX_MAX_LIGHTS, GFXLightContextAmbient(), GlobalEffects, GlobalEffectsAmbient, GlobalEffectsFreelist, and i.

Referenced by GameStarSystem::Draw(), Stars::Draw(), and Mesh::ProcessUndrawnMeshes().

68 {
69  if ( GlobalEffects.empty() )
70  return false;
71  //VSFileSystem::Fprintf (stderr,"GES %d",GlobalEffects.size());
72  for (int i = 0; i < GFX_MAX_LIGHTS; i++)
73  if (GlobalEffects.top()[i])
74  glEnable( GL_LIGHT0+i );
75 
76  if (GlobalEffectsFreelist.size() >= 10)
77  delete[] GlobalEffects.top();
78  else
80  GlobalEffects.pop();
81 
84  return true;
85 }
void GFXPushGlobalEffects ( )

In the case of shields and other T&L based effects, the global lights must be disabled yet saved for later use while the selected lights are used shortly then disabled. This saves values of global lights

Definition at line 45 of file gl_light.cpp.

References _ambient_light, _currentContext, GFX_MAX_LIGHTS, GFXLightContextAmbient(), OpenGLL::GL_ENABLED, GlobalEffects, GlobalEffectsAmbient, GlobalEffectsFreelist, i, OpenGLLights::options, and unpicklights().

Referenced by Stars::Draw(), and Mesh::ProcessUndrawnMeshes().

46 {
47  bool *tmp;
48  if (GlobalEffectsFreelist.empty()) {
49  tmp = new bool[GFX_MAX_LIGHTS];
50  } else {
51  tmp = GlobalEffectsFreelist.top();
53  }
54 
55  unpicklights(); //costly but necessary to get rid of pesky local enables that shoudln't be tagged to get reenabled
56  for (int i = 0; i < GFX_MAX_LIGHTS; i++) {
57  tmp[i] = ( 0 != (GLLights[i].options&OpenGLL::GL_ENABLED) );
58  if (GLLights[i].options&OpenGLL::GL_ENABLED)
59  glDisable( GL_LIGHT0+i );
60  }
61  //VSFileSystem::Fprintf (stderr,"PUSH %d",GlobalEffects.size());
62  GlobalEffects.push( tmp );
64  GFXLightContextAmbient( GFXColor( 0, 0, 0, 1 ) );
65 }
GFXBOOL GFXSetCutoff ( const float  ttcutoff)

Sets the intensity cutoff before picked lights are ignored.

Definition at line 214 of file gl_light.cpp.

References GFXFALSE, GFXTRUE, and intensity_cutoff.

Referenced by SetupGLLightGlobals().

215 {
216  if (ttcutoff < 0)
217  return GFXFALSE;
218  intensity_cutoff = ttcutoff;
219  return GFXTRUE;
220 }
GFXBOOL GFXSetLight ( const int  light,
const enum LIGHT_TARGET  lt,
const GFXColor color 
)

Modifies the parameters of the given light.

Definition at line 287 of file gl_light.cpp.

References _llights, GFXFALSE, and GFXTRUE.

Referenced by GamePlanet::Draw().

288 {
289  if ( (*_llights)[light].Target() == -2 )
290  return GFXFALSE;
291  (*_llights)[light].ResetProperties( lt, color );
292 
293  return GFXTRUE;
294 }
void GFXSetLightContext ( const int  con_number)

Sets active light context (restores all gllights in such context)

Definition at line 336 of file gl_light.cpp.

References _ambient_light, _currentContext, _llights, _local_lights_dat, GFX_MAX_LIGHTS, OpenGLL::GLL_OFF, i, OpenGLLights::index, lighttable, OpenGLLights::options, and unpicklights().

Referenced by ClientServerSetLightContext(), GFXCreateLightContext(), GameUniverse::StartGFX(), and GameStarSystem::SwapIn().

337 {
338  unpicklights();
339  int GLLindex = 0;
340  unsigned int i;
341  lighttable.Clear();
342  _currentContext = con_number;
343  _llights = &_local_lights_dat[con_number];
344  glLightModelfv( GL_LIGHT_MODEL_AMBIENT, (GLfloat*) &(_ambient_light[con_number]) );
345  //reset all lights so they arean't in GLLights
346  for (i = 0; i < _llights->size(); i++)
347  (*_llights)[i].Target() = -1;
348  for (i = 0; i < _llights->size() && GLLindex < GFX_MAX_LIGHTS; i++)
349  if ( (*_llights)[i].enabled() ) {
350  if ( (*_llights)[i].LocalLight() ) {
351  (*_llights)[i].AddToTable();
352  } else {
353  GLLights[GLLindex].index = -1; //make it clobber completley! no trace of old light.
354  (*_llights)[i].ClobberGLLight( GLLindex );
355  GLLindex++;
356  }
357  }
358  for (; GLLindex < GFX_MAX_LIGHTS; GLLindex++) {
359  GLLights[GLLindex].index = -1;
360  GLLights[GLLindex].options = OpenGLL::GLL_OFF;
361  glDisable( GL_LIGHT0+GLLindex );
362  }
363 }
void GFXSetOptimalIntensity ( const float  intensity,
const float  saturate 
)

Sets the optimal intensity given that Optimal number of lights is fulfilled.

Definition at line 222 of file gl_light.cpp.

References optintense, optsat, and saturate().

Referenced by SetupGLLightGlobals().

223 {
224  optintense = intensity;
225  optsat = saturate;
226 }
GFXBOOL GFXSetOptimalNumLights ( const int  numLights)

Sets number of lights graphics can handle optimally.

Definition at line 228 of file gl_light.cpp.

References GFX_MAX_LIGHTS, GFX_OPTIMAL_LIGHTS, GFXFALSE, and GFXTRUE.

Referenced by SetupGLLightGlobals().

229 {
230  if (numLights > GFX_MAX_LIGHTS || numLights < 0)
231  return GFXFALSE;
232  GFX_OPTIMAL_LIGHTS = numLights;
233  return GFXTRUE;
234 }
GFXBOOL GFXSetSeparateSpecularColor ( const GFXBOOL  spec)

Sets the light model to have separate specular color (if available)

Definition at line 236 of file gl_light.cpp.

References GFXFALSE, and GFXTRUE.

Referenced by RestoreEnvmapState(), RestoreSpecMapState(), SetupEnvmapPass(), SetupGLLightGlobals(), SetupSpecMapFirstPass(), and SetupSpecMapSecondPass().

237 {
238 #if !defined (_WIN32) && !defined (__CYGWIN__)
239  if (spec) {
240  glLightModeli( GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR );
241  } else {
242  glLightModeli( GL_LIGHT_MODEL_COLOR_CONTROL, GL_SINGLE_COLOR );
243  return GFXFALSE;
244  }
245 #else
246  return GFXFALSE;
247 #endif
248  return GFXTRUE;
249 }
static void SetupGLLightGlobals ( )
static

Definition at line 372 of file gl_light.cpp.

References VegaConfig::getVariable(), GFX_MAX_LIGHTS, GFXFALSE, GFXSetCutoff(), GFXSetOptimalIntensity(), GFXSetOptimalNumLights(), GFXSetSeparateSpecularColor(), GFXTRUE, i, index, XMLSupport::parse_bool(), XMLSupport::parse_float(), XMLSupport::parse_int(), and vs_config.

Referenced by GFXCreateLightContext().

373 {
374  int i;
375  glLightModeli( GL_LIGHT_MODEL_LOCAL_VIEWER, 1 ); //don't want lighting coming from infinity....we have to take the hit due to sphere mapping matrix tweaking
376  //
377  glGetIntegerv( GL_MAX_LIGHTS, &GFX_MAX_LIGHTS );
378  if (!GLLights) {
379  GLLights = (OpenGLLights*) malloc( sizeof (OpenGLLights)*GFX_MAX_LIGHTS );
380  for (i = 0; i < GFX_MAX_LIGHTS; i++)
381  GLLights[i].index = -1;
382  }
383  static float lightcutoff = XMLSupport::parse_float( vs_config->getVariable( "graphics", "lightcutoff", ".06" ) );
384  static float lightoptimalintensity =
385  XMLSupport::parse_float( vs_config->getVariable( "graphics", "lightoptimalintensity", ".06" ) );
386  static float lightsaturation = XMLSupport::parse_float( vs_config->getVariable( "graphics", "lightsaturation", ".95" ) );
387  static int numlights = XMLSupport::parse_int( vs_config->getVariable( "graphics", "numlights", "4" ) );
388  static bool separatespecularcolor =
389  XMLSupport::parse_bool( vs_config->getVariable( "graphics", "separatespecularcolor", "false" ) );
390 
391  GFXSetCutoff( lightcutoff );
392  GFXSetOptimalIntensity( lightoptimalintensity, lightsaturation );
393  GFXSetOptimalNumLights( numlights );
394  GFXSetSeparateSpecularColor( separatespecularcolor ? GFXTRUE : GFXFALSE );
395 }

Variable Documentation

vector< GFXColor > _ambient_light

The ambient lights that are around.

Definition at line 36 of file gl_light.cpp.

Referenced by GFXCreateLightContext(), GFXGetLightContextAmbient(), GFXLightContextAmbient(), GFXPushGlobalEffects(), and GFXSetLightContext().

int _currentContext = 0
vector< gfx_light >* _llights = NULL

The lights existing in a certain context. Points to _local_lights_dat.

Definition at line 37 of file gl_light.cpp.

Referenced by gfx_light::dopickenables(), GFXCreateLight(), GFXDisableLight(), GFXEnableLight(), GFXSetLight(), GFXSetLightContext(), light_rekey_frame(), gfx_light::lightNum(), gfx_light::TrashFromGLLights(), and unpicklights().

vector< vector< gfx_light > > _local_lights_dat

The light data _llights points to one of these.

Definition at line 35 of file gl_light.cpp.

Referenced by GFXCreateLightContext(), GFXDeleteLightContext(), and GFXSetLightContext().

GLint GFX_OPTIMAL_LIGHTS = 4

Definition at line 31 of file gl_light.cpp.

Referenced by GFXSetOptimalNumLights().

GFXBOOL GFXLIGHTING = GFXFALSE

Definition at line 32 of file gl_light.cpp.

Referenced by GFXDisable(), and GFXEnable().

stack< bool* > GlobalEffects
static

Definition at line 41 of file gl_light.cpp.

Referenced by GFXPopGlobalEffects(), and GFXPushGlobalEffects().

stack< GFXColor > GlobalEffectsAmbient
static

Definition at line 43 of file gl_light.cpp.

Referenced by GFXPopGlobalEffects(), and GFXPushGlobalEffects().

stack< bool* > GlobalEffectsFreelist
static

Definition at line 42 of file gl_light.cpp.

Referenced by GFXPopGlobalEffects(), and GFXPushGlobalEffects().