From 656c8b2fc03164d13352c067780037e87b98b9f2 Mon Sep 17 00:00:00 2001
From: Evan Goers <mega@osirion.org>
Date: Tue, 24 Jan 2012 08:13:28 +0000
Subject: Renderer now checks and enables lights based on availability instead
 of using arbitrary GL_LIGHT#s.

---
 src/render/draw.cc | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

(limited to 'src/render')

diff --git a/src/render/draw.cc b/src/render/draw.cc
index ec4f386..5e3a3de 100644
--- a/src/render/draw.cc
+++ b/src/render/draw.cc
@@ -53,6 +53,8 @@ bool has_zone_light = false;
 bool draw_particles = true;
 bool draw_lights = true;
 
+GLenum zone_gllight = GL_LIGHT0 + 6; //FIXME super magic number bros
+
 typedef std::map<float, core::EntityGlobe *> Globes;
 Globes globes_list;
 
@@ -61,7 +63,8 @@ Globes globes_list;
 void pass_reset_lights()
 {
 	// reset light state
-	gl::disable(GL_LIGHT0);
+	for (size_t i = 0; i < (GL_LIGHT0 + 7); i++)
+		gl::disable(GL_LIGHT0 + i);
 }
 
 void pass_prepare(float seconds)
@@ -114,6 +117,13 @@ void pass_prepare(float seconds)
 
 			// add level lights
 			if (globe->flag_is_set(core::Entity::Bright)) {
+				for (size_t i = 0; i < (GL_LIGHT0 + 7); i++) {
+					// check if a light is available
+					if (!glIsEnabled(GL_LIGHT0 + i)) {
+						zone_gllight = GL_LIGHT0 + i;
+						break;
+					}
+				}
 
 				// bright globes set level light
 				GLfloat diffuse_light[4];
@@ -132,14 +142,15 @@ void pass_prepare(float seconds)
 				diffuse_light[3] = 1.0f;
 				specular_light[3] = 1.0f;
 
-				glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, ((globe->color()[0] + globe->color()[1] + globe->color()[2]) / 3.0f) * 0.00025f);
+				glLightf(zone_gllight, GL_LINEAR_ATTENUATION, ((globe->color()[0] + globe->color()[1] + globe->color()[2]) / 3.0f) * 0.00025f);
+
+				glLightfv(zone_gllight, GL_POSITION, zone_light);
+				glLightfv(zone_gllight, GL_AMBIENT, ambient_light);
+				glLightfv(zone_gllight, GL_DIFFUSE, diffuse_light);
+				glLightfv(zone_gllight, GL_SPECULAR, specular_light);
 
-				glLightfv(GL_LIGHT0, GL_POSITION, zone_light);
-				glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_light);
-				glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_light);
-				glLightfv(GL_LIGHT0, GL_SPECULAR, specular_light);
+				gl::enable(zone_gllight);
 
-				gl::enable(GL_LIGHT0);
 				has_zone_light = true;
 			}
 
@@ -280,7 +291,7 @@ void draw_globe(const core::EntityGlobe* globe)
 				fake_light[i] = zone_light[i] + location[i] - globe->location()[i];
 			}
 			fake_light[3] = 1.0f;
-			glLightfv(GL_LIGHT0, GL_POSITION, fake_light);
+			glLightfv(zone_gllight, GL_POSITION, fake_light);
 		}
 	}
 
@@ -346,7 +357,7 @@ void draw_globe(const core::EntityGlobe* globe)
 
 		if (has_zone_light) {
 			// restore zone light
-			glLightfv(GL_LIGHT0, GL_POSITION, zone_light);
+			glLightfv(zone_gllight, GL_POSITION, zone_light);
 		}
 	}
 
-- 
cgit v1.2.3