From bab6eff9da1927a4fc1e22d97e56199c1b677670 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Fri, 14 Aug 2009 11:37:10 +0000 Subject: model API cleanup, render WORLDSCALE --- src/render/draw.cc | 242 ++++++++++++++++++++++++++--------------------------- 1 file changed, 120 insertions(+), 122 deletions(-) (limited to 'src/render/draw.cc') diff --git a/src/render/draw.cc b/src/render/draw.cc index 4768b7d..a48908f 100644 --- a/src/render/draw.cc +++ b/src/render/draw.cc @@ -58,6 +58,24 @@ void pass_prepare(float seconds) { using namespace model; + // lighting settings for the default light GL_LIGHT0 + GLfloat light_position[] = { 0.0, 0.0, 0.0, 1.0 }; + GLfloat ambient_light[] = { 0.01f, 0.01f, 0.01f, 1.0f }; + GLfloat diffuse_light[] = { 0.2f, 0.2f, 0.2f, 1.0f }; + GLfloat specular_light[] = { 0.2f, 0.2f, 0.2f, 1.0f }; + + for (size_t i=0; i <3; i++) { + light_position[i] = Camera::eye()[i]; + } + + glLightfv(GL_LIGHT0, GL_POSITION, light_position); + glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_light); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_light); + glLightfv(GL_LIGHT0, GL_SPECULAR, specular_light); + + // GL_LIGHT0 is always enabled + gl::enable(GL_LIGHT0); + // reset light state gl::disable(GL_LIGHT1); has_zone_light = false; @@ -206,10 +224,10 @@ void draw_globe(core::EntityGlobe *globe) gl::enable(GL_TEXTURE_2D); } - if (ext_render(globe)->distance() > farplane) { + if (ext_render(globe)->distance() > (FARPLANE - globe->radius())) { // globe is behind the far plane, make a fake size calculation - location = Camera::eye() + (location - Camera::eye()) * (farplane / ext_render(globe)->distance()); - radius *= farplane / ext_render(globe)->distance(); + location = Camera::eye() + (location - Camera::eye()) * (FARPLANE / ext_render(globe)->distance()); + radius *= FARPLANE / ext_render(globe)->distance(); gl::depthmask(GL_FALSE); @@ -287,7 +305,7 @@ void draw_globe(core::EntityGlobe *globe) } - if (ext_render(globe)->distance() > farplane) { + if (ext_render(globe)->distance() > (FARPLANE - globe->radius())) { gl::depthmask(GL_TRUE); @@ -861,7 +879,6 @@ void draw_pass_model_fx(float elapsed) float a = 0.0f; float light_size = 0.0f; bool power = true; - bool draw_quad = true; float thrust = 0.0f; math::Vector3f location; @@ -897,7 +914,6 @@ void draw_pass_model_fx(float elapsed) // draw model lights for (model::Model::Lights::iterator lit = entity->model()->lights().begin(); lit != entity->model()->lights().end(); lit++) { model::Light *light = (*lit); - draw_quad = true; // engine activated lights if (light->engine()) { @@ -908,66 +924,62 @@ void draw_pass_model_fx(float elapsed) } else { thrust = ec->thrust(); if (thrust < 0.001f) { - draw_quad = false; + continue; // next light } } } else { - draw_quad = false; + continue; // next light } } // strobe frequency - if (draw_quad && light->strobe()) { - + if (light->strobe()) { t = (core::application()->time() + ext_render(entity)->fuzz() - light->offset()) * light->frequency(); if ((t - floorf(t)) > light->time()) { - draw_quad = false; + continue; // next light } } - // draw visible lights - if (draw_quad) { - // default alpha is 0.8 - a = 0.8f; - if (light->entity()) { - color.assign(entity->color()); - } else if (light->engine()) { - color.assign(entity->model()->enginecolor()); - a *= thrust; - } else { - color.assign(light->color()); - } - color.a = a; - - location.assign(entity->location() + (entity->axis() * light->location())); - light_size = 0.0625f * light->radius(); - - // track OpenGL state changes - if (current_texture != light->texture()) { - gl::end(); - current_texture = Textures::bind(light->texture()); - gl::begin(gl::Quads); - } - - // draw the quad - gl::color(color); - - glTexCoord2f(0,1); - gl::vertex(location + (Camera::axis().up() - Camera::axis().left()) * light_size); - glTexCoord2f(0,0); - gl::vertex(location + (Camera::axis().up() + Camera::axis().left()) * light_size); - glTexCoord2f(1,0); - gl::vertex(location + (Camera::axis().up() * -1 + Camera::axis().left()) * light_size); - glTexCoord2f(1,1); - gl::vertex(location + (Camera::axis().up() * -1 - Camera::axis().left()) * light_size); - Stats::quads++; + // default alpha is 0.8 + a = 0.8f; + if (light->entity()) { + color.assign(entity->color()); + } else if (light->engine()) { + color.assign(entity->model()->enginecolor()); + a *= thrust; + } else { + color.assign(light->color()); } + color.a = a; + + location.assign(entity->location() + (entity->axis() * light->location())); + light_size = 0.0625f * light->radius(); + + // track OpenGL state changes + if (current_texture != light->texture()) { + gl::end(); + current_texture = Textures::bind(light->texture()); + gl::begin(gl::Quads); + } + + // draw the quad + gl::color(color); + + glTexCoord2f(0,1); + gl::vertex(location + (Camera::axis().up() - Camera::axis().left()) * light_size); + glTexCoord2f(0,0); + gl::vertex(location + (Camera::axis().up() + Camera::axis().left()) * light_size); + glTexCoord2f(1,0); + gl::vertex(location + (Camera::axis().up() * -1 + Camera::axis().left()) * light_size); + glTexCoord2f(1,1); + gl::vertex(location + (Camera::axis().up() * -1 - Camera::axis().left()) * light_size); + Stats::quads++; + } // draw flares for (model::Model::Flares::iterator flit = entity->model()->flares().begin(); flit != entity->model()->flares().end(); flit++) { model::Flare *flare = (*flit); - draw_quad = true; // engine activated flares if (flare->engine()) { @@ -978,95 +990,87 @@ void draw_pass_model_fx(float elapsed) } else { thrust = ec->thrust(); if (thrust < 0.001f) { - draw_quad = false; + continue; // next flare } } } else { - draw_quad = false; + continue; // next flare } } // strobe frequency - if (draw_quad && flare->strobe()) { - + if (flare->strobe()) { t = (core::application()->time() + ext_render(entity)->fuzz() - flare->offset()) * flare->frequency(); if ((t - floorf(t)) > flare->time()) { - draw_quad = false; + continue; // next flare } } // calulcate viewing angle factor - if (draw_quad) { - flare_axis.assign(entity->axis() * flare->axis()); - a = math::absf(dotproduct(flare_axis.forward(), Camera::axis().forward())); - - if (a < 0.01f) { - draw_quad = false; - } + flare_axis.assign(entity->axis() * flare->axis()); + a = math::absf(dotproduct(flare_axis.forward(), Camera::axis().forward())); + if (a < 0.01f) { + continue; // next flare } - // draw visible flares - if (draw_quad) { + // alpha decreases with viewing angle + a *= 0.8f; + if (flare->entity()) { + color.assign(entity->color()); + } else if (flare->engine()) { + color.assign(entity->model()->enginecolor()); + a *= thrust; + } else { + color.assign(flare->color()); + } + color.a = a; - // alpha decreases with viewing angle - a *= 0.8f; - if (flare->entity()) { - color.assign(entity->color()); - } else if (flare->engine()) { - color.assign(entity->model()->enginecolor()); - a *= thrust; - } else { - color.assign(flare->color()); - } - color.a = a; + location.assign(entity->location() + (entity->axis() * flare->location())); + light_size = 0.0625f * flare->radius(); - location.assign(entity->location() + (entity->axis() * flare->location())); - light_size = 0.0625f * flare->radius(); + // track OpenGL state changes + if ((current_cull != flare->cull()) || (current_texture != flare->texture())) { + gl::end(); - // track OpenGL state changes - if ((current_cull != flare->cull()) || (current_texture != flare->texture())) { - gl::end(); + if (current_texture != flare->texture()) { + current_texture = Textures::bind(flare->texture()); + } - if (current_texture != flare->texture()) { - current_texture = Textures::bind(flare->texture()); - } + if (current_cull != flare->cull()) { + if (flare->cull() == model::CullNone) { + gl::disable(GL_CULL_FACE); + current_cull = model::CullNone; + } else { + if (current_cull == model::CullNone) { + gl::enable(GL_CULL_FACE); + } - if (current_cull != flare->cull()) { - if (flare->cull() == model::CullNone) { - gl::disable(GL_CULL_FACE); - current_cull = model::CullNone; + if (flare->cull() == model::CullBack) { + gl::cullface(GL_BACK); + current_cull = model::CullBack; } else { - if (current_cull == model::CullNone) { - gl::enable(GL_CULL_FACE); - } - - if (flare->cull() == model::CullBack) { - gl::cullface(GL_BACK); - current_cull = model::CullBack; - } else { - gl::cullface(GL_FRONT); - current_cull = model::CullFront; - } + gl::cullface(GL_FRONT); + current_cull = model::CullFront; } } - - gl::begin(gl::Quads); } - // draw the quad - gl::color(color); - - glTexCoord2f(0,1); - gl::vertex(location + (flare_axis.up() + flare_axis.left()) * light_size); - glTexCoord2f(0,0); - gl::vertex(location + (flare_axis.up() - flare_axis.left()) * light_size); - glTexCoord2f(1,0); - gl::vertex(location + (flare_axis.up() * -1 - flare_axis.left()) * light_size); - glTexCoord2f(1,1); - gl::vertex(location + (flare_axis.up() * -1 + flare_axis.left()) * light_size); - - Stats::quads++; + gl::begin(gl::Quads); } + + // draw the quad + gl::color(color); + + glTexCoord2f(0,1); + gl::vertex(location + (flare_axis.up() + flare_axis.left()) * light_size); + glTexCoord2f(0,0); + gl::vertex(location + (flare_axis.up() - flare_axis.left()) * light_size); + glTexCoord2f(1,0); + gl::vertex(location + (flare_axis.up() * -1 - flare_axis.left()) * light_size); + glTexCoord2f(1,1); + gl::vertex(location + (flare_axis.up() * -1 + flare_axis.left()) * light_size); + + Stats::quads++; } gl::end(); @@ -1207,24 +1211,18 @@ void draw(float seconds) gl::enable(GL_CULL_FACE); // enable culling gl::enable(GL_COLOR_MATERIAL); // enable color tracking - gl::enable(GL_LIGHTING); - //gl::enable(GL_RESCALE_NORMAL); // rescale normals by the transformation matrix scale factor - gl::enable(GL_NORMALIZE); - + gl::enable(GL_LIGHTING); // enable lighting + gl::enable(GL_NORMALIZE); // enable rescaling of normals + draw_pass_globes(); // draw globes draw_pass_default(); // draw entities without model - - gl::disable(GL_NORMALIZE); - //gl::disable(GL_RESCALE_NORMAL); - -// glEnableClientState(GL_COLOR_ARRAY); draw_pass_model_fragments(); -// glDisableClientState(GL_COLOR_ARRAY); - gl::disable(GL_LIGHTING); + gl::disable(GL_NORMALIZE); // disable resaling of normals + gl::disable(GL_LIGHTING); // disable lighting gl::enable(GL_BLEND); gl::depthmask(GL_FALSE); // disable depth buffer writing -- cgit v1.2.3