From 82293065b52f5a4e5c4ccde5eade4ebae18014ca Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 3 May 2008 21:04:02 +0000 Subject: liibmodel --- src/Makefile.am | 33 +- src/core/Makefile.am | 8 +- src/core/entity.h | 13 +- src/core/gameinterface.cc | 8 +- src/core/model.cc | 913 ---------------------------------------------- src/core/model.h | 259 ------------- src/model/Makefile.am | 9 + src/model/light.cc | 27 ++ src/model/light.h | 63 ++++ src/model/model.cc | 763 ++++++++++++++++++++++++++++++++++++++ src/model/model.h | 170 +++++++++ src/model/vertexarray.cc | 144 ++++++++ src/model/vertexarray.h | 57 +++ src/render/draw.cc | 18 +- src/render/render.cc | 8 +- src/render/render.h | 4 +- 16 files changed, 1282 insertions(+), 1215 deletions(-) delete mode 100644 src/core/model.cc delete mode 100644 src/core/model.h create mode 100644 src/model/Makefile.am create mode 100644 src/model/light.cc create mode 100644 src/model/light.h create mode 100644 src/model/model.cc create mode 100644 src/model/model.h create mode 100644 src/model/vertexarray.cc create mode 100644 src/model/vertexarray.h (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index 74a31e3..19ad3be 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,33 +4,34 @@ SUFFIXES = .rc .rc.o: windres $< -o $@ -SUBDIRS = math sys filesystem core server render client game +SUBDIRS = sys math filesystem model core render server client game noinst_HEADERS = config.h bin_PROGRAMS = osiriond osirion # dedicated server osiriond_SOURCES = osiriond.cc EXTRA_osiriond_SOURCES = osiriond-res.rc -osiriond_DEPENDENCIES = $(ICON_SERVER) $(top_builddir)/src/game/libgame.la \ - $(top_builddir)/src/sys/libsys.la $(top_builddir)/src/math/libmath.la \ - $(top_builddir)/src/filesystem/libfilesystem.la $(top_builddir)/src/core/libcore.la \ - $(top_builddir)/src/server/libserver.la +osiriond_DEPENDENCIES = $(ICON_SERVER) $(top_builddir)/src/core/libcore.la \ + $(top_builddir)/src/model/libmodel.la $(top_builddir)/src/filesystem/libfilesystem.la \ + $(top_builddir)/src/game/libgame.la $(top_builddir)/src/math/libmath.la \ + $(top_builddir)/src/server/libserver.la $(top_builddir)/src/sys/libsys.la osiriond_LDADD = $(top_builddir)/src/game/libgame.la \ - $(top_builddir)/src/sys/libsys.la $(top_builddir)/src/math/libmath.la \ - $(top_builddir)/src/filesystem/libfilesystem.la $(top_builddir)/src/core/libcore.la \ - $(top_builddir)/src/server/libserver.la $(HOST_LIBS) $(ICON_SERVER) + $(top_builddir)/src/core/libcore.la $(top_builddir)/src/filesystem/libfilesystem.la \ + $(top_builddir)/src/model/libmodel.la $(top_builddir)/src/math/libmath.la \ + $(top_builddir)/src/server/libserver.la $(top_builddir)/src/sys/libsys.la $(HOST_LIBS) $(ICON_SERVER) # client osirion_SOURCES = osirion.cc EXTRA_osirion_SOURCES = osirion-res.rc -osirion_DEPENDENCIES = $(ICON_CLIENT) $(top_builddir)/src/game/libgame.la \ - $(top_builddir)/src/sys/libsys.la $(top_builddir)/src/math/libmath.la \ - $(top_builddir)/src/filesystem/libfilesystem.la $(top_builddir)/src/core/libcore.la \ - $(top_builddir)/src/render/librender.la $(top_builddir)/src/client/libclient.la +osirion_DEPENDENCIES = $(ICON_CLIENT) $(top_builddir)/src/client/libclient.la \ + $(top_builddir)/src/core/libcore.la $(top_builddir)/src/filesystem/libfilesystem.la \ + $(top_builddir)/src/game/libgame.la $(top_builddir)/src/math/libmath.la $(ICON_CLIENT) \ + $(top_builddir)/src/model/libmodel.la $(top_builddir)/src/render/librender.la \ + $(top_builddir)/src/sys/libsys.la osirion_CFLAGS = $(LIBSDL_CFLAGS) $(GL_CFLAGS) $(GLUT_CFLAGS) osirion_LDADD = $(top_builddir)/src/game/libgame.la \ - $(top_builddir)/src/sys/libsys.la $(top_builddir)/src/math/libmath.la \ - $(top_builddir)/src/filesystem/libfilesystem.la $(top_builddir)/src/core/libcore.la \ - $(top_builddir)/src/render/librender.la $(top_builddir)/src/client/libclient.la $(GL_LIBS) $(HOST_LIBS) \ - $(ICON_CLIENT) + $(top_builddir)/src/client/libclient.la $(top_builddir)/src/core/libcore.la \ + $(top_builddir)/src/model/libmodel.la $(top_builddir)/src/filesystem/libfilesystem.la \ + $(top_builddir)/src/math/libmath.la $(top_builddir)/src/render/librender.la \ + $(top_builddir)/src/sys/libsys.la $(GL_LIBS) $(HOST_LIBS) $(ICON_CLIENT) osirion_LDFLAGS = $(LIBSDL_LIBS) diff --git a/src/core/Makefile.am b/src/core/Makefile.am index e9091f2..33b4c44 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -2,11 +2,11 @@ METASOURCES = AUTO INCLUDES = -I$(top_srcdir)/src libcore_la_SOURCES = application.cc commandbuffer.cc core.cc cvar.cc entity.cc \ - func.cc gameconnection.cc gameinterface.cc gameserver.cc model.cc module.cc \ - netclient.cc netconnection.cc netserver.cc player.cc + func.cc gameconnection.cc gameinterface.cc gameserver.cc module.cc netclient.cc \ + netconnection.cc netserver.cc player.cc libcore_la_LDFLAGS = -avoid-version -no-undefined -libcore_la_LIBADD = $(top_builddir)/src/math/libmath.la \ - $(top_builddir)/src/sys/libsys.la $(top_builddir)/src/filesystem/libfilesystem.la +libcore_la_LIBADD = $(top_builddir)/src/filesystem/libfilesystem.la \ + $(top_builddir)/src/math/libmath.la $(top_builddir)/src/sys/libsys.la $(top_builddir)/src/model/libmodel.la noinst_LTLIBRARIES = libcore.la noinst_HEADERS = application.h commandbuffer.h core.h cvar.h entity.h func.h \ diff --git a/src/core/entity.h b/src/core/entity.h index 7e6493a..ba86c53 100644 --- a/src/core/entity.h +++ b/src/core/entity.h @@ -7,16 +7,19 @@ #ifndef __INCLUDED_CORE_ENTITY_H__ #define __INCLUDED_CORE_ENTITY_H__ +#include "model/model.h" +#include "math/axis.h" +#include "math/mathlib.h" + namespace core { + class Entity; class EntityControlable; + } -#include "core/model.h" #include "core/player.h" -#include "math/axis.h" -#include "math/mathlib.h" #include #include @@ -77,7 +80,7 @@ public: inline std::string const & modelname() { return entity_modelname; } /// pointer to the model, is used client-side - inline Model * model() { return entity_model; } + inline model::Model * model() { return entity_model; } /// dirty flag inline bool dirty() const { return entity_dirty; } @@ -145,7 +148,7 @@ public: float entity_radius; std::string entity_name; std::string entity_modelname; - Model *entity_model; + model::Model *entity_model; Shape entity_shape; math::Color entity_color; unsigned int entity_moduletypeid; diff --git a/src/core/gameinterface.cc b/src/core/gameinterface.cc index a739eb2..5789592 100644 --- a/src/core/gameinterface.cc +++ b/src/core/gameinterface.cc @@ -7,20 +7,20 @@ #include #include -#include "sys/sys.h" #include "core/application.h" #include "core/cvar.h" #include "core/func.h" #include "core/gameinterface.h" -#include "core/model.h" #include "core/player.h" +#include "model/model.h" +#include "sys/sys.h" namespace core { void func_list_model(std::string const &args) { - Model::list(); + model::Model::list(); } Player GameInterface::game_localplayer; @@ -90,7 +90,7 @@ void GameInterface::clear() } // remove all models - Model::clear(); + model::Model::clear(); } diff --git a/src/core/model.cc b/src/core/model.cc deleted file mode 100644 index c6f5dde..0000000 --- a/src/core/model.cc +++ /dev/null @@ -1,913 +0,0 @@ -/* - 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 = 16384; -const float delta = 10e-10; - -/* ---------- core::VertexArray ------------------------------------ */ - -VertexArray *VertexArray::vertex_instance = 0 ; - -VertexArray::VertexArray(size_t size) -{ - vertex_instance = this; - vertex_size = size * 1024*1024; // megabytes - vertex_size = vertex_size / sizeof(float); // sizeof float - vertex_size = vertex_size / 4; // 4 arrays - - vertex_vertex = (float *) malloc(vertex_size * sizeof(float)); - vertex_color = (float *) malloc(vertex_size * sizeof(float)); - vertex_normal = (float *) malloc(vertex_size * sizeof(float)); - vertex_texture = (float *) malloc(vertex_size * sizeof(float)); - - con_print << "Initializing vertex array..." << std::endl; - con_debug << " " << size << " Mb allocated" << std::endl; - - clear(); -} - -VertexArray::~VertexArray() -{ - free(vertex_vertex); - free(vertex_normal); - free(vertex_color); - free(vertex_texture); - - vertex_instance = 0 ; -} - -void VertexArray::clear() -{ - vertex_index = 0; - - memset(vertex_vertex, 0, sizeof(vertex_vertex)); - memset(vertex_color, 0, sizeof(vertex_color)); - memset(vertex_normal, 0, sizeof(vertex_normal)); - memset(vertex_texture, 0, sizeof(vertex_normal)); - - add_sphere(); -} - -void VertexArray::add_sphere() -{ - // load sphere vertices into the VertexArray - - // build sin/cos table - float *sintable; - float *costable; - - sintable = new float[SPHERESEGMENTS]; - costable = new float[SPHERESEGMENTS]; - float d = 2 * M_PI / (SPHERESEGMENTS-1); - - for (int i=0; i < SPHERESEGMENTS; i++) { - sintable[i] = sin( d * (float) i ); - costable[i] = cos ( d * (float) i ); - } - - // draw body - math::Color white(1.0f, 1.0f, 1.0f); - math::Vector3f v; - math::Vector3f n; - - int count; - - for (int j=0; j < SPHERESEGMENTS-1; j++) { - float r = sintable[j]; - float r1 = sintable[j+1]; - - // glBegin - v = math::Vector3f(r, 0, costable[j]); - n = v; - n.normalize(); - //normal(n); - //vertex(v); - add_vertex(v, n, white); - - v = math::Vector3f(r1, 0, costable[j+1]); - n = v; - n.normalize(); - //normal(n); - //vertex(v); - add_vertex(v, n, white); - - count =2; - - for (int i = SPHERESEGMENTS-1; i >= 0; i--) { - v = math::Vector3f(r*costable[i], r*sintable[i], costable[j]); - n = v; - n.normalize(); - //normal(n); - //vertex(v); - add_vertex(v, n, white); - - v = math::Vector3f(r1*costable[i], r1*sintable[i], costable[j+1]); - n = v; - n.normalize(); - //normal(n); - //vertex(v); - add_vertex(v, n, white); - count +=2; - } - // glEnd - - } - - delete[] sintable; - delete[] costable; -} - -void VertexArray::add_vertex(math::Vector3f const &v, math::Vector3f const &n, math::Color const &color) { - if (vertex_index + 3 >= vertex_size) { - con_warn << "VertexArray overflow!" << std::endl; - return; - } - - for (int i = 0; i < 3; i ++) { - vertex_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; -} - - -/* ---------- 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::Light ------------------------------------------ */ - -Light::Light(math::Vector3f const & location, math::Color const & color, bool strobe) : - light_location(location), - light_color(color) -{ - light_strobe = strobe; - light_radius = 1.0f; - light_frequency = 1.0f; - light_offset = 0.0f; - light_time = 0.5f; - light_flare = 0; - render_texture = 0; -} - -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_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 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 = 100; - 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 = 100; - 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::vectorpoints; - for (std::vector::iterator face = planes.begin(); face != planes.end(); face++) { - make_face((*face), planes, brush_detail); - } - } - - // clean planes - for (std::vector::iterator it = planes.begin(); it != planes.end(); it++) { - delete(*it); - } - planes.clear(); - brush_detail = false; - - } 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")) { - Light *light = new Light(class_origin * model_scale, class_color, (class_spawnflags & 1) == 1); - light->light_radius = class_light / 100.0f; - 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 == "\"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::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::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::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::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::iterator eit = model_engine.begin(); eit != model_engine.end(); eit++) { - (*eit)->engine_location -= center; - } - - for (std::list::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::~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(); -} - -size_t Model::tris() const -{ - return ((model_vertex_count + model_vertex_countdetail + - model_evertex_count + model_evertex_countdetail)/3); -} - -size_t Model::details() const -{ - return ((model_vertex_countdetail + model_evertex_countdetail)/3); -} - -void Model::make_face(math::Plane3f *face, std::vector & 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 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::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 - // unknown textures get hot pink - 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) { - // 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::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 - if (VertexArray::instance()) - VertexArray::instance()->clear(); -} - -void Model::list() -{ - for (std::map::iterator mit = registry.begin(); mit != registry.end(); mit++) { - con_print << " " << (*mit).second->name() << " " - << (*mit).second->tris() << " triangles (" - << (*mit).second->details() << " detail) " - << (*mit).second->model_engine.size() << " engines " - << (*mit).second->model_light.size() << " lights\n"; - } - con_print << registry.size() << " registered models" << std::endl; - if (VertexArray::instance()) - con_print << "vertex array " << (VertexArray::instance()->index() * 100 / VertexArray::instance()->size()) - << "% used" << std::endl; -} - -} diff --git a/src/core/model.h b/src/core/model.h deleted file mode 100644 index e9b3de4..0000000 --- a/src/core/model.h +++ /dev/null @@ -1,259 +0,0 @@ -/* - 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 -*/ - -#ifndef __INCLUDED_CORE_MODEL_H__ -#define __INCLUDED_CORE_MODEL_H__ - -namespace core -{ -class Model; -} - -#include -#include -#include - -#include "math/mathlib.h" -#include "math/plane3f.h" -#include "core/entity.h" - -namespace core -{ - -/// global vertex array - -const int SPHERESEGMENTS=33; - -class VertexArray -{ -public: - /// Create a new VertexArray with size in Mb - VertexArray(size_t size); - ~VertexArray(); - - void clear(); - - void add_vertex(math::Vector3f const &v, math::Vector3f const &n, math::Color const &color); - - - inline float *vertex() { return vertex_vertex; } - inline float *color() { return vertex_color; } - inline float *normal() { return vertex_normal; } - inline float *texture() { return vertex_texture; } - - inline size_t size() const { return vertex_size; } - inline size_t index() const { return vertex_index; } - - static inline VertexArray *instance() { return vertex_instance; } - -private: - /// model vertices - float *vertex_vertex; - float *vertex_color; - float *vertex_normal; - float *vertex_texture; - - size_t vertex_index; - size_t vertex_size; - - void add_sphere(); - - static VertexArray *vertex_instance; -}; - -/// 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; -}; - -/// a spacecraft engine -class Engine -{ -public: - Engine(math::Vector3f const & location); - ~Engine(); - - inline math::Vector3f const & location() const - { - return engine_location; - } - - math::Vector3f engine_location; -}; - -/// an exterior light -class Light -{ -public: - Light(math::Vector3f const & location, math::Color const & color, bool strobe=false); - ~Light(); - - inline math::Vector3f const & location() const { return light_location; } - - inline math::Color const & color() const { return light_color; }; - - /// true if this is a strobe light - inline bool strobe() const { return light_strobe; } - - /// size if the light, default is 1.0f - inline float radius() const { return light_radius; } - - /// strobe time offset, in seconds - inline float offset() const { return light_offset; } - - /// frequency in strobes per second - inline float frequency() const { return light_frequency; } - - /// fraction a strobe light will be on, default is 0.5f - inline float time() const { return light_time; } - - /// flare texture number - inline size_t flare() const { return light_flare; } - - /// render texture number - inline size_t texture() const { return render_texture; } - - math::Vector3f light_location; - math::Color light_color; - bool light_strobe; - float light_radius; - float light_frequency; - float light_offset; - float light_time; - - size_t light_flare; - - size_t render_texture; -}; - - -/// a 3D model contains a list of faces -class Model -{ -public: - /// load a model from disk - Model(std::string const & name); - ~Model(); - - /// the name of the model - inline std::string const & name() const - { - return model_name; - } - - /// maximum values of the bounding box - inline math::Vector3f const & maxbbox() const { return model_maxbbox; } - - /// minimum values of the bounding box - inline math::Vector3f const & minbbox() const { return model_minbbox; } - - /// first vertex in the global VertexArray - inline size_t first_vertex() const { return model_first_vertex; } - - /// number of structural vertices in this model - inline size_t vertex_structural() const { return model_vertex_count; } - - /// number of detail vertices - inline size_t vertex_detail() const { return model_vertex_countdetail; } - - /// first vertex in the global VertexArray - inline size_t first_evertex() const { return model_first_evertex; } - - /// number of structural evertices in this model - inline size_t evertex_structural() const { return model_evertex_count; } - - /// number of detail evertices in this model - inline size_t evertex_detail() const { return model_evertex_countdetail; } - - /// total number of triangles in this model - size_t tris() const; - - /// number of detail triangles in this model - size_t details() const; - - /// radius - inline float radius() const { return model_radius; } - - /// the Model registry - static std::map 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 model_engine; - - /// list of Lights - std::list model_light; - - - -private: - void make_face(math::Plane3f *face, std::vector & planes, bool detail); - void add_engine(Engine *engine); - 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 model_tris; - std::list model_etris; - - size_t model_first_vertex; - size_t model_vertex_count; - size_t model_vertex_countdetail; - - size_t model_first_evertex; - size_t model_evertex_count; - size_t model_evertex_countdetail; -}; - -} - -#endif // __INCLUDED_RENDER_MODEL_H__ - diff --git a/src/model/Makefile.am b/src/model/Makefile.am new file mode 100644 index 0000000..1490bfc --- /dev/null +++ b/src/model/Makefile.am @@ -0,0 +1,9 @@ +METASOURCES = AUTO + +libmodel_la_SOURCES = light.cc model.cc vertexarray.cc +libmodel_la_LDFLAGS = -avoid-version -no-undefined -lm + +noinst_LTLIBRARIES = libmodel.la +noinst_HEADERS = light.h model.h vertexarray.h + +INCLUDES = -I$(top_srcdir)/src diff --git a/src/model/light.cc b/src/model/light.cc new file mode 100644 index 0000000..83a0cb8 --- /dev/null +++ b/src/model/light.cc @@ -0,0 +1,27 @@ +/* + model/light.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/light.h" + +namespace model { + +Light::Light(math::Vector3f const & location, math::Color const & color, bool strobe) : + light_location(location), + light_color(color) +{ + light_strobe = strobe; + light_radius = 1.0f; + light_frequency = 1.0f; + light_offset = 0.0f; + light_time = 0.5f; + light_flare = 0; + render_texture = 0; +} + +Light::~Light() +{} + +} diff --git a/src/model/light.h b/src/model/light.h new file mode 100644 index 0000000..e202d21 --- /dev/null +++ b/src/model/light.h @@ -0,0 +1,63 @@ +/* + model/light.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_LIGHT_H__ +#define __INCLUDED_MODEL_LIGHT_H__ + +#include "math/vector3f.h" +#include "math/color.h" + +namespace model { + +/// an exterior light +class Light +{ +public: + Light(math::Vector3f const & location, math::Color const & color, bool strobe=false); + ~Light(); + + inline math::Vector3f const & location() const { return light_location; } + + inline math::Color const & color() const { return light_color; }; + + /// true if this is a strobe light + inline bool strobe() const { return light_strobe; } + + /// size if the light, default is 1.0f + inline float radius() const { return light_radius; } + + /// strobe time offset, in seconds + inline float offset() const { return light_offset; } + + /// frequency in strobes per second + inline float frequency() const { return light_frequency; } + + /// fraction a strobe light will be on, default is 0.5f + inline float time() const { return light_time; } + + /// flare texture number + inline size_t flare() const { return light_flare; } + + /// render texture number + inline size_t texture() const { return render_texture; } + + math::Vector3f light_location; + math::Color light_color; + bool light_strobe; + float light_radius; + float light_frequency; + float light_offset; + float light_time; + + size_t light_flare; + + size_t render_texture; +}; + +} + +#endif // __INCLUDED_MODEL_LIGHT_H__ + diff --git a/src/model/model.cc b/src/model/model.cc new file mode 100644 index 0000000..b3c8b03 --- /dev/null +++ b/src/model/model.cc @@ -0,0 +1,763 @@ +/* + 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 "model/model.h" +#include "filesystem/filesystem.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::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_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 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 = 100; + 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 = 100; + 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::vectorpoints; + for (std::vector::iterator face = planes.begin(); face != planes.end(); face++) { + make_face((*face), planes, brush_detail); + } + } + + // clean planes + for (std::vector::iterator it = planes.begin(); it != planes.end(); it++) { + delete(*it); + } + planes.clear(); + brush_detail = false; + + } 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")) { + Light *light = new Light(class_origin * model_scale, class_color, (class_spawnflags & 1) == 1); + light->light_radius = class_light / 100.0f; + 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 == "\"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::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::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::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::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::iterator eit = model_engine.begin(); eit != model_engine.end(); eit++) { + (*eit)->engine_location -= center; + } + + for (std::list::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::~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(); +} + +size_t Model::tris() const +{ + return ((model_vertex_count + model_vertex_countdetail + + model_evertex_count + model_evertex_countdetail)/3); +} + +size_t Model::details() const +{ + return ((model_vertex_countdetail + model_evertex_countdetail)/3); +} + +void Model::make_face(math::Plane3f *face, std::vector & 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 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::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 + // unknown textures get hot pink + 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) { + // 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::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 + if (VertexArray::instance()) + VertexArray::instance()->clear(); +} + +void Model::list() +{ + for (std::map::iterator mit = registry.begin(); mit != registry.end(); mit++) { + con_print << " " << (*mit).second->name() << " " + << (*mit).second->tris() << " triangles (" + << (*mit).second->details() << " detail) " + << (*mit).second->model_engine.size() << " engines " + << (*mit).second->model_light.size() << " lights\n"; + } + con_print << registry.size() << " registered models" << std::endl; + if (VertexArray::instance()) + con_print << "vertex array " << (VertexArray::instance()->index() * 100 / VertexArray::instance()->size()) + << "% used" << std::endl; +} + +} diff --git a/src/model/model.h b/src/model/model.h new file mode 100644 index 0000000..95f84cc --- /dev/null +++ b/src/model/model.h @@ -0,0 +1,170 @@ +/* + model/model.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_MODEL_H__ +#define __INCLUDED_MODEL_MODEL_H__ + +#include +#include +#include + +#include "math/mathlib.h" +#include "math/plane3f.h" +#include "model/light.h" +#include "model/vertexarray.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; +}; + +/// a spacecraft engine +class Engine +{ +public: + Engine(math::Vector3f const & location); + ~Engine(); + + inline math::Vector3f const & location() const + { + return engine_location; + } + + math::Vector3f engine_location; +}; + + + +/// a 3D model contains a list of faces +class Model +{ +public: + /// load a model from disk + Model(std::string const & name); + ~Model(); + + /// the name of the model + inline std::string const & name() const + { + return model_name; + } + + /// maximum values of the bounding box + inline math::Vector3f const & maxbbox() const { return model_maxbbox; } + + /// minimum values of the bounding box + inline math::Vector3f const & minbbox() const { return model_minbbox; } + + /// first vertex in the global VertexArray + inline size_t first_vertex() const { return model_first_vertex; } + + /// number of structural vertices in this model + inline size_t vertex_structural() const { return model_vertex_count; } + + /// number of detail vertices + inline size_t vertex_detail() const { return model_vertex_countdetail; } + + /// first vertex in the global VertexArray + inline size_t first_evertex() const { return model_first_evertex; } + + /// number of structural evertices in this model + inline size_t evertex_structural() const { return model_evertex_count; } + + /// number of detail evertices in this model + inline size_t evertex_detail() const { return model_evertex_countdetail; } + + /// total number of triangles in this model + size_t tris() const; + + /// number of detail triangles in this model + size_t details() const; + + /// radius + inline float radius() const { return model_radius; } + + /// the Model registry + static std::map 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 model_engine; + + /// list of Lights + std::list model_light; + + + +private: + void make_face(math::Plane3f *face, std::vector & planes, bool detail); + void add_engine(Engine *engine); + 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 model_tris; + std::list model_etris; + + size_t model_first_vertex; + size_t model_vertex_count; + size_t model_vertex_countdetail; + + size_t model_first_evertex; + size_t model_evertex_count; + size_t model_evertex_countdetail; +}; + +} + +#endif // __INCLUDED_MODEL_MODEL_H__ + diff --git a/src/model/vertexarray.cc b/src/model/vertexarray.cc new file mode 100644 index 0000000..420e816 --- /dev/null +++ b/src/model/vertexarray.cc @@ -0,0 +1,144 @@ + +/* + model/vertexarray.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#include "math/mathlib.h" +#include "model/vertexarray.h" +#include "sys/sys.h" + +namespace model { + +VertexArray *VertexArray::vertex_instance = 0 ; + +VertexArray::VertexArray(size_t size) +{ + vertex_instance = this; + vertex_size = size * 1024*1024; // megabytes + vertex_size = vertex_size / sizeof(float); // sizeof float + vertex_size = vertex_size / 4; // 4 arrays + + vertex_vertex = (float *) malloc(vertex_size * sizeof(float)); + vertex_color = (float *) malloc(vertex_size * sizeof(float)); + vertex_normal = (float *) malloc(vertex_size * sizeof(float)); + vertex_texture = (float *) malloc(vertex_size * sizeof(float)); + + con_print << "Initializing vertex array..." << std::endl; + con_debug << " " << size << " Mb allocated" << std::endl; + + clear(); +} + +VertexArray::~VertexArray() +{ + free(vertex_vertex); + free(vertex_normal); + free(vertex_color); + free(vertex_texture); + + vertex_instance = 0 ; +} + +void VertexArray::clear() +{ + vertex_index = 0; + + memset(vertex_vertex, 0, sizeof(vertex_vertex)); + memset(vertex_color, 0, sizeof(vertex_color)); + memset(vertex_normal, 0, sizeof(vertex_normal)); + memset(vertex_texture, 0, sizeof(vertex_normal)); + + add_sphere(); +} + +void VertexArray::add_sphere() +{ + // load sphere vertices into the VertexArray + + // build sin/cos table + float *sintable; + float *costable; + + sintable = new float[SPHERESEGMENTS]; + costable = new float[SPHERESEGMENTS]; + float d = 2 * M_PI / (SPHERESEGMENTS-1); + + for (int i=0; i < SPHERESEGMENTS; i++) { + sintable[i] = sin( d * (float) i ); + costable[i] = cos ( d * (float) i ); + } + + // draw body + math::Color white(1.0f, 1.0f, 1.0f); + math::Vector3f v; + math::Vector3f n; + + int count; + + for (int j=0; j < SPHERESEGMENTS-1; j++) { + float r = sintable[j]; + float r1 = sintable[j+1]; + + // glBegin + v = math::Vector3f(r, 0, costable[j]); + n = v; + n.normalize(); + //normal(n); + //vertex(v); + add_vertex(v, n, white); + + v = math::Vector3f(r1, 0, costable[j+1]); + n = v; + n.normalize(); + //normal(n); + //vertex(v); + add_vertex(v, n, white); + + count =2; + + for (int i = SPHERESEGMENTS-1; i >= 0; i--) { + v = math::Vector3f(r*costable[i], r*sintable[i], costable[j]); + n = v; + n.normalize(); + //normal(n); + //vertex(v); + add_vertex(v, n, white); + + v = math::Vector3f(r1*costable[i], r1*sintable[i], costable[j+1]); + n = v; + n.normalize(); + //normal(n); + //vertex(v); + add_vertex(v, n, white); + count +=2; + } + // glEnd + + } + + delete[] sintable; + delete[] costable; +} + +void VertexArray::add_vertex(math::Vector3f const &v, math::Vector3f const &n, math::Color const &color) { + if (vertex_index + 3 >= vertex_size) { + con_warn << "VertexArray overflow!" << std::endl; + return; + } + + for (int i = 0; i < 3; i ++) { + vertex_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; +} + +} + diff --git a/src/model/vertexarray.h b/src/model/vertexarray.h new file mode 100644 index 0000000..6145659 --- /dev/null +++ b/src/model/vertexarray.h @@ -0,0 +1,57 @@ +/* + model/vertexarray.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_VERTEXARRAY_H__ +#define __INCLUDED_MODEL_VERTEXARRAY_H__ + +#include "math/color.h" +#include "math/vector3f.h" + +namespace model { + +const int SPHERESEGMENTS=33; + +/// global vertex array +class VertexArray +{ +public: + /// create a new VertexArray with size in Mb + VertexArray(size_t size); + ~VertexArray(); + + void clear(); + + void add_vertex(math::Vector3f const &v, math::Vector3f const &n, math::Color const &color); + + + inline float *vertex() { return vertex_vertex; } + inline float *color() { return vertex_color; } + inline float *normal() { return vertex_normal; } + inline float *texture() { return vertex_texture; } + + inline size_t size() const { return vertex_size; } + inline size_t index() const { return vertex_index; } + + static inline VertexArray *instance() { return vertex_instance; } + +private: + /// model vertices + float *vertex_vertex; + float *vertex_color; + float *vertex_normal; + float *vertex_texture; + + size_t vertex_index; + size_t vertex_size; + + void add_sphere(); + + static VertexArray *vertex_instance; +}; + +} + +#endif // __INCLUDED_MODEL_VERTEXARRAY_H__ diff --git a/src/render/draw.cc b/src/render/draw.cc index 2938442..5627ac6 100644 --- a/src/render/draw.cc +++ b/src/render/draw.cc @@ -8,7 +8,7 @@ #include #include "core/core.h" -#include "core/model.h" +#include "model/model.h" #include "render/render.h" #include "render/textures.h" #include "render/draw.h" @@ -67,10 +67,10 @@ void draw_sphere(math::Color const & color, float radius) gl::color(color); size_t index = 0; - size_t count = (core::SPHERESEGMENTS+1)*2; + size_t count = (model::SPHERESEGMENTS+1)*2; // draw body - for (int j=0; j < core::SPHERESEGMENTS-1; j++) { + for (int j=0; j < model::SPHERESEGMENTS-1; j++) { glDrawArrays(gl::QuadStrip, index, count); index += count; Stats::quads += count/2-1; @@ -186,13 +186,13 @@ void draw_model_evertex(core::Entity *entity) void draw_model_engines(core::EntityControlable *entity) { - core::Model *model = entity->model(); + model::Model *model = entity->model(); if (model->model_engine.size() && entity->thrust()) { gl::color(1.0f, 0.0f ,0.0f, 1.0f); gl::begin(gl::Lines); - for (std::list::iterator eit = model->model_engine.begin(); eit != model->model_engine.end(); eit++) { + for (std::list::iterator eit = model->model_engine.begin(); eit != model->model_engine.end(); eit++) { math::Vector3f const & v = (*eit)->location(); gl::vertex(v); gl::vertex(v.x - 0.0625f*entity->thrust(), v.y, v.z); @@ -260,13 +260,13 @@ void pass_visibility() // load entity models if necessary if (!entity->model() && entity->modelname().size()) { - entity->entity_model = core::Model::get(entity->modelname()); + entity->entity_model = model::Model::get(entity->modelname()); if (!entity->model()) { entity->entity_modelname.clear(); } else { - for (std::list::iterator lit = entity->model()->model_light.begin(); lit != entity->model()->model_light.end(); lit++) { - core::Light *light = (*lit); + for (std::list::iterator lit = entity->model()->model_light.begin(); lit != entity->model()->model_light.end(); lit++) { + model::Light *light = (*lit); // load flare texture std::stringstream flarename; @@ -408,7 +408,7 @@ void draw_pass_model_lights() if (test_drawfx_distance(entity) && (entity->model()->model_light.size())) { - for (std::list::iterator lit = entity->model()->model_light.begin(); lit != entity->model()->model_light.end(); lit++) { + for (std::list::iterator lit = entity->model()->model_light.begin(); lit != entity->model()->model_light.end(); lit++) { // strobe frequency float t = 1.0f; if ((*lit)->strobe()) diff --git a/src/render/render.cc b/src/render/render.cc index 589dce3..8c4f0fb 100644 --- a/src/render/render.cc +++ b/src/render/render.cc @@ -26,7 +26,9 @@ core::Cvar *r_radius = 0; core::Cvar *r_wireframe = 0; core::Cvar * r_arraysize = 0; -core::VertexArray *vertexarray = 0; +using model::VertexArray; + +VertexArray *vertexarray = 0; bool texture(const char *filename, size_t id) { @@ -72,7 +74,7 @@ void init() if (mb > 256) mb = 256; (*r_arraysize) = (float) mb; - vertexarray = new core::VertexArray(mb); + vertexarray = new VertexArray(mb); r_radius = core::Cvar::get("r_radius", "0", core::Cvar::Archive); r_radius->set_info("[bool] render entity radius"); @@ -95,7 +97,7 @@ void shutdown() } // clear models - core::Model::clear(); + model::Model::clear(); // clear vertex array delete vertexarray; diff --git a/src/render/render.h b/src/render/render.h index ce638bb..0bde634 100644 --- a/src/render/render.h +++ b/src/render/render.h @@ -9,7 +9,7 @@ #include "GL/gl.h" #include "core/cvar.h" -#include "core/model.h" +#include "model/model.h" namespace render { @@ -25,7 +25,7 @@ namespace render { extern core::Cvar *r_wireframe; extern core::Cvar *r_arraysize; - extern core::VertexArray *vertexarray; + extern model::VertexArray *vertexarray; } #include "render/draw.h" -- cgit v1.2.3