Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2008-05-03 21:04:02 +0000
committerStijn Buys <ingar@osirion.org>2008-05-03 21:04:02 +0000
commit82293065b52f5a4e5c4ccde5eade4ebae18014ca (patch)
tree254f1fa3259f03f033b3d1fd225742a12de167b1 /src/model/vertexarray.cc
parent5388c37bdc040ba50d21ec16a01f399d20592a90 (diff)
liibmodel
Diffstat (limited to 'src/model/vertexarray.cc')
-rw-r--r--src/model/vertexarray.cc144
1 files changed, 144 insertions, 0 deletions
diff --git a/src/model/vertexarray.cc b/src/model/vertexarray.cc
new file mode 100644
index 0000000..420e816
--- /dev/null
+++ b/src/model/vertexarray.cc
@@ -0,0 +1,144 @@
+
+/*
+ model/vertexarray.cc
+ This file is part of the Osirion project and is distributed under
+ the terms of the GNU General Public License version 2
+*/
+
+#include "math/mathlib.h"
+#include "model/vertexarray.h"
+#include "sys/sys.h"
+
+namespace model {
+
+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 / 4; // 4 arrays
+
+ vertex_vertex = (float *) malloc(vertex_size * sizeof(float));
+ vertex_color = (float *) malloc(vertex_size * sizeof(float));
+ vertex_normal = (float *) malloc(vertex_size * sizeof(float));
+ vertex_texture = (float *) malloc(vertex_size * sizeof(float));
+
+ con_print << "Initializing vertex array..." << std::endl;
+ con_debug << " " << size << " Mb allocated" << std::endl;
+
+ clear();
+}
+
+VertexArray::~VertexArray()
+{
+ free(vertex_vertex);
+ free(vertex_normal);
+ free(vertex_color);
+ free(vertex_texture);
+
+ vertex_instance = 0 ;
+}
+
+void VertexArray::clear()
+{
+ vertex_index = 0;
+
+ memset(vertex_vertex, 0, sizeof(vertex_vertex));
+ memset(vertex_color, 0, sizeof(vertex_color));
+ memset(vertex_normal, 0, sizeof(vertex_normal));
+ memset(vertex_texture, 0, sizeof(vertex_normal));
+
+ add_sphere();
+}
+
+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
+
+ }
+
+ delete[] sintable;
+ delete[] costable;
+}
+
+void VertexArray::add_vertex(math::Vector3f const &v, math::Vector3f const &n, math::Color const &color) {
+ if (vertex_index + 3 >= vertex_size) {
+ con_warn << "VertexArray overflow!" << std::endl;
+ return;
+ }
+
+ for (int i = 0; i < 3; i ++) {
+ vertex_vertex[vertex_index+i] = v[i];
+ vertex_normal[vertex_index+i] = n[i];
+ }
+
+ vertex_color[vertex_index] = color.r;
+ vertex_color[vertex_index+1] = color.g;
+ vertex_color[vertex_index+2] = color.b;
+
+ vertex_index += 3;
+}
+
+}
+