dungeon_crawler

view prototype/src/tile.cc @ 9:b10ba85f75e0

lalala
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 17 Aug 2012 17:25:42 +0300
parents 8fb37db44fd8
children e5567ddbf2ef
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"
9 using std::map;
11 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node);
14 bool Tile::load(const char *fname)
15 {
16 if(!fname) {
17 return false;
18 }
20 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
21 aiProcess_Triangulate |
22 aiProcess_SortByPType |
23 aiProcess_FlipUVs;
25 const aiScene *scn = aiImportFile(fname, proc_flags);
26 if(!scn) {
27 fprintf(stderr, "failed to load tile: %s\n", fname);
28 return -1;
29 }
31 map<aiMesh*, aiNode*> nodemap;
32 build_nodemap(&nodemap, scn, scn->mRootNode);
34 load_lights(scn);
35 load_meshes(scn, nodemap);
37 printf("loaded tile %s: %d meshes, %d lights\n", fname, scn->mNumMeshes, scn->mNumLights);
38 return true;
39 }
41 void Tile::draw(unsigned int draw_mask) const
42 {
43 for(size_t i=0; i<meshes.size(); i++) {
44 if(mesh_side[i] & draw_mask) {
45 meshes[i]->draw();
46 }
47 }
48 }
50 int Tile::load_lights(const aiScene *scn)
51 {
52 int count = 0;
54 for(int i=0; i<(int)scn->mNumLights; i++) {
55 Light *lt;
56 aiLight *ailt = scn->mLights[i];
58 switch(ailt->mType) {
59 case aiLightSource_POINT:
60 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
61 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
62 ailt->mAttenuationQuadratic);
63 break;
65 case aiLightSource_DIRECTIONAL:
66 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
67 break;
69 default:
70 continue;
71 }
73 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
75 lights.push_back(lt);
76 count++;
77 }
79 return count;
80 }
82 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
83 {
84 int count = 0;
86 for(int i=0; i<(int)scn->mNumMeshes; i++) {
87 Mesh *mesh = new Mesh;
88 if(!mesh->create(scn, scn->mMeshes[i])) {
89 delete mesh;
90 continue;
91 }
93 // retrieve the node pointer
94 const char *name = "<unknown>";
96 auto iter = nmap.find(scn->mMeshes[i]);
97 if(iter != nmap.end()) {
98 aiNode *node = iter->second;
100 Matrix4x4 xform;
101 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
102 xform = *(Matrix4x4*)&node->mTransformation;
103 mesh->set_xform(xform);
105 name = node->mName.data;
106 mesh->set_name(name);
107 }
108 meshes.push_back(mesh);
110 printf(" mesh: \"%s\"", name);
112 // find which side is this mesh on
113 unsigned int side = 0;
114 if(strstr(name, "NORTH")) {
115 side |= TILE_NORTH;
116 }
117 if(strstr(name, "SOUTH")) {
118 side |= TILE_SOUTH;
119 }
120 if(strstr(name, "EAST")) {
121 side |= TILE_EAST;
122 }
123 if(strstr(name, "WEST")) {
124 side |= TILE_WEST;
125 }
126 if(!side) {
127 side = TILE_ALL;
128 }
129 printf(" (0x0%x)\n", side);
130 mesh_side.push_back(side);
132 count++;
133 }
134 return count;
135 }
137 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
138 {
139 unsigned int i;
141 for(i=0; i<node->mNumMeshes; i++) {
142 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
144 (*nmap)[m] = node;
145 }
147 for(i=0; i<node->mNumChildren; i++) {
148 build_nodemap(nmap, scn, node->mChildren[i]);
149 }
150 }