rev |
line source |
nuclear@0
|
1 #include <float.h>
|
nuclear@0
|
2 #include "object.h"
|
nuclear@0
|
3 #include "opengl.h"
|
nuclear@0
|
4 #include "shader.h"
|
nuclear@0
|
5 #include "unistate.h"
|
nuclear@0
|
6 #include "logger.h"
|
nuclear@0
|
7
|
nuclear@0
|
8 static void destroy_all_rec(XFormNode *node);
|
nuclear@0
|
9 static void get_all_meshes_rec(XFormNode *node, std::list<Mesh*> *reslist);
|
nuclear@0
|
10
|
nuclear@0
|
11 DrawMode Object::draw_mode = DRAW_DEFAULT;
|
nuclear@0
|
12
|
nuclear@0
|
13 Object::Object()
|
nuclear@0
|
14 {
|
nuclear@0
|
15 type = NODE_OBJECT;
|
nuclear@0
|
16 mesh = 0;
|
nuclear@0
|
17 }
|
nuclear@0
|
18
|
nuclear@0
|
19 void Object::destroy_all()
|
nuclear@0
|
20 {
|
nuclear@0
|
21 destroy_all_rec(this);
|
nuclear@0
|
22 }
|
nuclear@0
|
23
|
nuclear@0
|
24 static void destroy_all_rec(XFormNode *node)
|
nuclear@0
|
25 {
|
nuclear@0
|
26 if(!node) {
|
nuclear@0
|
27 return;
|
nuclear@0
|
28 }
|
nuclear@0
|
29
|
nuclear@0
|
30 for(int i=0; i<node->get_children_count(); i++) {
|
nuclear@0
|
31 destroy_all_rec(node->get_child(i));
|
nuclear@0
|
32 }
|
nuclear@0
|
33 delete node;
|
nuclear@0
|
34 }
|
nuclear@0
|
35
|
nuclear@0
|
36 void Object::set_mesh(Mesh *m)
|
nuclear@0
|
37 {
|
nuclear@0
|
38 mesh = m;
|
nuclear@0
|
39 }
|
nuclear@0
|
40
|
nuclear@0
|
41 Mesh *Object::get_mesh()
|
nuclear@0
|
42 {
|
nuclear@0
|
43 return mesh;
|
nuclear@0
|
44 }
|
nuclear@0
|
45
|
nuclear@0
|
46 const Mesh *Object::get_mesh() const
|
nuclear@0
|
47 {
|
nuclear@0
|
48 return mesh;
|
nuclear@0
|
49 }
|
nuclear@0
|
50
|
nuclear@0
|
51 std::list<Mesh*> Object::get_all_meshes()
|
nuclear@0
|
52 {
|
nuclear@0
|
53 std::list<Mesh*> meshes;
|
nuclear@0
|
54 get_all_meshes_rec(this, &meshes);
|
nuclear@0
|
55 return meshes;
|
nuclear@0
|
56 }
|
nuclear@0
|
57
|
nuclear@0
|
58 std::list<const Mesh*> Object::get_all_meshes() const
|
nuclear@0
|
59 {
|
nuclear@0
|
60 std::list<const Mesh*> meshes;
|
nuclear@0
|
61 get_all_meshes_rec((Object*)this, (std::list<Mesh*>*)&meshes);
|
nuclear@0
|
62 return meshes;
|
nuclear@0
|
63 }
|
nuclear@0
|
64
|
nuclear@0
|
65 static void get_all_meshes_rec(XFormNode *node, std::list<Mesh*> *reslist)
|
nuclear@0
|
66 {
|
nuclear@0
|
67 if(!node) {
|
nuclear@0
|
68 return;
|
nuclear@0
|
69 }
|
nuclear@0
|
70
|
nuclear@0
|
71 if(node->get_type() == NODE_OBJECT) {
|
nuclear@0
|
72 Object *obj = (Object*)node;
|
nuclear@0
|
73 if(obj) {
|
nuclear@0
|
74 Mesh *mesh = obj->get_mesh();
|
nuclear@0
|
75 if(mesh) {
|
nuclear@0
|
76 reslist->push_back(mesh);
|
nuclear@0
|
77 }
|
nuclear@0
|
78 }
|
nuclear@0
|
79 }
|
nuclear@0
|
80
|
nuclear@0
|
81 for(int i=0; i<node->get_children_count(); i++) {
|
nuclear@0
|
82 get_all_meshes_rec(node->get_child(i), reslist);
|
nuclear@0
|
83 }
|
nuclear@0
|
84 }
|
nuclear@0
|
85
|
nuclear@0
|
86 long Object::count_polys() const
|
nuclear@0
|
87 {
|
nuclear@0
|
88 long poly_count = mesh ? mesh->get_poly_count() : 0;
|
nuclear@0
|
89
|
nuclear@0
|
90 int nchildren = get_children_count();
|
nuclear@0
|
91 for(int i=0; i<nchildren; i++) {
|
nuclear@0
|
92 const XFormNode *child = get_child(i);
|
nuclear@0
|
93 if(child->get_type() == NODE_OBJECT) {
|
nuclear@0
|
94 poly_count += ((const Object*)child)->count_polys();
|
nuclear@0
|
95 }
|
nuclear@0
|
96 }
|
nuclear@0
|
97 return poly_count;
|
nuclear@0
|
98 }
|
nuclear@0
|
99
|
nuclear@0
|
100 AABox Object::get_aabbox() const
|
nuclear@0
|
101 {
|
nuclear@0
|
102 AABox box;
|
nuclear@0
|
103
|
nuclear@0
|
104 if(mesh) {
|
nuclear@0
|
105 box = mesh->get_aabbox();
|
nuclear@0
|
106 } else {
|
nuclear@0
|
107 box.min = Vector3(FLT_MAX, FLT_MAX, FLT_MAX);
|
nuclear@0
|
108 box.max = -box.min;
|
nuclear@0
|
109 }
|
nuclear@0
|
110
|
nuclear@0
|
111 int num_children = get_children_count();
|
nuclear@0
|
112 for(int i=0; i<num_children; i++) {
|
nuclear@0
|
113 const Object *obj = dynamic_cast<const Object*>(get_child(i));
|
nuclear@0
|
114 if(obj) {
|
nuclear@0
|
115 AABox child_box = obj->get_aabbox();
|
nuclear@0
|
116 box.set_union(&box, &child_box);
|
nuclear@0
|
117 }
|
nuclear@0
|
118 }
|
nuclear@0
|
119 return box;
|
nuclear@0
|
120 }
|
nuclear@0
|
121
|
nuclear@0
|
122 Sphere Object::get_bsphere() const
|
nuclear@0
|
123 {
|
nuclear@0
|
124 Sphere sph;
|
nuclear@0
|
125 bool valid = false;
|
nuclear@0
|
126
|
nuclear@0
|
127 if(mesh) {
|
nuclear@0
|
128 sph = mesh->get_bsphere();
|
nuclear@0
|
129 valid = true;
|
nuclear@0
|
130 } else {
|
nuclear@0
|
131 sph.radius = 0.0;
|
nuclear@0
|
132 }
|
nuclear@0
|
133
|
nuclear@0
|
134 int num_children = get_children_count();
|
nuclear@0
|
135 for(int i=0; i<num_children; i++) {
|
nuclear@0
|
136 // TODO: this is wrong
|
nuclear@0
|
137 const Object *obj = dynamic_cast<const Object*>(get_child(i));
|
nuclear@0
|
138 if(obj) {
|
nuclear@0
|
139 Sphere child_sph = obj->get_bsphere();
|
nuclear@0
|
140 if(valid) {
|
nuclear@0
|
141 sph.set_union(&sph, &child_sph);
|
nuclear@0
|
142 } else {
|
nuclear@0
|
143 sph = child_sph;
|
nuclear@0
|
144 valid = true;
|
nuclear@0
|
145 }
|
nuclear@0
|
146 }
|
nuclear@0
|
147 }
|
nuclear@0
|
148
|
nuclear@0
|
149 return sph;
|
nuclear@0
|
150 }
|
nuclear@0
|
151
|
nuclear@0
|
152 void Object::apply_xform(long msec)
|
nuclear@0
|
153 {
|
nuclear@0
|
154 std::list<XFormNode*> nodelist = get_all_nodes(NODE_OBJECT);
|
nuclear@0
|
155
|
nuclear@0
|
156 std::list<XFormNode*>::iterator it = nodelist.begin();
|
nuclear@0
|
157 while(it != nodelist.end()) {
|
nuclear@0
|
158 Object *obj = (Object*)*it++;
|
nuclear@0
|
159
|
nuclear@0
|
160 Mesh *mesh = obj->get_mesh();
|
nuclear@0
|
161 if(mesh) {
|
nuclear@0
|
162 Matrix4x4 xform;
|
nuclear@0
|
163 obj->get_xform(msec, &xform);
|
nuclear@0
|
164 mesh->apply_xform(xform);
|
nuclear@0
|
165 }
|
nuclear@0
|
166 }
|
nuclear@0
|
167
|
nuclear@0
|
168 // go through the list a second time and clear the transformations
|
nuclear@0
|
169 it = nodelist.begin();
|
nuclear@0
|
170 while(it != nodelist.end()) {
|
nuclear@0
|
171 Object *obj = (Object*)*it++;
|
nuclear@0
|
172
|
nuclear@0
|
173 obj->clear_xform();
|
nuclear@0
|
174 }
|
nuclear@0
|
175 }
|
nuclear@0
|
176
|
nuclear@0
|
177 /*static const char *attr_name[] = {
|
nuclear@0
|
178 "attr_vertex",
|
nuclear@0
|
179 "attr_normal",
|
nuclear@0
|
180 "attr_tangent",
|
nuclear@0
|
181 "attr_texcoord",
|
nuclear@0
|
182 "attr_boneweights",
|
nuclear@0
|
183 "attr_boneidx"
|
nuclear@0
|
184 };*/
|
nuclear@0
|
185
|
nuclear@0
|
186 void Object::draw(long msec) const
|
nuclear@0
|
187 {
|
nuclear@0
|
188 Matrix4x4 xform;
|
nuclear@0
|
189 get_xform(msec, &xform);
|
nuclear@0
|
190
|
nuclear@0
|
191 set_world_matrix(xform);
|
nuclear@0
|
192
|
nuclear@0
|
193 if(mesh) {
|
nuclear@0
|
194 /*unsigned int prog = sdrprog;
|
nuclear@0
|
195
|
nuclear@0
|
196 if(mesh->get_bones_count() > 0) {
|
nuclear@0
|
197 prog = sdrprog_skin;
|
nuclear@0
|
198 }
|
nuclear@0
|
199
|
nuclear@0
|
200 glUseProgram(prog);
|
nuclear@0
|
201 // get all the attribute locations
|
nuclear@0
|
202 for(int i=0; i<NUM_MESH_ATTR; i++) {
|
nuclear@0
|
203 int loc = glGetAttribLocation(prog, attr_name[i]);
|
nuclear@0
|
204 if(loc != -1) {
|
nuclear@0
|
205 Mesh::set_attrib_location(i, loc);
|
nuclear@0
|
206 }
|
nuclear@0
|
207 }
|
nuclear@0
|
208
|
nuclear@0
|
209 setup_bones(msec);
|
nuclear@0
|
210
|
nuclear@0
|
211 glUseProgram(prog);*/
|
nuclear@0
|
212
|
nuclear@0
|
213 #ifndef GL_ES_VERSION_2_0
|
nuclear@0
|
214 if(!get_current_shader()) {
|
nuclear@0
|
215 setup_gl_matrices();
|
nuclear@0
|
216 }
|
nuclear@0
|
217 #endif
|
nuclear@0
|
218
|
nuclear@0
|
219 material.setup();
|
nuclear@0
|
220 setup_unistate(); // set all state uniforms
|
nuclear@0
|
221
|
nuclear@0
|
222 switch(Object::draw_mode) {
|
nuclear@0
|
223 case DRAW_WIREFRAME:
|
nuclear@0
|
224 mesh->draw_wire();
|
nuclear@0
|
225 break;
|
nuclear@0
|
226
|
nuclear@0
|
227 case DRAW_VERTICES:
|
nuclear@0
|
228 mesh->draw_vertices();
|
nuclear@0
|
229 break;
|
nuclear@0
|
230
|
nuclear@0
|
231 case DRAW_DEFAULT:
|
nuclear@0
|
232 default:
|
nuclear@0
|
233 mesh->draw();
|
nuclear@0
|
234 }
|
nuclear@0
|
235 }
|
nuclear@0
|
236
|
nuclear@0
|
237 int num_children = get_children_count();
|
nuclear@0
|
238 for(int i=0; i<num_children; i++) {
|
nuclear@0
|
239 const XFormNode *c = get_child(i);
|
nuclear@0
|
240 c->draw(msec);
|
nuclear@0
|
241 }
|
nuclear@0
|
242 }
|
nuclear@0
|
243
|
nuclear@0
|
244
|
nuclear@0
|
245 bool Object::intersect(const Ray &inray, HitPoint *hit) const
|
nuclear@0
|
246 {
|
nuclear@0
|
247 Ray ray = inray;
|
nuclear@0
|
248 Matrix4x4 xform, inv_xform;
|
nuclear@0
|
249 get_xform(ray.time, &xform/*, &inv_xform*/);
|
nuclear@0
|
250 ray.transform(xform.inverse()); // TODO find out what's wrong with get_xform's inv_xform and use that
|
nuclear@0
|
251
|
nuclear@0
|
252 HitPoint nearest_hit;
|
nuclear@0
|
253 nearest_hit.dist = FLT_MAX;
|
nuclear@0
|
254 nearest_hit.obj = 0;
|
nuclear@0
|
255
|
nuclear@0
|
256 if(mesh) {
|
nuclear@0
|
257 if(mesh->intersect(ray, hit ? &nearest_hit : 0)) {
|
nuclear@0
|
258 if(!hit) {
|
nuclear@0
|
259 return true;
|
nuclear@0
|
260 }
|
nuclear@0
|
261
|
nuclear@0
|
262 if(Mesh::get_intersect_mode() & ISECT_FACE) {
|
nuclear@0
|
263 Triangle *face = (Triangle*)nearest_hit.obj;
|
nuclear@0
|
264 face->transform(xform);
|
nuclear@0
|
265 } else if(Mesh::get_intersect_mode() & ISECT_VERTICES) {
|
nuclear@0
|
266 Vector3 *v = (Vector3*)nearest_hit.obj;
|
nuclear@0
|
267 v->transform(xform);
|
nuclear@0
|
268 } else {
|
nuclear@0
|
269 nearest_hit.obj = this;
|
nuclear@0
|
270 }
|
nuclear@0
|
271 }
|
nuclear@0
|
272 }
|
nuclear@0
|
273
|
nuclear@0
|
274 int num_children = get_children_count();
|
nuclear@0
|
275 for(int i=0; i<num_children; i++) {
|
nuclear@0
|
276 const Object *obj = dynamic_cast<const Object*>(get_child(i));
|
nuclear@0
|
277
|
nuclear@0
|
278 HitPoint chit;
|
nuclear@0
|
279 if(obj && obj->intersect(inray, hit ? &chit : 0)) {
|
nuclear@0
|
280 if(!hit) {
|
nuclear@0
|
281 return true;
|
nuclear@0
|
282 }
|
nuclear@0
|
283
|
nuclear@0
|
284 if(chit.dist < nearest_hit.dist) {
|
nuclear@0
|
285 nearest_hit = chit;
|
nuclear@0
|
286 }
|
nuclear@0
|
287 }
|
nuclear@0
|
288 }
|
nuclear@0
|
289
|
nuclear@0
|
290 if(nearest_hit.obj) {
|
nuclear@0
|
291 if(hit) {
|
nuclear@0
|
292 *hit = nearest_hit;
|
nuclear@0
|
293 }
|
nuclear@0
|
294 return true;
|
nuclear@0
|
295 }
|
nuclear@0
|
296 return false;
|
nuclear@0
|
297 }
|
nuclear@0
|
298
|
nuclear@0
|
299
|
nuclear@0
|
300 bool Object::setup_bones(long msec) const
|
nuclear@0
|
301 {
|
nuclear@0
|
302 int num_bones;
|
nuclear@0
|
303 if(!mesh || !(num_bones = mesh->get_bones_count())) {
|
nuclear@0
|
304 return false;
|
nuclear@0
|
305 }
|
nuclear@0
|
306
|
nuclear@0
|
307 /*char uniname[32];
|
nuclear@0
|
308
|
nuclear@0
|
309 for(int i=0; i<num_bones; i++) {
|
nuclear@0
|
310 const XFormNode *bone = mesh->get_bone(i);
|
nuclear@0
|
311
|
nuclear@0
|
312 Matrix4x4 xform;
|
nuclear@0
|
313 bone->get_xform(msec, &xform);
|
nuclear@0
|
314
|
nuclear@0
|
315 xform = xform * bone->get_bone_matrix();
|
nuclear@0
|
316
|
nuclear@0
|
317 sprintf(uniname, "bone_xform[%d]", i);
|
nuclear@0
|
318 int loc = glGetUniformLocation(sdrprog_skin, uniname);
|
nuclear@0
|
319 if(loc == -1) {
|
nuclear@0
|
320 return false;
|
nuclear@0
|
321 }
|
nuclear@0
|
322 glUniformMatrix4fv(loc, 1, GL_TRUE, xform[0]);
|
nuclear@0
|
323 }*/
|
nuclear@0
|
324 return true;
|
nuclear@0
|
325 }
|
nuclear@0
|
326
|
nuclear@0
|
327 DrawMode Object::set_draw_mode(DrawMode mode)
|
nuclear@0
|
328 {
|
nuclear@0
|
329 DrawMode prev = Object::draw_mode;
|
nuclear@0
|
330 Object::draw_mode = mode;
|
nuclear@0
|
331 return prev;
|
nuclear@0
|
332 }
|