goat3dgfx

annotate src/scene.cc @ 13:25bf39105c82

lalal
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 27 Nov 2013 08:08:59 +0200
parents 1868c5975f31
children 7d6b667821cf
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@13 5 static Mesh *load_mesh(goat3d_mesh *gmesh);
nuclear@13 6
nuclear@0 7 Scene::~Scene()
nuclear@0 8 {
nuclear@0 9 destroy();
nuclear@0 10 }
nuclear@0 11
nuclear@0 12 void Scene::destroy()
nuclear@0 13 {
nuclear@0 14 for(size_t i=0; i<objects.size(); i++) {
nuclear@0 15 delete objects[i];
nuclear@0 16 }
nuclear@0 17 objects.clear();
nuclear@0 18
nuclear@0 19 for(size_t i=0; i<meshes.size(); i++) {
nuclear@0 20 delete meshes[i];
nuclear@0 21 }
nuclear@0 22 meshes.clear();
nuclear@0 23
nuclear@0 24 for(size_t i=0; i<curves.size(); i++) {
nuclear@0 25 delete curves[i];
nuclear@0 26 }
nuclear@0 27 curves.clear();
nuclear@0 28 }
nuclear@0 29
nuclear@12 30 bool Scene::load(const char *fname)
nuclear@12 31 {
nuclear@12 32 goat3d *goat = goat3d_create();
nuclear@12 33 if(!goat) {
nuclear@12 34 return false;
nuclear@12 35 }
nuclear@12 36 if(goat3d_load(goat, fname) == -1) {
nuclear@12 37 return false;
nuclear@12 38 }
nuclear@12 39
nuclear@12 40 int num_meshes = goat3d_get_mesh_count(goat);
nuclear@12 41 for(int i=0; i<num_meshes; i++) {
nuclear@12 42 goat3d_mesh *gmesh = goat3d_get_mesh(goat, i);
nuclear@13 43 Mesh *mesh = load_mesh(gmesh);
nuclear@13 44 if(mesh) {
nuclear@13 45 meshes.push_back(mesh);
nuclear@13 46 }
nuclear@12 47 }
nuclear@12 48
nuclear@12 49 goat3d_free(goat);
nuclear@12 50 return true;
nuclear@12 51 }
nuclear@12 52
nuclear@12 53 bool Scene::save(const char *fname) const
nuclear@12 54 {
nuclear@12 55 return false; // TODO
nuclear@12 56 }
nuclear@12 57
nuclear@0 58 void Scene::draw(long msec) const
nuclear@0 59 {
nuclear@0 60 for(size_t i=0; i<objects.size(); i++) {
nuclear@0 61 objects[i]->draw(msec);
nuclear@0 62 }
nuclear@0 63
nuclear@0 64 if(objects.empty()) {
nuclear@0 65 printf("rendering meshes directly\n");
nuclear@0 66 for(size_t i=0; i<meshes.size(); i++) {
nuclear@0 67 meshes[i]->draw();
nuclear@0 68 }
nuclear@0 69 }
nuclear@0 70 }
nuclear@13 71
nuclear@13 72 static Mesh *load_mesh(goat3d_mesh *gmesh)
nuclear@13 73 {
nuclear@13 74 int nverts = goat3d_get_mesh_attrib_count(gmesh, GOAT3D_MESH_ATTR_VERTEX);
nuclear@13 75 float *data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_VERTEX);
nuclear@13 76
nuclear@13 77 if(!nverts || !data) {
nuclear@13 78 return 0;
nuclear@13 79 }
nuclear@13 80 int nfaces = goat3d_get_mesh_face_count(gmesh);
nuclear@13 81
nuclear@13 82 Mesh *mesh = new Mesh;
nuclear@13 83 const char *name = goat3d_get_mesh_name(gmesh);
nuclear@13 84 if(name) {
nuclear@13 85 mesh->set_name(name);
nuclear@13 86 }
nuclear@13 87
nuclear@13 88 mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, nverts, data);
nuclear@13 89
nuclear@13 90 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_NORMAL))) {
nuclear@13 91 mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, nverts, data);
nuclear@13 92 }
nuclear@13 93 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_TANGENT))) {
nuclear@13 94 mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, nverts, data);
nuclear@13 95 }
nuclear@13 96 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
nuclear@13 97 mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts, data);
nuclear@13 98 }
nuclear@13 99 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_COLOR))) {
nuclear@13 100 mesh->set_attrib_data(MESH_ATTR_COLOR, 4, nverts, data);
nuclear@13 101 }
nuclear@13 102 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_SKIN_WEIGHT))) {
nuclear@13 103 mesh->set_attrib_data(MESH_ATTR_BONEWEIGHTS, 4, nverts, data);
nuclear@13 104 }
nuclear@13 105 int *idata = (int*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_SKIN_MATRIX);
nuclear@13 106 if(idata) {
nuclear@13 107 for(int i=0; i<nverts; i++) {
nuclear@13 108 mesh->set_attrib(MESH_ATTR_BONEIDX, i, Vector4(idata[0], idata[1], idata[2], idata[3]));
nuclear@13 109 idata += 4;
nuclear@13 110 }
nuclear@13 111 }
nuclear@13 112
nuclear@13 113 if(nfaces && !(idata = goat3d_get_mesh_faces(gmesh))) {
nuclear@13 114 mesh->set_index_data(nfaces * 3, (unsigned int*)idata);
nuclear@13 115 }
nuclear@13 116 return mesh;
nuclear@13 117 }