dungeon_crawler

view prototype/src/tile.cc @ 20:48cfd351a9be

mrt going on nicely
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 21 Aug 2012 04:57:33 +0300
parents e95462632f9a
children 0588f8a1a351
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);
14 Tile::Tile(TileSet *tileset)
15 {
16 tset = tileset;
17 }
19 bool Tile::load(const char *fname)
20 {
21 if(!fname) {
22 return false;
23 }
25 char *saved_fname = (char*)alloca(strlen(fname) + 1);
26 strcpy(saved_fname, fname);
28 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
29 aiProcess_Triangulate |
30 aiProcess_SortByPType |
31 aiProcess_FlipUVs;
33 const aiScene *scn = aiImportFile(fname, proc_flags);
34 if(!scn) {
35 fprintf(stderr, "failed to load tile: %s\n", fname);
36 return -1;
37 }
39 map<aiMesh*, aiNode*> nodemap;
40 build_nodemap(&nodemap, scn, scn->mRootNode);
42 load_lights(scn);
43 load_meshes(scn, nodemap);
45 printf("loaded tile %s: %d meshes, %d lights\n", saved_fname, scn->mNumMeshes, scn->mNumLights);
46 return true;
47 }
49 void Tile::draw(unsigned int draw_mask) const
50 {
51 for(size_t i=0; i<meshes.size(); i++) {
52 if(mesh_side[i] & draw_mask) {
53 meshes[i]->draw();
54 }
55 }
56 }
58 int Tile::load_lights(const aiScene *scn)
59 {
60 int count = 0;
62 for(int i=0; i<(int)scn->mNumLights; i++) {
63 Light *lt;
64 aiLight *ailt = scn->mLights[i];
66 switch(ailt->mType) {
67 case aiLightSource_POINT:
68 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
69 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
70 ailt->mAttenuationQuadratic);
71 break;
73 case aiLightSource_DIRECTIONAL:
74 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
75 break;
77 default:
78 continue;
79 }
81 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
83 lights.push_back(lt);
84 count++;
85 }
87 return count;
88 }
90 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
91 {
92 int count = 0;
94 for(int i=0; i<(int)scn->mNumMeshes; i++) {
95 Mesh *mesh = new Mesh;
96 if(!mesh->create(scn, scn->mMeshes[i])) {
97 delete mesh;
98 continue;
99 }
101 Material mat;
102 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
103 mesh->set_material(mat);
105 // retrieve the node pointer
106 const char *name = "<unknown>";
108 auto iter = nmap.find(scn->mMeshes[i]);
109 if(iter != nmap.end()) {
110 aiNode *node = iter->second;
112 Matrix4x4 xform;
113 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
114 xform = *(Matrix4x4*)&node->mTransformation;
115 mesh->set_xform(xform);
117 name = node->mName.data;
118 mesh->set_name(name);
119 }
120 meshes.push_back(mesh);
122 //printf(" mesh: \"%s\"", name);
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 }
141 //printf(" (0x0%x)\n", side);
142 mesh_side.push_back(side);
144 count++;
145 }
146 return count;
147 }
149 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
150 {
151 unsigned int i;
153 for(i=0; i<node->mNumMeshes; i++) {
154 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
156 (*nmap)[m] = node;
157 }
159 for(i=0; i<node->mNumChildren; i++) {
160 build_nodemap(nmap, scn, node->mChildren[i]);
161 }
162 }