From 745b4e04e5f23a02e5d9b12ebabf38d6dd034136 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 20 Feb 2010 21:52:31 +0000 Subject: Use interleaved vertex arrays --- src/model/fragment.cc | 2 +- src/model/model.cc | 10 ++-------- src/model/vertexarray.cc | 52 ++++++++++++++++++++++++++++-------------------- src/model/vertexarray.h | 28 +++++++++++--------------- 4 files changed, 45 insertions(+), 47 deletions(-) (limited to 'src/model') 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; -- cgit v1.2.3