Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/render/model.cc')
-rw-r--r--src/render/model.cc386
1 files changed, 386 insertions, 0 deletions
diff --git a/src/render/model.cc b/src/render/model.cc
new file mode 100644
index 0000000..f048b7f
--- /dev/null
+++ b/src/render/model.cc
@@ -0,0 +1,386 @@
+/*
+ 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 <iostream>
+#include <string>
+#include <fstream>
+#include <iomanip>
+#include <sstream>
+#include <vector>
+#include <list>
+
+#include "render/model.h"
+#include "render/gl.h"
+#include "filesystem/filesystem.h"
+
+namespace render
+{
+
+const float MAX_BOUNDS = 8192;
+const float delta = 10e-8;
+
+std::map<std::string, Model*> Model::registry;
+
+Model::Model(std::string const & name) :
+ model_name(name)
+{
+ model_scale = 1.0f / 64.0f;
+
+ 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;
+ }
+ con_debug << "Reading " << fn << "\n";
+
+ // --------- the actual reading
+ using math::Vector3f;
+ using math::Plane3f;
+
+ std::vector<Plane3f *> planes;
+ std::string classname;
+ unsigned int level = 0;
+ char data[1024];
+
+ 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 == "{") {
+ level ++;
+ //cout << " LEVEL +" << level << std::endl;
+ } else if (firstword == "}") {
+ //cout << " LEVEL -" << level << std::endl;
+ if ((level == 2) && (classname == "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);
+ }
+
+ // clean planes
+ for (std::vector<Plane3f *>::iterator it = planes.begin(); it != planes.end(); it++) {
+ delete (*it);
+ }
+ planes.clear();
+ }
+ level--;
+ } else if (firstword == "\"classname\"") {
+ classname.clear();
+ if (linestream >> classname) {
+ if (classname.size() > 2) {
+ classname.erase(0,1);
+ classname.erase(classname.size()-1, 1);
+ linestream >> classname;
+ //cout << " CLASS '" << classname << "'" << std::endl;
+ } else
+ classname.clear();
+ } else {
+ //cout << " EMPTY CLASS" << std::endl;
+ }
+ } else if (firstword == "(") {
+ if ((level == 2) && (classname == "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);
+ planes.push_back(plane);
+ //cout << "normal " << plane->normal() << std::endl;
+ } else {
+ //cout << " UNKNOWN line for '" << classname << "' level " << level << std::endl;
+ }
+ }
+ }
+ }
+
+ ifs.close();
+
+ con_debug << "Loaded model " << name << " with " << model_face.size() << " polygons\n";
+}
+
+Model::~Model()
+{
+ // delete all faces
+ for (std::list<Face *>::iterator fit = model_face.begin(); fit != model_face.end(); fit++) {
+ delete(*fit);
+ }
+
+ model_face.clear();
+}
+
+void Model::make_face(math::Plane3f *face, std::vector<math::Plane3f *> & planes)
+{
+ using math::Vector3f;
+ using math::Plane3f;
+
+ //cout << "Face with normal " << face->normal() << endl;
+ std::vector<math::Vector3f *> 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;
+ 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<Vector3f *>::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;
+ 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<Vector3f *>::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;
+ 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<Vector3f *>::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<Plane3f *>::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;
+ // using suggestions from
+ // http://www.flipcode.com/archives/Level_Editing.shtml
+
+ // 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.y*plane->normal().y + v.z*plane->normal().z +plane->d()) < -delta ) {
+
+ // find current
+ std::vector<Vector3f *>::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.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.y*plane->normal().y + next.z*plane->normal().z + plane->d()) > delta ) {
+ // calculate intersection
+
+ // calculate intersection
+ float t1 = -plane->normal().x * next.x - plane->normal().y * next.y - plane->normal().z * next.z -plane->d();
+ float t2 = (plane->normal().x * v.x - plane->normal().x * next.x +
+ plane->normal().y * v.y - plane->normal().y * next.y +
+ plane->normal().z * v.z - plane->normal().z * next.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] = next [j] + t1 * ( v[j] - next[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) {
+ Vector3f n = face->normal();
+ n.normalize();
+ Face *mf = new Face(n);
+
+ for (std::vector<Vector3f *>::iterator it = vl.begin(); it != vl.end(); it++) {
+ mf->add_vertex(*(*it));
+ }
+
+ //con_debug << "adding face\n";
+ add_face(mf);
+ }
+
+ for (std::vector<Vector3f *>::iterator it = vl.begin(); it != vl.end(); it++) {
+ delete (*it);
+ }
+
+ vl.clear();
+
+}
+
+void Model::add_face(Face *face)
+{
+ model_face.push_back(face);
+}
+
+void Model::draw()
+{
+ gl::scale(model_scale, model_scale, model_scale);
+
+ // draw all faces
+ for (std::list<Face *>::iterator fit = model_face.begin(); fit != model_face.end(); fit++) {
+ (*fit)->draw();
+ }
+
+}
+
+Model *Model::find(std::string const & name)
+{
+ std::map<std::string, Model*>::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<std::string, Model*>::iterator mit = registry.begin(); mit != registry.end(); mit++) {
+ delete (*mit).second;
+ }
+ registry.clear();
+}
+
+}