From 5c96b74c76b881b1533432a75d1a8cc42ecc5bda Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Wed, 23 Jul 2008 19:51:06 +0000 Subject: fragment renderer --- src/model/vertexarray.cc | 55 ++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 27 deletions(-) (limited to 'src/model/vertexarray.cc') diff --git a/src/model/vertexarray.cc b/src/model/vertexarray.cc index 6a635eb..0562bea 100644 --- a/src/model/vertexarray.cc +++ b/src/model/vertexarray.cc @@ -9,7 +9,8 @@ #include "model/vertexarray.h" #include "sys/sys.h" -namespace model { +namespace model +{ VertexArray *VertexArray::vertex_instance = 0 ; @@ -24,10 +25,10 @@ VertexArray::VertexArray(size_t size) 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 << "^BInitializing vertex array..." << std::endl; con_print << " " << size << " Mb allocated" << std::endl; - + clear(); } @@ -45,46 +46,46 @@ void VertexArray::clear() { vertex_index = 0; vertex_overflow = false; - + 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_texture)); - + 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 ); + 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; float texx, texy; - + int quad_count = 0; - + // add sphere for (int j=0; j < (SPHERESEGMENTS-1) / 2; j++) { - + float r = sintable[j]; float r1 = sintable[j+1]; - + for (int i = 0; i < SPHERESEGMENTS; i++) { v = math::Vector3f(r*costable[i], r*sintable[i], costable[j]); n = v; @@ -95,23 +96,23 @@ void VertexArray::add_sphere() v = math::Vector3f(r1*costable[i], r1*sintable[i], costable[j+1]); n = v; - n.normalize(); + n.normalize(); texx = (float)i/(float)(SPHERESEGMENTS-1); texy = -costable[j+1]/2 + 0.5f; add_vertex(v, n, white, texx, texy); - + quad_count++; } quad_count--; } - + // add inside-out sphere for (int j=0; j < (SPHERESEGMENTS-1) / 2; j++) { - + float r = sintable[j]; float r1 = sintable[j+1]; - + for (int i = SPHERESEGMENTS -1 ; i >= 0; i--) { v = math::Vector3f(r*costable[i], r*sintable[i], costable[j]); @@ -123,14 +124,14 @@ void VertexArray::add_sphere() v = math::Vector3f(r1*costable[i], r1*sintable[i], costable[j+1]); n = v; - n.normalize(); + n.normalize(); texx = 1-(float)i/(float)(SPHERESEGMENTS-1); texy = -costable[j+1]/2 + 0.5f; add_vertex(v, n, white, texx, texy); } } - + delete[] sintable; delete[] costable; } @@ -144,23 +145,23 @@ size_t VertexArray::add_vertex(math::Vector3f const &v, math::Vector3f const &n, } return 0; } - + 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_texture[vertex_index] = tex_x; vertex_texture[vertex_index+1] = tex_y; // reserved vertex_texture[vertex_index+2] = 0; - + vertex_index += 3; - + return 1; } -- cgit v1.2.3