diff options
author | Stijn Buys <ingar@osirion.org> | 2008-03-09 18:40:31 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2008-03-09 18:40:31 +0000 |
commit | 517d35b2bbaeb3ee4b7e29301cd41bb58628bf3e (patch) | |
tree | 5473c128e64029b35ecfe22c9e48628c02af5407 /src | |
parent | 289bf4b622b95b794e438ac257d75ea437e3e023 (diff) |
parse of light entities in models
Diffstat (limited to 'src')
-rw-r--r-- | src/client/camera.cc | 57 | ||||
-rw-r--r-- | src/client/view.cc | 2 | ||||
-rw-r--r-- | src/core/model.cc | 48 | ||||
-rw-r--r-- | src/core/model.h | 22 | ||||
-rw-r--r-- | src/math/color.cc | 12 | ||||
-rw-r--r-- | src/math/color.h | 5 | ||||
-rw-r--r-- | src/math/vector3f.cc | 1 | ||||
-rw-r--r-- | src/math/vector3f.h | 4 | ||||
-rw-r--r-- | src/render/draw.cc | 45 | ||||
-rw-r--r-- | src/render/draw.h | 2 |
10 files changed, 163 insertions, 35 deletions
diff --git a/src/client/camera.cc b/src/client/camera.cc index dd49aa6..b9fa1b5 100644 --- a/src/client/camera.cc +++ b/src/client/camera.cc @@ -167,21 +167,27 @@ void draw(float elapsed) gl::rotate(90.0f, 0, 1.0, 0); gl::rotate(-90.0f, 1.0f , 0, 0); + float x,y,z; + // map camera coordinates to opengl coordinates switch (mode) { case Free: // calculate the position of the eye - eye.x = -distance-1; - eye.y = 0; - eye.z = distance/2; + x = -distance; + y = 0; + z = distance/2; + + eye.x = x * cos(-pitch_current / 180.0f * M_PI) - z * sin(-pitch_current / 180.0f * M_PI); + eye.y = y; + eye.z = x * sin(-pitch_current / 180.0f * M_PI) + z * cos(-pitch_current / 180.0f * M_PI); - eye.x = eye.z * sin(-pitch_current * M_1_PI * 0.5f) + eye.x * cos(-pitch_current * M_1_PI * 0.5f); - eye.y = eye.y; - eye.z = eye.z * sin(-pitch_current * M_1_PI * 0.5f) - eye.x * cos(-pitch_current * M_1_PI * 0.5f); + x = eye.x; + y = eye.y; + z = eye.z; - eye.x = eye.x * cos(-yaw_current * M_1_PI * 0.5f) - eye.y * sin(-yaw_current * M_1_PI * 0.5f); - eye.y = eye.x * sin(-yaw_current * M_1_PI * 0.5f) + eye.y * cos(-yaw_current * M_1_PI * 0.5f); - eye.z = eye.z; + eye.x = x * cos(yaw_current / 180.0f * M_PI) - y * sin(yaw_current / 180.0f * M_PI); + eye.y = x * sin(yaw_current / 180.0f * M_PI) + y * cos(yaw_current / 180.0f * M_PI); + eye.z = z; eye = eye + target; @@ -194,17 +200,21 @@ void draw(float elapsed) case Track: // calculate the position of the eye - eye.x = -distance-1; - eye.y = 0; - eye.z = distance; + x = -distance; + y = 0; + z = distance; + + eye.x = z * sin(pitch_current / 180.0f * M_PI) + x * cos(pitch_current / 180.0f * M_PI); + eye.y = y; + eye.z = z * sin(pitch_current / 180.0f * M_PI) - x * cos(pitch_current / 180.0f * M_PI); - eye.x = eye.z * sin(-pitch_current * M_1_PI * 0.5f) + eye.x * cos(-pitch_current * M_1_PI * 0.5f); - eye.y = eye.y; - eye.z = eye.z * sin(-pitch_current * M_1_PI * 0.5f) - eye.x * cos(-pitch_current * M_1_PI * 0.5f); + x = eye.x; + y = eye.y; + z = eye.z; - eye.x = eye.x * cos(-yaw_current * M_1_PI * 0.5f) - eye.y * sin(-yaw_current * M_1_PI * 0.5f); - eye.y = eye.x * sin(-yaw_current * M_1_PI * 0.5f) + eye.y * cos(-yaw_current * M_1_PI * 0.5f); - eye.z = eye.z; + eye.x = x * cos(yaw_current / 180.0f * M_PI) - y * sin(yaw_current / 180.0f * M_PI); + eye.y = x * sin(yaw_current / 180.0f * M_PI) + y * cos(yaw_current / 180.0f * M_PI); + eye.z = z; eye = eye + target; @@ -216,8 +226,9 @@ void draw(float elapsed) break; case Overview: + eye = target; - eye.z = eye.z + distance; + eye.z = eye.z + distance-1; gl::rotate(-75.0f, 0.0f, 1.0f, 0.0f); gl::translate(0.0f, 0.0f, -distance); @@ -250,8 +261,8 @@ void key_left() void key_up() { if (mode == Free) { - pitch_target = pitch_target - rotate_offset_inc; - if (pitch_target < -90.0f) pitch_target = -90.0f; + pitch_target = pitch_target + rotate_offset_inc; + if (pitch_target > 90.0f) pitch_target = 90.0f; } else if (mode == Overview) { x_offset += translate_offset_inc; } @@ -260,8 +271,8 @@ void key_up() void key_down() { if (mode == Free) { - pitch_target = pitch_target + rotate_offset_inc; - if (pitch_target > 90.0f) pitch_target = 90.0f; + pitch_target = pitch_target - rotate_offset_inc; + if (pitch_target < -90.0f) pitch_target = -90.0f; } else if (mode == Overview) { x_offset -= translate_offset_inc; } diff --git a/src/client/view.cc b/src/client/view.cc index 8da7892..db7f3a3 100644 --- a/src/client/view.cc +++ b/src/client/view.cc @@ -210,7 +210,7 @@ void frame(float seconds) camera::draw(seconds); // draw the current camera transformation - render::draw(camera::target, seconds); // draw the world + render::draw(camera::eye, camera::target, seconds); // draw the world } diff --git a/src/core/model.cc b/src/core/model.cc index 52f0dde..da179e5 100644 --- a/src/core/model.cc +++ b/src/core/model.cc @@ -21,6 +21,16 @@ namespace core const float MAX_BOUNDS = 8192; const float delta = 10e-10; +/* ---------- core::Light ------------------------------------------ */ + +Light::Light(math::Vector3f const & location, math::Color const & color) : + light_location(location), + light_color(color) +{} + +Light::~Light() +{} + /* ---------- core::Engine ------------------------------------------ */ Engine::Engine(math::Vector3f const & location) : @@ -102,6 +112,7 @@ Model::Model(std::string const & name) : std::string class_name; math::Vector3f class_origin; float class_angle; + math::Color class_color; while (ifs) { ifs.getline(data, 1023); @@ -113,6 +124,12 @@ Model::Model(std::string const & name) : //cout << " COMMENT!" << std::endl; continue; } else if (firstword == "{") { + if (!level) { + class_angle = 0; + class_name.clear(); + class_origin = math::Vector3f(0,0,0); + class_color = math::Color(1, 1, 1); + } level ++; //cout << " LEVEL +" << level << std::endl; } else if (firstword == "}") { @@ -135,6 +152,8 @@ Model::Model(std::string const & name) : } else if ((level == 1) && (class_name == "target_engine")) { //con_debug << " engine at " << class_origin << "\n"; add_engine(new Engine(class_origin * model_scale)); + } else if ((level == 1) && (class_name == "light")) { + add_light(new Light(class_origin * model_scale, class_color)); } if (level == 1) { @@ -170,7 +189,18 @@ Model::Model(std::string const & name) : is >> class_origin.y; is >> class_origin.z; //con_debug << " origin '" << class_origin << "'" << std::endl; - + + } else if (firstword == "\"_color\"") { + std::string tmp; + char c; + while ((linestream.get(c)) && (c != '"')); + while ((linestream.get(c)) && (c != '"')) + tmp += c; + std::istringstream is(tmp); + is >> class_color.r; + is >> class_color.g; + is >> class_color.b; + } else if (firstword == "\"angle\"") { std::string tmp; char c; @@ -180,6 +210,7 @@ Model::Model(std::string const & name) : std::istringstream is(tmp); is >> class_angle; //con_debug << " angle '" << class_angle << "'" << std::endl; + } else if (firstword == "(") { if ((level == 2) && (class_name == "worldspawn")) { //cout << " BRUSH PLANE" << std::endl; @@ -234,6 +265,12 @@ Model::~Model() delete(*eit); } model_engine.clear(); + + // delete all lights + for (std::list<Light *>::iterator lit = model_light.begin(); lit != model_light.end(); lit++) { + delete (*lit); + } + model_light.clear(); } void Model::make_face(math::Plane3f *face, std::vector<math::Plane3f *> & planes) @@ -500,6 +537,11 @@ void Model::add_engine(Engine *engine) model_engine.push_back(engine); } +void Model::add_light(Light *light) +{ + model_light.push_back(light); +} + Model *Model::find(std::string const & name) { std::map<std::string, Model*>::iterator it = registry.find(name); @@ -531,7 +573,9 @@ void Model::clear() void Model::list() { for (std::map<std::string, Model*>::iterator mit = registry.begin(); mit != registry.end(); mit++) { - con_print << " " << (*mit).second->model_name << " " << (*mit).second->model_face.size() << " polys\n"; + con_print << " " << (*mit).second->model_name << " " << (*mit).second->model_face.size() << " polys " + << (*mit).second->model_engine.size() << " engines " + << (*mit).second->model_light.size() << " lights\n"; } con_print << registry.size() << " registered models" << std::endl; } diff --git a/src/core/model.h b/src/core/model.h index b273c46..013edcc 100644 --- a/src/core/model.h +++ b/src/core/model.h @@ -58,6 +58,23 @@ private: math::Vector3f engine_location; }; +/// an exterior light +class Light +{ +public: + Light(math::Vector3f const & location, math::Color const & color); + ~Light(); + + inline math::Vector3f const & location() const { return light_location; } + + inline math::Color const & color() const { return light_color; }; + +private: + math::Vector3f light_location; + math::Color light_color; +}; + + /// a 3D model contains a list of faces class Model { @@ -94,17 +111,20 @@ public: /// list of Engines std::list<Engine *> model_engine; + + /// list of Lights + std::list<Light *> model_light; private: void make_face(math::Plane3f *face, std::vector<math::Plane3f *> & planes); void add_engine(Engine *engine); void add_face(Face *face); + void add_light(Light *light); std::string model_name; float model_scale; bool model_valid; - }; } diff --git a/src/math/color.cc b/src/math/color.cc index 05f3b4c..ecdfd2f 100644 --- a/src/math/color.cc +++ b/src/math/color.cc @@ -10,13 +10,15 @@ namespace math { -Color::Color() +Color::Color() : + r(_r), g(_g), b(_b), a(_a) { _r = _g = _b = 0.0f; _a = 1.0f; } -Color::Color(const float red, const float green , const float blue , const float alpha) +Color::Color(const float red, const float green , const float blue , const float alpha) : + r(_r), g(_g), b(_b), a(_a) { _r = red; _g = green; @@ -24,13 +26,15 @@ Color::Color(const float red, const float green , const float blue , const float _a = alpha; } -Color::Color(const float grey, const float alpha) +Color::Color(const float grey, const float alpha) : + r(_r), g(_g), b(_b), a(_a) { _r = _g = _b = grey; _a = alpha; } -Color::Color(const Color &other) +Color::Color(const Color &other) : + r(_r), g(_g), b(_b), a(_a) { this->operator=(other); } diff --git a/src/math/color.h b/src/math/color.h index 078faaa..51b415f 100644 --- a/src/math/color.h +++ b/src/math/color.h @@ -37,6 +37,11 @@ public: static const Color Green() { return Color(0.0f,1.0f,0.0f); }; static const Color Blue() { return Color(0.0f, 0.0f, 1.0f); }; static const Color Yellow() { return Color(1.0f, 1.0f, 0.0f); }; + + float &r; + float &g; + float &b; + float &a; private: void normalize(); diff --git a/src/math/vector3f.cc b/src/math/vector3f.cc index 371ada1..3f1f4ac 100644 --- a/src/math/vector3f.cc +++ b/src/math/vector3f.cc @@ -170,4 +170,5 @@ float dotproduct(const Vector3f& first, const Vector3f& second) r += first[i] * second[i]; return (r); } + } // namespace math diff --git a/src/math/vector3f.h b/src/math/vector3f.h index c2ebc6a..5d1fa41 100644 --- a/src/math/vector3f.h +++ b/src/math/vector3f.h @@ -145,9 +145,9 @@ Vector3f operator*(float scalar, const Vector3f& vector); /// vector cross product const Vector3f crossproduct(Vector3f const & first, Vector3f const & second); -/// vetor dot product +/// vector dot product float dotproduct(const Vector3f& first, const Vector3f& second); -} // namespace Math +} // namespace math #endif // __INCLUDED_MATH_VECTOR3F_H__ diff --git a/src/render/draw.cc b/src/render/draw.cc index 3fed4dd..1d27631 100644 --- a/src/render/draw.cc +++ b/src/render/draw.cc @@ -63,6 +63,33 @@ void draw_model_engines(core::Model *model, core::EntityControlable *entity) } +void draw_model_lights(math::Vector3f const & eye, math::Vector3f const & target) +{ + glPointSize(10); + gl::begin(gl::Points); + + std::map<unsigned int, core::Entity *>::iterator it; + for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { + core::Model *model = 0; + if ((*it).second->modelname().size()) + model = core::Model::get((*it).second->modelname()); + + if (model) { + for (std::list<core::Light *>::iterator lit = model->model_light.begin(); lit != model->model_light.end(); lit++) { + math::Vector3f location = (*it).second->location() + (*lit)->location(); + math::Vector3f d = location-eye; + if ((d.lengthsquared() < 16*16) && (dotproduct(d, eye-target) < 0)) { + gl::color((*lit)->color()); + gl::vertex(location); + } + } + } + } + + gl::end(); + glPointSize(1); +} + void draw_entity_sphere(core::Entity *entity) { sphere.sphere_color = entity->color(); @@ -266,7 +293,7 @@ void draw_spacegrid(math::Vector3f const &target) gl::end(); } -void draw(math::Vector3f const &target, float seconds) +void draw(math::Vector3f const &eye, math::Vector3f const &target, float seconds) { // used for animations angle += 180.0f * seconds; @@ -306,6 +333,22 @@ void draw(math::Vector3f const &target, float seconds) gl::enable(GL_BLEND); // enable alpha blending again + /* + // DEBUG target lines + gl::push(); + gl::color(0,1, 0, .7); + //gl::translate(target*-1); + gl::begin(gl::Lines); + for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { + gl::vertex(eye); + gl::vertex((*it).second->location()); + } + gl::end(); + gl::pop(); + + draw_model_lights(eye, target); // second pass - draw lights + */ + draw_spacegrid(target); // draw the blue spacegrid gl::disable(GL_DEPTH_TEST); // disable depth buffer writing diff --git a/src/render/draw.h b/src/render/draw.h index c1273aa..2590be2 100644 --- a/src/render/draw.h +++ b/src/render/draw.h @@ -13,7 +13,7 @@ namespace render { /// draw the world -void draw(math::Vector3f const &target, float seconds); +void draw(math::Vector3f const &eye, math::Vector3f const &target, float seconds); } |