dungeon_crawler

view prototype/src/tile.cc @ 31:ddb68dc4ba07

OBJ hack
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 27 Aug 2012 04:14:04 +0300
parents fa8f89d06f6f
children d0e93b4d9ec9
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 bool ass_obj_hack;
17 Tile::Tile(TileSet *tileset)
18 {
19 tset = tileset;
20 }
22 bool Tile::load(const char *fname)
23 {
24 if(!fname) {
25 return false;
26 }
28 char *saved_fname = (char*)alloca(strlen(fname) + 1);
29 strcpy(saved_fname, fname);
31 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
32 aiProcess_Triangulate |
33 aiProcess_SortByPType |
34 aiProcess_FlipUVs;
36 const aiScene *scn = aiImportFile(fname, proc_flags);
37 if(!scn) {
38 fprintf(stderr, "failed to load tile: %s\n", fname);
39 return -1;
40 }
42 map<aiMesh*, aiNode*> nodemap;
43 build_nodemap(&nodemap, scn, scn->mRootNode);
45 if(strstr(fname, ".obj") == fname + strlen(fname) - 4) {
46 ass_obj_hack = true;
47 } else {
48 ass_obj_hack = false;
49 }
51 //load_lights(scn);
52 load_meshes(scn, nodemap);
54 printf("loaded tile %s: %d meshes, %d lights\n", saved_fname, (int)meshes.size(), (int)lights.size());
55 return true;
56 }
58 void Tile::draw(unsigned int draw_mask) const
59 {
60 for(size_t i=0; i<meshes.size(); i++) {
61 if(mesh_side[i] & draw_mask) {
62 meshes[i]->draw();
63 }
64 }
65 }
67 void Tile::draw_lights(unsigned int draw_mask) const
68 {
69 for(size_t i=0; i<lights.size(); i++) {
70 if(light_side[i] & draw_mask) {
71 lights[i]->draw();
72 }
73 }
74 }
76 /*
77 int Tile::load_lights(const aiScene *scn)
78 {
79 int count = 0;
81 for(int i=0; i<(int)scn->mNumLights; i++) {
82 Light *lt;
83 aiLight *ailt = scn->mLights[i];
85 switch(ailt->mType) {
86 case aiLightSource_POINT:
87 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
88 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
89 ailt->mAttenuationQuadratic);
90 break;
92 case aiLightSource_DIRECTIONAL:
93 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
94 break;
96 default:
97 continue;
98 }
100 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
102 lights.push_back(lt);
103 count++;
104 }
106 return count;
107 }
108 */
110 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
111 {
112 int count = 0;
114 for(int i=0; i<(int)scn->mNumMeshes; i++) {
115 Mesh *mesh = new Mesh;
116 if(!mesh->create(scn, scn->mMeshes[i])) {
117 delete mesh;
118 continue;
119 }
121 Material mat;
122 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
123 mesh->set_material(mat);
125 // retrieve the node pointer
126 const char *name = "<unknown>";
128 auto iter = nmap.find(scn->mMeshes[i]);
129 if(iter != nmap.end()) {
130 aiNode *node = iter->second;
132 Matrix4x4 xform;
133 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
134 xform = *(Matrix4x4*)&node->mTransformation;
135 mesh->set_xform(xform);
137 name = node->mName.data;
138 mesh->set_name(name);
139 }
141 // find which side is this mesh on
142 unsigned int side = 0;
143 if(strstr(name, "NORTH")) {
144 side |= TILE_NORTH;
145 }
146 if(strstr(name, "SOUTH")) {
147 side |= TILE_SOUTH;
148 }
149 if(strstr(name, "EAST")) {
150 side |= TILE_EAST;
151 }
152 if(strstr(name, "WEST")) {
153 side |= TILE_WEST;
154 }
155 if(!side) {
156 side = TILE_ALL;
157 }
159 // what a sordid hack... if the name contains "LIGHT", then make a light out of this
160 // and destroy the mesh...
161 if(strstr(name, "LIGHT")) {
162 PointLight *lt = mesh_to_light(mesh);
163 if(!lt) {
164 fprintf(stderr, "failed to convert mesh %s to light\n", name);
165 } else {
166 lights.push_back(lt);
167 light_side.push_back(side);
168 }
169 delete mesh;
171 } else {
172 meshes.push_back(mesh);
173 mesh_side.push_back(side);
174 count++;
175 }
176 }
177 return count;
178 }
180 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
181 {
182 unsigned int i;
184 for(i=0; i<node->mNumMeshes; i++) {
185 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
187 (*nmap)[m] = node;
188 }
190 for(i=0; i<node->mNumChildren; i++) {
191 build_nodemap(nmap, scn, node->mChildren[i]);
192 }
193 }
195 static PointLight *mesh_to_light(Mesh *m)
196 {
197 Vector3 center = m->get_bsph_center();
198 float rad = m->get_bsph_radius();
200 PointLight *lt = new PointLight(center);
201 lt->set_radius(rad);
203 lt->set_color(m->get_material().kd);
205 return lt;
206 }