From 772338f8cd62594d222d48466f558b002eb1efb8 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 23 Mar 2008 19:16:12 +0000 Subject: model pointer caching --- src/render/draw.cc | 100 ++++++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 48 deletions(-) (limited to 'src/render') diff --git a/src/render/draw.cc b/src/render/draw.cc index 77f8457..8a03e40 100644 --- a/src/render/draw.cc +++ b/src/render/draw.cc @@ -121,12 +121,12 @@ void draw_entity_axis(core::Entity *entity) /* ----- Entity models --------------------------------------------- */ -void draw_model_vertex(core::Model *model, core::Entity *entity) +void draw_model_vertex(core::Entity *entity) { // draw model vertices - if (model->vertex_count()) { - size_t index = model->first_vertex(); - size_t count = model->vertex_count(); + if (entity->model()->vertex_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); @@ -138,12 +138,12 @@ void draw_model_vertex(core::Model *model, core::Entity *entity) } } -void draw_model_evertex(core::Model *model, core::Entity *entity) +void draw_model_evertex(core::Entity *entity) { // draw model evertices - if (model->evertex_count()) { - size_t index = model->first_evertex(); - size_t count = model->evertex_count(); + if (entity->model()->evertex_count()) { + size_t index = entity->model()->first_evertex(); + size_t count = entity->model()->evertex_count(); render::gl::color(entity->color()); glVertexPointer(3, GL_FLOAT, 0, core::VertexArray::evertex); @@ -162,8 +162,10 @@ void draw_model_evertex(core::Model *model, core::Entity *entity) } } -void draw_model_lights(core::Model *model, core::Entity *entity) +void draw_model_lights(core::Entity *entity) { + core::Model *model = entity->model(); + if (model->model_light.size()) { glPointSize(10); gl::begin(gl::Points); @@ -179,8 +181,10 @@ void draw_model_lights(core::Model *model, core::Entity *entity) } } -void draw_model_engines(core::Model *model, core::EntityControlable *entity) +void draw_model_engines(core::EntityControlable *entity) { + core::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); @@ -195,20 +199,20 @@ void draw_model_engines(core::Model *model, core::EntityControlable *entity) } -void draw_model_radius(core::Model *model, core::Entity *entity) +void draw_model_radius(core::Entity *entity) { sphere.sphere_color = entity->color(); sphere.sphere_color.a = 0.25f; - sphere.radius = model->radius(); + sphere.radius = entity->model()->radius(); sphere.draw(); Stats::spheres += 1; } -void draw_model_shield(core::Model *model, core::EntityControlable *entity) +void draw_model_shield(core::EntityControlable *entity) { // shield rotation gl::rotate(angle, 0.0f, 0.0f, 1.0f ); - gl::scale(model->radius(), model->radius(), model->radius()); + gl::scale(entity->model()->radius(), entity->model()->radius(), entity->model()->radius()); // draw the shield gl::color(math::Color(0.0f, 1.0f ,0.0f)); @@ -242,14 +246,20 @@ void draw_model_shield(core::Model *model, core::EntityControlable *entity) /* ----- Render passes --------------------------------------------- */ -inline bool test_draw_distance(core::Model *model, core::Entity *entity) +inline bool test_draw_distance(core::Entity *entity) { - return (model && (math::distancesquared(camera_target, entity->location()) <= (drawdistance*drawdistance*model->radius()))); + if (!entity->model()) + return false; + else + return (math::distancesquared(camera_target, entity->location()) <= (drawdistance*drawdistance*entity->model()->radius())); } -inline bool test_drawfx_distance(core::Model *model, core::Entity *entity) +inline bool test_drawfx_distance(core::Entity *entity) { - return (model && (math::distancesquared(camera_target, entity->location()) <= (drawfxdistance*drawfxdistance*model->radius()))); + if (!entity->model()) + return false; + else + return (math::distancesquared(camera_target, entity->location()) <= (drawfxdistance*drawfxdistance*entity->model()->radius())); } /* Draw entities without model */ @@ -259,7 +269,16 @@ void draw_pass_default() for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { core::Entity *entity = (*it).second; - if (!entity->modelname().size()) { + // load entity models if necessary + if (!entity->model() && entity->modelname().size()) { + entity->entity_model = core::Model::get(entity->modelname()); + + if (!entity->model()) + entity->entity_modelname.clear(); + } + + // draw entities without model + if (!entity->model()) { gl::push(); gl::translate(entity->location()); gl::rotate(entity->direction(), 0.0f, 0.0f, 1.0f ); @@ -305,19 +324,15 @@ void draw_pass_model_vertex() std::map::iterator it; for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { - core::Entity *entity = (*it).second; - core::Model *model = 0; - if (entity->modelname().size()) { - model = core::Model::get(entity->modelname()); - } + core::Entity *entity = (*it).second; - if (test_draw_distance(model, entity)) { + if (test_draw_distance(entity)) { gl::push(); gl::translate(entity->location()); gl::rotate(entity->direction(), 0.0f, 0.0f, 1.0f ); - draw_model_vertex(model, entity); + draw_model_vertex(entity); gl::pop(); } @@ -339,19 +354,15 @@ void draw_pass_model_evertex() std::map::iterator it; for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { - core::Entity *entity = (*it).second; - core::Model *model = 0; - if (entity->modelname().size()) { - model = core::Model::get(entity->modelname()); - } + core::Entity *entity = (*it).second; - if (test_draw_distance(model, entity)) { + if (test_draw_distance(entity)) { gl::push(); gl::translate(entity->location()); gl::rotate(entity->direction(), 0.0f, 0.0f, 1.0f ); - draw_model_evertex(model, entity); + draw_model_evertex(entity); gl::pop(); } @@ -365,24 +376,20 @@ void draw_pass_model_evertex() void draw_pass_model_fx() { for (std::map::iterator it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { - core::Entity *entity = (*it).second; - core::Model *model = 0; - if (entity->modelname().size()) { - model = core::Model::get(entity->modelname()); - } + core::Entity *entity = (*it).second; - if (test_drawfx_distance(model, entity) && (model->model_light.size() || (entity->type() == core::Entity::Controlable))) { + if (test_drawfx_distance(entity)) { gl::push(); gl::translate(entity->location()); gl::rotate(entity->direction(), 0.0f, 0.0f, 1.0f ); - draw_model_lights(model, entity); + draw_model_lights(entity); if (entity->type() == core::Entity::Controlable) { - draw_model_engines(model, (core::EntityControlable *)entity); - draw_model_shield(model, (core::EntityControlable *)entity); + draw_model_engines((core::EntityControlable *)entity); + draw_model_shield((core::EntityControlable *)entity); } gl::pop(); } @@ -395,17 +402,14 @@ void draw_pass_model_radius() return; for (std::map::iterator it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { + core::Entity *entity = (*it).second; - core::Model *model = 0; - if (entity->modelname().size()) { - model = core::Model::get(entity->modelname()); - } - if (test_draw_distance(model, entity)) { + if (test_draw_distance(entity)) { gl::push(); gl::translate(entity->location()); - draw_model_radius(model, entity); + draw_model_radius(entity); gl::pop(); } -- cgit v1.2.3