diff options
author | Stijn Buys <ingar@osirion.org> | 2010-02-20 21:52:31 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2010-02-20 21:52:31 +0000 |
commit | 745b4e04e5f23a02e5d9b12ebabf38d6dd034136 (patch) | |
tree | 53bf2cbe44067c5900a07d577e8533bf0b2b0796 | |
parent | 60cbb49128dd9561858855886c765515c7e89593 (diff) |
Use interleaved vertex arrays
-rw-r--r-- | src/model/fragment.cc | 2 | ||||
-rw-r--r-- | src/model/model.cc | 10 | ||||
-rw-r--r-- | src/model/vertexarray.cc | 52 | ||||
-rw-r--r-- | src/model/vertexarray.h | 28 | ||||
-rw-r--r-- | src/render/draw.cc | 36 |
5 files changed, 64 insertions, 64 deletions
diff --git a/src/model/fragment.cc b/src/model/fragment.cc index 8e6483f..9071b00 100644 --- a/src/model/fragment.cc +++ b/src/model/fragment.cc @@ -28,7 +28,7 @@ Fragment::Fragment(const Fragment &other) Fragment::Fragment(Type type, const Material *material) { fragment_type = type; - fragment_index = VertexArray::instance()->index() / 3; + fragment_index = VertexArray::instance()->index() / 8; fragment_material = material; fragment_structural_size = 0; fragment_detail_size = 0; diff --git a/src/model/model.cc b/src/model/model.cc index 85bf609..2ac2b50 100644 --- a/src/model/model.cc +++ b/src/model/model.cc @@ -161,16 +161,10 @@ void Model::list() list_model((*mit).second); } - con_print << model_registry.size() << " registered models" << std::endl; + if (VertexArray::instance()) { - - con_print << "vertex array " - << VertexArray::instance()->index() * 3 * sizeof(float) / (1024*1024) << "/" - << VertexArray::instance()->size() * 3 * sizeof(float) / (1024*1024) << "Mb " - << VertexArray::instance()->index() / 3 << "/" << VertexArray::instance()->size() / 3 << " verts " - << (VertexArray::instance()->index() * 100 / VertexArray::instance()->size()) << "% used" - << std::endl; + VertexArray::instance()->info(); } } diff --git a/src/model/vertexarray.cc b/src/model/vertexarray.cc index 057b80d..861090c 100644 --- a/src/model/vertexarray.cc +++ b/src/model/vertexarray.cc @@ -19,25 +19,21 @@ 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 / 3; // 3 arrays - vertex_vertex = (float *) malloc(vertex_size * sizeof(float)); - vertex_normal = (float *) malloc(vertex_size * sizeof(float)); - vertex_texture = (float *) malloc(vertex_size * sizeof(float)); + vertex_size = size * 1024 * 1024; // megabytes + vertex_size = vertex_size / sizeof(float); // sizeof float + + vertex_data = (float *) malloc(vertex_size * sizeof(float)); con_print << "^BInitializing vertex array..." << std::endl; - con_print << " " << size << " Mb allocated" << std::endl; + con_print << " " << sizeof(vertex_data) / (1024 * 1024) << " Mb allocated" << std::endl; clear(); } VertexArray::~VertexArray() { - free(vertex_vertex); - free(vertex_normal); - free(vertex_texture); + free(vertex_data); vertex_instance = 0 ; } @@ -47,9 +43,7 @@ void VertexArray::clear() vertex_index = 0; vertex_overflow = false; - memset(vertex_vertex, 0, sizeof(vertex_vertex)); - memset(vertex_normal, 0, sizeof(vertex_normal)); - memset(vertex_texture, 0, sizeof(vertex_texture)); + memset(vertex_data, 0, sizeof(vertex_data)); add_sphere(); } @@ -136,7 +130,8 @@ void VertexArray::add_sphere() size_t VertexArray::add_vertex(math::Vector3f const &v, math::Vector3f const &n, float tex_x, float tex_y) { - if (vertex_index + 3 >= vertex_size) { + + if (vertex_index + 8 >= vertex_size) { if (!vertex_overflow) { con_warn << "VertexArray overflow!" << std::endl; vertex_overflow = true; @@ -144,20 +139,33 @@ size_t VertexArray::add_vertex(math::Vector3f const &v, math::Vector3f const &n, return 0; } + // GL_T2F_N3F_V3F + + // texture coordinates + vertex_data[vertex_index] = tex_x; + vertex_data[vertex_index+1] = tex_y; + for (int i = 0; i < 3; i ++) { - vertex_vertex[vertex_index+i] = v[i]; - vertex_normal[vertex_index+i] = n[i]; + // normal + vertex_data[vertex_index+2+i] = n[i]; + // vertex coordinates + vertex_data[vertex_index+5+i] = v[i]; } - - vertex_texture[vertex_index] = tex_x; - vertex_texture[vertex_index+1] = tex_y; - vertex_texture[vertex_index+2] = 0; - - vertex_index += 3; + + vertex_index += 8; return 1; } +void VertexArray::info() { + const size_t mbfl = 1024 * 1024 / sizeof(float); + + con_print << " vertex array " + << vertex_index / mbfl << "/" << vertex_size / mbfl << "Mib " + << vertex_index / 8 << "/" << vertex_size / 8 << " verts " + << vertex_index * 100 / vertex_size << "% used" + << std::endl; +} } diff --git a/src/model/vertexarray.h b/src/model/vertexarray.h index 4377f67..2e43f2a 100644 --- a/src/model/vertexarray.h +++ b/src/model/vertexarray.h @@ -25,25 +25,23 @@ public: VertexArray(size_t size); ~VertexArray(); + /// clear array, set memory to 0 void clear(); + + /// print vertex array usage to console + void info(); size_t add_vertex(math::Vector3f const &v, math::Vector3f const &n, float tex_x = 0.0f, float tex_y = 0.0f); - inline float *vertex() { - return vertex_vertex; - } - - inline float *normal() { - return vertex_normal; - } - - inline float *texture() { - return vertex_texture; - } - + /// true when the array is full inline bool overflow() const { return vertex_overflow; } + + /// pointer to model vertices, sequential in GL_T2F_N3F_V3F format + inline const float *ptr() const { + return vertex_data; + } /// size of the vertex array in number of floats (for a single array) inline size_t size() const { @@ -60,10 +58,8 @@ public: } private: - /// model vertices - float *vertex_vertex; - float *vertex_normal; - float *vertex_texture; + /// model vertices, sequential in GL_T2F_N3F_V3F format + float *vertex_data; size_t vertex_index; size_t vertex_size; diff --git a/src/render/draw.cc b/src/render/draw.cc index 4e5e2a9..0016bcf 100644 --- a/src/render/draw.cc +++ b/src/render/draw.cc @@ -561,17 +561,17 @@ void draw_fragment(model::Fragment *fragment, bool draw_details) gl::color(1.0f, 0.0f, 0.0f); - const float s = 0.1f; - - const float *n = core::game()->vertexarray()->normal() + index * 3; - const float *v = core::game()->vertexarray()->vertex() + index * 3; - for (size_t i = 0; i < vertex_count; i++) { - gl::normal(-n[i*3], -n[i*3 +1 ], -n[i*3 +2]); - gl::vertex(v[i*3], v[i*3 +1 ], v[i*3 +2]); + const float s = 0.25f; + const float *n = &core::game()->vertexarray()->ptr()[(index+i) * 8 + 2]; + const float *v = &core::game()->vertexarray()->ptr()[(index+i) * 8 + 5]; - gl::normal(n[i*3], n[i*3 +1 ], n[i*3 +2]); - gl::vertex(v[i*3] + n[i*3] * s, v[i*3 +1] + n[i*3 +1] * s, v[i*3 +2] + n[i*3+2] * s); + gl::normal(-n[0], -n[1], -n[2]); + gl::vertex(v[0], v[1], v[2]); + + gl::normal(n[0], n[1], n[2]); + gl::vertex(v[0] + n[0] * s, v[1] + n[1] * s, v[2] + n[2] * s); + } gl::end(); } @@ -589,7 +589,6 @@ void draw_model_fragments(model::Model *model, bool use_light = true; // gl::disable(GL_LIGHTING) is set bool use_texture = false; // texturing bool use_env = false; // environment mapping - //bool use_color_array = true; // glEnableClientState(GL_COLOR_ARRAY) is set // TODO this should probably be initialized somewhere else glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_REFLECTION_MAP); @@ -1242,17 +1241,20 @@ void draw(float seconds) } // set vertex array pointers - glVertexPointer(3, GL_FLOAT, 0, core::game()->vertexarray()->vertex()); - glNormalPointer(GL_FLOAT, 0, core::game()->vertexarray()->normal()); - glTexCoordPointer(3, GL_FLOAT, 0, core::game()->vertexarray()->texture()); -// glColorPointer(3, GL_FLOAT, sizeof(float) * (model::VERTEXSIZE - 3), core::game()->vertexarray()->color()); + glInterleavedArrays(GL_T2F_N3F_V3F, 0, core::game()->vertexarray()->ptr()); + + /* + // this doesnt work + glTexCoordPointer(2, GL_FLOAT, 6 * sizeof(float), core::game()->vertexarray()->ptr()); + glNormalPointer(GL_FLOAT, 5 * sizeof(float), &core::game()->vertexarray()->ptr()[2]); + glVertexPointer(3, GL_FLOAT, 5 * sizeof(float), &core::game()->vertexarray()->ptr()[5]); + */ // enable vertex arrays - glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); -// glDisableClientState(GL_COLOR_ARRAY); - + glEnableClientState(GL_VERTEX_ARRAY); + gl::disable(GL_DEPTH_TEST); gl::depthmask(GL_FALSE); // disable depth buffer writing |