rayzor

view src/object.cc @ 14:a9a948809c6f

starting the renderer screen, plus misc stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 13 Apr 2014 08:06:21 +0300
parents d94a69933a71
children 79609d482762
line source
1 #include <math.h>
2 #include "object.h"
3 #include "vmath.h"
4 #include "min3d.h"
5 #include "logger.h"
7 Object::Object()
8 {
9 type = NODE_OBJECT;
10 }
12 Object::~Object()
13 {
14 }
16 // ---- sphere ----
17 Sphere::Sphere()
18 {
19 }
21 Sphere::~Sphere()
22 {
23 }
25 #define USUB 12
26 #define VSUB 6
28 void Sphere::draw(bool emph) const
29 {
30 static Vector3 *varr;
31 static unsigned int *iarr;
32 static int num_verts, num_indices;
33 if(!varr) {
34 int i, j;
35 int uverts = USUB;
36 int vverts = VSUB + 1;
38 num_verts = uverts * vverts;
39 varr = new Vector3[num_verts];
41 Vector3 *vptr = varr;
42 for(i=0; i<vverts; i++) {
43 float v = (float)i / (float)VSUB;
44 float phi = v * M_PI;
45 for(j=0; j<uverts; j++) {
46 float u = (float)j / (float)uverts;
47 float theta = u * M_PI * 2.0;
49 float x = sin(theta) * sin(phi);
50 float y = -cos(phi);
51 float z = cos(theta) * sin(phi);
53 *vptr++ = Vector3(x, y, z);
54 }
55 }
57 num_indices = USUB * VSUB * 4;
58 iarr = new unsigned int[num_indices];
60 unsigned int *iptr = iarr;
61 for(i=0; i<VSUB; i++) {
62 for(j=0; j<USUB; j++) {
63 iptr[0] = i * uverts + j;
64 iptr[1] = i * uverts + ((j + 1) % uverts);
65 iptr[2] = iptr[1] + uverts;
66 iptr[3] = iptr[0] + uverts;
67 iptr += 4;
68 }
69 }
71 printlog("created sphere mesh\n");
72 printlog(" vertices: %d (%d slices, %d stacks)\n", num_verts, uverts, vverts);
73 printlog(" quads: %d (%d indices, %d usub, %d vsub)\n", USUB * VSUB, num_indices, USUB, VSUB);
74 }
76 pre_draw();
77 SceneNode::draw(emph);
79 m3d_vertex_array(&varr->x);
80 m3d_draw_indexed(M3D_QUADS, iarr, num_indices);
81 m3d_vertex_array(0);
83 post_draw();
84 }
86 bool Sphere::intersect(const Ray &wray, float *dist) const
87 {
88 Ray ray = transform(get_inv_matrix(), wray);
90 // assumes center is 0,0,0, and radius is 1
91 float a = dot(ray.dir, ray.dir);
92 float b = 2.0 * ray.dir.x * ray.origin.x +
93 2.0 * ray.dir.y * ray.origin.y +
94 2.0 * ray.dir.z * ray.origin.z;
95 float c = dot(ray.origin, ray.origin) - 1.0;
97 float discr = b * b - 4.0 * a * c;
98 if(discr < 1e-4)
99 return false;
101 float sqrt_discr = sqrt(discr);
102 float t0 = (-b + sqrt_discr) / (2.0 * a);
103 float t1 = (-b - sqrt_discr) / (2.0 * a);
105 if(t0 < 1e-4)
106 t0 = t1;
107 if(t1 < 1e-4)
108 t1 = t0;
110 float t = t0 < t1 ? t0 : t1;
111 if(t < 1e-4)
112 return false;
114 if(dist) *dist = t;
115 return true;
116 }
118 // ---- box ----
120 Box::Box()
121 {
122 }
124 Box::~Box()
125 {
126 }
128 /*
129 3--------2
130 /. .\
131 0------------1
132 | 7--------6 |
133 |/ \|
134 4------------5
136 */
137 void Box::draw(bool emph) const
138 {
139 static const float verts[] = {
140 -1, 1, 1,
141 1, 1, 1,
142 1, 1, -1,
143 -1, 1, -1,
144 -1, -1, 1,
145 1, -1, 1,
146 1, -1, -1,
147 -1, -1, -1
148 };
149 static const unsigned int indices[] = {
150 0, 1, 2, 3, // top
151 4, 7, 6, 5, // bottom
152 0, 4, 5, 1, // front
153 5, 6, 2, 1, // right
154 6, 7, 3, 2, // back
155 7, 4, 0, 3 // left
156 };
158 pre_draw();
159 SceneNode::draw(emph);
161 m3d_vertex_array(verts);
162 m3d_draw_indexed(M3D_QUADS, indices, sizeof indices / sizeof *indices);
163 m3d_vertex_array(0);
165 post_draw();
166 }
168 bool Box::intersect(const Ray &ray, float *dist) const
169 {
170 return false; // TODO
171 }