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