From b32c086a9b9deed4c34ade6e2447861a9c4bfc46 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Mon, 24 Mar 2008 12:35:48 +0000 Subject: moved the sphere into the vertex array --- src/client/view.cc | 4 +- src/core/model.cc | 82 ++++++++++++++++++++++++++++-- src/core/model.h | 4 ++ src/render/Makefile.am | 4 +- src/render/draw.cc | 87 +++++++++++++++++++++----------- src/render/draw.h | 2 +- src/render/sphere.cc | 132 ------------------------------------------------- src/render/sphere.h | 46 ----------------- 8 files changed, 143 insertions(+), 218 deletions(-) delete mode 100644 src/render/sphere.cc delete mode 100644 src/render/sphere.h (limited to 'src') diff --git a/src/client/view.cc b/src/client/view.cc index 15b8606..a19f98a 100644 --- a/src/client/view.cc +++ b/src/client/view.cc @@ -148,8 +148,8 @@ void draw_status() std::stringstream stats; stats << "fps " << std::setw(6) << fps << "\n"; if (core::application()->connected()) { - stats << "tris " << std::setw(6) << render::Stats::tris << "\n"; - stats << "spheres " << std::setw(3) << render::Stats::spheres << "\n"; + stats << "tris " << std::setw(5) << render::Stats::tris << "\n"; + stats << "quads " << std::setw(5) << render::Stats::quads << "\n"; } draw_text(video::width-CHARWIDTH*12, CHARHEIGHT*2, stats); } diff --git a/src/core/model.cc b/src/core/model.cc index 9f135b4..6a82989 100644 --- a/src/core/model.cc +++ b/src/core/model.cc @@ -19,7 +19,9 @@ namespace core { -const float MAX_BOUNDS = 8192; +//const float MAX_BOUNDS = 8192; +const float MAX_BOUNDS = 16384; + const float delta = 10e-10; /* ---------- core::VertexArray ------------------------------------ */ @@ -36,18 +38,88 @@ size_t VertexArray::vertex_index; void VertexArray::clear() { + vertex_index = 0; + // The VertexArray is only used by the client if (!(Cvar::sv_dedicated && Cvar::sv_dedicated->value())) { memset(vertex, 0, sizeof(vertex)); memset(vertex_color, 0, sizeof(vertex_color)); memset(vertex_normal, 0, sizeof(vertex_normal)); - //memset(evertex, 0, sizeof(evertex)); - //memset(evertex_normal, 0, sizeof(evertex_normal)); + add_sphere(); } - vertex_index = 0; - //evertex_index = 0; + +} + +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 + + } + + con_debug << "SPhere segment count " << count <value()) { + glDrawArrays(gl::LineLoop, index, count); + } else { + glDrawArrays(gl::QuadStrip, index, count); + } + index += count; + Stats::quads += count/2-1; + } + + //gl::pop(); +} + void draw_entity_sphere(core::Entity *entity) { - sphere.sphere_color = entity->color(); - sphere.radius = entity->radius(); - sphere.draw(); - Stats::spheres += 1; + draw_sphere(entity->color(), entity->radius()); } void draw_entity_cube(core::Entity *entity) @@ -198,16 +215,17 @@ void draw_model_engines(core::EntityControlable *entity) void draw_model_radius(core::Entity *entity) { - sphere.sphere_color = entity->color(); - sphere.sphere_color.a = 0.25f; - sphere.radius = entity->model()->radius(); - sphere.draw(); - Stats::spheres += 1; + + math::Color color = entity->color(); + color.a = 0.25f; + + draw_sphere(color, entity->model()->radius()); } void draw_model_shield(core::EntityControlable *entity) { // shield rotation + //gl::push(); gl::rotate(angle, 0.0f, 0.0f, 1.0f ); gl::scale(entity->model()->radius(), entity->model()->radius(), entity->model()->radius()); @@ -237,7 +255,7 @@ void draw_model_shield(core::EntityControlable *entity) gl::end(); - gl::rotate(-angle, 0.0f, 0.0f, 1.0f ); + //gl::pop(); } /* ----- Render passes --------------------------------------------- */ @@ -266,14 +284,6 @@ void draw_pass_default() for (it=core::Entity::registry.begin(); it != core::Entity::registry.end(); it++) { core::Entity *entity = (*it).second; - // 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(); @@ -316,6 +326,15 @@ void draw_pass_model_vertex() core::Entity *entity = (*it).second; + // 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(); + } + + if (test_draw_distance(entity)) { gl::push(); gl::translate(entity->location()); @@ -448,8 +467,7 @@ void draw(math::Vector3f const &eye, math::Vector3f const &target, float seconds gl::enable(GL_COLOR_MATERIAL); // enable color tracking gl::enable(GL_LIGHTING); gl::disable(GL_BLEND); // disbable alpha blending for world polys - - draw_pass_default(); // draw entities without model + gl::disable(GL_RESCALE_NORMAL); glVertexPointer(3, GL_FLOAT, 0, core::VertexArray::vertex); glNormalPointer(GL_FLOAT, 0, core::VertexArray::vertex_normal); @@ -458,25 +476,32 @@ void draw(math::Vector3f const &eye, math::Vector3f const &target, float seconds glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_COLOR_ARRAY); - + draw_pass_model_vertex(); // draw entities with model glDisableClientState(GL_COLOR_ARRAY); + gl::enable(GL_RESCALE_NORMAL); // rescale normals by the transformation matrix scale factor + + draw_pass_default(); // draw entities without model + + gl::disable(GL_RESCALE_NORMAL); draw_pass_model_evertex(); // draw entities with model, vertices with entity color - glDisableClientState(GL_VERTEX_ARRAY); - glDisableClientState(GL_NORMAL_ARRAY); - gl::disable(GL_LIGHTING); gl::enable(GL_BLEND); draw_pass_model_fx(); // draw entity lights and engines gl::enable(GL_LIGHTING); + gl::enable(GL_RESCALE_NORMAL); draw_pass_model_radius(); //draw entity radius + + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_NORMAL_ARRAY); + gl::disable(GL_RESCALE_NORMAL); gl::disable(GL_LIGHTING); gl::disable(GL_COLOR_MATERIAL); // disable color tracking gl::disable(GL_CULL_FACE); // disable culling @@ -484,6 +509,8 @@ void draw(math::Vector3f const &eye, math::Vector3f const &target, float seconds draw_pass_spacegrid(); // draw the blue spacegrid gl::disable(GL_DEPTH_TEST); // disable depth buffer writing + + // GL_BLEND must be enabled for the GUI } } diff --git a/src/render/draw.h b/src/render/draw.h index f9f35f3..b423aad 100644 --- a/src/render/draw.h +++ b/src/render/draw.h @@ -20,7 +20,7 @@ public: static void clear(); static size_t tris; - static size_t spheres; + static size_t quads; }; } diff --git a/src/render/sphere.cc b/src/render/sphere.cc deleted file mode 100644 index d44c9e6..0000000 --- a/src/render/sphere.cc +++ /dev/null @@ -1,132 +0,0 @@ -/* - gl/sphere.cc - This file is part of the Osirion project and is distributed under - the terms of the GNU General Public License version 2 -*/ - -#include "render/sphere.h" -#include "render/render.h" -#include "math/mathlib.h" - -using math::Vector3f; -using math::Color; - -namespace render { - -const int segments = 33; - -Sphere::Sphere(float r) -{ - radius = r; - - // TODO make global sine-cosine lists - sintable = new float[segments]; - costable = new float[segments]; - float d = 2 * M_PI / (segments-1); - - for (int i=0; i < segments; i++) { - sintable[i] = sin( d * (float) i ); - costable[i] = cos ( d * (float) i ); - } -} - -Sphere::~Sphere() -{ - delete[] sintable; - delete[] costable; -} - -Sphere::Sphere(const Sphere &other) -{ - (*this) = other; -} - -Sphere& Sphere::operator=(const Sphere &other) -{ - sphere_color = other.sphere_color; - radius = other.radius; - return (*this); -} - -void Sphere::draw() -{ - using namespace gl; - - float r = radius*sintable[1]; - //float h = radius*costable[1]; - - gl::color(sphere_color); - - /* - // draw top - begin(Polygon); - normal(0, 1, 0); - for (int i = segments-1; i >= 0; i--) { - v = Vector3f(r*costable[i], h, r*sintable[i]); - n = v; - n.normalize(); - normal(n); - vertex(v); - } - end(); - - // draw bottom - begin(Polygon); - normal(0, -1, 0); - for (int i = 0; i< segments; i++) { - //for (int i = segments-1; i >= 0; i--) - v = Vector3f(r*costable[i], -h, r*sintable[i]); - n = v; - n.normalize(); - normal(n); - vertex(v); - } - end(); - */ - - Vector3f v; - Vector3f n; - - // draw body - for (int j=0; j < segments-1; j++) { - r = radius*sintable[j]; - float r1 = radius*sintable[j+1]; - // draw all vertexes - if (r_drawwireframe && r_drawwireframe->value()) { - gl::begin(gl::LineStrip); - } else { - gl::begin(gl::QuadStrip); - } - v = Vector3f(r, 0, radius*costable[j]); - n = v; - n.normalize(); - normal(n); - vertex(v); - - v = Vector3f(r1, 0, radius*costable[j+1]); - n = v; - n.normalize(); - normal(n); - vertex(v); - - for (int i = segments-1; i >= 0; i--) { - v = Vector3f(r*costable[i], r*sintable[i], radius*costable[j]); - n = v; - n.normalize(); - normal(n); - vertex(v); - - v = Vector3f(r1*costable[i], r1*sintable[i], radius*costable[j+1]); - n = v; - n.normalize(); - normal(n); - vertex(v); - } - end(); - - } -} - - -} - diff --git a/src/render/sphere.h b/src/render/sphere.h deleted file mode 100644 index 73a2169..0000000 --- a/src/render/sphere.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - gl/sphere.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_GL_SPHERE_H__ -#define __INCLUDED_GL_SPHERE_H__ - -#include "render/gl.h" - -namespace render { - -/// a drawable OpenGL block shape -class Sphere -{ -public: - /// create a new sphere - Sphere(float r = 1.0f); - - /// copy constructor - Sphere(const Sphere &other); - - /// destructor - ~Sphere(); - - /// assignment operator - Sphere& operator=(const Sphere &other); - - /// radius of the sphere - float radius; - - /// draw the sphere - void draw(); - - /// color - math::Color sphere_color; - -private: - float *sintable; - float *costable; -}; - -} // namespace gl - -#endif // __INCLUDED_GL_SPHERE_H__ -- cgit v1.2.3