goat3dgfx

annotate src/scene.cc @ 15:7d6b667821cf

wrapped everything in the goatgfx namespace
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 30 Nov 2013 20:52:21 +0200
parents 25bf39105c82
children 6f82b9b6d6c3
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@12 39 return false;
nuclear@12 40 }
nuclear@12 41
nuclear@12 42 int num_meshes = goat3d_get_mesh_count(goat);
nuclear@12 43 for(int i=0; i<num_meshes; i++) {
nuclear@12 44 goat3d_mesh *gmesh = goat3d_get_mesh(goat, i);
nuclear@13 45 Mesh *mesh = load_mesh(gmesh);
nuclear@13 46 if(mesh) {
nuclear@13 47 meshes.push_back(mesh);
nuclear@13 48 }
nuclear@12 49 }
nuclear@12 50
nuclear@12 51 goat3d_free(goat);
nuclear@12 52 return true;
nuclear@12 53 }
nuclear@12 54
nuclear@12 55 bool Scene::save(const char *fname) const
nuclear@12 56 {
nuclear@12 57 return false; // TODO
nuclear@12 58 }
nuclear@12 59
nuclear@0 60 void Scene::draw(long msec) const
nuclear@0 61 {
nuclear@0 62 for(size_t i=0; i<objects.size(); i++) {
nuclear@0 63 objects[i]->draw(msec);
nuclear@0 64 }
nuclear@0 65
nuclear@0 66 if(objects.empty()) {
nuclear@0 67 printf("rendering meshes directly\n");
nuclear@0 68 for(size_t i=0; i<meshes.size(); i++) {
nuclear@0 69 meshes[i]->draw();
nuclear@0 70 }
nuclear@0 71 }
nuclear@0 72 }
nuclear@13 73
nuclear@13 74 static Mesh *load_mesh(goat3d_mesh *gmesh)
nuclear@13 75 {
nuclear@13 76 int nverts = goat3d_get_mesh_attrib_count(gmesh, GOAT3D_MESH_ATTR_VERTEX);
nuclear@13 77 float *data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_VERTEX);
nuclear@13 78
nuclear@13 79 if(!nverts || !data) {
nuclear@13 80 return 0;
nuclear@13 81 }
nuclear@13 82 int nfaces = goat3d_get_mesh_face_count(gmesh);
nuclear@13 83
nuclear@13 84 Mesh *mesh = new Mesh;
nuclear@13 85 const char *name = goat3d_get_mesh_name(gmesh);
nuclear@13 86 if(name) {
nuclear@13 87 mesh->set_name(name);
nuclear@13 88 }
nuclear@13 89
nuclear@13 90 mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, nverts, data);
nuclear@13 91
nuclear@13 92 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_NORMAL))) {
nuclear@13 93 mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, nverts, data);
nuclear@13 94 }
nuclear@13 95 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_TANGENT))) {
nuclear@13 96 mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, nverts, data);
nuclear@13 97 }
nuclear@13 98 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
nuclear@13 99 mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts, data);
nuclear@13 100 }
nuclear@13 101 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_COLOR))) {
nuclear@13 102 mesh->set_attrib_data(MESH_ATTR_COLOR, 4, nverts, data);
nuclear@13 103 }
nuclear@13 104 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_SKIN_WEIGHT))) {
nuclear@13 105 mesh->set_attrib_data(MESH_ATTR_BONEWEIGHTS, 4, nverts, data);
nuclear@13 106 }
nuclear@13 107 int *idata = (int*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_SKIN_MATRIX);
nuclear@13 108 if(idata) {
nuclear@13 109 for(int i=0; i<nverts; i++) {
nuclear@13 110 mesh->set_attrib(MESH_ATTR_BONEIDX, i, Vector4(idata[0], idata[1], idata[2], idata[3]));
nuclear@13 111 idata += 4;
nuclear@13 112 }
nuclear@13 113 }
nuclear@13 114
nuclear@13 115 if(nfaces && !(idata = goat3d_get_mesh_faces(gmesh))) {
nuclear@13 116 mesh->set_index_data(nfaces * 3, (unsigned int*)idata);
nuclear@13 117 }
nuclear@13 118 return mesh;
nuclear@13 119 }