From 4f6b27b58bfae9ce860a005edf890d8f1136a85f Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Wed, 5 Mar 2008 18:21:39 +0000 Subject: OpenGL lighting --- src/client/draw.cc | 38 ++++++++++++++++++++++++++++------ src/client/view.cc | 59 ++++++++++++++++++++++++++++++++--------------------- src/game/game.cc | 10 +++++++-- src/render/face.cc | 1 + src/render/gl.cc | 14 +++++++++---- src/render/gl.h | 11 ++++++---- src/render/model.cc | 17 +++++++-------- 7 files changed, 101 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/client/draw.cc b/src/client/draw.cc index 1ea315b..87be92c 100644 --- a/src/client/draw.cc +++ b/src/client/draw.cc @@ -139,17 +139,22 @@ void draw_ship(core::EntityControlable *entity) // draw an entity of entity_type core::Entity::Default void draw_entity_default(core::Entity *entity) { - render::Model *model = 0; + using namespace render; + + Model *model = 0; if (entity->modelname().size()) - model = render::Model::get(entity->modelname()); + model = Model::get(entity->modelname()); - render::gl::push(); - render::gl::translate(entity->location()); - render::gl::rotate(entity->direction(), 0.0f, 0.0f, 1.0f ); + gl::push(); + gl::translate(entity->location()); + gl::rotate(entity->direction(), 0.0f, 0.0f, 1.0f ); if (model) { model->draw(entity, camera::eye); } else { + gl::disable(GL_LIGHTING); + gl::disable(GL_LIGHT0); + switch(entity->shape()) { case core::Entity::Sphere: draw_entity_sphere(entity); @@ -165,9 +170,12 @@ void draw_entity_default(core::Entity *entity) draw_entity_cube(entity); break; } + + gl::enable(GL_LIGHTING); + gl::enable(GL_LIGHT0); // disable camera light } - render::gl::pop(); + gl::pop(); } // draw an entity of entity_type core::Entity::Controlable @@ -205,6 +213,8 @@ void draw_spacegrid() float dy = camera::target.y - floorf(camera::target.y); color(0,0, 1.0f); + normal(0, 0, 1.0f); + begin(Lines); for (int i=-gridsize; i <= gridsize; i++) { color(0,0, 0, 0); @@ -235,6 +245,14 @@ void draw_world(float seconds) // draw entities using namespace render; + + gl::enable(GL_DEPTH_TEST); // enable depth buffer writing + gl::enable(GL_CULL_FACE); // enable culling + gl::enable(GL_COLOR_MATERIAL); // enable color tracking + gl::enable(GL_LIGHTING); + gl::enable(GL_LIGHT0); // enable camera light + //gl::enable(GL_NORMALIZE); + std::map::iterator it; for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { switch ((*it).second->type()) { @@ -249,8 +267,16 @@ void draw_world(float seconds) } } + gl::disable(GL_CULL_FACE); // disable culling + gl::disable(GL_COLOR_MATERIAL); // disable color tracking + gl::disable(GL_LIGHTING); + gl::disable(GL_LIGHT0); // disable camera light + //gl::disable(GL_NORMALIZE); + // draw the background grid draw_spacegrid(); + + gl::disable(GL_DEPTH_TEST); // disable depth buffer writing } } diff --git a/src/client/view.cc b/src/client/view.cc index 8ddd33c..1863d00 100644 --- a/src/client/view.cc +++ b/src/client/view.cc @@ -4,6 +4,13 @@ the terms and conditions of the GNU General Public License version 2 */ +#include + +#include +#include +#include +#include + #include "client/client.h" #include "client/camera.h" #include "client/chat.h" @@ -15,11 +22,6 @@ #include "math/mathlib.h" #include "sys/sys.h" -#include -#include -#include -#include - namespace client { @@ -45,21 +47,42 @@ void reset() // set clear color gl::clearcolor(0.0f, 0.0f, 0.0f, 1.0f); - // shading model: Gouraud (smooth). + // shading model: Gouraud (smooth, the default) gl::shademodel(GL_SMOOTH); + //gl::shademodel(GL_FLAT); + + // lighting + GLfloat ambient_light[] = { 0.2f, 0.2f, 0.2f, 1.0f }; + GLfloat diffuse_light[] = { 0.6f, 0.6f, 0.6f, 1.0f }; + GLfloat specular_light[] = { 0.2f, 0.2f, 0.2f, 1.0f }; + GLfloat specular_reflectance[] = { 1.0f, 1.0f, 1.0f, 1.0f }; + + glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_light); + glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_light); + glLightfv(GL_LIGHT0, GL_SPECULAR, specular_light); + + // color tracking + glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); + + glMaterialfv(GL_FRONT, GL_SPECULAR, specular_reflectance); + glMateriali(GL_FRONT, GL_SHININESS, 16); // shininess 1-128 + gl::disable(GL_LIGHTING); + gl::disable(GL_LIGHT0); + gl::disable(GL_COLOR_MATERIAL); + // culling gl::cullface(GL_BACK); gl::frontface(GL_CCW); - - // alpha-blending - gl::blendfunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - gl::disable(GL_BLEND); + gl::disable(GL_CULL_FACE); // depth gl::depthmask(GL_TRUE); - gl::disable(GL_DEPTH_TEST); - gl::disable(GL_CULL_FACE); + gl::disable(GL_DEPTH_TEST); + + // alpha-blending + gl::blendfunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + gl::enable(GL_BLEND); } void draw_loader() @@ -169,6 +192,7 @@ void frame(float seconds) gl::matrixmode(GL_PROJECTION); gl::loadidentity(); + // FIXME width must always be one const float frustumsize = 0.5f; gl::frustum(-frustumsize*video::aspect, frustumsize*video::aspect, -frustumsize, frustumsize, 1.0f, 1024.0f); @@ -177,14 +201,7 @@ void frame(float seconds) camera::draw(seconds); // draw the current camera transformation - gl::enable(GL_DEPTH_TEST); // enable depth buffer writing - gl::enable(GL_CULL_FACE); // enable culling - gl::enable(GL_BLEND); // enable alpha blending - draw_world(seconds); // draw the world - - gl::disable(GL_CULL_FACE); // disable culling - gl::disable(GL_DEPTH_TEST); // disable depth buffer writing } // switch to ortographic projection to draw the GUI @@ -198,7 +215,6 @@ void frame(float seconds) if (!core::application()->connected()) { // draw the loader bitmap draw_loader(); - gl::enable(GL_BLEND); // enable alpha blending } // draw the console @@ -207,9 +223,6 @@ void frame(float seconds) // draw the status line draw_status(); - - gl::disable(GL_BLEND); - } } //namespace view diff --git a/src/game/game.cc b/src/game/game.cc index da8ef2e..77e534f 100644 --- a/src/game/game.cc +++ b/src/game/game.cc @@ -120,7 +120,7 @@ void Game::init() cube->entity_modelname = "cube"; cube->entity_moduletypeid = cube_enttype; - // Thornaider + // Canasta cube = new core::Entity(core::Entity::Solid & core::Entity::Static); cube->entity_shape = core::Entity::Diamond; cube->entity_color = Color(0.5f, 1.0f, 5.0f); @@ -132,10 +132,16 @@ void Game::init() cube = new core::Entity(core::Entity::Solid & core::Entity::Static); cube->entity_shape = core::Entity::Diamond; cube->entity_color = Color(0.5f, 1.0f, 5.0f); - cube->entity_location = Vector3f(18.0f, 22.0f, 0.0f); + cube->entity_location = Vector3f(17.0f, 21.0f, 0.0f); cube->entity_name = "wreck: Micron Vector"; cube->entity_modelname = "ships/micron_vector"; + // Alexandria + core::Entity *alexandria = new core::Entity(core::Entity::Solid & core::Entity::Static); + alexandria->entity_location = Vector3f(0.0f, -64.0f, 0.0f); + alexandria->entity_name = "station: Alexandria"; + alexandria->entity_modelname = "stations/alexandria"; + // the yellow sphere core::Entity *sphere = new core::Entity(core::Entity::Solid & core::Entity::Static); sphere->entity_shape = core::Entity::Sphere; diff --git a/src/render/face.cc b/src/render/face.cc index 764c452..31b7fcc 100644 --- a/src/render/face.cc +++ b/src/render/face.cc @@ -43,6 +43,7 @@ void Face::draw() { //gl::begin(gl::LineLoop); gl::begin(gl::Polygon); + gl::normal(face_normal); // face_normal already has unit lenght for (std::vector::iterator it = face_vertex.begin(); it != face_vertex.end(); it++) { gl::vertex(*(*it)); } diff --git a/src/render/gl.cc b/src/render/gl.cc index 6eb91b1..682bfc8 100644 --- a/src/render/gl.cc +++ b/src/render/gl.cc @@ -4,12 +4,10 @@ the terms of the GNU General Public License version 2 */ -// project includes -#include "render/gl.h" - -// system includes #include "GL/gl.h" +#include "render/gl.h" + using math::Vector3f; using math::Color; @@ -130,6 +128,14 @@ void vertex(const float x, const float y, const float z) { glVertex3f(x, y, z); } +void normal(const Vector3f & vector) { + glNormal3fv(vector.coord); +} + +void normal(const float x, const float y, const float z) { + glNormal3f(x, y, z); +} + void push() { glPushMatrix(); } diff --git a/src/render/gl.h b/src/render/gl.h index 0513410..d5d6418 100644 --- a/src/render/gl.h +++ b/src/render/gl.h @@ -7,13 +7,11 @@ #ifndef __INCLUDED_RENDER_GL_H__ #define __INCLUDED_RENDER_GL_H__ -// project headers +#include + #include "math/vector3f.h" #include "math/color.h" -// OpenGL headers -#include - namespace render { /// wrapper namespace for OpenGL operations @@ -99,6 +97,11 @@ namespace gl void vertex(const math::Vector3f& vector); void vertex(const float x, const float y, const float z); + + /// glNormal + void normal(const math::Vector3f & vector); + + void normal(const float x, const float y, const float z); /// multiply the current matrix by a general rotation matrix /** @param angle The angle of the rotation, in degrees [0-360] diff --git a/src/render/model.cc b/src/render/model.cc index 70b5f18..848bc49 100644 --- a/src/render/model.cc +++ b/src/render/model.cc @@ -414,9 +414,7 @@ void Model::make_face(math::Plane3f *face, std::vector & planes color = 0; } - Vector3f n = face->normal(); - n.normalize(); - Face *mf = new Face(n, color); + Face *mf = new Face(face->normal()*-1, color); if (color) delete color; for (std::vector::iterator it = vl.begin(); it != vl.end(); it++) { @@ -459,16 +457,16 @@ void Model::draw(core::Entity const * entity, math::Vector3f const & eye) for (std::list::iterator fit = model_face.begin(); fit != model_face.end(); fit++) { // poor man's lighting // set the face color depending on the viewing direction - float d = fabsf(math::dotproduct(n, (*fit)->normal())); + //float d = fabsf(math::dotproduct(n, (*fit)->normal())); - if (d > 1) - d = 1; - d = 0.5f + d/2; + //if (d > 1) + // d = 1; + //d = 0.5f + d/2; if ((*fit)->color()) { - render::gl::color(*(*fit)->color() * d); + render::gl::color(*(*fit)->color()); } else { - render::gl::color(entity->color() * d); + render::gl::color(entity->color()); } (*fit)->draw(); } @@ -482,7 +480,6 @@ void Model::draw(core::EntityControlable const * entity, math::Vector3f const & // draw engines // all engines are assumed to point to the rear if (model_engine.size() && entity->thrust()) { - gl::color(1.0f,0 ,0); gl::begin(gl::Lines); -- cgit v1.2.3