dungeon_crawler

view prototype/src/tile.cc @ 22:7b65bba15553

minor
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 21 Aug 2012 06:34:14 +0300
parents 0588f8a1a351
children fa8f89d06f6f
line source
1 #include <stdio.h>
2 #include <map>
3 #include "opengl.h"
4 #include <assimp/cimport.h>
5 #include <assimp/scene.h>
6 #include <assimp/postprocess.h>
7 #include "tile.h"
8 #include "tileset.h"
10 using std::map;
12 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node);
13 static PointLight *mesh_to_light(Mesh *m);
15 Tile::Tile(TileSet *tileset)
16 {
17 tset = tileset;
18 }
20 bool Tile::load(const char *fname)
21 {
22 if(!fname) {
23 return false;
24 }
26 char *saved_fname = (char*)alloca(strlen(fname) + 1);
27 strcpy(saved_fname, fname);
29 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
30 aiProcess_Triangulate |
31 aiProcess_SortByPType |
32 aiProcess_FlipUVs;
34 const aiScene *scn = aiImportFile(fname, proc_flags);
35 if(!scn) {
36 fprintf(stderr, "failed to load tile: %s\n", fname);
37 return -1;
38 }
40 map<aiMesh*, aiNode*> nodemap;
41 build_nodemap(&nodemap, scn, scn->mRootNode);
43 //load_lights(scn);
44 load_meshes(scn, nodemap);
46 printf("loaded tile %s: %d meshes, %d lights\n", saved_fname, (int)meshes.size(), (int)lights.size());
47 return true;
48 }
50 void Tile::draw(unsigned int draw_mask) const
51 {
52 for(size_t i=0; i<meshes.size(); i++) {
53 if(mesh_side[i] & draw_mask) {
54 meshes[i]->draw();
55 }
56 }
57 }
59 /*
60 int Tile::load_lights(const aiScene *scn)
61 {
62 int count = 0;
64 for(int i=0; i<(int)scn->mNumLights; i++) {
65 Light *lt;
66 aiLight *ailt = scn->mLights[i];
68 switch(ailt->mType) {
69 case aiLightSource_POINT:
70 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
71 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
72 ailt->mAttenuationQuadratic);
73 break;
75 case aiLightSource_DIRECTIONAL:
76 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
77 break;
79 default:
80 continue;
81 }
83 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
85 lights.push_back(lt);
86 count++;
87 }
89 return count;
90 }
91 */
93 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
94 {
95 int count = 0;
97 for(int i=0; i<(int)scn->mNumMeshes; i++) {
98 Mesh *mesh = new Mesh;
99 if(!mesh->create(scn, scn->mMeshes[i])) {
100 delete mesh;
101 continue;
102 }
104 Material mat;
105 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
106 mesh->set_material(mat);
108 // retrieve the node pointer
109 const char *name = "<unknown>";
111 auto iter = nmap.find(scn->mMeshes[i]);
112 if(iter != nmap.end()) {
113 aiNode *node = iter->second;
115 Matrix4x4 xform;
116 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
117 xform = *(Matrix4x4*)&node->mTransformation;
118 mesh->set_xform(xform);
120 name = node->mName.data;
121 mesh->set_name(name);
122 }
124 // find which side is this mesh on
125 unsigned int side = 0;
126 if(strstr(name, "NORTH")) {
127 side |= TILE_NORTH;
128 }
129 if(strstr(name, "SOUTH")) {
130 side |= TILE_SOUTH;
131 }
132 if(strstr(name, "EAST")) {
133 side |= TILE_EAST;
134 }
135 if(strstr(name, "WEST")) {
136 side |= TILE_WEST;
137 }
138 if(!side) {
139 side = TILE_ALL;
140 }
142 // what a sordid hack... if the name contains "LIGHT", then make a light out of this
143 // and destroy the mesh...
144 if(strstr(name, "LIGHT")) {
145 PointLight *lt = mesh_to_light(mesh);
146 if(!lt) {
147 fprintf(stderr, "failed to convert mesh %s to light\n", name);
148 } else {
149 lights.push_back(lt);
150 light_side.push_back(side);
151 }
152 delete mesh;
154 } else {
155 meshes.push_back(mesh);
156 mesh_side.push_back(side);
157 count++;
158 }
159 }
160 return count;
161 }
163 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
164 {
165 unsigned int i;
167 for(i=0; i<node->mNumMeshes; i++) {
168 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
170 (*nmap)[m] = node;
171 }
173 for(i=0; i<node->mNumChildren; i++) {
174 build_nodemap(nmap, scn, node->mChildren[i]);
175 }
176 }
178 static PointLight *mesh_to_light(Mesh *m)
179 {
180 Vector3 center = m->get_bsph_center();
181 float rad = m->get_bsph_radius();
183 PointLight *lt = new PointLight(center);
184 lt->set_radius(rad);
186 lt->set_color(m->get_material().kd);
188 return lt;
189 }