goat3dgfx

annotate src/scene.cc @ 34:3eb6c8f89fe1

merge
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Mar 2014 17:41:10 +0200
parents d9c8cd19c606
children
rev   line source
nuclear@0 1 #include "scene.h"
nuclear@0 2 #include "opengl.h"
nuclear@12 3 #include "goat3d.h"
nuclear@0 4
nuclear@15 5 using namespace goatgfx;
nuclear@15 6
nuclear@13 7 static Mesh *load_mesh(goat3d_mesh *gmesh);
nuclear@13 8
nuclear@0 9 Scene::~Scene()
nuclear@0 10 {
nuclear@0 11 destroy();
nuclear@0 12 }
nuclear@0 13
nuclear@0 14 void Scene::destroy()
nuclear@0 15 {
nuclear@0 16 for(size_t i=0; i<objects.size(); i++) {
nuclear@0 17 delete objects[i];
nuclear@0 18 }
nuclear@0 19 objects.clear();
nuclear@0 20
nuclear@0 21 for(size_t i=0; i<meshes.size(); i++) {
nuclear@0 22 delete meshes[i];
nuclear@0 23 }
nuclear@0 24 meshes.clear();
nuclear@0 25
nuclear@0 26 for(size_t i=0; i<curves.size(); i++) {
nuclear@0 27 delete curves[i];
nuclear@0 28 }
nuclear@0 29 curves.clear();
nuclear@0 30 }
nuclear@0 31
nuclear@12 32 bool Scene::load(const char *fname)
nuclear@12 33 {
nuclear@12 34 goat3d *goat = goat3d_create();
nuclear@12 35 if(!goat) {
nuclear@12 36 return false;
nuclear@12 37 }
nuclear@12 38 if(goat3d_load(goat, fname) == -1) {
nuclear@20 39 goat3d_free(goat);
nuclear@12 40 return false;
nuclear@12 41 }
nuclear@12 42
nuclear@28 43 // load all the meshes
nuclear@12 44 int num_meshes = goat3d_get_mesh_count(goat);
nuclear@12 45 for(int i=0; i<num_meshes; i++) {
nuclear@12 46 goat3d_mesh *gmesh = goat3d_get_mesh(goat, i);
nuclear@13 47 Mesh *mesh = load_mesh(gmesh);
nuclear@13 48 if(mesh) {
nuclear@13 49 meshes.push_back(mesh);
nuclear@13 50 }
nuclear@12 51 }
nuclear@12 52
nuclear@28 53 // load all the nodes recursively
nuclear@28 54 int num_nodes = goat3d_get_node_count(goat);
nuclear@28 55 for(int i=0; i<num_nodes; i++) {
nuclear@28 56 goat3d_node *gnode = goat3d_get_node(goat, i);
nuclear@28 57 }
nuclear@28 58
nuclear@12 59 goat3d_free(goat);
nuclear@12 60 return true;
nuclear@12 61 }
nuclear@12 62
nuclear@12 63 bool Scene::save(const char *fname) const
nuclear@12 64 {
nuclear@12 65 return false; // TODO
nuclear@12 66 }
nuclear@12 67
nuclear@0 68 void Scene::draw(long msec) const
nuclear@0 69 {
nuclear@0 70 for(size_t i=0; i<objects.size(); i++) {
nuclear@0 71 objects[i]->draw(msec);
nuclear@0 72 }
nuclear@0 73
nuclear@28 74 // if there are no objects in the scene, just draw the meshes instead
nuclear@0 75 if(objects.empty()) {
nuclear@0 76 for(size_t i=0; i<meshes.size(); i++) {
nuclear@0 77 meshes[i]->draw();
nuclear@0 78 }
nuclear@0 79 }
nuclear@0 80 }
nuclear@13 81
nuclear@13 82 static Mesh *load_mesh(goat3d_mesh *gmesh)
nuclear@13 83 {
nuclear@13 84 int nverts = goat3d_get_mesh_attrib_count(gmesh, GOAT3D_MESH_ATTR_VERTEX);
nuclear@13 85 float *data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_VERTEX);
nuclear@13 86
nuclear@13 87 if(!nverts || !data) {
nuclear@13 88 return 0;
nuclear@13 89 }
nuclear@13 90 int nfaces = goat3d_get_mesh_face_count(gmesh);
nuclear@13 91
nuclear@13 92 Mesh *mesh = new Mesh;
nuclear@13 93 const char *name = goat3d_get_mesh_name(gmesh);
nuclear@13 94 if(name) {
nuclear@13 95 mesh->set_name(name);
nuclear@13 96 }
nuclear@13 97
nuclear@13 98 mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, nverts, data);
nuclear@13 99
nuclear@13 100 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_NORMAL))) {
nuclear@13 101 mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, nverts, data);
nuclear@13 102 }
nuclear@13 103 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_TANGENT))) {
nuclear@13 104 mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, nverts, data);
nuclear@13 105 }
nuclear@13 106 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
nuclear@13 107 mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts, data);
nuclear@13 108 }
nuclear@13 109 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_COLOR))) {
nuclear@13 110 mesh->set_attrib_data(MESH_ATTR_COLOR, 4, nverts, data);
nuclear@13 111 }
nuclear@13 112 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_SKIN_WEIGHT))) {
nuclear@13 113 mesh->set_attrib_data(MESH_ATTR_BONEWEIGHTS, 4, nverts, data);
nuclear@13 114 }
nuclear@13 115 int *idata = (int*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_SKIN_MATRIX);
nuclear@13 116 if(idata) {
nuclear@13 117 for(int i=0; i<nverts; i++) {
nuclear@13 118 mesh->set_attrib(MESH_ATTR_BONEIDX, i, Vector4(idata[0], idata[1], idata[2], idata[3]));
nuclear@13 119 idata += 4;
nuclear@13 120 }
nuclear@13 121 }
nuclear@13 122
nuclear@20 123 if(nfaces && (idata = goat3d_get_mesh_faces(gmesh))) {
nuclear@13 124 mesh->set_index_data(nfaces * 3, (unsigned int*)idata);
nuclear@13 125 }
nuclear@13 126 return mesh;
nuclear@13 127 }