goat3dgfx

view src/scene.cc @ 16:f61cc1df533c

added viewscn example (under dev)
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 30 Nov 2013 20:53:26 +0200
parents 25bf39105c82
children 6f82b9b6d6c3
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 return false;
40 }
42 int num_meshes = goat3d_get_mesh_count(goat);
43 for(int i=0; i<num_meshes; i++) {
44 goat3d_mesh *gmesh = goat3d_get_mesh(goat, i);
45 Mesh *mesh = load_mesh(gmesh);
46 if(mesh) {
47 meshes.push_back(mesh);
48 }
49 }
51 goat3d_free(goat);
52 return true;
53 }
55 bool Scene::save(const char *fname) const
56 {
57 return false; // TODO
58 }
60 void Scene::draw(long msec) const
61 {
62 for(size_t i=0; i<objects.size(); i++) {
63 objects[i]->draw(msec);
64 }
66 if(objects.empty()) {
67 printf("rendering meshes directly\n");
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 }