goat3dgfx

annotate src/object.cc @ 34:3eb6c8f89fe1

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