goat3dgfx

view src/object.cc @ 33:253542d715f4

default texture
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Mar 2014 06:32:37 +0200
parents 1873dfd13f2d
children
line source
1 #include <float.h>
2 #include "object.h"
3 #include "opengl.h"
4 #include "unistate.h"
5 #include "logger.h"
7 using namespace goatgfx;
9 static void destroy_all_rec(XFormNode *node);
10 static void get_all_meshes_rec(XFormNode *node, std::list<Mesh*> *reslist);
12 DrawMode Object::draw_mode = DRAW_DEFAULT;
14 Object::Object()
15 {
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 Object *obj = dynamic_cast<Object*>(node);
72 if(obj) {
73 Mesh *mesh = obj->get_mesh();
74 if(mesh) {
75 reslist->push_back(mesh);
76 }
77 }
79 for(int i=0; i<node->get_children_count(); i++) {
80 get_all_meshes_rec(node->get_child(i), reslist);
81 }
82 }
84 AABox Object::get_aabbox() const
85 {
86 AABox box;
88 if(mesh) {
89 box = mesh->get_aabbox();
90 } else {
91 box.min = Vector3(FLT_MAX, FLT_MAX, FLT_MAX);
92 box.max = -box.min;
93 }
95 int num_children = get_children_count();
96 for(int i=0; i<num_children; i++) {
97 const Object *obj = dynamic_cast<const Object*>(get_child(i));
98 if(obj) {
99 AABox child_box = obj->get_aabbox();
100 box.set_union(&box, &child_box);
101 }
102 }
103 return box;
104 }
106 Sphere Object::get_bsphere() const
107 {
108 Sphere sph;
109 bool valid = false;
111 if(mesh) {
112 sph = mesh->get_bsphere();
113 valid = true;
114 } else {
115 sph.radius = 0.0;
116 }
118 int num_children = get_children_count();
119 for(int i=0; i<num_children; i++) {
120 const Object *obj = dynamic_cast<const Object*>(get_child(i));
121 if(obj) {
122 Sphere child_sph = obj->get_bsphere();
123 if(valid) {
124 sph.set_union(&sph, &child_sph);
125 } else {
126 sph = child_sph;
127 valid = true;
128 }
129 }
130 }
132 return sph;
133 }
135 /*static const char *attr_name[] = {
136 "attr_vertex",
137 "attr_normal",
138 "attr_tangent",
139 "attr_texcoord",
140 "attr_boneweights",
141 "attr_boneidx"
142 };*/
144 void Object::draw(long msec) const
145 {
146 Matrix4x4 xform;
147 get_xform(msec, &xform);
149 set_world_matrix(xform);
151 if(mesh) {
152 /*unsigned int prog = sdrprog;
154 if(mesh->get_bones_count() > 0) {
155 prog = sdrprog_skin;
156 }
158 glUseProgram(prog);
159 // get all the attribute locations
160 for(int i=0; i<NUM_MESH_ATTR; i++) {
161 int loc = glGetAttribLocation(prog, attr_name[i]);
162 if(loc != -1) {
163 Mesh::set_attrib_location(i, loc);
164 }
165 }
167 setup_bones(msec);
169 glUseProgram(prog);*/
171 material.setup();
172 setup_unistate(); // set all state uniforms
174 switch(Object::draw_mode) {
175 case DRAW_WIREFRAME:
176 mesh->draw_wire();
177 break;
179 case DRAW_VERTICES:
180 mesh->draw_vertices();
181 break;
183 case DRAW_DEFAULT:
184 default:
185 mesh->draw();
186 }
187 }
189 int num_children = get_children_count();
190 for(int i=0; i<num_children; i++) {
191 const Object *obj = dynamic_cast<const Object*>(get_child(i));
192 if(obj) {
193 obj->draw(msec);
194 }
195 }
196 }
199 bool Object::intersect(const Ray &inray, HitPoint *hit) const
200 {
201 Ray ray = inray;
202 Matrix4x4 xform, inv_xform;
203 get_xform(ray.time, &xform/*, &inv_xform*/);
204 ray.transform(xform.inverse()); // TODO find out what's wrong with get_xform's inv_xform and use that
206 HitPoint nearest_hit;
207 nearest_hit.dist = FLT_MAX;
208 nearest_hit.obj = 0;
210 if(mesh) {
211 if(mesh->intersect(ray, hit ? &nearest_hit : 0)) {
212 if(!hit) {
213 return true;
214 }
216 if(Mesh::get_intersect_mode() & ISECT_FACE) {
217 Triangle *face = (Triangle*)nearest_hit.obj;
218 face->transform(xform);
219 } else if(Mesh::get_intersect_mode() & ISECT_VERTICES) {
220 Vector3 *v = (Vector3*)nearest_hit.obj;
221 v->transform(xform);
222 } else {
223 nearest_hit.obj = this;
224 }
225 }
226 }
228 int num_children = get_children_count();
229 for(int i=0; i<num_children; i++) {
230 const Object *obj = dynamic_cast<const Object*>(get_child(i));
232 HitPoint chit;
233 if(obj && obj->intersect(inray, hit ? &chit : 0)) {
234 if(!hit) {
235 return true;
236 }
238 if(chit.dist < nearest_hit.dist) {
239 nearest_hit = chit;
240 }
241 }
242 }
244 if(nearest_hit.obj) {
245 if(hit) {
246 *hit = nearest_hit;
247 }
248 return true;
249 }
250 return false;
251 }
254 bool Object::setup_bones(long msec) const
255 {
256 int num_bones;
257 if(!mesh || !(num_bones = mesh->get_bones_count())) {
258 return false;
259 }
261 /*char uniname[32];
263 for(int i=0; i<num_bones; i++) {
264 const XFormNode *bone = mesh->get_bone(i);
266 Matrix4x4 xform;
267 bone->get_xform(msec, &xform);
269 xform = xform * bone->get_bone_matrix();
271 sprintf(uniname, "bone_xform[%d]", i);
272 int loc = glGetUniformLocation(sdrprog_skin, uniname);
273 if(loc == -1) {
274 return false;
275 }
276 glUniformMatrix4fv(loc, 1, GL_TRUE, xform[0]);
277 }*/
278 return true;
279 }
281 DrawMode Object::set_draw_mode(DrawMode mode)
282 {
283 DrawMode prev = Object::draw_mode;
284 Object::draw_mode = mode;
285 return prev;
286 }