rev |
line source |
nuclear@1
|
1 #include <stdio.h>
|
nuclear@7
|
2 #include <map>
|
nuclear@1
|
3 #include "opengl.h"
|
nuclear@7
|
4 #include <assimp/cimport.h>
|
nuclear@7
|
5 #include <assimp/scene.h>
|
nuclear@7
|
6 #include <assimp/postprocess.h>
|
nuclear@1
|
7 #include "tile.h"
|
nuclear@1
|
8
|
nuclear@7
|
9 using std::map;
|
nuclear@7
|
10
|
nuclear@7
|
11 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node);
|
nuclear@7
|
12
|
nuclear@7
|
13
|
nuclear@1
|
14 bool Tile::load(const char *fname)
|
nuclear@1
|
15 {
|
nuclear@5
|
16 if(!fname) {
|
nuclear@5
|
17 return false;
|
nuclear@5
|
18 }
|
nuclear@5
|
19
|
nuclear@3
|
20 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
|
nuclear@7
|
21 aiProcess_Triangulate |
|
nuclear@7
|
22 aiProcess_GenNormals |
|
nuclear@7
|
23 aiProcess_SortByPType |
|
nuclear@7
|
24 aiProcess_FlipUVs;
|
nuclear@4
|
25
|
nuclear@3
|
26 const aiScene *scn = aiImportFile(fname, proc_flags);
|
nuclear@3
|
27 if(!scn) {
|
nuclear@3
|
28 fprintf(stderr, "failed to load tile: %s\n", fname);
|
nuclear@3
|
29 return -1;
|
nuclear@3
|
30 }
|
nuclear@3
|
31
|
nuclear@7
|
32 map<aiMesh*, aiNode*> nodemap;
|
nuclear@7
|
33 build_nodemap(&nodemap, scn, scn->mRootNode);
|
nuclear@7
|
34
|
nuclear@3
|
35 load_lights(scn);
|
nuclear@7
|
36 load_meshes(scn, nodemap);
|
nuclear@3
|
37
|
nuclear@3
|
38 printf("loaded tile %s: %d meshes, %d lights\n", fname, scn->mNumMeshes, scn->mNumLights);
|
nuclear@1
|
39 return true;
|
nuclear@1
|
40 }
|
nuclear@1
|
41
|
nuclear@5
|
42 void Tile::draw(unsigned int draw_mask) const
|
nuclear@1
|
43 {
|
nuclear@4
|
44 for(size_t i=0; i<meshes.size(); i++) {
|
nuclear@4
|
45 if(mesh_side[i] & draw_mask) {
|
nuclear@5
|
46 meshes[i]->draw();
|
nuclear@4
|
47 }
|
nuclear@4
|
48 }
|
nuclear@4
|
49 }
|
nuclear@1
|
50
|
nuclear@4
|
51 int Tile::load_lights(const aiScene *scn)
|
nuclear@4
|
52 {
|
nuclear@4
|
53 int count = 0;
|
nuclear@4
|
54
|
nuclear@4
|
55 for(int i=0; i<(int)scn->mNumLights; i++) {
|
nuclear@4
|
56 Light *lt;
|
nuclear@4
|
57 aiLight *ailt = scn->mLights[i];
|
nuclear@4
|
58
|
nuclear@4
|
59 switch(ailt->mType) {
|
nuclear@4
|
60 case aiLightSource_POINT:
|
nuclear@4
|
61 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
|
nuclear@4
|
62 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
|
nuclear@4
|
63 ailt->mAttenuationQuadratic);
|
nuclear@4
|
64 break;
|
nuclear@4
|
65
|
nuclear@4
|
66 case aiLightSource_DIRECTIONAL:
|
nuclear@4
|
67 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
|
nuclear@4
|
68 break;
|
nuclear@4
|
69
|
nuclear@4
|
70 default:
|
nuclear@4
|
71 continue;
|
nuclear@4
|
72 }
|
nuclear@4
|
73
|
nuclear@4
|
74 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
|
nuclear@4
|
75
|
nuclear@4
|
76 lights.push_back(lt);
|
nuclear@4
|
77 count++;
|
nuclear@4
|
78 }
|
nuclear@4
|
79
|
nuclear@4
|
80 return count;
|
nuclear@1
|
81 }
|
nuclear@4
|
82
|
nuclear@7
|
83 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
|
nuclear@4
|
84 {
|
nuclear@4
|
85 int count = 0;
|
nuclear@4
|
86
|
nuclear@4
|
87 for(int i=0; i<(int)scn->mNumMeshes; i++) {
|
nuclear@4
|
88 Mesh *mesh = new Mesh;
|
nuclear@4
|
89 if(!mesh->create(scn, scn->mMeshes[i])) {
|
nuclear@4
|
90 delete mesh;
|
nuclear@4
|
91 continue;
|
nuclear@4
|
92 }
|
nuclear@4
|
93
|
nuclear@7
|
94 // retrieve the node pointer
|
nuclear@7
|
95 const char *name = "<unknown>";
|
nuclear@7
|
96
|
nuclear@7
|
97 auto iter = nmap.find(scn->mMeshes[i]);
|
nuclear@7
|
98 if(iter != nmap.end()) {
|
nuclear@7
|
99 aiNode *node = iter->second;
|
nuclear@7
|
100
|
nuclear@7
|
101 Matrix4x4 xform;
|
nuclear@7
|
102 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
|
nuclear@7
|
103 xform = *(Matrix4x4*)&node->mTransformation;
|
nuclear@7
|
104 mesh->set_xform(xform);
|
nuclear@7
|
105
|
nuclear@7
|
106 name = node->mName.data;
|
nuclear@7
|
107 mesh->set_name(name);
|
nuclear@7
|
108 }
|
nuclear@4
|
109 meshes.push_back(mesh);
|
nuclear@4
|
110
|
nuclear@7
|
111
|
nuclear@7
|
112 printf("name: \"%s\" ... ", name);
|
nuclear@7
|
113
|
nuclear@4
|
114 // find which side is this mesh on
|
nuclear@7
|
115 unsigned int side = 0;
|
nuclear@7
|
116 if(strstr(name, "NORTH")) {
|
nuclear@7
|
117 side |= TILE_NORTH;
|
nuclear@7
|
118 }
|
nuclear@7
|
119 if(strstr(name, "SOUTH")) {
|
nuclear@7
|
120 side |= TILE_SOUTH;
|
nuclear@7
|
121 }
|
nuclear@7
|
122 if(strstr(name, "EAST")) {
|
nuclear@7
|
123 side |= TILE_EAST;
|
nuclear@7
|
124 }
|
nuclear@7
|
125 if(strstr(name, "WEST")) {
|
nuclear@7
|
126 side |= TILE_WEST;
|
nuclear@7
|
127 }
|
nuclear@7
|
128 if(!side) {
|
nuclear@4
|
129 side = TILE_ALL;
|
nuclear@4
|
130 }
|
nuclear@7
|
131 printf("side: 0x0%x\n", side);
|
nuclear@4
|
132 mesh_side.push_back(side);
|
nuclear@4
|
133
|
nuclear@4
|
134 count++;
|
nuclear@4
|
135 }
|
nuclear@4
|
136 return count;
|
nuclear@4
|
137 }
|
nuclear@7
|
138
|
nuclear@7
|
139 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
|
nuclear@7
|
140 {
|
nuclear@7
|
141 unsigned int i;
|
nuclear@7
|
142
|
nuclear@7
|
143 for(i=0; i<node->mNumMeshes; i++) {
|
nuclear@7
|
144 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
|
nuclear@7
|
145
|
nuclear@7
|
146 (*nmap)[m] = node;
|
nuclear@7
|
147 }
|
nuclear@7
|
148
|
nuclear@7
|
149 for(i=0; i<node->mNumChildren; i++) {
|
nuclear@7
|
150 build_nodemap(nmap, scn, node->mChildren[i]);
|
nuclear@7
|
151 }
|
nuclear@7
|
152 }
|