From b4f77d62eae0b0a6781f853b0f8f85b29a088007 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 24 Mar 2008 14:54:25 +0000 Subject: support for detail brushes --- src/core/model.cc | 126 +++++++++++++++++++++++++++++++++++++++++------------ src/core/model.h | 28 ++++++++---- src/render/draw.cc | 47 +++++++++++--------- 3 files changed, 144 insertions(+), 57 deletions(-) diff --git a/src/core/model.cc b/src/core/model.cc index 6a82989..a0aa489 100644 --- a/src/core/model.cc +++ b/src/core/model.cc @@ -117,7 +117,6 @@ void VertexArray::add_sphere() } - con_debug << "SPhere segment count " << count <points; for (std::vector::iterator face = planes.begin(); face != planes.end(); face++) { - make_face((*face), planes); + make_face((*face), planes, brush_detail); } } @@ -282,6 +285,7 @@ Model::Model(std::string const & name) : delete(*it); } planes.clear(); + brush_detail = false; } else if ((level == 1) && (class_name == "target_engine")) { //con_debug << " engine at " << class_origin << "\n"; @@ -376,6 +380,13 @@ Model::Model(std::string const & name) : linestream >> tmp; // ) linestream >> texture; + // 5 numbers (texture alignment?) + for (int i=0; i < 5; i++) + linestream >> tmp; + + if (linestream >> tmp) + brush_detail = true; + //cout << data << std::endl; //cout << "(" << p1 << ") (" << p2 << ") (" << p3 << ") " << texture << std::endl; @@ -392,29 +403,76 @@ Model::Model(std::string const & name) : ifs.close(); - model_first_evertex = VertexArray::vertex_index/3; - if (model_etris.size()) { + 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::vertex_index/3; + for (std::list::iterator it = model_tris.begin(); it != model_tris.end(); it++) { + Triangle *triangle = (*it); + if (!triangle->detail()) { + VertexArray::add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() ); + VertexArray::add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() ); + VertexArray::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::add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() ); + VertexArray::add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() ); + VertexArray::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::vertex_index/3; + for (std::list::iterator it = model_etris.begin(); it != model_etris.end(); it++) { + Triangle *triangle = (*it); + if (!triangle->detail()) { + VertexArray::add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() ); + VertexArray::add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() ); + VertexArray::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); - VertexArray::add_vertex(triangle->triangle_v0, triangle->normal(), triangle->color() ); - VertexArray::add_vertex(triangle->triangle_v1, triangle->normal(), triangle->color() ); - VertexArray::add_vertex(triangle->triangle_v2, triangle->normal(), triangle->color() ); - model_evertex_count += 3; + if (triangle->detail()) { + VertexArray::add_vertex(triangle->triangle_v0-center, triangle->normal(), triangle->color() ); + VertexArray::add_vertex(triangle->triangle_v1-center, triangle->normal(), triangle->color() ); + VertexArray::add_vertex(triangle->triangle_v2-center, triangle->normal(), triangle->color() ); + model_evertex_countdetail += 3; + } delete triangle; } model_etris.clear(); - } - if ((model_vertex_count + model_evertex_count) > 0 ) { - if (model_maxbbox.lengthsquared() > model_minbbox.lengthsquared()) { - model_radius = model_maxbbox.length(); - } else { - model_radius = model_minbbox.length(); + // reposition light and engines + for (std::list::iterator eit = model_engine.begin(); eit != model_engine.end(); eit++) { + (*eit)->engine_location -= center; } - model_valid = true; + + 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 " << model_face.size() << " polygons\n"; - con_debug << " maps/" << name << ".map " << tris() << " triangles" << std::endl; + + con_debug << " maps/" << name << ".map " << tris() << " triangles (" << details() << " detail)" << std::endl; } Model::~Model() @@ -432,7 +490,18 @@ Model::~Model() model_light.clear(); } -void Model::make_face(math::Plane3f *face, std::vector & planes) +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; @@ -681,17 +750,19 @@ void Model::make_face(math::Plane3f *face, std::vector & planes if (!color) { // evertices will be added to the VertexArray after normal vertices - Triangle *triangle = new Triangle(*(*vn1), *(*vn), *(*v0), n, 0, false); + 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 { - VertexArray::add_vertex(*(*vn1), n, *color); - VertexArray::add_vertex(*(*vn), n, *color); - VertexArray::add_vertex(*(*v0), n, *color); - model_vertex_count += 3; + 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); @@ -756,12 +827,13 @@ void Model::list() { for (std::map::iterator mit = registry.begin(); mit != registry.end(); mit++) { con_print << " " << (*mit).second->name() << " " - << ((*mit).second->vertex_count() + (*mit).second->evertex_count())/3 << " triangles " + << (*mit).second->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; - con_print << "vertex array " << VertexArray::vertex_index << "/" << VERTEXARRAYSIZE << " used" << std::endl; + con_print << "vertex array " << VertexArray::vertex_index/3 << "/" << VERTEXARRAYSIZE/3 << " used" << std::endl; } } diff --git a/src/core/model.h b/src/core/model.h index 4fc4d97..958acc0 100644 --- a/src/core/model.h +++ b/src/core/model.h @@ -87,7 +87,6 @@ public: return engine_location; } -private: math::Vector3f engine_location; }; @@ -104,7 +103,6 @@ public: inline bool strobe() const { return light_strobe; } -private: math::Vector3f light_location; math::Color light_color; bool light_strobe; @@ -134,17 +132,26 @@ public: /// first vertex in the global VertexArray inline size_t first_vertex() const { return model_first_vertex; } - /// number of vertexes in this model - inline size_t vertex_count() const { return model_vertex_count; } + /// 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 vertexes in this model - inline size_t evertex_count() const { return model_evertex_count; } + /// number of structural evertices in this model + inline size_t evertex_structural() const { return model_evertex_count; } - /// number of triangles in this mdel - inline size_t tris() const { return (model_vertex_count + model_evertex_count)/3; } + /// 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; } @@ -175,7 +182,7 @@ public: private: - void make_face(math::Plane3f *face, std::vector & planes); + void make_face(math::Plane3f *face, std::vector & planes, bool detail); void add_engine(Engine *engine); void add_light(Light *light); @@ -194,8 +201,11 @@ private: 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; }; } diff --git a/src/render/draw.cc b/src/render/draw.cc index 55f173c..c3aed94 100644 --- a/src/render/draw.cc +++ b/src/render/draw.cc @@ -37,6 +37,22 @@ const float drawfxdistance = 32.0f; math::Vector3f camera_target; float angle = 0; + +/* ----- Distance test functions ----------------------------------- */ + +inline bool test_draw_distance(core::Entity *entity) +{ + return (entity->model() && + (math::distancesquared(camera_target, entity->location()) <= (drawdistance*drawdistance*entity->model()->radius()))); +} + +inline bool test_drawfx_distance(core::Entity *entity) +{ + return (entity->model() && + (math::distancesquared(camera_target, entity->location()) <= (drawfxdistance*drawfxdistance*entity->model()->radius()))); +} + + /* ----- Default Entity shapes ------------------------------------- */ void draw_sphere(math::Color const & color, float radius) @@ -140,10 +156,13 @@ void draw_entity_axis(core::Entity *entity) void draw_model_vertex(core::Entity *entity) { + size_t count = entity->model()->vertex_structural(); + if (test_drawfx_distance(entity)) + count += entity->model()->vertex_detail(); + // draw model vertices - if (entity->model()->vertex_count()) { + if (count) { size_t index = entity->model()->first_vertex(); - size_t count = entity->model()->vertex_count(); if (r_drawwireframe && r_drawwireframe->value()) { glDrawArrays(gl::LineLoop, index, count); @@ -157,10 +176,13 @@ void draw_model_vertex(core::Entity *entity) void draw_model_evertex(core::Entity *entity) { + size_t count = entity->model()->evertex_structural(); + if (test_drawfx_distance(entity)) + count += entity->model()->evertex_detail(); + // draw model evertices - if (entity->model()->evertex_count()) { + if (count) { size_t index = entity->model()->first_evertex(); - size_t count = entity->model()->evertex_count(); render::gl::color(entity->color()); @@ -260,23 +282,6 @@ void draw_model_shield(core::EntityControlable *entity) /* ----- Render passes --------------------------------------------- */ - -inline bool test_draw_distance(core::Entity *entity) -{ - if (!entity->model()) - return false; - else - return (math::distancesquared(camera_target, entity->location()) <= (drawdistance*drawdistance*entity->model()->radius())); -} - -inline bool test_drawfx_distance(core::Entity *entity) -{ - if (!entity->model()) - return false; - else - return (math::distancesquared(camera_target, entity->location()) <= (drawfxdistance*drawfxdistance*entity->model()->radius())); -} - /* Draw entities without model */ void draw_pass_default() { -- cgit v1.2.3