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