diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/gameinterface.h | 2 | ||||
-rw-r--r-- | src/filesystem/inifile.cc | 8 | ||||
-rw-r--r-- | src/filesystem/inifile.h | 5 | ||||
-rw-r--r-- | src/game/game.cc | 35 | ||||
-rw-r--r-- | src/math/Makefile.am | 6 | ||||
-rw-r--r-- | src/math/mathlib.h | 10 | ||||
-rw-r--r-- | src/model/Makefile.am | 6 | ||||
-rw-r--r-- | src/model/engine.cc | 6 | ||||
-rw-r--r-- | src/model/engine.h | 11 | ||||
-rw-r--r-- | src/model/light.cc | 13 | ||||
-rw-r--r-- | src/model/light.h | 7 | ||||
-rw-r--r-- | src/model/map.cc | 175 | ||||
-rw-r--r-- | src/model/map.h | 27 | ||||
-rw-r--r-- | src/model/mapfile.cc | 546 | ||||
-rw-r--r-- | src/model/mapfile.h | 130 | ||||
-rw-r--r-- | src/model/model.cc | 675 | ||||
-rw-r--r-- | src/model/model.h | 88 | ||||
-rw-r--r-- | src/model/plane.cc | 37 | ||||
-rw-r--r-- | src/model/plane.h | 58 | ||||
-rw-r--r-- | src/model/tirangle.cc | 31 | ||||
-rw-r--r-- | src/model/triangle.h | 47 | ||||
-rw-r--r-- | src/render/draw.cc | 2 | ||||
-rw-r--r-- | src/render/draw.h | 1 | ||||
-rw-r--r-- | src/render/gl.cc | 2 | ||||
-rw-r--r-- | src/render/gl.h | 4 | ||||
-rw-r--r-- | src/render/render.h | 14 | ||||
-rw-r--r-- | src/render/tga.h | 3 |
27 files changed, 1155 insertions, 794 deletions
diff --git a/src/core/gameinterface.h b/src/core/gameinterface.h index 0c28421..e38c061 100644 --- a/src/core/gameinterface.h +++ b/src/core/gameinterface.h @@ -40,7 +40,7 @@ public: /*----- virtual mutators ------------------------------------------ */ /// run one game time frame - /// @param sec time since the previous frame, in seconds + /// @param seconds time since the previous frame, in seconds virtual void frame(float seconds) = 0; protected: diff --git a/src/filesystem/inifile.cc b/src/filesystem/inifile.cc index d9f2839..337cf45 100644 --- a/src/filesystem/inifile.cc +++ b/src/filesystem/inifile.cc @@ -16,7 +16,7 @@ IniFile::IniFile() {} IniFile::~IniFile() {} -void IniFile::open(std::string const & name) { +bool IniFile::open(std::string const & name) { last_read_was_section = false; last_read_was_key = false; @@ -32,7 +32,7 @@ void IniFile::open(std::string const & name) { filesystem::File *f = filesystem::open(inifile_name.c_str()); if (!f) { con_warn << "Could not open " << inifile_name << std::endl; - return; + return false; } std::string fn = f->path(); @@ -42,8 +42,10 @@ void IniFile::open(std::string const & name) { inifile_ifs.open(fn.c_str()); if (!inifile_ifs.is_open()) { con_warn << "Could not stream " << fn << "!\n"; - return; + return false; } + + return true; } diff --git a/src/filesystem/inifile.h b/src/filesystem/inifile.h index 6503053..f5c903d 100644 --- a/src/filesystem/inifile.h +++ b/src/filesystem/inifile.h @@ -24,13 +24,14 @@ namespace filesystem { class IniFile { public: IniFile(); - virtual ~IniFile(); + ~IniFile(); /// open the file for reading /** the filename will get the "ini/" prefix and ".ini" suffix */ - virtual void open(std::string const & name); + bool open(std::string const & name); + /// parse one line, returns false on end-of-file bool getline(); /// current section label diff --git a/src/game/game.cc b/src/game/game.cc index 74aa2d9..31b99c2 100644 --- a/src/game/game.cc +++ b/src/game/game.cc @@ -197,39 +197,6 @@ void Game::init() } worldini.close(); -/* - // the green cube - core::Entity *cube = new core::Entity(core::Entity::Solid & core::Entity::Static); - cube->entity_shape = core::Entity::Cube; - cube->entity_color = Color(0.0f, 0.8f, 0.0f); - cube->entity_location = Vector3f(24.0f, 24.0f, 0.0f); - cube->entity_name ="ccube: Borg cube green"; - cube->entity_modelname = "ccube"; - cube->entity_moduletypeid = cube_enttype; - - // the red cube - cube = new core::Entity(core::Entity::Solid & core::Entity::Static); - cube->entity_shape = core::Entity::Cube; - cube->entity_color = Color(1.0f, 0.0f, 0.0f); - cube->entity_location = Vector3f(16.0f, 16.0f, 0.0f); - cube->entity_name = "cube: Borg cube red"; - cube->entity_modelname = "cube"; - cube->entity_moduletypeid = cube_enttype; - - // the yellow sphere - core::Entity *sphere = new core::Entity(core::Entity::Solid & core::Entity::Static); - sphere->entity_shape = core::Entity::Sphere; - sphere->entity_color = Color(0.8f, 0.8f, 0.0f); - sphere->entity_location = Vector3f(0.0f, 32.0f, 0.0f); - sphere->entity_name ="sphere: The Sphere"; - - // the galactic origin - core::Entity *axis = new core::Entity(core::Entity::Static); - axis->entity_shape = core::Entity::Diamond; - axis->entity_color = Color(1.0f, 1.0f, 0.0f); - axis->entity_location = Vector3f(0, 0, 0); - axis->entity_name = "axis: Origin"; -*/ // read ship model specifications // note: // do not reuse the previous IniFile instance, some gcc versions do not like it @@ -243,7 +210,7 @@ void Game::init() while (shipsini.getline()) { if (shipsini.got_key()) { - if (shipsini.section() == "ship") { + if (shipsini.section().compare("ship") == 0) { if (shipsini.got_key_string("name",shipmodel->shipmodel_name)) { continue; diff --git a/src/math/Makefile.am b/src/math/Makefile.am index 7ea7d66..ab4577e 100644 --- a/src/math/Makefile.am +++ b/src/math/Makefile.am @@ -1,11 +1,9 @@ METASOURCES = AUTO -libmath_la_SOURCES = axis.cc color.cc functions.cc matrix4f.cc plane3f.cc \ - vector3f.cc +libmath_la_SOURCES = axis.cc color.cc functions.cc matrix4f.cc vector3f.cc libmath_la_LDFLAGS = -avoid-version -no-undefined -lm noinst_LTLIBRARIES = libmath.la -noinst_HEADERS = axis.h color.h functions.h mathlib.h matrix4f.h plane3f.h \ - vector3f.h +noinst_HEADERS = axis.h color.h functions.h mathlib.h matrix4f.h vector3f.h INCLUDES = -I$(top_srcdir)/src diff --git a/src/math/mathlib.h b/src/math/mathlib.h index 77ccde5..721a1c0 100644 --- a/src/math/mathlib.h +++ b/src/math/mathlib.h @@ -4,8 +4,8 @@ the terms of the GNU General Public License version 2 */ -#ifndef __INCLUDED_MATHLIB_H__ -#define __INCLUDED_MATHLIB_H__ +#ifndef __INCLUDED_MATH_MATHLIB_H__ +#define __INCLUDED_MATH_MATHLIB_H__ /// this namespace contains mathematical classes and functions /** This is an independent library @@ -13,10 +13,10 @@ namespace math {} #include "math/vector3f.h" -#include "math/plane3f.h" -#include "math/matrix4f.h" #include "math/color.h" +#include "math/axis.h" +#include "math/matrix4f.h" #include "math/functions.h" -#endif // __INCLUDED_MATHLIB_H__ +#endif // __INCLUDED_MATH_MATHLIB_H__ diff --git a/src/model/Makefile.am b/src/model/Makefile.am index 5df1fd7..25985bb 100644 --- a/src/model/Makefile.am +++ b/src/model/Makefile.am @@ -1,9 +1,11 @@ METASOURCES = AUTO -libmodel_la_SOURCES = engine.cc light.cc model.cc vertexarray.cc +libmodel_la_SOURCES = engine.cc light.cc map.cc mapfile.cc model.cc plane.cc \ + tirangle.cc vertexarray.cc libmodel_la_LDFLAGS = -avoid-version -no-undefined -lm noinst_LTLIBRARIES = libmodel.la -noinst_HEADERS = engine.h light.h model.h vertexarray.h +noinst_HEADERS = engine.h light.h map.h mapfile.h model.h plane.h triangle.h \ + vertexarray.h INCLUDES = -I$(top_srcdir)/src diff --git a/src/model/engine.cc b/src/model/engine.cc index 17201d0..1a89ea2 100644 --- a/src/model/engine.cc +++ b/src/model/engine.cc @@ -12,6 +12,12 @@ namespace model { /* ---------- core::Engine ------------------------------------------ */ +Engine::Engine() : + engine_location() +{ + engine_radius = 1.0f; +} + Engine::Engine(math::Vector3f const & location) : engine_location(location) { diff --git a/src/model/engine.h b/src/model/engine.h index fc868c9..5254973 100644 --- a/src/model/engine.h +++ b/src/model/engine.h @@ -8,6 +8,7 @@ #define __INCLUDED_MODEL_ENGINE_H__ #include "math/vector3f.h" +#include "math/color.h" namespace model { @@ -15,15 +16,25 @@ namespace model { class Engine { public: + Engine(); + Engine(math::Vector3f const & location); ~Engine(); inline math::Vector3f const & location() const { return engine_location; } inline float radius() const { return engine_radius; } + + inline unsigned int flare() const { return engine_flare; } + + inline unsigned int texture() const { return render_texture; } math::Vector3f engine_location; float engine_radius; + math::Color engine_color; + unsigned int engine_flare; + + unsigned int render_texture; }; } diff --git a/src/model/light.cc b/src/model/light.cc index 83a0cb8..f9f6b5c 100644 --- a/src/model/light.cc +++ b/src/model/light.cc @@ -8,6 +8,19 @@ namespace model { +Light::Light() : + light_location(), + light_color(1.0f, 1.0f, 1.0f) +{ + light_strobe = false; + light_radius = 1.0f; + light_frequency = 1.0f; + light_offset = 0.0f; + light_time = 0.5f; + light_flare = 0; + render_texture = 0; +} + Light::Light(math::Vector3f const & location, math::Color const & color, bool strobe) : light_location(location), light_color(color) diff --git a/src/model/light.h b/src/model/light.h index e202d21..9605497 100644 --- a/src/model/light.h +++ b/src/model/light.h @@ -16,7 +16,10 @@ namespace model { class Light { public: + Light(); + Light(math::Vector3f const & location, math::Color const & color, bool strobe=false); + ~Light(); inline math::Vector3f const & location() const { return light_location; } @@ -39,7 +42,7 @@ public: inline float time() const { return light_time; } /// flare texture number - inline size_t flare() const { return light_flare; } + inline unsigned int flare() const { return light_flare; } /// render texture number inline size_t texture() const { return render_texture; } @@ -52,7 +55,7 @@ public: float light_offset; float light_time; - size_t light_flare; + unsigned int light_flare; size_t render_texture; }; diff --git a/src/model/map.cc b/src/model/map.cc new file mode 100644 index 0000000..8b70b95 --- /dev/null +++ b/src/model/map.cc @@ -0,0 +1,175 @@ +/* + model/map.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "model/engine.h" +#include "model/light.h" +#include "model/map.h" +#include "model/mapfile.h" +#include "model/vertexarray.h" + +#include "sys/sys.h" + +namespace model { + +Model * Map::load(std::string const &name) +{ + // open the .map file + MapFile mapfile; + + if (!mapfile.open(name)) { + return 0; + } + + Model *model = new Model(name); + Light *light = 0; + Engine *engine = 0; + + unsigned int u; + + while (mapfile.getline()) { + + if (mapfile.got_classname("worldspawn")) { + + } else if (mapfile.got_classname("light")) { + // new light + light = new Light(); + model->add_light(light); + + } else if (mapfile.classname().compare("light") == 0 ) { + // light attributes + + if (mapfile.got_key_vector3f("origin", light->light_location)) { + light->light_location *= SCALE; + continue; + + } else if (mapfile.got_key_color("_color", light->light_color)) { + continue; + + } else if (mapfile.got_key_int("spawnflags", u)) { + light->light_strobe = ((u & 1) == 1); + + } else if (mapfile.got_key_float("light", light->light_radius)) { + light->light_radius /= 100.0f; + + } else if (mapfile.got_key_float("frequency", light->light_frequency)) { + continue; + + } else if (mapfile.got_key_float("offset", light->light_offset)) { + continue; + + } else if (mapfile.got_key_float("time", light->light_time)) { + continue; + + } else if (mapfile.got_key_int("flare", light->light_flare)) { + continue; + + } + + } else if (mapfile.got_classname("target_engine")) { + // new engine + engine = new Engine(); + model->add_engine(engine); + + } else if (mapfile.classname().compare("target_engine") == 0 ) { + // engine attributes + + if (mapfile.got_key_vector3f("origin", engine->engine_location)) { + engine->engine_location *= SCALE; + continue; + + } else if (mapfile.got_key_color("_color", engine->engine_color)) { + continue; + + } else if (mapfile.got_key_float("radius", engine->engine_radius)) { + engine->engine_radius /= 100.0f; + continue; + + } else if (mapfile.got_key_int("flare", engine->engine_flare)) { + continue; + + } + } + } + + mapfile.close(); + + if (VertexArray::instance() && ((mapfile.class_tris.size() + mapfile.class_etris.size()) > 0)) { + + math::Vector3f center = (mapfile.class_minbbox + mapfile.class_maxbbox) / 2; + + model->model_minbbox = mapfile.class_minbbox - center; + model->model_maxbbox = mapfile.class_maxbbox - center; + + model->model_radius = model->model_maxbbox.length(); + + // structural triangles + model->model_first_vertex = VertexArray::instance()->index()/3; + for (std::list<Triangle *>::iterator it = mapfile.class_tris.begin(); it != mapfile.class_tris.end(); it++) { + Triangle *triangle = (*it); + if (!triangle->detail()) { + VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() ); + VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() ); + VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() ); + model->model_vertex_count += 3; + } + } + + // detail triangles + for (std::list<Triangle *>::iterator it = mapfile.class_tris.begin(); it != mapfile.class_tris.end(); it++) { + Triangle *triangle = (*it); + if (triangle->detail()) { + VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() ); + VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() ); + VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() ); + model->model_vertex_countdetail += 3; + } + delete triangle; + } + mapfile.class_tris.clear(); + + // structural etriangles + model->model_first_evertex = VertexArray::instance()->index()/3; + for (std::list<Triangle *>::iterator it = mapfile.class_etris.begin(); it != mapfile.class_etris.end(); it++) { + Triangle *triangle = (*it); + if (!triangle->detail()) { + VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() ); + VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() ); + VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() ); + model->model_evertex_count += 3; + } + } + + // detail etriangles + for (std::list<Triangle *>::iterator it = mapfile.class_etris.begin(); it != mapfile.class_etris.end(); it++) { + Triangle *triangle = (*it); + if (triangle->detail()) { + VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() ); + VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() ); + VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() ); + model->model_evertex_countdetail += 3; + } + delete triangle; + } + mapfile.class_etris.clear(); + + // reposition light and engines + for (std::list<Engine *>::iterator eit = model->model_engine.begin(); eit != model->model_engine.end(); eit++) { + (*eit)->engine_location -= center; + } + + for (std::list<Light *>::iterator lit = model->model_light.begin(); lit != model->model_light.end(); lit++) { + (*lit)->light_location -= center; + } + + } + + con_debug << " maps/" << model->name() << ".map " << mapfile.brushes << " brush " << model->tris() + << " tris (" << model->details() << " detail)" << std::endl; + + return model; +} + +} diff --git a/src/model/map.h b/src/model/map.h new file mode 100644 index 0000000..2db4075 --- /dev/null +++ b/src/model/map.h @@ -0,0 +1,27 @@ +/* + model/map.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_MODEL_MAP_H__ +#define __INCLUDED_MODEL_MAP_H__ + +#include "model/model.h" + +namespace model { + +/// class to load .map files into models +class Map { + +public: + /// load a MAP file from disk + /** @param name name of the model to be loaded + */ + static Model *load(std::string const &name); + +}; + +} + +#endif // __INCLUDED_MODEL_MAP_H__ diff --git a/src/model/mapfile.cc b/src/model/mapfile.cc new file mode 100644 index 0000000..6252886 --- /dev/null +++ b/src/model/mapfile.cc @@ -0,0 +1,546 @@ +/* + filesystem/mapfile.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "filesystem/filesystem.h" +#include "math/mathlib.h" +#include "model/mapfile.h" +#include "model/vertexarray.h" +#include "sys/sys.h" + +#include <sstream> +#include <string> + +namespace model { + +const float MAX_BOUNDS = 16384; +const float MIN_DELTA = 10e-10; + +MapFile::MapFile() {} + +MapFile::~MapFile() {} + +bool MapFile::open(std::string const & name) { + + last_read_was_classname = false; + last_read_was_key = false; + key_current = ""; + value_current = ""; + classname_current = ""; + line_number = 0; + parse_level = 0; + brushes = 0; + + mapfile_name.assign("maps/"); + mapfile_name.append(name); + mapfile_name.append(".map"); + + filesystem::File *f = filesystem::open(mapfile_name.c_str()); + if (!f) { + con_warn << "Could not open " << mapfile_name << std::endl; + return false; + } + + std::string fn = f->path(); + fn.append(f->name()); + filesystem::close(f); + + mapfile_ifs.open(fn.c_str()); + if (!mapfile_ifs.is_open()) { + con_warn << "Could not stream " << fn << "!\n"; + return false; + } + + return true; +} + + +bool MapFile::got_classname() const { + return last_read_was_classname; +} + +bool MapFile::got_classname(const char * classnamelabel) const { + return (last_read_was_classname && (classname_current.compare(classnamelabel) == 0)); +} + +bool MapFile::getline() { + using math::Vector3f; + + char data[1024]; + + last_read_was_classname = false; + last_read_was_key = false; + + key_current = ""; + value_current = ""; + + if (!mapfile_ifs.is_open()) + return false; + + if (mapfile_ifs.getline(data, 1023)) { + line_number++; + std::istringstream linestream(data); + std::string firstword; + + if (linestream >> firstword) { + if (!firstword.size()) { + return true; + + } else if (firstword == "//") { + return true; + + } else if (firstword == "{") { + parse_level++; + + } else if (firstword == "}") { + if ((parse_level == 2) && (classname_current == "worldspawn")) { + // brush + if (VertexArray::instance()) { + // for every face + for (std::vector<Plane *>::iterator face = planes.begin(); face != planes.end(); face++) { + make_brushface((*face)); + } + + // clean planes + for (std::vector<Plane *>::iterator it = planes.begin(); it != planes.end(); it++) { + delete(*it); + } + planes.clear(); + + brushes++; + } + value_current.clear(); + } + parse_level--; + + } else if (parse_level == 1) { + + if (firstword == "\"classname\"") { + classname_current.clear(); + + if (linestream >> classname_current) { + if (classname_current.size() > 2) { + classname_current.erase(0,1); + classname_current.erase(classname_current.size()-1, 1); + last_read_was_classname = true; + } else { + classname_current.clear(); + } + } + + } else if ((firstword.size() > 2) && (firstword[0] == '\"') && (firstword[firstword.size()-1] == '\"')) { + + key_current.assign(firstword); + key_current.erase(0,1); + key_current.erase(key_current.size()-1, 1); + + value_current.clear(); + char c; + while ((linestream.get(c)) && (c != '"')); + while ((linestream.get(c)) && (c != '"')) + value_current += c; + + last_read_was_key = true; + } + + } else if (parse_level == 2) { + + if ((firstword == "(") && (classname_current == "worldspawn")) { + // brush plane + if (VertexArray::instance()) { + Vector3f p1, p2, p3; + std::string tmp; + std::string texture; + int n = 0; + + linestream >> p1; + linestream >> tmp; // ) + linestream >> tmp; // ( + linestream >> p2; + linestream >> tmp; // ) + linestream >> tmp; // ( + linestream >> p3; + linestream >> tmp; // ) + linestream >> texture; + + // 5 numbers (texture alignment?) + for (int i=0; i < 5; i++) + linestream >> tmp; + + // surface flags ? + if (!(linestream >> n)) + n = 0; + + Plane *plane = new Plane(p1, p2, p3); + plane->texture() = texture; + if (n > 0) + plane->detail() = true; + planes.push_back(plane); + } + value_current.clear(); + } + } + } + } else { + + return false; + } + + return true; +} + +void MapFile::make_brushface(Plane *face) +{ + using math::Vector3f; + + // 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 + + // vertex list + std::vector<math::Vector3f *> vl; + + // calculate initial vertices on the bounding box + + // 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))) { + + 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<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))) { + + 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<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 { + + 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<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<Plane *>::iterator pit = planes.begin(); pit != planes.end(); pit++) { + Plane *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; + } + + // 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) { + next = *vl.at(i+1); + } else { + next = *vl.front(); + } + + Vector3f prev; + if (i > 0) { + prev = *vl.at(i-1); + } else { + prev = *vl.back(); + } + + if ((v.x*plane->normal().x + v.y*plane->normal().y + v.z*plane->normal().z +plane->d()) < MIN_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.z*plane->normal().z + plane->d()) > -MIN_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); + + 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; + } + + 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()) > -MIN_DELTA) { + + // 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; + } + + 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 + // unknown textures get hot pink + 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) *= SCALE; + + for (int i=0; i < 3; i++) { + if (class_maxbbox[i] < (*(*it))[i]) + class_maxbbox[i] = (*(*it))[i]; + + if (class_minbbox[i] > (*(*it))[i]) + class_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; + + Vector3f n(face->normal()*-1); + n.normalize(); + + if (!color) { + // evertices will be added to the VertexArray after normal vertices + Triangle *triangle = new Triangle(*(*vn1), *(*vn), *(*v0), n, 0, face->detail()); + class_etris.push_back(triangle); + } else { + Triangle *triangle = new Triangle(*(*vn1), *(*vn), *(*v0), n, color, face->detail()); + class_tris.push_back(triangle); + } + + delete (*vn); + vl.pop_back(); + } + if (color) delete color; + } else { + con_debug << "Unresolved face!\n"; + } + + // clean up the vertex list + for (std::vector<Vector3f *>::iterator it = vl.begin(); it != vl.end(); it++) { + delete(*it); + } + + vl.clear(); +} + +bool MapFile::got_key_string(const char * keylabel, std::string & valuestring) { + if (last_read_was_key && (key_current.compare(keylabel) == 0 )) { + valuestring.assign(value_current); + return true; + } else { + return false; + } +} + +bool MapFile::got_key_vector3f(const char * keylabel, math::Vector3f & v) { + if (last_read_was_key && (key_current.compare(keylabel) == 0 )) { + std::istringstream is(value_current); + float x, y, z; + if ((is >> x) && (is >> y) && (is >> z)) { + v = math::Vector3f(x,y,z); + } else { + v= math::Vector3f(); + } + return true; + } else { + return false; + } +} + +bool MapFile::got_key_float(const char * keylabel, float & f) { + if (last_read_was_key && (key_current.compare(keylabel) == 0 )) { + std::istringstream is(value_current); + if (!(is >> f)) { + f = 0; + } + return true; + } else { + return false; + } +} + +bool MapFile::got_key_int(const char * keylabel, unsigned int & u) +{ + if (last_read_was_key && (key_current.compare(keylabel) == 0 )) { + std::istringstream is(value_current); + if (!(is >> u)) { + u = 0; + } + return true; + } else { + return false; + } +} + +bool MapFile::got_key(const char * keylabel) { + return (last_read_was_key && (key_current.compare(keylabel) == 0 )); +} + +bool MapFile::got_key_angle(const char * keylabel, float & f) { + if (last_read_was_key && (key_current.compare(keylabel) == 0 )) { + std::istringstream is(value_current); + if ((is >> f)) { + f = math::degrees360f(f); + } else { + f = 0; + } + return true; + } else { + return false; + } +} + +bool MapFile::got_key_color(const char * keylabel, math::Color & color) { + if (last_read_was_key && (key_current.compare(keylabel) == 0 )) { + std::istringstream is(value_current); + float r, g, b; + if ((is >> r) && (is >> g) && (is >> b)) { + if ((r > 1) || (g > 1) || (b > 1)) { + r /= 255; g /= 255; b /= 255; + } + color = math::Color(r, g, b); + } else { + color = math::Color(); + } + return true; + } else { + return false; + } +} + +void MapFile::close() +{ + mapfile_ifs.close(); +} + +} // namespace filesystem + diff --git a/src/model/mapfile.h b/src/model/mapfile.h new file mode 100644 index 0000000..4349621 --- /dev/null +++ b/src/model/mapfile.h @@ -0,0 +1,130 @@ +/* + model/mapfile.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_MODEL_MAPFILE_H__ +#define __INCLUDED_MODEL_MAPFILE_H__ + +#include <fstream> +#include <string> +#include <vector> + +#include "model/model.h" +#include "model/plane.h" +#include "model/triangle.h" + +namespace model { + +/// class to parse .map files +class MapFile { + +public: + MapFile(); + ~MapFile(); + + /// open the file for reading + /** the filename will get the "maps/" prefix and ".map" suffix + */ + bool open(std::string const & name); + + /// parse one line, returns false on end-of-file + bool getline(); + + /// current classname + inline std::string classname() const { + return classname_current; + } + + /// current key + inline std::string key() const { + return key_current; + } + + /// current value + inline std::string value() const { + return value_current; + } + + /// true if the last read line contained a new classname + bool got_classname() const; + + /// true if the last read line contained a new classname + bool got_classname(const char*) const; + + /// true if the last read statement was a key=value pair + inline bool got_key() const { + return last_read_was_key; + } + + bool got_key(const char * keylabel); + + /// check if the last read key=value pair matches keylabel and store the value in valuestring + bool got_key_string(const char * keylabel, std::string & valuestring); + + /// check if the last read key=value pair matches keylabel and store the value in color + bool got_key_color(const char * keylabel, math::Color & color); + + /// check if the last read key=value pair matches keylabel and store the value in f + bool got_key_float(const char * keylabel, float & f); + + /// check if the last read key=value pair matches keylabel and store the value in f + bool got_key_int(const char * keylabel, unsigned int & u); + + /// check if the last read key=value pair matches keylabel and store the value in valuestring + bool got_key_angle(const char * keylabel, float & f); + + bool got_key_vector3f(const char * keylabel, math::Vector3f & v); + + + /// return the number of lines read so far + inline unsigned int line() const { + return line_number; + } + + /// return true of the ini file is open for reading + inline bool is_open() { return mapfile_ifs.is_open(); } + + /// current filename + inline std::string const & name() const {return mapfile_name; } + + /// close the file + void close(); + + /// tmp lists with triangles + std::list<Triangle *> class_tris; + + /// tmp lists with entity color triangles + std::list<Triangle *> class_etris; + + math::Vector3f class_maxbbox; + + math::Vector3f class_minbbox; + + unsigned int brushes; + +protected: + /// generate triangles for one plane in the plane list + void make_brushface(Plane *face); + + /// list of planes for the current brush + std::vector<Plane *> planes; + +private: + std::string classname_current; + std::string key_current; + std::string value_current; + + bool last_read_was_key; + bool last_read_was_classname; + + unsigned int parse_level; + unsigned int line_number; + std::ifstream mapfile_ifs; + std::string mapfile_name; +}; + +} + +#endif // __INCLUDED_MODEL_MAPFILE_H__ diff --git a/src/model/model.cc b/src/model/model.cc index 8c853f8..c7af2e3 100644 --- a/src/model/model.cc +++ b/src/model/model.cc @@ -4,53 +4,19 @@ 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 "sys/sys.h" #include "model/model.h" -#include "filesystem/filesystem.h" +#include "model/map.h" +#include "model/vertexarray.h" namespace model { -const float MAX_BOUNDS = 16384; -const float delta = 10e-10; - -/* ---------- core::Triangle --------------------------------------- */ -Triangle::Triangle(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2, math::Vector3f const &n, math::Color *color, bool detail) : - triangle_v0(v0), - triangle_v1(v1), - triangle_v2(v2), - triangle_normal(n) -{ - - if (color) - triangle_color = *color; - else - math::Color(1.0f, 1.0f, 1.0f); - - triangle_detail = detail; -} - -Triangle::~Triangle() -{ -} - -/* ---------- core::Model ------------------------------------------ */ - std::map<std::string, Model*> 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; @@ -58,350 +24,7 @@ Model::Model(std::string const & name) : model_vertex_countdetail = 0; model_evertex_count = 0; model_evertex_countdetail = 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<Plane3f *> planes; - unsigned int level = 0; - char data[1024]; - - std::string class_name; - math::Vector3f class_origin; - float class_angle = 0; - math::Color class_color; - unsigned int class_spawnflags = 0; - float class_light = 0.0f; - float class_radius = 0.0f; - float class_frequency = 1.0f; - float class_offset = 0; - float class_time = 0.0f; - unsigned int class_flare = 0; - bool brush_detail = false; - - 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; - class_light = 0.0f; - class_radius = 0.0f; - class_offset = 0; - class_frequency = 1.0f; - class_time = 0.0f; - class_flare = 0; - brush_detail = false; - } - level ++; - //cout << " LEVEL +" << level << std::endl; - } else if (firstword == "}") { - //cout << " LEVEL -" << level << std::endl; - if ((level == 2) && (class_name == "worldspawn")) { - - if (VertexArray::instance()) { - // for every face - std::vector<Vector3f *>points; - for (std::vector<Plane3f *>::iterator face = planes.begin(); face != planes.end(); face++) { - make_face((*face), planes, brush_detail); - } - } - - // clean planes - for (std::vector<Plane3f *>::iterator it = planes.begin(); it != planes.end(); it++) { - delete(*it); - } - planes.clear(); - brush_detail = false; - - } else if ((level == 1) && (class_name == "target_engine")) { - model::Engine *engine = new Engine(class_origin * model_scale); - if (class_radius) - engine->engine_radius = class_radius / 100.0f; - add_engine(engine); - - } else if ((level == 1) && (class_name == "light")) { - Light *light = new Light(class_origin * model_scale, class_color, (class_spawnflags & 1) == 1); - if (class_light) - light->light_radius = class_light / 100.0f; - else if (class_radius) - light->light_radius = class_radius / 100.0f; - if (class_offset > 0) - light->light_offset = class_offset; - if (class_frequency > 0 ) - light->light_frequency = class_frequency; - if (class_time > 0 ) - light->light_time = class_time; - if (class_flare > 0) - light->light_flare = class_flare; - add_light(light); - } - - 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 == "\"light\"") { - std::string tmp; - char c; - while ((linestream.get(c)) && (c != '"')); - while ((linestream.get(c)) && (c != '"')) - tmp += c; - std::istringstream is(tmp); - is >> class_light; - - } else if (firstword == "\"radius\"") { - std::string tmp; - char c; - while ((linestream.get(c)) && (c != '"')); - while ((linestream.get(c)) && (c != '"')) - tmp += c; - std::istringstream is(tmp); - is >> class_radius; - - } else if (firstword == "\"frequency\"") { - std::string tmp; - char c; - while ((linestream.get(c)) && (c != '"')); - while ((linestream.get(c)) && (c != '"')) - tmp += c; - std::istringstream is(tmp); - is >> class_frequency; - - } else if (firstword == "\"offset\"") { - std::string tmp; - char c; - while ((linestream.get(c)) && (c != '"')); - while ((linestream.get(c)) && (c != '"')) - tmp += c; - std::istringstream is(tmp); - is >> class_offset; - - } else if (firstword == "\"time\"") { - std::string tmp; - char c; - while ((linestream.get(c)) && (c != '"')); - while ((linestream.get(c)) && (c != '"')) - tmp += c; - std::istringstream is(tmp); - is >> class_time; - - } else if (firstword == "\"flare\"") { - std::string tmp; - char c; - while ((linestream.get(c)) && (c != '"')); - while ((linestream.get(c)) && (c != '"')) - tmp += c; - std::istringstream is(tmp); - is >> class_flare; - - } 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; - int n; - - linestream >> p1; - linestream >> tmp; // ) - linestream >> tmp; // ( - linestream >> p2; - linestream >> tmp; // ) - linestream >> tmp; // ( - linestream >> p3; - linestream >> tmp; // ) - linestream >> texture; - - // 5 numbers (texture alignment?) - for (int i=0; i < 5; i++) - linestream >> tmp; - - if (linestream >> n) { - if (n > 0) - brush_detail = true; - } - //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_tris.size() + model_etris.size()) > 0) { - - math::Vector3f center = (model_minbbox + model_maxbbox) / 2; - - model_minbbox -= center; - model_maxbbox -= center; - model_radius = model_maxbbox.length(); - - // structural triangles - model_first_vertex = VertexArray::instance()->index()/3; - for (std::list<Triangle *>::iterator it = model_tris.begin(); it != model_tris.end(); it++) { - Triangle *triangle = (*it); - if (!triangle->detail()) { - VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() ); - VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() ); - VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() ); - model_vertex_count += 3; - } - } - // detail triangles - for (std::list<Triangle *>::iterator it = model_tris.begin(); it != model_tris.end(); it++) { - Triangle *triangle = (*it); - if (triangle->detail()) { - VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() ); - VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() ); - VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() ); - model_vertex_countdetail += 3; - } - delete triangle; - } - model_tris.clear(); - - // structural etriangles - model_first_evertex = VertexArray::instance()->index()/3; - for (std::list<Triangle *>::iterator it = model_etris.begin(); it != model_etris.end(); it++) { - Triangle *triangle = (*it); - if (!triangle->detail()) { - VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() ); - VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() ); - VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() ); - model_evertex_count += 3; - } - } - - // detail etriangles - for (std::list<Triangle *>::iterator it = model_etris.begin(); it != model_etris.end(); it++) { - Triangle *triangle = (*it); - if (triangle->detail()) { - VertexArray::instance()->add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() ); - VertexArray::instance()->add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() ); - VertexArray::instance()->add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() ); - model_evertex_countdetail += 3; - } - delete triangle; - } - model_etris.clear(); - - // reposition light and engines - for (std::list<Engine *>::iterator eit = model_engine.begin(); eit != model_engine.end(); eit++) { - (*eit)->engine_location -= center; - } - - for (std::list<Light *>::iterator lit = model_light.begin(); lit != model_light.end(); lit++) { - (*lit)->light_location -= center; - } - - model_valid = true; - } - - con_debug << " maps/" << name << ".map " << tris() << " triangles (" << details() << " detail)" << std::endl; + model_radius = 0.5f; } Model::~Model() @@ -430,287 +53,6 @@ size_t Model::details() const return ((model_vertex_countdetail + model_evertex_countdetail)/3); } -void Model::make_face(math::Plane3f *face, std::vector<math::Plane3f *> & planes, bool detail) -{ - 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<math::Vector3f *> vl; - - // inital vertices - - // 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<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; - - 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<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; - 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<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; - - // 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<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.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 - // unknown textures get hot pink - 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; - - Vector3f n(face->normal()*-1); - n.normalize(); - - if (!color) { - // evertices will be added to the VertexArray after normal vertices - Triangle *triangle = new Triangle(*(*vn1), *(*vn), *(*v0), n, 0, detail); - model_etris.push_back(triangle); - //VertexArray::add_evertex(*(*vn1), n); - //VertexArray::add_evertex(*(*vn), n); - //VertexArray::add_evertex(*(*v0), n); - //model_evertex_count += 3; - } else { - Triangle *triangle = new Triangle(*(*vn1), *(*vn), *(*v0), n, color, detail); - model_tris.push_back(triangle); - //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<Vector3f *>::iterator it = vl.begin(); it != vl.end(); it++) { - delete(*it); - } - - vl.clear(); - -} - void Model::add_engine(Engine *engine) { model_engine.push_back(engine); @@ -721,6 +63,7 @@ void Model::add_light(Light *light) model_light.push_back(light); } + Model *Model::find(std::string const & name) { std::map<std::string, Model*>::iterator it = registry.find(name); @@ -730,13 +73,15 @@ Model *Model::find(std::string const & name) return (*it).second; } -Model *Model::get(std::string const & name) +Model *Model::load(std::string const & name) { Model *model = find(name); if (!model) { - model = new Model(name); - registry[model->name()] = model; + model = Map::load(name); + if (model) + registry[model->name()] = model; } + return model; } diff --git a/src/model/model.h b/src/model/model.h index f94c170..f13c466 100644 --- a/src/model/model.h +++ b/src/model/model.h @@ -7,48 +7,20 @@ #ifndef __INCLUDED_MODEL_MODEL_H__ #define __INCLUDED_MODEL_MODEL_H__ -#include <vector> -#include <map> +#include <string> #include <list> +#include <map> #include "math/mathlib.h" -#include "math/plane3f.h" #include "model/engine.h" #include "model/light.h" -#include "model/vertexarray.h" +/// classes representing 3D geometry namespace model { -/// a model triangle -class Triangle -{ -public: - /// a new triangle with 3 vertices, a normal, color and a detail flag - Triangle(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2, math::Vector3f const &n, - math::Color *color=0, bool detail=false); - ~Triangle(); - - /// normal of the triangle - inline math::Vector3f const & normal() const { return triangle_normal; } - /// color of the triangle - inline math::Color const & color() const { return triangle_color;} - /// indidcates if this triangle was generated from a detail brush - inline bool detail() const { return triangle_detail; } - - /// triangle vertex 0 - math::Vector3f triangle_v0; - /// triangle vertex 1 - math::Vector3f triangle_v1; - /// triangle vertex 2 - math::Vector3f triangle_v2; - -private: - math::Vector3f triangle_normal; - math::Color triangle_color; - bool triangle_detail; -}; - +/// scaling factor when loading .map geometry +const float SCALE = 1.0f / 1024.0f; /// a 3D model contains a list of faces class Model @@ -97,49 +69,24 @@ public: /// radius inline float radius() const { return model_radius; } - /// the Model registry - static std::map<std::string, Model*> registry; - - /* ---- static functions for the Model registry -------------------- */ - - /// get name model, returns 0 if not found - static Model *find(std::string const & name); - - /// get named model from the registry and load it if necessary - static Model *get(std::string const & name); - - /// clear the model registry - static void clear(); - - /// list the content of the model registry - static void list(); - /// list of Engines std::list<Engine *> model_engine; + /// add an engine to the model + void add_engine(Engine *engine); + /// list of Lights std::list<Light *> model_light; - - -private: - void make_face(math::Plane3f *face, std::vector<math::Plane3f *> & planes, bool detail); - void add_engine(Engine *engine); + /// add a light to the model void add_light(Light *light); std::string model_name; - float model_radius; - float model_scale; - bool model_valid; math::Vector3f model_maxbbox; math::Vector3f model_minbbox; - // tmp lists with triangles - std::list<Triangle *> model_tris; - std::list<Triangle *> model_etris; - size_t model_first_vertex; size_t model_vertex_count; size_t model_vertex_countdetail; @@ -147,6 +94,23 @@ private: size_t model_first_evertex; size_t model_evertex_count; size_t model_evertex_countdetail; + + /* ---- static functions for the Model registry -------------------- */ + + /// the Model registry + static std::map<std::string, Model*> registry; + + /// get name model, returns 0 if not found + static Model *find(std::string const & name); + + /// get named model from the registry and load it if necessary + static Model *load(std::string const & name); + + /// clear the model registry + static void clear(); + + /// list the content of the model registry + static void list(); }; } diff --git a/src/model/plane.cc b/src/model/plane.cc new file mode 100644 index 0000000..2d548ce --- /dev/null +++ b/src/model/plane.cc @@ -0,0 +1,37 @@ +/* + model/plane.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include <string> + +#include "model/plane.h" + +namespace model +{ + +using math::Vector3f; + +Plane::Plane(Vector3f const & point0, Vector3f const &point1, Vector3f const &point2) +{ + plane_detail = false; + + plane_point[0] = point0; + plane_point[1] = point1; + plane_point[2] = point2; + + plane_normal = crossproduct((plane_point[1] - plane_point[0]) , (plane_point[2] - plane_point[0]) ); + pd = -1 * (plane_normal.x * plane_point[0].x + plane_normal.y * plane_point[0].y + plane_normal.z * plane_point[0].z); +} + +Plane::Plane(Plane const & other) +{ + for (size_t i=0; i < 3; i++) + this->plane_point[i] = other.plane_point[i]; + + plane_normal = crossproduct((plane_point[1] - plane_point[0]) , (plane_point[2] - plane_point[0]) ); + pd = -1 * (plane_normal.x * plane_point[0].x + plane_normal.y * plane_point[0].y + plane_normal.z * plane_point[0].z); +} + +} diff --git a/src/model/plane.h b/src/model/plane.h new file mode 100644 index 0000000..5fdc99f --- /dev/null +++ b/src/model/plane.h @@ -0,0 +1,58 @@ +/* + model/plane.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_MODEL_PLANE_H__ +#define __INCLUDED_MODEL_PLANE_H__ + +#include <string> + +#include "math/vector3f.h" + +namespace model +{ + +/** @brief A class representing a plane in 3d space + * all points p(x, y, z) on the plane satisfy the general equation + * x*a() + y*b() + z*c() + d() = 0 + */ +class Plane +{ +public: + /// a plane defined by 3 points in space + Plane(math::Vector3f const & point0, math::Vector3f const &point1, math::Vector3f const &point2); + /// copy constructor + Plane(Plane const & other); + + /// normal of the plane, not normalized to lenght 1 + inline math::Vector3f const & normal() const { return plane_normal; } + /// the points defining the plane. + /// @param i 0 <= i < 3 + inline math::Vector3f const & point(size_t i) const { return plane_point[i]; } + /// plane texture + inline std::string & texture() { return plane_texture; } + /// first parameter of the general equation + inline float a() const { return plane_normal[0]; } + /// second parameter of the general equation + inline float b() const { return plane_normal[1]; } + /// third param of the general equation + inline float c() const { return plane_normal[2]; } + /// fourth parameter of the general equation + inline float d() const { return pd; } + /// indidcates if this plane was generated from a detail brush + inline bool & detail() { return plane_detail; } + + +private: + math::Vector3f plane_point[3]; + math::Vector3f plane_normal; + std::string plane_texture; + bool plane_detail; + float pd; +}; + +} + +#endif // __INCLUDED_MODEL_PLANE_H__ diff --git a/src/model/tirangle.cc b/src/model/tirangle.cc new file mode 100644 index 0000000..fd8ad1a --- /dev/null +++ b/src/model/tirangle.cc @@ -0,0 +1,31 @@ +/* + model/triangle.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "model/triangle.h" + +namespace model +{ + +Triangle::Triangle(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2, math::Vector3f const &n, math::Color *color, bool detail) : + triangle_v0(v0), + triangle_v1(v1), + triangle_v2(v2), + triangle_normal(n) +{ + + if (color) + triangle_color = *color; + else + math::Color(1.0f, 1.0f, 1.0f); + + triangle_detail = detail; +} + +Triangle::~Triangle() +{ +} + +} diff --git a/src/model/triangle.h b/src/model/triangle.h new file mode 100644 index 0000000..58e25e4 --- /dev/null +++ b/src/model/triangle.h @@ -0,0 +1,47 @@ +/* + model/triangle.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_MODEL_TRIANGLE_H__ +#define __INCLUDED_MODEL_TRIANGLE_H__ + +#include "math/color.h" +#include "math/vector3f.h" + +namespace model +{ + +/// a model triangle +class Triangle +{ +public: + /// a new triangle with 3 vertices, a normal, color and a detail flag + Triangle(math::Vector3f const &v0, math::Vector3f const &v1, math::Vector3f const &v2, math::Vector3f const &n, + math::Color *color=0, bool detail=false); + ~Triangle(); + + /// normal of the triangle + inline math::Vector3f const & normal() const { return triangle_normal; } + /// color of the triangle + inline math::Color const & color() const { return triangle_color;} + /// indidcates if this triangle was generated from a detail brush + inline bool detail() const { return triangle_detail; } + + /// triangle vertex 0 + math::Vector3f triangle_v0; + /// triangle vertex 1 + math::Vector3f triangle_v1; + /// triangle vertex 2 + math::Vector3f triangle_v2; + +private: + math::Vector3f triangle_normal; + math::Color triangle_color; + bool triangle_detail; +}; + +} + +#endif // __INCLUDED_MODEL_TRIANGLE_H__ diff --git a/src/render/draw.cc b/src/render/draw.cc index 7342b69..c0e21cc 100644 --- a/src/render/draw.cc +++ b/src/render/draw.cc @@ -260,7 +260,7 @@ void pass_visibility() // load entity models if necessary if (!entity->model() && entity->modelname().size()) { - entity->entity_model = model::Model::get(entity->modelname()); + entity->entity_model = model::Model::load(entity->modelname()); if (!entity->model()) { entity->entity_modelname.clear(); diff --git a/src/render/draw.h b/src/render/draw.h index 65e15ee..c7d68b1 100644 --- a/src/render/draw.h +++ b/src/render/draw.h @@ -7,6 +7,7 @@ #ifndef __INCLUDED_RENDER_DRAW_H__ #define __INCLUDED_RENDER_DRAW_H__ +#include "math/axis.h" #include "math/vector3f.h" namespace render diff --git a/src/render/gl.cc b/src/render/gl.cc index 37ff75f..5540af0 100644 --- a/src/render/gl.cc +++ b/src/render/gl.cc @@ -4,8 +4,6 @@ the terms of the GNU General Public License version 2 */ -#include "GL/gl.h" - #include "render/gl.h" #include "math/matrix4f.h" diff --git a/src/render/gl.h b/src/render/gl.h index 4383e52..fb8bdf2 100644 --- a/src/render/gl.h +++ b/src/render/gl.h @@ -7,8 +7,8 @@ #ifndef __INCLUDED_RENDER_GL_H__ #define __INCLUDED_RENDER_GL_H__ -#include <GL/gl.h> -#include <GL/glu.h> +#include "GL/gl.h" +#include "GL/glu.h" #include "math/vector3f.h" #include "math/matrix4f.h" diff --git a/src/render/render.h b/src/render/render.h index 0bde634..d1f79a7 100644 --- a/src/render/render.h +++ b/src/render/render.h @@ -7,9 +7,13 @@ #ifndef __INCLUDED_RENDER_H__ #define __INCLUDED_RENDER_H__ -#include "GL/gl.h" #include "core/cvar.h" -#include "model/model.h" +#include "model/vertexarray.h" + +#include "render/draw.h" +#include "render/gl.h" +#include "render/text.h" +#include "render/tga.h" namespace render { @@ -19,8 +23,6 @@ namespace render { /// shutdown the render subsystem void shutdown(); - extern GLuint textures[32]; - extern core::Cvar *r_radius; extern core::Cvar *r_wireframe; extern core::Cvar *r_arraysize; @@ -28,9 +30,5 @@ namespace render { extern model::VertexArray *vertexarray; } -#include "render/draw.h" -#include "render/gl.h" -#include "render/text.h" -#include "render/tga.h" #endif // __INCLUDED_RENDER_H__ diff --git a/src/render/tga.h b/src/render/tga.h index 0da65fb..767a871 100644 --- a/src/render/tga.h +++ b/src/render/tga.h @@ -13,10 +13,11 @@ namespace render { +/// a class for loading and saving .tga files class TGA { public: - /// load a TGA image from disk + /// load a TGA image file from disk /** @param filename short path to the filename to be loaded */ static Image *load(const char * filename); |