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@20
|
39 goat3d_free(goat);
|
nuclear@12
|
40 return false;
|
nuclear@12
|
41 }
|
nuclear@12
|
42
|
nuclear@12
|
43 int num_meshes = goat3d_get_mesh_count(goat);
|
nuclear@12
|
44 for(int i=0; i<num_meshes; i++) {
|
nuclear@12
|
45 goat3d_mesh *gmesh = goat3d_get_mesh(goat, i);
|
nuclear@13
|
46 Mesh *mesh = load_mesh(gmesh);
|
nuclear@13
|
47 if(mesh) {
|
nuclear@13
|
48 meshes.push_back(mesh);
|
nuclear@13
|
49 }
|
nuclear@12
|
50 }
|
nuclear@12
|
51
|
nuclear@12
|
52 goat3d_free(goat);
|
nuclear@12
|
53 return true;
|
nuclear@12
|
54 }
|
nuclear@12
|
55
|
nuclear@12
|
56 bool Scene::save(const char *fname) const
|
nuclear@12
|
57 {
|
nuclear@12
|
58 return false; // TODO
|
nuclear@12
|
59 }
|
nuclear@12
|
60
|
nuclear@0
|
61 void Scene::draw(long msec) const
|
nuclear@0
|
62 {
|
nuclear@0
|
63 for(size_t i=0; i<objects.size(); i++) {
|
nuclear@0
|
64 objects[i]->draw(msec);
|
nuclear@0
|
65 }
|
nuclear@0
|
66
|
nuclear@0
|
67 if(objects.empty()) {
|
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@20
|
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 }
|