rayzor

view 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
line source
1 #include "object.h"
2 #include "vmath.h"
3 #include "min3d.h"
4 #include "logger.h"
6 Object::Object()
7 {
8 }
10 Object::~Object()
11 {
12 }
14 // ---- sphere ----
15 Sphere::Sphere()
16 {
17 }
19 Sphere::~Sphere()
20 {
21 }
23 #define USUB 12
24 #define VSUB 6
26 void Sphere::draw() const
27 {
28 static Vector3 *varr;
29 static unsigned int *iarr;
30 static int num_verts, num_indices;
31 if(!varr) {
32 int i, j;
33 int uverts = USUB;
34 int vverts = VSUB + 1;
36 num_verts = uverts * vverts;
37 varr = new Vector3[num_verts];
39 Vector3 *vptr = varr;
40 for(i=0; i<vverts; i++) {
41 float v = (float)i / (float)VSUB;
42 float phi = v * M_PI;
43 for(j=0; j<uverts; j++) {
44 float u = (float)j / (float)uverts;
45 float theta = u * M_PI * 2.0;
47 float x = sin(theta) * sin(phi);
48 float y = cos(phi);
49 float z = cos(theta) * sin(phi);
51 *vptr++ = Vector3(x, y, z);
52 }
53 }
55 num_indices = USUB * VSUB * 4;
56 iarr = new unsigned int[num_indices];
58 unsigned int *iptr = iarr;
59 for(i=0; i<VSUB; i++) {
60 for(j=0; j<USUB; j++) {
61 iptr[0] = i * uverts + j;
62 iptr[1] = i * uverts + ((j + 1) % uverts);
63 iptr[2] = iptr[1] + uverts;
64 iptr[3] = iptr[0] + uverts;
65 iptr += 4;
66 }
67 }
69 printlog("created sphere mesh\n");
70 printlog(" vertices: %d (%d slices, %d stacks)\n", num_verts, uverts, vverts);
71 printlog(" quads: %d (%d indices, %d usub, %d vsub)\n", USUB * VSUB, num_indices, USUB, VSUB);
72 }
74 m3d_color(1, 1, 1);
76 m3d_vertex_array(&varr->x);
77 m3d_draw_indexed(M3D_QUADS, iarr, num_indices);
78 m3d_vertex_array(0);
79 }