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