diff options
author | Stijn Buys <ingar@osirion.org> | 2009-08-14 11:37:10 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2009-08-14 11:37:10 +0000 |
commit | bab6eff9da1927a4fc1e22d97e56199c1b677670 (patch) | |
tree | 14a06a6abd5b74c8b368b992594cb2f3133ab553 /src/render | |
parent | 69d54b593f96c0f7e525c0dff560ca0614071cf3 (diff) |
model API cleanup, render WORLDSCALE
Diffstat (limited to 'src/render')
-rw-r--r-- | src/render/camera.cc | 64 | ||||
-rw-r--r-- | src/render/camera.h | 15 | ||||
-rw-r--r-- | src/render/draw.cc | 242 | ||||
-rw-r--r-- | src/render/render.h | 4 | ||||
-rw-r--r-- | src/render/renderext.cc | 22 | ||||
-rw-r--r-- | src/render/state.cc | 14 |
6 files changed, 166 insertions, 195 deletions
diff --git a/src/render/camera.cc b/src/render/camera.cc index 092cc26..ba82ea6 100644 --- a/src/render/camera.cc +++ b/src/render/camera.cc @@ -28,9 +28,6 @@ const float pitch_free = -30.0f; const float pitch_track = -5.0f; const float pitch_overview = -5.0f; -float Camera::camera_frustum_size = 0.5f; -float Camera::camera_frustum_front = 1.0f; - math::Vector3f Camera::camera_eye; math::Vector3f Camera::camera_target; math::Axis Camera::camera_axis; @@ -52,9 +49,6 @@ float Camera::camera_zoom; void Camera::init() { - camera_frustum_size = 0.5f; - camera_frustum_front = 1.0f; - direction_current = 0; direction_target = 0; @@ -92,9 +86,6 @@ void Camera::set_mode(Mode newmode) { target_pitch = 0.0f; distance = 0.4f; - camera_frustum_size = 0.5f; - camera_frustum_front = 1.0f; - camera_axis.clear(); if (camera_mode != Overview) @@ -296,15 +287,17 @@ void Camera::frame(float seconds) if (angle > MIN_DELTA) camera_axis.rotate(n, -angle); } -/* + if (core::localcontrol()->model()) { - camera_target -= (core::localcontrol()->model()->maxbbox().x + core::localcontrol()->model()->radius() + 0.1f) * 0.5f * camera_axis.forward(); - camera_target += (core::localcontrol()->model()->maxbbox().z + core::localcontrol()->model()->radius() + 0.1f) * 0.5f * camera_axis.up(); + camera_target -= camera_axis.forward() * math::max(FRUSTUMFRONT / WORLDSCALE, core::localcontrol()->model()->maxbbox().x); + camera_target += camera_axis.up() * math::max(FRUSTUMFRONT / WORLDSCALE, core::localcontrol()->model()->maxbbox().z); + } else { + camera_target -= camera_axis.forward() * math::max (FRUSTUMFRONT / WORLDSCALE, FRUSTUMFRONT / WORLDSCALE + core::localcontrol()->radius()); + camera_target += camera_axis.up() * math::max (FRUSTUMFRONT / WORLDSCALE, FRUSTUMFRONT / WORLDSCALE + core::localcontrol()->radius()); } -*/ - float f = ( camera_frustum_size/State::aspect() ); - camera_target += camera_axis.up() * math::max(f, core::localcontrol()->radius()); - distance = math::max(f, core::localcontrol()->radius()) + camera_zoom * core::localcontrol()->radius(); + + distance = math::max (FRUSTUMFRONT / WORLDSCALE, FRUSTUMFRONT / WORLDSCALE + camera_zoom * core::localcontrol()->radius()) + 0.001f; + } else if (mode() == Free) { camera_axis.assign(target_axis); @@ -321,33 +314,24 @@ void Camera::frame(float seconds) d = degrees180f(pitch_current - pitch_target); pitch_current = degrees360f(pitch_current - d * seconds); camera_axis.change_pitch(pitch_current); - /* - // set distance and location - distance = camera_zoom * core::localcontrol()->radius(); - //distance += (2.0f*core::localcontrol()->radius()) / ( camera_frustum_size/State::aspect() ); - - camera_target -= COS_PI_4 * (core::localcontrol()->model() ? core::localcontrol()->model()->maxbbox().x : core::localcontrol()->radius()) * camera_axis.forward(); - camera_target += COS_PI_4 * (core::localcontrol()->model() ? core::localcontrol()->model()->maxbbox().z : core::localcontrol()->radius()) * camera_axis.up(); camera_target += camera_frustum_size/State::aspect() * target_axis.up(); - */ - float f = ( camera_frustum_size/State::aspect() ); - distance = f + camera_zoom * core::localcontrol()->radius(); + + distance = math::max (FRUSTUMFRONT / WORLDSCALE, FRUSTUMFRONT / WORLDSCALE + camera_zoom * core::localcontrol()->radius()) + 0.001f; + } else if (mode() == Cockpit) { camera_axis.assign(target_axis); if (core::localcontrol()->model()) { - camera_target += (core::localcontrol()->model()->maxbbox().x+0.05) * + camera_target += (core::localcontrol()->model()->maxbbox().x) * core::localcontrol()->axis().forward(); } else { - camera_target += (core::localcontrol()->radius() + 0.05) * + camera_target += (core::localcontrol()->radius()) * core::localcontrol()->axis().forward(); } - distance = 0.0f; + distance = (FRUSTUMFRONT / WORLDSCALE) - 0.001f; } } - distance += camera_frustum_front; - // calculate eye position camera_eye = camera_target - (distance * camera_axis.forward()); } @@ -358,7 +342,7 @@ void Camera::frustum() gl::matrixmode(GL_PROJECTION); gl::loadidentity(); - gl::frustum(-camera_frustum_size, camera_frustum_size, -camera_frustum_size/State::aspect(), camera_frustum_size/State::aspect(), camera_frustum_front, 1023.0f); + gl::frustum(-FRUSTUMSIZE, FRUSTUMSIZE, -FRUSTUMSIZE/State::aspect(), FRUSTUMSIZE/State::aspect(), FRUSTUMFRONT, core::range::maxdistance * WORLDSCALE); gl::matrixmode(GL_MODELVIEW); gl::loadidentity(); @@ -371,7 +355,10 @@ void Camera::frustum() matrix.assign(camera_axis); gl::multmatrix(matrix.transpose()); + gl::scale(4.0f, 4.0f, 4.0f); + gl::translate(-1.0f * camera_eye); + } void Camera::frustum_default(float distance, float cx, float cy) @@ -380,10 +367,11 @@ void Camera::frustum_default(float distance, float cx, float cy) gl::matrixmode(GL_PROJECTION); gl::loadidentity(); + // move eye to (cx, cy) // note: the factor 2.0f probably has to be 1.0f/frustum_size gl::translate(2.0f*(-State::width() * 0.5f + cx)/State::width() , 2.0f*(State::height() * 0.5f - cy)/State::height(), 0.0f); - gl::frustum(-camera_frustum_size, camera_frustum_size, -camera_frustum_size/State::aspect(), camera_frustum_size/State::aspect(), camera_frustum_front, 1023.0f); + gl::frustum(-FRUSTUMSIZE, FRUSTUMSIZE, -FRUSTUMSIZE/State::aspect(), FRUSTUMSIZE/State::aspect(), FRUSTUMFRONT, 1023.0f); gl::matrixmode(GL_MODELVIEW); gl::loadidentity(); @@ -426,14 +414,4 @@ void Camera::reset() set_mode(camera_mode); } -float Camera::frustum_front() -{ - return camera_frustum_front; -} - -float Camera::frustum_size() -{ - return camera_frustum_size; -} - } diff --git a/src/render/camera.h b/src/render/camera.h index 349aef8..49cba74 100644 --- a/src/render/camera.h +++ b/src/render/camera.h @@ -8,9 +8,15 @@ #define __INCLUDED_RENDER_CAMERA_H__ #include "math/mathlib.h" +#include "core/range.h" namespace render { +const float WORLDSCALE = 4.0f; +const float FARPLANE = core::range::maxdistance; +const float FRUSTUMSIZE = 0.5f; +const float FRUSTUMFRONT = 1.0f; + /// camera functions class Camera { @@ -76,20 +82,13 @@ public: /// set specified camera mode static void set_mode(Mode newmode); - /// current frustum front - static float frustum_front(); - - /// current frustum size (height); - static float frustum_size(); - private: static math::Vector3f camera_eye; static math::Vector3f camera_target; static math::Axis camera_axis; static Mode camera_mode; static Mode camera_previous_mode; - static float camera_frustum_size; - static float camera_frustum_front; + // current and target yaw angle in XZ plane, positive is looking left static float direction_current; 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 diff --git a/src/render/render.h b/src/render/render.h index a453464..3d07796 100644 --- a/src/render/render.h +++ b/src/render/render.h @@ -56,10 +56,6 @@ namespace render { extern core::Cvar *r_mipmap; inline RenderExt *ext_render(core::Entity *entity) { return static_cast<RenderExt *>(entity->extension((size_t)core::Extension::Render)); } - - const float drawdistance = 256.0f; - const float drawfxdistance = 64.0f; - const float farplane = 1016.0f; } diff --git a/src/render/renderext.cc b/src/render/renderext.cc index 6a59870..b0462d4 100644 --- a/src/render/renderext.cc +++ b/src/render/renderext.cc @@ -107,20 +107,34 @@ void RenderExt::frame(float elapsed) if ((entity()->type() == core::Entity::Controlable)) { if (static_cast<core::EntityDynamic *>(entity())->state() == core::Entity::Docked) { state_visible = false; + return; } } if (state_visible && entity()->model()) { - float r = entity()->model()->radius(); - math::clamp(r, 1.0f, farplane / drawfxdistance); - if (distance() < drawfxdistance * r) { + + if (distance() < core::range::fxdistance) { // entity within detail range state_visible = true; state_detailvisible = true; - } else if ((distance() < drawdistance * r) && (distance() < core::range::max)) { + + } else if (distance() < core::range::maxvisible) { // entity within drawing distance, outside detail range state_visible = true; state_detailvisible = false; + + } else if (distance() < core::range::maxdistance) { + + if ((entity()->type() == core::Entity::Controlable)) { + // controlable entity out of range + state_visible = false; + state_detailvisible = false; + } else { + // entity within drawing distance, outside detail range + state_visible = true; + state_detailvisible = false; + + } } else { // entity out of range state_visible = false; diff --git a/src/render/state.cc b/src/render/state.cc index 2bf50af..96c7c0a 100644 --- a/src/render/state.cc +++ b/src/render/state.cc @@ -79,20 +79,6 @@ void State::clear() gl::shademodel(GL_SMOOTH); //gl::shademodel(GL_FLAT); - // 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 }; - - 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); - // color tracking glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); |