goat3dgfx

view src/scene.cc @ 27:be9754def417

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