dungeon_crawler

view prototype/src/tile.cc @ 28:f5fb04fe12cd

moved compiler detection to the configure script
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Aug 2012 20:20:56 +0300
parents 7b65bba15553
children ddb68dc4ba07
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 void Tile::draw_lights(unsigned int draw_mask) const
60 {
61 for(size_t i=0; i<lights.size(); i++) {
62 if(light_side[i] & draw_mask) {
63 lights[i]->draw();
64 }
65 }
66 }
68 /*
69 int Tile::load_lights(const aiScene *scn)
70 {
71 int count = 0;
73 for(int i=0; i<(int)scn->mNumLights; i++) {
74 Light *lt;
75 aiLight *ailt = scn->mLights[i];
77 switch(ailt->mType) {
78 case aiLightSource_POINT:
79 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
80 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
81 ailt->mAttenuationQuadratic);
82 break;
84 case aiLightSource_DIRECTIONAL:
85 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
86 break;
88 default:
89 continue;
90 }
92 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
94 lights.push_back(lt);
95 count++;
96 }
98 return count;
99 }
100 */
102 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
103 {
104 int count = 0;
106 for(int i=0; i<(int)scn->mNumMeshes; i++) {
107 Mesh *mesh = new Mesh;
108 if(!mesh->create(scn, scn->mMeshes[i])) {
109 delete mesh;
110 continue;
111 }
113 Material mat;
114 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
115 mesh->set_material(mat);
117 // retrieve the node pointer
118 const char *name = "<unknown>";
120 auto iter = nmap.find(scn->mMeshes[i]);
121 if(iter != nmap.end()) {
122 aiNode *node = iter->second;
124 Matrix4x4 xform;
125 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
126 xform = *(Matrix4x4*)&node->mTransformation;
127 mesh->set_xform(xform);
129 name = node->mName.data;
130 mesh->set_name(name);
131 }
133 // find which side is this mesh on
134 unsigned int side = 0;
135 if(strstr(name, "NORTH")) {
136 side |= TILE_NORTH;
137 }
138 if(strstr(name, "SOUTH")) {
139 side |= TILE_SOUTH;
140 }
141 if(strstr(name, "EAST")) {
142 side |= TILE_EAST;
143 }
144 if(strstr(name, "WEST")) {
145 side |= TILE_WEST;
146 }
147 if(!side) {
148 side = TILE_ALL;
149 }
151 // what a sordid hack... if the name contains "LIGHT", then make a light out of this
152 // and destroy the mesh...
153 if(strstr(name, "LIGHT")) {
154 PointLight *lt = mesh_to_light(mesh);
155 if(!lt) {
156 fprintf(stderr, "failed to convert mesh %s to light\n", name);
157 } else {
158 lights.push_back(lt);
159 light_side.push_back(side);
160 }
161 delete mesh;
163 } else {
164 meshes.push_back(mesh);
165 mesh_side.push_back(side);
166 count++;
167 }
168 }
169 return count;
170 }
172 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
173 {
174 unsigned int i;
176 for(i=0; i<node->mNumMeshes; i++) {
177 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
179 (*nmap)[m] = node;
180 }
182 for(i=0; i<node->mNumChildren; i++) {
183 build_nodemap(nmap, scn, node->mChildren[i]);
184 }
185 }
187 static PointLight *mesh_to_light(Mesh *m)
188 {
189 Vector3 center = m->get_bsph_center();
190 float rad = m->get_bsph_radius();
192 PointLight *lt = new PointLight(center);
193 lt->set_radius(rad);
195 lt->set_color(m->get_material().kd);
197 return lt;
198 }