From 980b47266c5fba9a7e7fabfa7a6d77167a47cb60 Mon Sep 17 00:00:00 2001 From: Evan Goers Date: Wed, 21 Mar 2012 11:57:11 +0000 Subject: Functionize light creation. --- src/render/draw.cc | 84 +++++++++++++++++++++++++++++++++-------------------- src/render/draw.h | 3 ++ src/ui/modelview.cc | 48 ++++++------------------------ 3 files changed, 64 insertions(+), 71 deletions(-) (limited to 'src') diff --git a/src/render/draw.cc b/src/render/draw.cc index 2efa411..3b7acad 100644 --- a/src/render/draw.cc +++ b/src/render/draw.cc @@ -46,14 +46,15 @@ math::Vector3f v6(-1, 1, -1); math::Vector3f v7(-1, -1, -1); core::Zone *zone = 0; -float zone_light[4]; // locaton of the zone light +float zone_light[] = { 0.0f, 0.0f, 0.0f, 1.0f }; // location of the zone light math::Color zone_color; // color of the zone light bool has_zone_light = false; bool draw_particles = true; bool draw_lights = true; +size_t max_lights = 8; -GLenum zone_gllight = GL_LIGHT0 + 6; //FIXME super magic number bros +GLenum zone_gllight = GL_LIGHT0; typedef std::map Globes; Globes globes_list; @@ -63,10 +64,56 @@ Globes globes_list; void pass_reset_lights() { // reset light state - for (size_t i = 0; i < 8; i++) + for (size_t i = 0; i < max_lights; i++) gl::disable(GL_LIGHT0 + i); } +// Lights +int create_light(float* location, float attenuation, math::Color color) +{ + int gllight; + + // check if a light is available + for (size_t i = 0; i < max_lights; i++) { + if (!glIsEnabled(GL_LIGHT0 + i)) { + gllight = GL_LIGHT0 + i; + break; + } + else { + gllight = GL_LIGHT0; + return gllight; + } + } + + // set up color and location + GLfloat gllight_location[4]; + GLfloat diffuse_light[4]; + GLfloat ambient_light[4]; + GLfloat specular_light[4]; + + for (size_t i = 0; i < 3; i++) { + gllight_location[i] = location[i]; + ambient_light[i] = color[i] * 0.1; + diffuse_light[i] = color[i] * 0.75; + specular_light[i] = color[i] * 0.75; + } + gllight_location[3] = 1.0f; + ambient_light[3] = 1.0f; + diffuse_light[3] = 1.0f; + specular_light[3] = 1.0f; + + // set up the light + // we use a simple averaging of the color times the specified attenuation value(such as 0.00025f). + // FIXME: use a more standardized and realistic way of doing this. + glLightf(gllight, GL_LINEAR_ATTENUATION, ((color[0] + color[1] + color[2]) / 3.0f) * attenuation); + + glLightfv(gllight, GL_POSITION, gllight_location); + glLightfv(gllight, GL_AMBIENT, ambient_light); + glLightfv(gllight, GL_DIFFUSE, diffuse_light); + glLightfv(gllight, GL_SPECULAR, specular_light); + return gllight; +} + void pass_prepare(float seconds) { using namespace model; @@ -116,41 +163,14 @@ void pass_prepare(float seconds) } // add level lights + if (globe->flag_is_set(core::Entity::Bright)) { - for (size_t i = 0; i < 8; 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]; - GLfloat ambient_light[4]; - GLfloat specular_light[4]; - for (size_t i = 0; i < 3; i++) { zone_light[i] = globe->location()[i]; zone_color[i] = globe->color()[i]; - ambient_light[i] = globe->color()[i] * 0.1; - diffuse_light[i] = globe->color()[i] * 0.75; - specular_light[i] = globe->color()[i] * 0.75; } - zone_light[3] = 1.0f; - ambient_light[3] = 1.0f; - diffuse_light[3] = 1.0f; - specular_light[3] = 1.0f; - - 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); - + GLenum zone_gllight = create_light(zone_light, 0.00025f, globe->color()); gl::enable(zone_gllight); - has_zone_light = true; } diff --git a/src/render/draw.h b/src/render/draw.h index bc02bf5..e35c7c5 100644 --- a/src/render/draw.h +++ b/src/render/draw.h @@ -26,6 +26,9 @@ void draw_target(const core::Entity *entity); /// reset void reset(); +/// create light +int create_light(float* location, float attenuation, math::Color color); + /// draw a sphere void draw_sphere(math::Color const & color, float radius); diff --git a/src/ui/modelview.cc b/src/ui/modelview.cc index 033e4e3..e3a3c79 100755 --- a/src/ui/modelview.cc +++ b/src/ui/modelview.cc @@ -19,7 +19,7 @@ namespace ui { -GLenum modelview_gllight = GL_LIGHT0 + 7; +GLenum modelview_gllight = GL_LIGHT0; ModelView::ModelView(Widget *parent) : Widget(parent) { @@ -204,25 +204,10 @@ void ModelView::draw_globe() glEnableClientState(GL_NORMAL_ARRAY); // we set up the light in camera space - for (size_t i = 0; i < 8; i++) { - // check if a light is available - if (!glIsEnabled(GL_LIGHT0 + i)) { - modelview_gllight = GL_LIGHT0 + i; - break; - } - } - - GLfloat modelview_light[] = { -10.0f * reference_radius, 0, 0, 1.0f }; - GLfloat ambient_light[] = { 0.1f, 0.1f, 0.1f, 1.0f }; - GLfloat diffuse_light[] = { 0.75f, 0.75f, 0.75f, 1.0f }; - GLfloat specular_light[] = { 0.75f, 0.75f, 0.75f, 1.0f }; - - glLightfv(modelview_gllight, GL_POSITION, modelview_light); - glLightfv(modelview_gllight, GL_AMBIENT, ambient_light); - glLightfv(modelview_gllight, GL_DIFFUSE, diffuse_light); - glLightfv(modelview_gllight, GL_SPECULAR, specular_light); - - glLightf(modelview_gllight, GL_LINEAR_ATTENUATION, 0.05f); + float modelview_light_location[] = {-10.0f * reference_radius, 0, 0}; + math::Color modelview_light_color; + modelview_light_color.assign(1.0); + modelview_gllight = render::create_light(modelview_light_location, 0.05f, modelview_light_color); gl::enable(modelview_gllight); @@ -319,25 +304,10 @@ void ModelView::draw_model() glEnableClientState(GL_NORMAL_ARRAY); // we set up the light in camera space - for (size_t i = 0; i < 8; i++) { - // check if a light is available - if (!glIsEnabled(GL_LIGHT0 + i)) { - modelview_gllight = GL_LIGHT0 + i; - break; - } - } - - GLfloat modelview_light[] = { -10.0f * model->radius(), 0, 0, 1.0f }; - GLfloat ambient_light[] = { 0.1f, 0.1f, 0.1f, 1.0f }; - GLfloat diffuse_light[] = { 0.75f, 0.75f, 0.75f, 1.0f }; - GLfloat specular_light[] = { 0.75f, 0.75f, 0.75f, 1.0f }; - - glLightfv(modelview_gllight, GL_POSITION, modelview_light); - glLightfv(modelview_gllight, GL_AMBIENT, ambient_light); - glLightfv(modelview_gllight, GL_DIFFUSE, diffuse_light); - glLightfv(modelview_gllight, GL_SPECULAR, specular_light); - - glLightf(modelview_gllight, GL_LINEAR_ATTENUATION, 0.05f); + float modelview_light_location[] = {-10.0f * reference_radius, 0, 0}; + math::Color modelview_light_color; + modelview_light_color.assign(1.0); + modelview_gllight = render::create_light(modelview_light_location, 0.05f, modelview_light_color); gl::enable(modelview_gllight); -- cgit v1.2.3