goat3dgfx
view 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 |
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 // load all the meshes
44 int num_meshes = goat3d_get_mesh_count(goat);
45 for(int i=0; i<num_meshes; i++) {
46 goat3d_mesh *gmesh = goat3d_get_mesh(goat, i);
47 Mesh *mesh = load_mesh(gmesh);
48 if(mesh) {
49 meshes.push_back(mesh);
50 }
51 }
53 // load all the nodes recursively
54 int num_nodes = goat3d_get_node_count(goat);
55 for(int i=0; i<num_nodes; i++) {
56 goat3d_node *gnode = goat3d_get_node(goat, i);
57 }
59 goat3d_free(goat);
60 return true;
61 }
63 bool Scene::save(const char *fname) const
64 {
65 return false; // TODO
66 }
68 void Scene::draw(long msec) const
69 {
70 for(size_t i=0; i<objects.size(); i++) {
71 objects[i]->draw(msec);
72 }
74 // if there are no objects in the scene, just draw the meshes instead
75 if(objects.empty()) {
76 for(size_t i=0; i<meshes.size(); i++) {
77 meshes[i]->draw();
78 }
79 }
80 }
82 static Mesh *load_mesh(goat3d_mesh *gmesh)
83 {
84 int nverts = goat3d_get_mesh_attrib_count(gmesh, GOAT3D_MESH_ATTR_VERTEX);
85 float *data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_VERTEX);
87 if(!nverts || !data) {
88 return 0;
89 }
90 int nfaces = goat3d_get_mesh_face_count(gmesh);
92 Mesh *mesh = new Mesh;
93 const char *name = goat3d_get_mesh_name(gmesh);
94 if(name) {
95 mesh->set_name(name);
96 }
98 mesh->set_attrib_data(MESH_ATTR_VERTEX, 3, nverts, data);
100 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_NORMAL))) {
101 mesh->set_attrib_data(MESH_ATTR_NORMAL, 3, nverts, data);
102 }
103 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_TANGENT))) {
104 mesh->set_attrib_data(MESH_ATTR_TANGENT, 3, nverts, data);
105 }
106 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_TEXCOORD))) {
107 mesh->set_attrib_data(MESH_ATTR_TEXCOORD, 2, nverts, data);
108 }
109 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_COLOR))) {
110 mesh->set_attrib_data(MESH_ATTR_COLOR, 4, nverts, data);
111 }
112 if((data = (float*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_SKIN_WEIGHT))) {
113 mesh->set_attrib_data(MESH_ATTR_BONEWEIGHTS, 4, nverts, data);
114 }
115 int *idata = (int*)goat3d_get_mesh_attribs(gmesh, GOAT3D_MESH_ATTR_SKIN_MATRIX);
116 if(idata) {
117 for(int i=0; i<nverts; i++) {
118 mesh->set_attrib(MESH_ATTR_BONEIDX, i, Vector4(idata[0], idata[1], idata[2], idata[3]));
119 idata += 4;
120 }
121 }
123 if(nfaces && (idata = goat3d_get_mesh_faces(gmesh))) {
124 mesh->set_index_data(nfaces * 3, (unsigned int*)idata);
125 }
126 return mesh;
127 }