diff options
author | Stijn Buys <ingar@osirion.org> | 2009-08-12 20:22:20 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2009-08-12 20:22:20 +0000 |
commit | ea18c25aef4aa0104852f314dd76e4ce446673f3 (patch) | |
tree | 928d7ecef63acda7b32d5243ef5cf14760e555c9 | |
parent | 83b66522dfcad1b2428ce4fd81d83e39a051a4c5 (diff) |
triangle/quad code cleanup,corrects submodel origin, enable quad rendering again
-rw-r--r-- | src/model/asefile.cc | 3 | ||||
-rw-r--r-- | src/model/mapfile.cc | 58 | ||||
-rw-r--r-- | src/model/model.cc | 16 | ||||
-rw-r--r-- | src/model/model.h | 55 | ||||
-rw-r--r-- | src/model/primitives.cc | 10 | ||||
-rw-r--r-- | src/model/primitives.h | 7 | ||||
-rw-r--r-- | src/model/quad.cc | 7 | ||||
-rw-r--r-- | src/model/quad.h | 97 | ||||
-rw-r--r-- | src/model/triangle.cc | 31 | ||||
-rw-r--r-- | src/model/triangle.h | 56 | ||||
-rw-r--r-- | src/render/draw.cc | 2 |
11 files changed, 190 insertions, 152 deletions
diff --git a/src/model/asefile.cc b/src/model/asefile.cc index 67be531..edbd848 100644 --- a/src/model/asefile.cc +++ b/src/model/asefile.cc @@ -450,7 +450,8 @@ Model * ASEFile::load(const std::string &name) // caculate bounding box model->model_minbbox = (asefile.ase_minbbox - center) * scale; model->model_maxbbox = (asefile.ase_maxbbox - center) * scale; - model->model_radius = model->model_maxbbox.length(); + model->set_radius(model->model_maxbbox.length()); + model->set_origin(center * scale); // load the model faces into the fragment for (FaceList::iterator it = asefile.ase_facelist.begin(); it != asefile.ase_facelist.end(); it++) { diff --git a/src/model/mapfile.cc b/src/model/mapfile.cc index 681a3be..385f986 100644 --- a/src/model/mapfile.cc +++ b/src/model/mapfile.cc @@ -582,7 +582,10 @@ void MapFile::make_brushface(Face *face) } } -/* + // the actual polygon normal is on the other side + Vector3f face_normal(face->normal()*-1); + face_normal.normalize(); + #ifndef HAVE_BULLET // Quads are disable to use model data for bullet physics @@ -597,17 +600,22 @@ void MapFile::make_brushface(Face *face) ++vn2; ++vn2; - Vector3f n(face->normal()*-1); - n.normalize(); - - primitives->add_quad(*(*vn2), *(*vn1), *(*vn), *(*v0), n, face->detail()); + Quad *quad = new Quad(*(*vn2) * SCALE, *(*vn1) * SCALE, *(*vn) * SCALE, *(*v0) * SCALE, face_normal, face->detail()); + primitives->add_quad(quad); + + if (face->material()->flags() & Material::Texture) { + quad->t0().assign(map_texture_coords(face, *(*vn2))); + quad->t1().assign(map_texture_coords(face, *(*vn1))); + quad->t2().assign(map_texture_coords(face, *(*vn))); + quad->t3().assign(map_texture_coords(face, *(*v0))); + } + delete(*vn); delete(*vn1); vl.pop_back(); vl.pop_back(); } #endif -*/ // split polygon into triangles while (vl.size() > 2) { std::vector<Vector3f *>::iterator v0 = vl.begin(); @@ -615,21 +623,14 @@ void MapFile::make_brushface(Face *face) std::vector<Vector3f *>::reverse_iterator vn1 = vl.rbegin(); ++vn1; - Vector3f n(face->normal()*-1); - n.normalize(); - - Triangle *triangle = primitives->add_triangle(*(*vn1) * SCALE, *(*vn) * SCALE, *(*v0) * SCALE, n, face->detail()); + Triangle * triangle = new Triangle (*(*vn1) * SCALE, *(*vn) * SCALE, *(*v0) * SCALE, face_normal, face->detail()); + primitives->add_triangle(triangle); if (face->material()->flags() & Material::Texture) { triangle->t0().assign(map_texture_coords(face, *(*vn1))); triangle->t1().assign(map_texture_coords(face, *(*vn))); triangle->t2().assign(map_texture_coords(face, *(*v0))); } - /* - triangle->t0().assign(0, 0); - triangle->t1().assign(0, 1); - triangle->t2().assign(1, 1); - */ delete(*vn); vl.pop_back(); } @@ -760,13 +761,12 @@ void MapFile::load_worldspawn(Model *model) return; // FIXME center in maps without brushes - map_center = (class_minbbox + class_maxbbox) / 2.0f; - + map_center = (class_minbbox + class_maxbbox) * 0.5f; + model->model_minbbox = class_minbbox - map_center; model->model_maxbbox = class_maxbbox - map_center; - - model->model_radius = model->model_maxbbox.length(); - + model->set_radius(model->model_maxbbox.length()); + model->set_origin(map_center); load_fragmentgroup(model, FragmentGroup::None); } @@ -850,10 +850,10 @@ void MapFile::load_fragmentgroup(Model *model, const FragmentGroup::Type class_t Quad *quad = (*quad_it); if (!quad->detail()) { size_t count = 0; - count += fragment->add_vertex(quad->v0()-group_center, quad->normal(), false); - count += fragment->add_vertex(quad->v1()-group_center, quad->normal(), false); - count += fragment->add_vertex(quad->v2()-group_center, quad->normal(), false); - count += fragment->add_vertex(quad->v3()-group_center, quad->normal(), false); + count += fragment->add_vertex(quad->v0()-group_center, quad->normal(), quad->t0(), false); + count += fragment->add_vertex(quad->v1()-group_center, quad->normal(), quad->t1(), false); + count += fragment->add_vertex(quad->v2()-group_center, quad->normal(), quad->t2(), false); + count += fragment->add_vertex(quad->v3()-group_center, quad->normal(), quad->t3(), false); if (count == 4) model->model_quad_count++; } @@ -864,10 +864,10 @@ void MapFile::load_fragmentgroup(Model *model, const FragmentGroup::Type class_t Quad *quad = (*quad_it); if (quad->detail()) { size_t count = 0; - count += fragment->add_vertex(quad->v0()-group_center, quad->normal(), false); - count += fragment->add_vertex(quad->v1()-group_center, quad->normal(), false); - count += fragment->add_vertex(quad->v2()-group_center, quad->normal(), false); - count += fragment->add_vertex(quad->v3()-group_center, quad->normal(), false); + count += fragment->add_vertex(quad->v0()-group_center, quad->normal(), quad->t0(), false); + count += fragment->add_vertex(quad->v1()-group_center, quad->normal(), quad->t1(), false); + count += fragment->add_vertex(quad->v2()-group_center, quad->normal(), quad->t2(), false); + count += fragment->add_vertex(quad->v3()-group_center, quad->normal(), quad->t3(), false); if (count == 4) { model->model_quad_count++; model->model_quad_detail_count++; @@ -1302,7 +1302,7 @@ Model * MapFile::load(std::string const &name) groupdst->set_type(groupsrc->type()); groupdst->set_scale(groupsrc->scale() * submodel->scale()); groupdst->set_speed(groupsrc->speed()); - groupdst->set_location(groupsrc->location() + submodel->location() - mapfile.map_center); + groupdst->set_location((submodel->location() - mapfile.map_center) + (submodel_model->origin() + groupsrc->location()) * submodel->scale() ); groupdst->set_axis(groupsrc->axis() * submodel->axis()); // copy fragments diff --git a/src/model/model.cc b/src/model/model.cc index 1f9fcb5..3f0b00b 100644 --- a/src/model/model.cc +++ b/src/model/model.cc @@ -16,7 +16,7 @@ namespace model // static model registry Model::Registry Model::model_registry; -Model::Model(std::string const & name) : +Model::Model(const std::string & name) : model_enginecolor(1.0f, 0.0f, 0.0f), model_name(name) @@ -64,6 +64,16 @@ Model::~Model() model_flares.clear(); } +void Model::set_radius(const float radius) +{ + model_radius = radius; +} + +void Model::set_origin(const math::Vector3f &origin) +{ + model_origin.assign(origin); +} + void Model::add_particles(Particles *particles) { model_particles.push_back(particles); @@ -89,7 +99,7 @@ void Model::add_dock(Dock *dock) model_docks.push_back(dock); } -Model *Model::find(std::string const & name) +Model *Model::find(const std::string & name) { Registry::iterator it = model_registry.find(name); if (it == model_registry.end()) @@ -98,7 +108,7 @@ Model *Model::find(std::string const & name) return (*it).second; } -Model *Model::load(std::string const & name) +Model *Model::load(const std::string & name) { Model *model = find(name); if (!model) { diff --git a/src/model/model.h b/src/model/model.h index 2ca2734..af3b1dc 100644 --- a/src/model/model.h +++ b/src/model/model.h @@ -52,13 +52,13 @@ public: typedef std::list<FragmentGroup *> Groups; /// create a model with a name - Model(std::string const & name); + Model(const std::string & name); /// delete the model, and all fragments, lights, etc ~Model(); /// the name of the model - inline std::string const & name() const + inline const std::string & name() const { return model_name; } @@ -100,13 +100,13 @@ public: } /// maximum values of the bounding box - inline math::Vector3f const & maxbbox() const + inline const math::Vector3f & maxbbox() const { return model_maxbbox; } /// minimum values of the bounding box - inline math::Vector3f const & minbbox() const + inline const math::Vector3f & minbbox() const { return model_minbbox; } @@ -124,7 +124,16 @@ public: } /// engine color for this model - inline math::Color const & enginecolor() const { return model_enginecolor; } + inline const math::Color & enginecolor() const + { + return model_enginecolor; + } + + /// original origin + inline const math::Vector3f & origin() const + { + return model_origin; + } /// add a light to the model void add_light(Light *light); @@ -140,8 +149,10 @@ public: /// add a fragment group to the model void add_group(FragmentGroup *group); - - float model_radius; + + void set_radius(const float radius); + + void set_origin(const math::Vector3f &origin); math::Vector3f model_maxbbox; math::Vector3f model_minbbox; @@ -151,6 +162,15 @@ public: math::Color model_enginecolor; + /// total number of triangles + size_t model_tris_count; + /// number of detail triangles + size_t model_tris_detail_count; + /// total number of quads + size_t model_quad_count; + /// number of detail quads + size_t model_quad_detail_count; + /* ---- static functions for the Model registry -------------------- */ /// the model registry @@ -160,10 +180,10 @@ public: } /// get name model, returns 0 if not found - static Model *find(std::string const & name); + static Model *find(const std::string & name); /// get named model from the registry and load it if necessary - static Model *load(std::string const & name); + static Model *load(const std::string & name); /// clear the model registry static void clear(); @@ -173,19 +193,9 @@ public: /// list one model static void list_model(Model *model); - - /// total number of triangles - size_t model_tris_count; - /// number of detail triangles - size_t model_tris_detail_count; - /// total number of quads - size_t model_quad_count; - /// number of detail quads - size_t model_quad_detail_count; + private: - static Registry model_registry; - std::string model_name; Docks model_docks; @@ -193,6 +203,11 @@ private: Lights model_lights; ParticleSystems model_particles; Groups model_groups; + + math::Vector3f model_origin; + float model_radius; + + static Registry model_registry; }; } diff --git a/src/model/primitives.cc b/src/model/primitives.cc index 8eec0f4..f21a6d5 100644 --- a/src/model/primitives.cc +++ b/src/model/primitives.cc @@ -27,20 +27,14 @@ Primitives::~Primitives() primitives_quads.clear(); } -Triangle *Primitives::add_triangle(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2, - math::Vector3f const &normal, bool detail) +void Primitives::add_triangle(Triangle *triangle) { - Triangle *triangle = new Triangle(v0, v1, v2, normal, detail); primitives_triangles.push_back(triangle); - return triangle; } -Quad *Primitives::add_quad(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2, math::Vector3f const &v3, - math::Vector3f const &normal, bool detail) +void Primitives::add_quad(Quad *quad) { - Quad *quad = new Quad(v0, v1, v2, v3, normal, detail); primitives_quads.push_back(quad); - return quad; } } diff --git a/src/model/primitives.h b/src/model/primitives.h index 116d01b..84c3f9b 100644 --- a/src/model/primitives.h +++ b/src/model/primitives.h @@ -49,12 +49,11 @@ public: } /// add a Triangle primitive - Triangle *add_triangle(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2, - math::Vector3f const &normal, bool detail); + void add_triangle(Triangle *triangle); /// add a Quad primitive - Quad *add_quad(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2, math::Vector3f const &v3, - math::Vector3f const &normal, bool detail); + void add_quad(Quad *quad); + private: Triangles primitives_triangles; Quads primitives_quads; diff --git a/src/model/quad.cc b/src/model/quad.cc index 5bed2e4..c012bf0 100644 --- a/src/model/quad.cc +++ b/src/model/quad.cc @@ -9,12 +9,15 @@ namespace model { -Quad::Quad(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2, math::Vector3f const &v3, - math::Vector3f const &normal, bool detail) : +Quad::Quad(const math::Vector3f &v0,const math::Vector3f &v1, const math::Vector3f &v2, const math::Vector3f &v3, const math::Vector3f &normal, const bool detail) : quad_v0(v0), + quad_n0(normal), quad_v1(v1), + quad_n1(normal), quad_v2(v2), + quad_n2(normal), quad_v3(v3), + quad_n3(normal), quad_normal(normal) { quad_detail = detail; diff --git a/src/model/quad.h b/src/model/quad.h index 2950477..4728866 100644 --- a/src/model/quad.h +++ b/src/model/quad.h @@ -7,7 +7,7 @@ #ifndef __INCLUDED_MODEL_QUAD_H__ #define __INCLUDED_MODEL_QUAD_H__ -#include "math/color.h" +#include "math/vector2f.h" #include "math/vector3f.h" namespace model @@ -17,54 +17,115 @@ namespace model class Quad { public: - /// a new quad with 4 vertices, a normal, color and a detail flag - Quad(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2, math::Vector3f const &v3, - math::Vector3f const &normal, bool detail); + /** + * @brief a new quad with 4 vertices + * this constructor assigns the face normal to every vertex normal + */ + Quad(const math::Vector3f &v0,const math::Vector3f &v1, const math::Vector3f &v2, const math::Vector3f &v3, const math::Vector3f &normal, const bool detail = false); /// delete quad ~Quad(); - /// normal of the quad - inline math::Vector3f const & normal() const + /// quad vertex 0 + inline math::Vector3f & v0() { - return quad_normal; + return quad_v0; } - /// indidcates if this quad was generated from a detail brush - inline bool detail() const + /// quad vertex 0 normal + inline math::Vector3f & n0() { - return quad_detail; + return quad_n0; } - - /// quad vertex 0 - inline math::Vector3f & v0() + + /// quad vertex 0 texture coordinates + inline math::Vector2f & t0() { - return quad_v0; + return quad_t0; } - + /// quad vertex 1 inline math::Vector3f & v1() { return quad_v1; } + + /// quad vertex 1 normal + inline math::Vector3f & n1() + { + return quad_n1; + } + + /// quad vertex 1 texture coordinates + inline math::Vector2f & t1() + { + return quad_t1; + } /// quad vertex 2 inline math::Vector3f & v2() { return quad_v2; } - + + /// quad vertex 2 normal + inline math::Vector3f & n2() + { + return quad_n2; + } + + /// quad vertex 2 texture coordinates + inline math::Vector2f & t2() + { + return quad_t2; + } + /// quad vertex 3 inline math::Vector3f & v3() { return quad_v3; } + + /// quad vertex 3 normal + inline math::Vector3f & n3() + { + return quad_n3; + } + + /// quad vertex 3 texture coordinates + inline math::Vector2f & t3() + { + return quad_t3; + } + + /// indidcates if this quad was generated from a detail brush + inline bool detail() const + { + return quad_detail; + } + + /// face normal + inline math::Vector3f &normal() { + return quad_normal; + } + private: - math::Vector3f quad_v0; + math::Vector3f quad_v0; + math::Vector3f quad_n0; + math::Vector2f quad_t0; + math::Vector3f quad_v1; + math::Vector3f quad_n1; + math::Vector2f quad_t1; + math::Vector3f quad_v2; + math::Vector3f quad_n2; + math::Vector2f quad_t2; + math::Vector3f quad_v3; - + math::Vector3f quad_n3; + math::Vector2f quad_t3; + math::Vector3f quad_normal; bool quad_detail; }; diff --git a/src/model/triangle.cc b/src/model/triangle.cc index 429c1d9..90d52b7 100644 --- a/src/model/triangle.cc +++ b/src/model/triangle.cc @@ -9,14 +9,15 @@ namespace model { -Triangle::Triangle() +Triangle::Triangle(const math::Vector3f &v0, const math::Vector3f &v1, const math::Vector3f &v2) : + triangle_v0(v0), + triangle_v1(v1), + triangle_v2(v2) { triangle_detail = false; } -Triangle::Triangle( - math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2, - math::Vector3f const &normal, bool detail) : +Triangle::Triangle(const math::Vector3f &v0,const math::Vector3f &v1, const math::Vector3f &v2, const math::Vector3f &normal, const bool detail) : triangle_v0(v0), triangle_n0(normal), triangle_v1(v1), @@ -28,30 +29,8 @@ Triangle::Triangle( triangle_detail = detail; } -Triangle::Triangle(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2) : - triangle_v0(v0), - triangle_v1(v1), - triangle_v2(v2) -{ - triangle_detail = false; -} - Triangle::~Triangle() -{} - -void Triangle::assign(const Triangle &other) { - triangle_v0.assign(other.triangle_v0); - triangle_n0.assign(other.triangle_n0); - - triangle_v1.assign(other.triangle_v1); - triangle_n1.assign(other.triangle_n1); - - triangle_v2.assign(other.triangle_v2); - triangle_n2.assign(other.triangle_n2); - - triangle_normal.assign(other.triangle_normal); - triangle_detail = other.triangle_detail; } } diff --git a/src/model/triangle.h b/src/model/triangle.h index a3fb643..f2a741a 100644 --- a/src/model/triangle.h +++ b/src/model/triangle.h @@ -7,7 +7,6 @@ #ifndef __INCLUDED_MODEL_TRIANGLE_H__ #define __INCLUDED_MODEL_TRIANGLE_H__ -#include "math/color.h" #include "math/vector2f.h" #include "math/vector3f.h" @@ -19,54 +18,20 @@ class Triangle { public: /** - * @brief default constructor - */ - Triangle(); - - /** - * @brief copy constructor + * @brief a new triangle with 3 vertices + * this constructor is used by the ASE reader sets the detail flag to false */ - Triangle(const Triangle &other); + Triangle(const math::Vector3f &v0, const math::Vector3f &v1, const math::Vector3f &v2); /** * @brief a new triangle with 3 vertices - * this constructor sets the detail flag to false + * this constructor is used by the MAP reader and assigns the face normal to every vertex normal */ - Triangle(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2); - - /// a new triangle with 3 vertices, a normal and detail flag - Triangle(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2, - math::Vector3f const &normal, bool detail); + Triangle(const math::Vector3f &v0,const math::Vector3f &v1, const math::Vector3f &v2, const math::Vector3f &normal, const bool detail = false); /// delete triangle ~Triangle(); - /** - * @brief assignment - */ - void assign(const Triangle &other); - - /** - * @brief assignment oeprator - */ - inline const Triangle & operator=(const Triangle &other) - { - assign(other); - return (*this); - } - - /// normal of the triangle - inline math::Vector3f & normal() - { - return triangle_normal; - } - - /// indidcates if this triangle was generated from a detail brush - inline bool detail() const - { - return triangle_detail; - } - /// triangle vertex 0 inline math::Vector3f & v0() { @@ -120,6 +85,17 @@ public: { return triangle_t2; } + + /// indidcates if this triangle was generated from a detail brush + inline bool detail() const + { + return triangle_detail; + } + + /// face normal + inline math::Vector3f &normal() { + return triangle_normal; + } private: diff --git a/src/render/draw.cc b/src/render/draw.cc index e800d62..da8c8d3 100644 --- a/src/render/draw.cc +++ b/src/render/draw.cc @@ -663,7 +663,7 @@ void draw_model_fragments(model::Model *model, if (material->flags() & model::Material::Texture) { - Textures::bind(material->texture()); + Textures::bind(material->texture_id()); if (!use_texture) { gl::enable(GL_TEXTURE_2D); |