From 7fef8856b21215b0dd28dcc57f04c8a98ab5226f Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 22 Mar 2008 22:36:39 +0000 Subject: Implemented vertex arrays --- src/core/model.cc | 180 +++++++++++++++++++++++++++++++------------------- src/core/model.h | 57 ++++++++++------ src/core/netserver.cc | 2 +- 3 files changed, 152 insertions(+), 87 deletions(-) (limited to 'src/core') diff --git a/src/core/model.cc b/src/core/model.cc index 88a1040..df6e563 100644 --- a/src/core/model.cc +++ b/src/core/model.cc @@ -12,6 +12,7 @@ #include #include +#include "core/cvar.h" #include "core/model.h" #include "filesystem/filesystem.h" @@ -21,6 +22,64 @@ namespace core const float MAX_BOUNDS = 8192; const float delta = 10e-10; +const size_t VERTEXARRAYVERTEXARRAYSIZE=65536*3; + +/* ---------- core::VertexArray ------------------------------------ */ +float VertexArray::vertex[VERTEXARRAYSIZE]; +float VertexArray::vertex_color[VERTEXARRAYSIZE]; +float VertexArray::vertex_normal[VERTEXARRAYSIZE]; + +float VertexArray::evertex[VERTEXARRAYSIZE]; +float VertexArray::evertex_normal[VERTEXARRAYSIZE]; + +size_t VertexArray::vertex_index; +size_t VertexArray::evertex_index; + +void VertexArray::clear() +{ + memset(vertex, 0, sizeof(vertex)); + memset(vertex_color, 0, sizeof(vertex_color)); + memset(vertex_normal, 0, sizeof(vertex_normal)); + + memset(evertex, 0, sizeof(evertex)); + memset(evertex_normal, 0, sizeof(evertex_normal)); + + vertex_index = 0; + evertex_index = 0; +} + +void VertexArray::add_vertex(math::Vector3f const &v, math::Vector3f const &n, math::Color const &color) { + if (vertex_index + 3 >= VERTEXARRAYSIZE) { + con_warn << "VertexArray overflow!" << std::endl; + return; + } + + for (int i = 0; i < 3; i ++) { + vertex[vertex_index+i] = v[i]; + vertex_normal[vertex_index+i] = n[i]; + } + + vertex_color[vertex_index] = color.r; + vertex_color[vertex_index+1] = color.g; + vertex_color[vertex_index+2] = color.b; + + vertex_index += 3; +} + +void VertexArray::add_evertex(math::Vector3f const &v, math::Vector3f const &n) { + if (evertex_index + 3 >= VERTEXARRAYSIZE) { + con_warn << "EVertexArray overflow!" << std::endl; + return; + } + + for (int i = 0; i < 3; i ++) { + evertex[evertex_index+i] = v[i]; + evertex_normal[evertex_index+i] = n[i]; + } + + evertex_index += 3; +} + /* ---------- core::Light ------------------------------------------ */ Light::Light(math::Vector3f const & location, math::Color const & color) : @@ -40,38 +99,6 @@ Engine::Engine(math::Vector3f const & location) : Engine::~Engine() {} -/* ---------- core::Face ------------------------------------------ */ - -Face::Face(math::Vector3f const & normal, math::Color const *color) : - face_normal(normal) -{ - face_normal.normalize(); - - if (color) - face_color = new math::Color(*color); - else - face_color = 0; -} - -Face::~Face() -{ - for (std::vector::iterator it = face_vertex.begin(); it != face_vertex.end(); it++) { - delete (*it); - } - - face_vertex.clear(); - - if (face_color) - delete face_color; -} - -void Face::add_vertex(math::Vector3f const & vertex) -{ - math::Vector3f *v = new math::Vector3f(vertex); - - face_vertex.push_back(v); -} - /* ---------- core::Model ------------------------------------------ */ std::map Model::registry; @@ -81,6 +108,10 @@ Model::Model(std::string const & name) : { model_valid = false; model_scale = 1.0f / 1024.0f; + model_first_vertex = 0; + model_first_evertex = 0; + model_vertex_count = 0; + model_evertex_count = 0; std::string fn("maps/"); fn.append(name); @@ -113,6 +144,9 @@ Model::Model(std::string const & name) : math::Vector3f class_origin; float class_angle; math::Color class_color; + + model_first_evertex = VertexArray::evertex_index/3; + model_first_vertex = VertexArray::vertex_index/3; while (ifs) { ifs.getline(data, 1023); @@ -135,14 +169,15 @@ Model::Model(std::string const & name) : } else if (firstword == "}") { //cout << " LEVEL -" << level << std::endl; if ((level == 2) && (class_name == "worldspawn")) { - //cout << "brush with " << planes.size() << " faces" << std::endl; - - // for every face - std::vectorpoints; - for (std::vector::iterator face = planes.begin(); face != planes.end(); face++) { - make_face((*face), planes); + + if (!(Cvar::sv_dedicated && Cvar::sv_dedicated->value())) { + // for every face + std::vectorpoints; + for (std::vector::iterator face = planes.begin(); face != planes.end(); face++) { + make_face((*face), planes); + } } - + // clean planes for (std::vector::iterator it = planes.begin(); it != planes.end(); it++) { delete(*it); @@ -246,7 +281,7 @@ Model::Model(std::string const & name) : ifs.close(); - if (model_face.size()) { + if ((model_vertex_count + model_evertex_count) > 0 ) { if (model_maxbbox.lengthsquared() > model_minbbox.lengthsquared()) { model_radius = model_maxbbox.length(); } else { @@ -254,17 +289,12 @@ Model::Model(std::string const & name) : } model_valid = true; } - con_debug << " maps/" << name << ".map " << model_face.size() << " polygons\n"; + //con_debug << " maps/" << name << ".map " << model_face.size() << " polygons\n"; + con_debug << " maps/" << name << ".map " << (model_vertex_count + model_evertex_count)/3 << " triangles" << std::endl; } Model::~Model() { - // delete all faces - for (std::list::iterator fit = model_face.begin(); fit != model_face.end(); fit++) { - delete(*fit); - } - model_face.clear(); - // delete all engines for (std::list::iterator eit = model_engine.begin(); eit != model_engine.end(); eit++) { delete(*eit); @@ -291,7 +321,6 @@ void Model::make_face(math::Plane3f *face, std::vector & planes // using suggestions from // http://www.flipcode.com/archives/Level_Editing.shtml - //cout << "Face with normal " << face->normal() << endl; std::vector vl; // inital vertexes @@ -496,28 +525,46 @@ void Model::make_face(math::Plane3f *face, std::vector & planes } else color = new math::Color(1.0f, 0.0, 1.0f); - /* + // calculate bounding box + for (std::vector::iterator it = vl.begin(); it != vl.end(); it++) { + + *(*it) *= model_scale; + + for (int i=0; i < 3; i++) { + if (model_maxbbox[i] < (*(*it))[i]) + model_maxbbox[i] = (*(*it))[i]; + + if (model_minbbox[i] > (*(*it))[i]) + model_minbbox[i] = (*(*it))[i]; + } + } + // split face into triangles while (vl.size() >2 ) { std::vector::iterator v0 = vl.begin(); std::vector::reverse_iterator vn = vl.rbegin(); std::vector::reverse_iterator vn1 = vl.rbegin(); ++vn1; - */ - Face *mf = new Face(face->normal()*-1, color); - for (std::vector::iterator it = vl.begin(); it != vl.end(); it++) { - mf->add_vertex(*(*it) * model_scale); - // bounding box - for (int i=0; i < 3; i++) { - if (model_maxbbox[i] < (*(*it))[i] * model_scale) - model_maxbbox[i] = (*(*it))[i] * model_scale; - - if (model_minbbox[i] > (*(*it))[i] * model_scale) - model_minbbox[i] = (*(*it))[i] * model_scale; + Vector3f n(face->normal()*-1); + n.normalize(); + + if (!color) { + VertexArray::add_evertex(*(*vn1), n); + VertexArray::add_evertex(*(*vn), n); + VertexArray::add_evertex(*(*v0), n); + model_evertex_count += 3; + } else { + VertexArray::add_vertex(*(*vn1), n, *color); + VertexArray::add_vertex(*(*vn), n, *color); + VertexArray::add_vertex(*(*v0), n, *color); + model_vertex_count += 3; } + + delete (*vn); + vl.pop_back(); } - add_face(mf); + //add_face(mf); if (color) delete color; } else { con_debug << "Unresolved face!\n"; @@ -531,11 +578,6 @@ void Model::make_face(math::Plane3f *face, std::vector & planes } -void Model::add_face(Face *face) -{ - model_face.push_back(face); -} - void Model::add_engine(Engine *engine) { model_engine.push_back(engine); @@ -572,12 +614,16 @@ void Model::clear() delete(*mit).second; } registry.clear(); + + // clear the vertex array + VertexArray::clear(); } void Model::list() { for (std::map::iterator mit = registry.begin(); mit != registry.end(); mit++) { - con_print << " " << (*mit).second->model_name << " " << (*mit).second->model_face.size() << " polys " + con_print << " " << (*mit).second->name() << " " + << ((*mit).second->vertex_count() + (*mit).second->evertex_count())/3 << " triangles " << (*mit).second->model_engine.size() << " engines " << (*mit).second->model_light.size() << " lights\n"; } diff --git a/src/core/model.h b/src/core/model.h index 00c8a48..5deee35 100644 --- a/src/core/model.h +++ b/src/core/model.h @@ -18,27 +18,31 @@ namespace core { -/// one face (polygon) of a model -class Face { +/// Global vertex array + + +const size_t VERTEXARRAYSIZE=65536*512; + +class VertexArray +{ public: - Face(math::Vector3f const & normal, math::Color const *color=0); - ~Face(); + /// model vertices + static float vertex[VERTEXARRAYSIZE]; + static float vertex_color[VERTEXARRAYSIZE]; + static float vertex_normal[VERTEXARRAYSIZE]; - /// the normal of this face - inline math::Vector3f const & normal() const { return face_normal; }; + /// model vertices with entity color + static float evertex[VERTEXARRAYSIZE]; + static float evertex_normal[VERTEXARRAYSIZE]; - /// the color of this face - inline math::Color const *color() const { return face_color; }; + static size_t vertex_index; + static size_t evertex_index; - /// add a vertex to the face - void add_vertex(math::Vector3f const &vertex); + static void clear(); - /// face vertexes - std::vector face_vertex; + static void add_vertex(math::Vector3f const &v, math::Vector3f const &n, math::Color const &color); + static void add_evertex(math::Vector3f const &v, math::Vector3f const &n); -private: - math::Vector3f face_normal; - math::Color *face_color; }; @@ -95,6 +99,18 @@ public: /// minimum values of the bounding box inline math::Vector3f const & minbbox() const { return model_minbbox; } + /// first vertex in the global VertexArray + inline size_t first_vertex() const { return model_first_vertex; } + + /// number of vertexes in this model + inline size_t vertex_count() const { return model_vertex_count; } + + /// first vertex in the global VertexArray + inline size_t first_evertex() const { return model_first_evertex; } + + /// number of vertexes in this model + inline size_t evertex_count() const { return model_evertex_count; } + /// radius inline float radius() const { return model_radius; } @@ -115,19 +131,17 @@ public: /// list the content of the model registry static void list(); - /// list of Faces - std::list model_face; - /// list of Engines std::list model_engine; /// list of Lights std::list model_light; + + private: void make_face(math::Plane3f *face, std::vector & planes); void add_engine(Engine *engine); - void add_face(Face *face); void add_light(Light *light); std::string model_name; @@ -138,6 +152,11 @@ private: math::Vector3f model_maxbbox; math::Vector3f model_minbbox; + + size_t model_first_vertex; + size_t model_vertex_count; + size_t model_first_evertex; + size_t model_evertex_count; }; } diff --git a/src/core/netserver.cc b/src/core/netserver.cc index c8c0a9d..16beee0 100644 --- a/src/core/netserver.cc +++ b/src/core/netserver.cc @@ -284,7 +284,7 @@ void NetServer::broadcast(std::string const & message, Player *ignore_player) } } -// find the client corresponding to a player id +// find the client corresponding to a player NetClient *NetServer::find_client(Player const *player) { for (std::list::iterator it = clients.begin(); it != clients.end(); it++) { -- cgit v1.2.3