conworlds
view src/object.cc @ 15:9b0db7dbde6e
hmmm doesn't work properly yes
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 24 Aug 2014 14:36:00 +0300 |
parents | |
children |
line source
1 #include <float.h>
2 #include "object.h"
3 #include "opengl.h"
4 #include "unistate.h"
5 #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 mesh = 0;
16 }
18 void Object::destroy_all()
19 {
20 destroy_all_rec(this);
21 }
23 static void destroy_all_rec(XFormNode *node)
24 {
25 if(!node) {
26 return;
27 }
29 for(int i=0; i<node->get_children_count(); i++) {
30 destroy_all_rec(node->get_child(i));
31 }
32 delete node;
33 }
35 void Object::set_mesh(Mesh *m)
36 {
37 mesh = m;
38 }
40 Mesh *Object::get_mesh()
41 {
42 return mesh;
43 }
45 const Mesh *Object::get_mesh() const
46 {
47 return mesh;
48 }
50 std::list<Mesh*> Object::get_all_meshes()
51 {
52 std::list<Mesh*> meshes;
53 get_all_meshes_rec(this, &meshes);
54 return meshes;
55 }
57 std::list<const Mesh*> Object::get_all_meshes() const
58 {
59 std::list<const Mesh*> meshes;
60 get_all_meshes_rec((Object*)this, (std::list<Mesh*>*)&meshes);
61 return meshes;
62 }
64 static void get_all_meshes_rec(XFormNode *node, std::list<Mesh*> *reslist)
65 {
66 if(!node) {
67 return;
68 }
70 Object *obj = dynamic_cast<Object*>(node);
71 if(obj) {
72 Mesh *mesh = obj->get_mesh();
73 if(mesh) {
74 reslist->push_back(mesh);
75 }
76 }
78 for(int i=0; i<node->get_children_count(); i++) {
79 get_all_meshes_rec(node->get_child(i), reslist);
80 }
81 }
83 AABox Object::get_aabbox() const
84 {
85 AABox box;
87 if(mesh) {
88 box = mesh->get_aabbox();
89 } else {
90 box.min = Vector3(FLT_MAX, FLT_MAX, FLT_MAX);
91 box.max = -box.min;
92 }
94 int num_children = get_children_count();
95 for(int i=0; i<num_children; i++) {
96 const Object *obj = dynamic_cast<const Object*>(get_child(i));
97 if(obj) {
98 AABox child_box = obj->get_aabbox();
99 box.set_union(&box, &child_box);
100 }
101 }
102 return box;
103 }
105 Sphere Object::get_bsphere() const
106 {
107 Sphere sph;
108 bool valid = false;
110 if(mesh) {
111 sph = mesh->get_bsphere();
112 valid = true;
113 } else {
114 sph.radius = 0.0;
115 }
117 int num_children = get_children_count();
118 for(int i=0; i<num_children; i++) {
119 const Object *obj = dynamic_cast<const Object*>(get_child(i));
120 if(obj) {
121 Sphere child_sph = obj->get_bsphere();
122 if(valid) {
123 sph.set_union(&sph, &child_sph);
124 } else {
125 sph = child_sph;
126 valid = true;
127 }
128 }
129 }
131 return sph;
132 }
134 /*static const char *attr_name[] = {
135 "attr_vertex",
136 "attr_normal",
137 "attr_tangent",
138 "attr_texcoord",
139 "attr_boneweights",
140 "attr_boneidx"
141 };*/
143 void Object::draw(long msec) const
144 {
145 Matrix4x4 xform;
146 get_xform(msec, &xform);
148 set_world_matrix(xform);
150 if(mesh) {
151 /*unsigned int prog = sdrprog;
153 if(mesh->get_bones_count() > 0) {
154 prog = sdrprog_skin;
155 }
157 glUseProgram(prog);
158 // get all the attribute locations
159 for(int i=0; i<NUM_MESH_ATTR; i++) {
160 int loc = glGetAttribLocation(prog, attr_name[i]);
161 if(loc != -1) {
162 Mesh::set_attrib_location(i, loc);
163 }
164 }
166 setup_bones(msec);
168 glUseProgram(prog);*/
170 material.setup();
171 setup_unistate(); // set all state uniforms
173 switch(Object::draw_mode) {
174 case DRAW_WIREFRAME:
175 mesh->draw_wire();
176 break;
178 case DRAW_VERTICES:
179 mesh->draw_vertices();
180 break;
182 case DRAW_DEFAULT:
183 default:
184 mesh->draw();
185 }
186 }
188 int num_children = get_children_count();
189 for(int i=0; i<num_children; i++) {
190 const Object *obj = dynamic_cast<const Object*>(get_child(i));
191 if(obj) {
192 obj->draw(msec);
193 }
194 }
195 }
198 bool Object::intersect(const Ray &inray, HitPoint *hit) const
199 {
200 Ray ray = inray;
201 Matrix4x4 xform, inv_xform;
202 get_xform(ray.time, &xform/*, &inv_xform*/);
203 ray.transform(xform.inverse()); // TODO find out what's wrong with get_xform's inv_xform and use that
205 HitPoint nearest_hit;
206 nearest_hit.dist = FLT_MAX;
207 nearest_hit.obj = 0;
209 if(mesh) {
210 if(mesh->intersect(ray, hit ? &nearest_hit : 0)) {
211 if(!hit) {
212 return true;
213 }
215 if(Mesh::get_intersect_mode() & ISECT_FACE) {
216 Triangle *face = (Triangle*)nearest_hit.obj;
217 face->transform(xform);
218 } else if(Mesh::get_intersect_mode() & ISECT_VERTICES) {
219 Vector3 *v = (Vector3*)nearest_hit.obj;
220 v->transform(xform);
221 } else {
222 nearest_hit.obj = this;
223 }
224 }
225 }
227 int num_children = get_children_count();
228 for(int i=0; i<num_children; i++) {
229 const Object *obj = dynamic_cast<const Object*>(get_child(i));
231 HitPoint chit;
232 if(obj && obj->intersect(inray, hit ? &chit : 0)) {
233 if(!hit) {
234 return true;
235 }
237 if(chit.dist < nearest_hit.dist) {
238 nearest_hit = chit;
239 }
240 }
241 }
243 if(nearest_hit.obj) {
244 if(hit) {
245 *hit = nearest_hit;
246 }
247 return true;
248 }
249 return false;
250 }
253 bool Object::setup_bones(long msec) const
254 {
255 int num_bones;
256 if(!mesh || !(num_bones = mesh->get_bones_count())) {
257 return false;
258 }
260 /*char uniname[32];
262 for(int i=0; i<num_bones; i++) {
263 const XFormNode *bone = mesh->get_bone(i);
265 Matrix4x4 xform;
266 bone->get_xform(msec, &xform);
268 xform = xform * bone->get_bone_matrix();
270 sprintf(uniname, "bone_xform[%d]", i);
271 int loc = glGetUniformLocation(sdrprog_skin, uniname);
272 if(loc == -1) {
273 return false;
274 }
275 glUniformMatrix4fv(loc, 1, GL_TRUE, xform[0]);
276 }*/
277 return true;
278 }
280 DrawMode Object::set_draw_mode(DrawMode mode)
281 {
282 DrawMode prev = Object::draw_mode;
283 Object::draw_mode = mode;
284 return prev;
285 }