goat3dgfx

annotate src/object.cc @ 9:25b911c7c35c

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