goat3dgfx
view src/object.cc @ 13:25bf39105c82
lalal
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 27 Nov 2013 08:08:59 +0200 |
parents | |
children | 7d6b667821cf |
line source
1 #include <float.h>
2 #include "object.h"
3 #include "opengl.h"
4 #include "unistate.h"
5 #include "logger.h"
7 static void destroy_all_rec(XFormNode *node);
8 static void get_all_meshes_rec(XFormNode *node, std::list<Mesh*> *reslist);
10 DrawMode Object::draw_mode = DRAW_DEFAULT;
12 Object::Object()
13 {
14 mesh = 0;
15 }
17 void Object::destroy_all()
18 {
19 destroy_all_rec(this);
20 }
22 static void destroy_all_rec(XFormNode *node)
23 {
24 if(!node) {
25 return;
26 }
28 for(int i=0; i<node->get_children_count(); i++) {
29 destroy_all_rec(node->get_child(i));
30 }
31 delete node;
32 }
34 void Object::set_mesh(Mesh *m)
35 {
36 mesh = m;
37 }
39 Mesh *Object::get_mesh()
40 {
41 return mesh;
42 }
44 const Mesh *Object::get_mesh() const
45 {
46 return mesh;
47 }
49 std::list<Mesh*> Object::get_all_meshes()
50 {
51 std::list<Mesh*> meshes;
52 get_all_meshes_rec(this, &meshes);
53 return meshes;
54 }
56 std::list<const Mesh*> Object::get_all_meshes() const
57 {
58 std::list<const Mesh*> meshes;
59 get_all_meshes_rec((Object*)this, (std::list<Mesh*>*)&meshes);
60 return meshes;
61 }
63 static void get_all_meshes_rec(XFormNode *node, std::list<Mesh*> *reslist)
64 {
65 if(!node) {
66 return;
67 }
69 Object *obj = dynamic_cast<Object*>(node);
70 if(obj) {
71 Mesh *mesh = obj->get_mesh();
72 if(mesh) {
73 reslist->push_back(mesh);
74 }
75 }
77 for(int i=0; i<node->get_children_count(); i++) {
78 get_all_meshes_rec(node->get_child(i), reslist);
79 }
80 }
82 AABox Object::get_aabbox() const
83 {
84 AABox box;
86 if(mesh) {
87 box = mesh->get_aabbox();
88 } else {
89 box.min = Vector3(FLT_MAX, FLT_MAX, FLT_MAX);
90 box.max = -box.min;
91 }
93 int num_children = get_children_count();
94 for(int i=0; i<num_children; i++) {
95 const Object *obj = dynamic_cast<const Object*>(get_child(i));
96 if(obj) {
97 AABox child_box = obj->get_aabbox();
98 box.set_union(&box, &child_box);
99 }
100 }
101 return box;
102 }
104 Sphere Object::get_bsphere() const
105 {
106 Sphere sph;
107 bool valid = false;
109 if(mesh) {
110 sph = mesh->get_bsphere();
111 valid = true;
112 } else {
113 sph.radius = 0.0;
114 }
116 int num_children = get_children_count();
117 for(int i=0; i<num_children; i++) {
118 const Object *obj = dynamic_cast<const Object*>(get_child(i));
119 if(obj) {
120 Sphere child_sph = obj->get_bsphere();
121 if(valid) {
122 sph.set_union(&sph, &child_sph);
123 } else {
124 sph = child_sph;
125 valid = true;
126 }
127 }
128 }
130 return sph;
131 }
133 /*static const char *attr_name[] = {
134 "attr_vertex",
135 "attr_normal",
136 "attr_tangent",
137 "attr_texcoord",
138 "attr_boneweights",
139 "attr_boneidx"
140 };*/
142 void Object::draw(long msec) const
143 {
144 Matrix4x4 xform;
145 get_xform(msec, &xform);
147 set_world_matrix(xform);
149 if(mesh) {
150 /*unsigned int prog = sdrprog;
152 if(mesh->get_bones_count() > 0) {
153 prog = sdrprog_skin;
154 }
156 glUseProgram(prog);
157 // get all the attribute locations
158 for(int i=0; i<NUM_MESH_ATTR; i++) {
159 int loc = glGetAttribLocation(prog, attr_name[i]);
160 if(loc != -1) {
161 Mesh::set_attrib_location(i, loc);
162 }
163 }
165 setup_bones(msec);
167 glUseProgram(prog);*/
169 material.setup();
170 setup_unistate(); // set all state uniforms
172 switch(Object::draw_mode) {
173 case DRAW_WIREFRAME:
174 mesh->draw_wire();
175 break;
177 case DRAW_VERTICES:
178 mesh->draw_vertices();
179 break;
181 case DRAW_DEFAULT:
182 default:
183 mesh->draw();
184 }
185 }
187 int num_children = get_children_count();
188 for(int i=0; i<num_children; i++) {
189 const Object *obj = dynamic_cast<const Object*>(get_child(i));
190 if(obj) {
191 obj->draw(msec);
192 }
193 }
194 }
197 bool Object::intersect(const Ray &inray, HitPoint *hit) const
198 {
199 Ray ray = inray;
200 Matrix4x4 xform, inv_xform;
201 get_xform(ray.time, &xform/*, &inv_xform*/);
202 ray.transform(xform.inverse()); // TODO find out what's wrong with get_xform's inv_xform and use that
204 HitPoint nearest_hit;
205 nearest_hit.dist = FLT_MAX;
206 nearest_hit.obj = 0;
208 if(mesh) {
209 if(mesh->intersect(ray, hit ? &nearest_hit : 0)) {
210 if(!hit) {
211 return true;
212 }
214 if(Mesh::get_intersect_mode() & ISECT_FACE) {
215 Triangle *face = (Triangle*)nearest_hit.obj;
216 face->transform(xform);
217 } else if(Mesh::get_intersect_mode() & ISECT_VERTICES) {
218 Vector3 *v = (Vector3*)nearest_hit.obj;
219 v->transform(xform);
220 } else {
221 nearest_hit.obj = this;
222 }
223 }
224 }
226 int num_children = get_children_count();
227 for(int i=0; i<num_children; i++) {
228 const Object *obj = dynamic_cast<const Object*>(get_child(i));
230 HitPoint chit;
231 if(obj && obj->intersect(inray, hit ? &chit : 0)) {
232 if(!hit) {
233 return true;
234 }
236 if(chit.dist < nearest_hit.dist) {
237 nearest_hit = chit;
238 }
239 }
240 }
242 if(nearest_hit.obj) {
243 if(hit) {
244 *hit = nearest_hit;
245 }
246 return true;
247 }
248 return false;
249 }
252 bool Object::setup_bones(long msec) const
253 {
254 int num_bones;
255 if(!mesh || !(num_bones = mesh->get_bones_count())) {
256 return false;
257 }
259 /*char uniname[32];
261 for(int i=0; i<num_bones; i++) {
262 const XFormNode *bone = mesh->get_bone(i);
264 Matrix4x4 xform;
265 bone->get_xform(msec, &xform);
267 xform = xform * bone->get_bone_matrix();
269 sprintf(uniname, "bone_xform[%d]", i);
270 int loc = glGetUniformLocation(sdrprog_skin, uniname);
271 if(loc == -1) {
272 return false;
273 }
274 glUniformMatrix4fv(loc, 1, GL_TRUE, xform[0]);
275 }*/
276 return true;
277 }
279 DrawMode Object::set_draw_mode(DrawMode mode)
280 {
281 DrawMode prev = Object::draw_mode;
282 Object::draw_mode = mode;
283 return prev;
284 }