rayzor

annotate src/object.cc @ 9:70e332156d02

moving along
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 10 Apr 2014 02:31:31 +0300
parents a68dbf80d547
children d94a69933a71
rev   line source
nuclear@1 1 #include "object.h"
nuclear@1 2 #include "vmath.h"
nuclear@1 3 #include "min3d.h"
nuclear@9 4 #include "logger.h"
nuclear@1 5
nuclear@1 6 Object::Object()
nuclear@1 7 {
nuclear@1 8 }
nuclear@1 9
nuclear@1 10 Object::~Object()
nuclear@1 11 {
nuclear@1 12 }
nuclear@1 13
nuclear@1 14 // ---- sphere ----
nuclear@1 15 Sphere::Sphere()
nuclear@1 16 {
nuclear@1 17 }
nuclear@1 18
nuclear@1 19 Sphere::~Sphere()
nuclear@1 20 {
nuclear@1 21 }
nuclear@1 22
nuclear@9 23 #define USUB 12
nuclear@9 24 #define VSUB 6
nuclear@1 25
nuclear@1 26 void Sphere::draw() const
nuclear@1 27 {
nuclear@1 28 static Vector3 *varr;
nuclear@9 29 static unsigned int *iarr;
nuclear@9 30 static int num_verts, num_indices;
nuclear@1 31 if(!varr) {
nuclear@9 32 int i, j;
nuclear@1 33 int uverts = USUB;
nuclear@1 34 int vverts = VSUB + 1;
nuclear@9 35
nuclear@1 36 num_verts = uverts * vverts;
nuclear@1 37 varr = new Vector3[num_verts];
nuclear@1 38
nuclear@1 39 Vector3 *vptr = varr;
nuclear@9 40 for(i=0; i<vverts; i++) {
nuclear@9 41 float v = (float)i / (float)VSUB;
nuclear@9 42 float phi = v * M_PI;
nuclear@9 43 for(j=0; j<uverts; j++) {
nuclear@9 44 float u = (float)j / (float)uverts;
nuclear@9 45 float theta = u * M_PI * 2.0;
nuclear@1 46
nuclear@9 47 float x = sin(theta) * sin(phi);
nuclear@9 48 float y = cos(phi);
nuclear@9 49 float z = cos(theta) * sin(phi);
nuclear@1 50
nuclear@1 51 *vptr++ = Vector3(x, y, z);
nuclear@1 52 }
nuclear@1 53 }
nuclear@9 54
nuclear@9 55 num_indices = USUB * VSUB * 4;
nuclear@9 56 iarr = new unsigned int[num_indices];
nuclear@9 57
nuclear@9 58 unsigned int *iptr = iarr;
nuclear@9 59 for(i=0; i<VSUB; i++) {
nuclear@9 60 for(j=0; j<USUB; j++) {
nuclear@9 61 iptr[0] = i * uverts + j;
nuclear@9 62 iptr[1] = i * uverts + ((j + 1) % uverts);
nuclear@9 63 iptr[2] = iptr[1] + uverts;
nuclear@9 64 iptr[3] = iptr[0] + uverts;
nuclear@9 65 iptr += 4;
nuclear@9 66 }
nuclear@9 67 }
nuclear@9 68
nuclear@9 69 printlog("created sphere mesh\n");
nuclear@9 70 printlog(" vertices: %d (%d slices, %d stacks)\n", num_verts, uverts, vverts);
nuclear@9 71 printlog(" quads: %d (%d indices, %d usub, %d vsub)\n", USUB * VSUB, num_indices, USUB, VSUB);
nuclear@1 72 }
nuclear@1 73
nuclear@9 74 m3d_color(1, 1, 1);
nuclear@9 75
nuclear@5 76 m3d_vertex_array(&varr->x);
nuclear@9 77 m3d_draw_indexed(M3D_QUADS, iarr, num_indices);
nuclear@5 78 m3d_vertex_array(0);
nuclear@1 79 }