/* render/model.h This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ #include #include #include #include #include #include #include #include "core/cvar.h" #include "core/model.h" #include "filesystem/filesystem.h" 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, bool strobe) : light_location(location), light_color(color) { light_strobe = strobe; } Light::~Light() {} /* ---------- core::Engine ------------------------------------------ */ Engine::Engine(math::Vector3f const & location) : engine_location(location) {} Engine::~Engine() {} /* ---------- core::Model ------------------------------------------ */ std::map Model::registry; Model::Model(std::string const & name) : model_name(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); fn.append(".map"); filesystem::File *f = filesystem::open(fn.c_str()); if (!f) { return; } fn = f->path(); fn.append(f->name()); filesystem::close(f); std::ifstream ifs(fn.c_str()); if (!ifs.is_open()) { con_warn << "Could not stream " << fn << "!\n"; return; } // --------- the actual reading using math::Vector3f; using math::Plane3f; std::vector planes; unsigned int level = 0; char data[1024]; std::string class_name; math::Vector3f class_origin; float class_angle; math::Color class_color; unsigned int class_spawnflags; model_first_evertex = VertexArray::evertex_index/3; model_first_vertex = VertexArray::vertex_index/3; while (ifs) { ifs.getline(data, 1023); std::istringstream linestream(data); std::string firstword; if (linestream >> firstword) { if (firstword == "//") { //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); class_spawnflags = 0; } level ++; //cout << " LEVEL +" << level << std::endl; } else if (firstword == "}") { //cout << " LEVEL -" << level << std::endl; if ((level == 2) && (class_name == "worldspawn")) { 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); } planes.clear(); } 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, (class_spawnflags & 1) == 1)); } if (level == 1) { class_angle = 0; class_name.clear(); class_origin = Vector3f(0,0,0); class_color = math::Color(1, 1, 1); class_spawnflags = 0; } level--; } else if (firstword == "\"classname\"") { class_name.clear(); if (linestream >> class_name) { if (class_name.size() > 2) { class_name.erase(0,1); class_name.erase(class_name.size()-1, 1); //linestream >> class_name; //con_debug << " classname '" << class_name << "'" << std::endl; } else { class_name.clear(); } } else { //cout << " EMPTY CLASS" << std::endl; } } else if (firstword == "\"origin\"") { std::string tmp; char c; while ((linestream.get(c)) && (c != '"')); while ((linestream.get(c)) && (c != '"')) tmp += c; std::istringstream is(tmp); is >> class_origin.x; 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; while ((linestream.get(c)) && (c != '"')); while ((linestream.get(c)) && (c != '"')) tmp += c; std::istringstream is(tmp); is >> class_angle; //con_debug << " angle '" << class_angle << "'" << std::endl; } else if (firstword == "\"spawnflags\"") { std::string tmp; char c; while ((linestream.get(c)) && (c != '"')); while ((linestream.get(c)) && (c != '"')) tmp += c; std::istringstream is(tmp); is >> class_spawnflags; //con_debug << " spawnflags '" << class_spawnflags << "'" << std::endl; } else if (firstword == "(") { if ((level == 2) && (class_name == "worldspawn")) { //cout << " BRUSH PLANE" << std::endl; Vector3f p1; Vector3f p2; Vector3f p3; std::string tmp; std::string texture; linestream >> p1; linestream >> tmp; // ) linestream >> tmp; // ( linestream >> p2; linestream >> tmp; // ) linestream >> tmp; // ( linestream >> p3; linestream >> tmp; // ) linestream >> texture; //cout << data << std::endl; //cout << "(" << p1 << ") (" << p2 << ") (" << p3 << ") " << texture << std::endl; Plane3f *plane = new Plane3f(p1, p2, p3); plane->texture() = texture; planes.push_back(plane); //cout << "normal " << plane->normal() << std::endl; } else { //cout << " UNKNOWN line for '" << classname << "' level " << level << std::endl; } } } } ifs.close(); if ((model_vertex_count + model_evertex_count) > 0 ) { if (model_maxbbox.lengthsquared() > model_minbbox.lengthsquared()) { model_radius = model_maxbbox.length(); } else { model_radius = model_minbbox.length(); } model_valid = true; } //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 engines for (std::list::iterator eit = model_engine.begin(); eit != model_engine.end(); eit++) { delete(*eit); } model_engine.clear(); // delete all lights for (std::list::iterator lit = model_light.begin(); lit != model_light.end(); lit++) { delete (*lit); } model_light.clear(); } void Model::make_face(math::Plane3f *face, std::vector & planes) { using math::Vector3f; using math::Plane3f; // ignore caulk if (face->texture() == "common/caulk") { return; } // FIXME clip should be parsed as collision blocks if (face->texture() == "common/clip") { return; } // using suggestions from // http://www.flipcode.com/archives/Level_Editing.shtml std::vector vl; // inital vertexes // check if the face is x-axis oriented if ((fabsf(face->normal().x) >= fabsf(face->normal().y)) && (fabsf(face->normal().x) >= fabsf(face->normal().z))) { //cout << " x oriented" << std::endl; if (face->normal().x >= 0) { vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, -MAX_BOUNDS)); vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, MAX_BOUNDS)); vl.push_back(new math::Vector3f(0, MAX_BOUNDS, MAX_BOUNDS)); vl.push_back(new math::Vector3f(0, MAX_BOUNDS, -MAX_BOUNDS)); } else { vl.push_back(new math::Vector3f(0, MAX_BOUNDS, -MAX_BOUNDS)); vl.push_back(new math::Vector3f(0, MAX_BOUNDS, MAX_BOUNDS)); vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, MAX_BOUNDS)); vl.push_back(new math::Vector3f(0, -MAX_BOUNDS, -MAX_BOUNDS)); } // calculate the x coordinate of each face vertex for (std::vector::iterator it = vl.begin(); it != vl.end(); it++) { (*it)->x = (-face->d() - face->normal().z * (*it)->z - face->normal().y * (*it)->y) / face->normal().x; } } // check if the face is y-axis oriented else if ((fabsf(face->normal().y) >= fabsf(face->normal().x)) && (fabsf(face->normal().y) >= fabsf(face->normal().z))) { //cout << " y oriented" << std::endl; if (face->normal().y >= 0) { vl.push_back(new Vector3f(MAX_BOUNDS, 0, -MAX_BOUNDS)); vl.push_back(new Vector3f(MAX_BOUNDS, 0, MAX_BOUNDS)); vl.push_back(new Vector3f(-MAX_BOUNDS, 0, MAX_BOUNDS)); vl.push_back(new Vector3f(-MAX_BOUNDS, 0, -MAX_BOUNDS)); } else { vl.push_back(new Vector3f(-MAX_BOUNDS, 0, -MAX_BOUNDS)); vl.push_back(new Vector3f(-MAX_BOUNDS, 0, MAX_BOUNDS)); vl.push_back(new Vector3f(MAX_BOUNDS, 0, MAX_BOUNDS)); vl.push_back(new Vector3f(MAX_BOUNDS, 0, -MAX_BOUNDS)); } // calculate the x coordinate of each face vertex for (std::vector::iterator it = vl.begin(); it != vl.end(); it++) { (*it)->y = (-face->d() - face->normal().z * (*it)->z - face->normal().x * (*it)->x) / face->normal().y; } } // face must be z-axis oriented else { //cout << " z oriented" << std::endl; if (face->normal().z >= 0) { vl.push_back(new Vector3f(-MAX_BOUNDS, -MAX_BOUNDS, 0)); vl.push_back(new Vector3f(-MAX_BOUNDS, MAX_BOUNDS, 0)); vl.push_back(new Vector3f(MAX_BOUNDS, MAX_BOUNDS, 0)); vl.push_back(new Vector3f(MAX_BOUNDS, -MAX_BOUNDS, 0)); } else { vl.push_back(new Vector3f(MAX_BOUNDS, -MAX_BOUNDS, 0)); vl.push_back(new Vector3f(MAX_BOUNDS, MAX_BOUNDS, 0)); vl.push_back(new Vector3f(-MAX_BOUNDS, MAX_BOUNDS, 0)); vl.push_back(new Vector3f(-MAX_BOUNDS, -MAX_BOUNDS, 0)); } // calculate the x coordinate of each face vertex for (std::vector::iterator it = vl.begin(); it != vl.end(); it++) { (*it)->z = (-face->d() - face->normal().x * (*it)->x - face->normal().y * (*it)->y) / face->normal().z; } } // intersect the face with every plane for (std::vector::iterator pit = planes.begin(); pit != planes.end(); pit++) { Plane3f *plane = (*pit); if (plane == face) { continue; } Vector3f fn = crossproduct(face->point(1)-face->point(0), face->point(2)-face->point(0)); Vector3f pn = crossproduct(plane->point(1)-plane->point(0), plane->point(2)-plane->point(0)); Vector3f t = crossproduct(fn, pn); if ((t.x == 0) && (t.y == 0) && (t.z == 0)) { continue; } //cout << " intersecting with plane with normal " << plane->normal() << std::endl; // intersect face with plane for (int i=0; vl.size() - i > 0; i++) { Vector3f v(*vl.at(i)); Vector3f next; if (vl.size() - i > 1) { //cout << " -- at " << i+1 << std::endl; next = *vl.at(i+1); } else { next = *vl.front(); } Vector3f prev; if (i > 0) { //cout << " -- at " << i-1 << std::endl; prev = *vl.at(i-1); } else { prev = *vl.back(); } //cout << " vertex " << i << " prev " << prev << " v " << v << " next " << next << std::endl; if ((v.x*plane->normal().x + v.y*plane->normal().y + v.z*plane->normal().z +plane->d()) < delta) { // find current std::vector::iterator vit = vl.begin(); while ((*vit) != vl.at(i)) { vit++; } // check if prev - v intersects with plane if ((prev.x*plane->normal().x + prev.y*plane->normal().y + prev.z*plane->normal().z + plane->d()) > -delta) { // calculate intersection float t1 = -plane->normal().x * prev.x - plane->normal().y * prev.y - plane->normal().z * prev.z -plane->d(); float t2 = (plane->normal().x * v.x - plane->normal().x * prev.x + plane->normal().y * v.y - plane->normal().y * prev.y + plane->normal().z * v.z - plane->normal().z * prev.z); //cout << "prev t2 " << t2 << std::endl; Vector3f *s = new Vector3f; if (t2 == 0) { *s = v; } else { for (int j = 0; j < 3; j++) (*s)[j] = prev [j] + t1 * (v[j] - prev[j]) / t2; } //cout << " added " << *s << std::endl; vit = vl.insert(vit,s); vit++; i++; } // check if next - v intersects with plane if ((next.x*plane->normal().x + next.y*plane->normal().y + next.z*plane->normal().z + plane->d()) > -delta) { // calculate intersection // calculate intersection float t1 = -plane->normal().x * v.x - plane->normal().y * v.y - plane->normal().z * v.z -plane->d(); float t2 = (plane->normal().x * next.x - plane->normal().x * v.x + plane->normal().y * next.y - plane->normal().y * v.y + plane->normal().z * next.z - plane->normal().z * v.z); //cout << "next t2 " << t2 << std::endl; Vector3f *s = new Vector3f; if (t2 == 0) { *s = v; } else { for (int j = 0; j < 3; j++) (*s)[j] = v [j] + t1 * (next[j] - v[j]) / t2; } //cout << " added " << *s << std::endl; vit = vl.insert(vit,s); vit++; i++; } // erase delete *vit; vl.erase(vit); i--; } } } if (vl.size() > 2) { math::Color *color = 0; if (face->texture() == "colors/white") { color = new math::Color(1, 1, 1); } else if (face->texture() == "colors/grey90") { color = new math::Color(0.9, 0.9, 0.9); } else if (face->texture() == "colors/grey75") { color = new math::Color(0.75, 0.75, 0.75); } else if (face->texture() == "colors/grey50") { color = new math::Color(0.5, 0.5, 0.5); } else if (face->texture() == "colors/grey25") { color = new math::Color(0.25, 0.25, 0.25); } else if (face->texture() == "colors/black") { color = new math::Color(0, 0, 0); } else if (face->texture() == "common/entity") { color = 0; } 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; 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); if (color) delete color; } else { con_debug << "Unresolved face!\n"; } for (std::vector::iterator it = vl.begin(); it != vl.end(); it++) { delete(*it); } vl.clear(); } 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::iterator it = registry.find(name); if (it == registry.end()) return 0; else return (*it).second; } Model *Model::get(std::string const & name) { Model *model = find(name); if (!model) { model = new Model(name); registry[model->name()] = model; } return model; } void Model::clear() { // delete all models in the registry for (std::map::iterator mit = registry.begin(); mit != registry.end(); mit++) { 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->name() << " " << ((*mit).second->vertex_count() + (*mit).second->evertex_count())/3 << " triangles " << (*mit).second->model_engine.size() << " engines " << (*mit).second->model_light.size() << " lights\n"; } con_print << registry.size() << " registered models" << std::endl; con_print << "vertex/evertex array usage " << (VertexArray::vertex_index * 100) / VERTEXARRAYSIZE << "%/" << (VertexArray::evertex_index * 100) / VERTEXARRAYSIZE << "%\n"; } }