rayzor

view src/object.cc @ 12:d94a69933a71

lots of stuff, can't remember
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 12 Apr 2014 23:28:24 +0300
parents 70e332156d02
children a9a948809c6f
line source
1 #include "object.h"
2 #include "vmath.h"
3 #include "min3d.h"
4 #include "logger.h"
6 Object::Object()
7 {
8 type = NODE_OBJECT;
9 }
11 Object::~Object()
12 {
13 }
15 void Object::pre_draw() const
16 {
17 m3d_matrix_mode(M3D_MODELVIEW);
18 m3d_push_matrix();
19 m3d_mult_matrix(get_matrix()[0]);
20 }
22 void Object::post_draw() const
23 {
24 m3d_pop_matrix();
25 }
27 // ---- sphere ----
28 Sphere::Sphere()
29 {
30 }
32 Sphere::~Sphere()
33 {
34 }
36 #define USUB 12
37 #define VSUB 6
39 void Sphere::draw() const
40 {
41 static Vector3 *varr;
42 static unsigned int *iarr;
43 static int num_verts, num_indices;
44 if(!varr) {
45 int i, j;
46 int uverts = USUB;
47 int vverts = VSUB + 1;
49 num_verts = uverts * vverts;
50 varr = new Vector3[num_verts];
52 Vector3 *vptr = varr;
53 for(i=0; i<vverts; i++) {
54 float v = (float)i / (float)VSUB;
55 float phi = v * M_PI;
56 for(j=0; j<uverts; j++) {
57 float u = (float)j / (float)uverts;
58 float theta = u * M_PI * 2.0;
60 float x = sin(theta) * sin(phi);
61 float y = -cos(phi);
62 float z = cos(theta) * sin(phi);
64 *vptr++ = Vector3(x, y, z);
65 }
66 }
68 num_indices = USUB * VSUB * 4;
69 iarr = new unsigned int[num_indices];
71 unsigned int *iptr = iarr;
72 for(i=0; i<VSUB; i++) {
73 for(j=0; j<USUB; j++) {
74 iptr[0] = i * uverts + j;
75 iptr[1] = i * uverts + ((j + 1) % uverts);
76 iptr[2] = iptr[1] + uverts;
77 iptr[3] = iptr[0] + uverts;
78 iptr += 4;
79 }
80 }
82 printlog("created sphere mesh\n");
83 printlog(" vertices: %d (%d slices, %d stacks)\n", num_verts, uverts, vverts);
84 printlog(" quads: %d (%d indices, %d usub, %d vsub)\n", USUB * VSUB, num_indices, USUB, VSUB);
85 }
87 pre_draw();
89 m3d_vertex_array(&varr->x);
90 m3d_draw_indexed(M3D_QUADS, iarr, num_indices);
91 m3d_vertex_array(0);
93 post_draw();
94 }
96 bool Sphere::intersect(const Ray &ray, float *dist) const
97 {
98 return false; // TODO
99 }
101 // ---- box ----
103 Box::Box()
104 {
105 }
107 Box::~Box()
108 {
109 }
111 /*
112 3--------2
113 /. .\
114 0------------1
115 | 7--------6 |
116 |/ \|
117 4------------5
119 */
120 void Box::draw() const
121 {
122 static const float verts[] = {
123 -1, 1, 1,
124 1, 1, 1,
125 1, 1, -1,
126 -1, 1, -1,
127 -1, -1, 1,
128 1, -1, 1,
129 1, -1, -1,
130 -1, -1, -1
131 };
132 static const unsigned int indices[] = {
133 0, 1, 2, 3, // top
134 4, 7, 6, 5, // bottom
135 0, 4, 5, 1, // front
136 5, 6, 2, 1, // right
137 6, 7, 3, 2, // back
138 7, 4, 0, 3 // left
139 };
141 pre_draw();
143 m3d_vertex_array(verts);
144 m3d_draw_indexed(M3D_QUADS, indices, sizeof indices / sizeof *indices);
145 m3d_vertex_array(0);
147 post_draw();
148 }
150 bool Box::intersect(const Ray &ray, float *dist) const
151 {
152 return false; // TODO
153 }