From 26b1401d6cfdfd35afe275e287dc7d205902e55e Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 1 May 2012 20:44:51 +0000 Subject: Added light environment classes --- src/render/light.h | 96 ++++++++++++++++++++++++++++++++++++ src/render/lightenvironment.cc | 108 +++++++++++++++++++++++++++++++++++++++++ src/render/lightenvironment.h | 80 ++++++++++++++++++++++++++++++ 3 files changed, 284 insertions(+) create mode 100644 src/render/light.h create mode 100644 src/render/lightenvironment.cc create mode 100644 src/render/lightenvironment.h (limited to 'src') diff --git a/src/render/light.h b/src/render/light.h new file mode 100644 index 0000000..7e64106 --- /dev/null +++ b/src/render/light.h @@ -0,0 +1,96 @@ +/* + render/light.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_RENDER_LIGHT_H__ +#define __INCLUDED_RENDER_LIGHT_H__ + +#include "math/vector3f.h" +#include "math/color.h" + +namespace render +{ + +/** + * @brief paramaters for a single light source + * */ +class Light +{ +public: + /** + * @brief default constructor, creates a white light at (0,0,0) + * */ + Light(); + + /** + * @brief creates a light source with given color and location + * */ + Light(const math::Vector3f & location, const math::Color & color); + + /** + * @brief destructor + */ + ~Light(); + + /* ---- inspectors ----------------------------------------- */ + + /** + * @brief returns the location for this light source + * */ + inline const math::Vector3f & location() const { + return light_location; + } + + /** + * @brief returns the color for this light source + * */ + inline const math::Color & color() const { + return light_color; + } + + /** + * @brief returns the attenuation settings for this light source + * */ + inline const math::Vector3f & attenuation() const { + return light_attenuation; + } + + /* ---- mutators ------------------------------------------- */ + + /** + * @brief set the location for this light source + * @see location() + * */ + inline void set_location(const math::Vector3f & location) { + light_location.assign(location); + } + + /** + * @brief set the location for this light source + * @see color() + * */ + inline void set_color(const math::Color & color) { + light_color.assign(color); + } + + /** + * @brief set the attenuation settings for this light source + * */ + inline void set_attenuation(const float constant, const float linear, const float quadratic) { + light_attenuation.assign(constant, linear, quadratic); + } + +private: + math::Vector3f light_location; + math::Color light_color; + math::Vector3f light_attenuation; + + +}; + +} + +#endif + diff --git a/src/render/lightenvironment.cc b/src/render/lightenvironment.cc new file mode 100644 index 0000000..8c65848 --- /dev/null +++ b/src/render/lightenvironment.cc @@ -0,0 +1,108 @@ +/* + render/lightenvironment.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "render/lightenvironment.h" +#include "render/gl.h" + +namespace render +{ + +LightEnvironment::LightEnvironment() +{ + env_base_id = GL_LIGHT0; + + env_attenuation = 0.0005f; + env_ambient_intensity = 0.1f; + env_diffuse_intensity = 0.75f; + env_specular_intensity = 0.75f; +} + +LightEnvironment::~LightEnvironment() +{ + clear(); +} + +void LightEnvironment::clear() +{ + for (Container::iterator it = env_container.begin(); it != env_container.end(); ++it) { + Light *light = (*it); + delete light; + (*it) = 0; + } + env_container.clear(); +} + +void LightEnvironment::add(Light *light) +{ + env_container.push_back(light); +} + +void LightEnvironment::enable() +{ + int gl_light_id = env_base_id; + for (Container::iterator it = env_container.begin(); it != env_container.end(); ++it) { + //Light *light = (*it); + gl::enable(gl_light_id); + + gl_light_id++; + } +} + +void LightEnvironment::disable() +{ + int gl_light_id = env_base_id; + for (Container::iterator it = env_container.begin(); it != env_container.end(); ++it) { + //Light *light = (*it); + gl::disable(gl_light_id); + gl_light_id++; + } +} + +void LightEnvironment::draw(const math::Vector3f translate) +{ + GLfloat gl_light_location[4]; + GLfloat gl_ambient_light[4]; + GLfloat gl_diffuse_light[4]; + GLfloat gl_specular_light[4]; + + int gl_light_id = env_base_id; + + for (Container::iterator it = env_container.begin(); it != env_container.end(); ++it) { + Light *light = (*it); + + for (size_t i = 0; i < 3; i++) { + gl_light_location[i] = light->location()[i]; + gl_ambient_light[i] = light->color()[i] * env_ambient_intensity; + gl_diffuse_light[i] = light->color()[i] * env_diffuse_intensity; + gl_specular_light[i] = light->color()[i] * env_specular_intensity; + } + + gl_light_location[3] = 1.0f; + gl_ambient_light[3] = 1.0f; + gl_diffuse_light[3] = 1.0f; + gl_specular_light[3] = 1.0f; + + glLightfv(gl_light_id, GL_POSITION, gl_light_location); + glLightfv(gl_light_id, GL_AMBIENT, gl_ambient_light); + glLightfv(gl_light_id, GL_DIFFUSE, gl_diffuse_light); + glLightfv(gl_light_id, GL_SPECULAR, gl_specular_light); + + // attenuation + glLightf(gl_light_id, GL_CONSTANT_ATTENUATION, light->attenuation()[0]); + glLightf(gl_light_id, GL_LINEAR_ATTENUATION, light->attenuation()[1]); + glLightf(gl_light_id, GL_QUADRATIC_ATTENUATION, light->attenuation()[2]); + + + gl_light_id++; + } +} + +void LightEnvironment::draw() +{ + draw(math::Vector3f()); +} + +} // namespace render diff --git a/src/render/lightenvironment.h b/src/render/lightenvironment.h new file mode 100644 index 0000000..1f323d9 --- /dev/null +++ b/src/render/lightenvironment.h @@ -0,0 +1,80 @@ +/* + render/lightenvironment.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_RENDER_LIGHTENVIRONMENT_H__ +#define __INCLUDED_RENDER_LIGHTENVIRONMENT_H__ + +#include + +#include "render/light.h" + +namespace render +{ + +/** + * @brief a collection of lights + * */ +class LightEnvironment +{ +public: + LightEnvironment(); + + ~LightEnvironment(); + + /** + * @brief enable all lights in the environment + * */ + void enable(); + + /** + * @brief disable all lights in the environment + * */ + void disable(); + + /** + * @brief remove all lights in the environment + * */ + void clear(); + + /** + * @brief add a light to the environment + * */ + void add(Light *light); + + /** + * @brief transfer light environment to OpenGL state + */ + void draw(); + + /** + * @brief transfer light environment to OpenGL state + */ + void draw(const math::Vector3f translate); + + /** + * @brief number of lights in the environment + * */ + inline size_t size() const { + return env_container.size(); + } + +private: + typedef std::vector Container; + + Container env_container; + + int env_base_id; + + float env_attenuation; + float env_ambient_intensity; + float env_diffuse_intensity; + float env_specular_intensity; +}; + +} + +#endif // __INCLUDED_RENDER_LIGHTENVIRONMENT_H__ + -- cgit v1.2.3