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