dungeon_crawler

view prototype/src/tile.cc @ 7:8fb37db44fd8

first person motion
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 17 Aug 2012 14:29:37 +0300
parents 252a00508411
children b10ba85f75e0
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_GenNormals |
23 aiProcess_SortByPType |
24 aiProcess_FlipUVs;
26 const aiScene *scn = aiImportFile(fname, proc_flags);
27 if(!scn) {
28 fprintf(stderr, "failed to load tile: %s\n", fname);
29 return -1;
30 }
32 map<aiMesh*, aiNode*> nodemap;
33 build_nodemap(&nodemap, scn, scn->mRootNode);
35 load_lights(scn);
36 load_meshes(scn, nodemap);
38 printf("loaded tile %s: %d meshes, %d lights\n", fname, scn->mNumMeshes, scn->mNumLights);
39 return true;
40 }
42 void Tile::draw(unsigned int draw_mask) const
43 {
44 for(size_t i=0; i<meshes.size(); i++) {
45 if(mesh_side[i] & draw_mask) {
46 meshes[i]->draw();
47 }
48 }
49 }
51 int Tile::load_lights(const aiScene *scn)
52 {
53 int count = 0;
55 for(int i=0; i<(int)scn->mNumLights; i++) {
56 Light *lt;
57 aiLight *ailt = scn->mLights[i];
59 switch(ailt->mType) {
60 case aiLightSource_POINT:
61 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
62 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
63 ailt->mAttenuationQuadratic);
64 break;
66 case aiLightSource_DIRECTIONAL:
67 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
68 break;
70 default:
71 continue;
72 }
74 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
76 lights.push_back(lt);
77 count++;
78 }
80 return count;
81 }
83 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
84 {
85 int count = 0;
87 for(int i=0; i<(int)scn->mNumMeshes; i++) {
88 Mesh *mesh = new Mesh;
89 if(!mesh->create(scn, scn->mMeshes[i])) {
90 delete mesh;
91 continue;
92 }
94 // retrieve the node pointer
95 const char *name = "<unknown>";
97 auto iter = nmap.find(scn->mMeshes[i]);
98 if(iter != nmap.end()) {
99 aiNode *node = iter->second;
101 Matrix4x4 xform;
102 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
103 xform = *(Matrix4x4*)&node->mTransformation;
104 mesh->set_xform(xform);
106 name = node->mName.data;
107 mesh->set_name(name);
108 }
109 meshes.push_back(mesh);
112 printf("name: \"%s\" ... ", name);
114 // find which side is this mesh on
115 unsigned int side = 0;
116 if(strstr(name, "NORTH")) {
117 side |= TILE_NORTH;
118 }
119 if(strstr(name, "SOUTH")) {
120 side |= TILE_SOUTH;
121 }
122 if(strstr(name, "EAST")) {
123 side |= TILE_EAST;
124 }
125 if(strstr(name, "WEST")) {
126 side |= TILE_WEST;
127 }
128 if(!side) {
129 side = TILE_ALL;
130 }
131 printf("side: 0x0%x\n", side);
132 mesh_side.push_back(side);
134 count++;
135 }
136 return count;
137 }
139 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
140 {
141 unsigned int i;
143 for(i=0; i<node->mNumMeshes; i++) {
144 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
146 (*nmap)[m] = node;
147 }
149 for(i=0; i<node->mNumChildren; i++) {
150 build_nodemap(nmap, scn, node->mChildren[i]);
151 }
152 }