rayzor

view src/object.cc @ 20:6b11a3f8706e

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 15 Apr 2014 01:36:03 +0300
parents a9a948809c6f
children
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 Vector3 Object::hit_normal(const RayHit &hit) const
17 {
18 return Vector3(0, 0, 0);
19 }
21 // ---- sphere ----
22 Sphere::Sphere()
23 {
24 }
26 Sphere::~Sphere()
27 {
28 }
30 #define USUB 12
31 #define VSUB 6
33 void Sphere::draw(bool emph) const
34 {
35 static Vector3 *varr;
36 static unsigned int *iarr;
37 static int num_verts, num_indices;
38 if(!varr) {
39 int i, j;
40 int uverts = USUB;
41 int vverts = VSUB + 1;
43 num_verts = uverts * vverts;
44 varr = new Vector3[num_verts];
46 Vector3 *vptr = varr;
47 for(i=0; i<vverts; i++) {
48 float v = (float)i / (float)VSUB;
49 float phi = v * M_PI;
50 for(j=0; j<uverts; j++) {
51 float u = (float)j / (float)uverts;
52 float theta = u * M_PI * 2.0;
54 float x = sin(theta) * sin(phi);
55 float y = -cos(phi);
56 float z = cos(theta) * sin(phi);
58 *vptr++ = Vector3(x, y, z);
59 }
60 }
62 num_indices = USUB * VSUB * 4;
63 iarr = new unsigned int[num_indices];
65 unsigned int *iptr = iarr;
66 for(i=0; i<VSUB; i++) {
67 for(j=0; j<USUB; j++) {
68 iptr[0] = i * uverts + j;
69 iptr[1] = i * uverts + ((j + 1) % uverts);
70 iptr[2] = iptr[1] + uverts;
71 iptr[3] = iptr[0] + uverts;
72 iptr += 4;
73 }
74 }
76 printlog("created sphere mesh\n");
77 printlog(" vertices: %d (%d slices, %d stacks)\n", num_verts, uverts, vverts);
78 printlog(" quads: %d (%d indices, %d usub, %d vsub)\n", USUB * VSUB, num_indices, USUB, VSUB);
79 }
81 pre_draw();
82 SceneNode::draw(emph);
84 m3d_vertex_array(&varr->x);
85 m3d_draw_indexed(M3D_QUADS, iarr, num_indices);
86 m3d_vertex_array(0);
88 post_draw();
89 }
91 bool Sphere::intersect(const Ray &wray, RayHit *hit) const
92 {
93 Ray ray = transform(get_inv_matrix(), wray);
95 // assumes center is 0,0,0, and radius is 1
96 float a = dot(ray.dir, ray.dir);
97 float b = 2.0 * ray.dir.x * ray.origin.x +
98 2.0 * ray.dir.y * ray.origin.y +
99 2.0 * ray.dir.z * ray.origin.z;
100 float c = dot(ray.origin, ray.origin) - 1.0;
102 float discr = b * b - 4.0 * a * c;
103 if(discr < 1e-4)
104 return false;
106 float sqrt_discr = sqrt(discr);
107 float t0 = (-b + sqrt_discr) / (2.0 * a);
108 float t1 = (-b - sqrt_discr) / (2.0 * a);
110 if(t0 < 1e-4)
111 t0 = t1;
112 if(t1 < 1e-4)
113 t1 = t0;
115 float t = t0 < t1 ? t0 : t1;
116 if(t < 1e-4)
117 return false;
119 if(hit) {
120 hit->ray = wray;
121 hit->lray = ray;
122 hit->dist = t;
123 hit->obj = this;
124 hit->subobj = 0;
125 }
126 return true;
127 }
129 Vector3 Sphere::hit_normal(const RayHit &hit) const
130 {
131 return hit.lray.origin + hit.lray.dir * hit.dist;
132 }
134 // ---- box ----
136 Box::Box()
137 {
138 }
140 Box::~Box()
141 {
142 }
144 /*
145 3--------2
146 /. .\
147 0------------1
148 | 7--------6 |
149 |/ \|
150 4------------5
152 */
153 void Box::draw(bool emph) const
154 {
155 static const float verts[] = {
156 -1, 1, 1,
157 1, 1, 1,
158 1, 1, -1,
159 -1, 1, -1,
160 -1, -1, 1,
161 1, -1, 1,
162 1, -1, -1,
163 -1, -1, -1
164 };
165 static const unsigned int indices[] = {
166 0, 1, 2, 3, // top
167 4, 7, 6, 5, // bottom
168 0, 4, 5, 1, // front
169 5, 6, 2, 1, // right
170 6, 7, 3, 2, // back
171 7, 4, 0, 3 // left
172 };
174 pre_draw();
175 SceneNode::draw(emph);
177 m3d_vertex_array(verts);
178 m3d_draw_indexed(M3D_QUADS, indices, sizeof indices / sizeof *indices);
179 m3d_vertex_array(0);
181 post_draw();
182 }
184 bool Box::intersect(const Ray &wray, RayHit *hit) const
185 {
186 Ray ray = transform(get_inv_matrix(), wray);
188 Vector3 param[2] = { Vector3(-1, -1, -1), Vector3(1, 1, 1) };
189 Vector3 inv_dir(1.0 / ray.dir.x, 1.0 / ray.dir.y, 1.0 / ray.dir.z);
190 int sign[3] = {inv_dir.x < 0, inv_dir.y < 0, inv_dir.z < 0};
192 float tmin = (param[sign[0]].x - ray.origin.x) * inv_dir.x;
193 float tmax = (param[1 - sign[0]].x - ray.origin.x) * inv_dir.x;
194 float tymin = (param[sign[1]].y - ray.origin.y) * inv_dir.y;
195 float tymax = (param[1 - sign[1]].y - ray.origin.y) * inv_dir.y;
197 if(tmin > tymax || tymin > tmax) {
198 return false;
199 }
200 if(tymin > tmin) tmin = tymin;
201 if(tymax < tmax) tmax = tymax;
203 float tzmin = (param[sign[2]].z - ray.origin.z) * inv_dir.z;
204 float tzmax = (param[1 - sign[2]].z - ray.origin.z) * inv_dir.z;
206 if(tmin > tzmax || tzmin > tmax) {
207 return false;
208 }
209 if(tzmin > tmin) tmin = tzmin;
210 if(tzmax < tmax) tmax = tzmax;
212 float t = tmin < 1e-4 ? tmax : tmin;
213 if(t >= 1e-4) {
214 if(hit) {
215 hit->dist = t;
216 hit->ray = wray;
217 hit->lray = ray;
218 hit->obj = this;
219 hit->subobj = 0;
220 }
221 return true;
222 }
223 return false;
224 }
226 Vector3 Box::hit_normal(const RayHit &hit) const
227 {
228 Vector3 lpt = hit.lray.origin + hit.lray.dir * hit.dist;
230 if(fabs(lpt.x) > fabs(lpt.y) && fabs(lpt.x) > fabs(lpt.z)) {
231 return Vector3(lpt.x, 0, 0);
232 }
233 if(fabs(lpt.y) > fabs(lpt.z)) {
234 return Vector3(0, lpt.y, 0);
235 }
236 return Vector3(0, 0, lpt.z);
237 }