Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/model.cc')
-rw-r--r--src/core/model.cc180
1 files changed, 113 insertions, 67 deletions
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 <vector>
#include <list>
+#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<math::Vector3f *>::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<std::string, Model*> 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::vector<Vector3f *>points;
- for (std::vector<Plane3f *>::iterator face = planes.begin(); face != planes.end(); face++) {
- make_face((*face), planes);
+
+ if (!(Cvar::sv_dedicated && Cvar::sv_dedicated->value())) {
+ // for every face
+ std::vector<Vector3f *>points;
+ for (std::vector<Plane3f *>::iterator face = planes.begin(); face != planes.end(); face++) {
+ make_face((*face), planes);
+ }
}
-
+
// clean planes
for (std::vector<Plane3f *>::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<Face *>::iterator fit = model_face.begin(); fit != model_face.end(); fit++) {
- delete(*fit);
- }
- model_face.clear();
-
// delete all engines
for (std::list<Engine *>::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<math::Plane3f *> & planes
// using suggestions from
// http://www.flipcode.com/archives/Level_Editing.shtml
- //cout << "Face with normal " << face->normal() << endl;
std::vector<math::Vector3f *> vl;
// inital vertexes
@@ -496,28 +525,46 @@ void Model::make_face(math::Plane3f *face, std::vector<math::Plane3f *> & planes
} else
color = new math::Color(1.0f, 0.0, 1.0f);
- /*
+ // calculate bounding box
+ for (std::vector<Vector3f *>::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<Vector3f *>::iterator v0 = vl.begin();
std::vector<Vector3f *>::reverse_iterator vn = vl.rbegin();
std::vector<Vector3f *>::reverse_iterator vn1 = vl.rbegin();
++vn1;
- */
- Face *mf = new Face(face->normal()*-1, color);
- for (std::vector<Vector3f *>::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<math::Plane3f *> & 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<std::string, Model*>::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";
}