dungeon_crawler

view prototype/src/tile.cc @ 11:e5567ddbf2ef

- Texture set (texture manager) - materials
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 19 Aug 2012 03:11:36 +0300
parents b10ba85f75e0
children e95462632f9a
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 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
26 aiProcess_Triangulate |
27 aiProcess_SortByPType |
28 aiProcess_FlipUVs;
30 const aiScene *scn = aiImportFile(fname, proc_flags);
31 if(!scn) {
32 fprintf(stderr, "failed to load tile: %s\n", fname);
33 return -1;
34 }
36 map<aiMesh*, aiNode*> nodemap;
37 build_nodemap(&nodemap, scn, scn->mRootNode);
39 load_lights(scn);
40 load_meshes(scn, nodemap);
42 printf("loaded tile %s: %d meshes, %d lights\n", fname, scn->mNumMeshes, scn->mNumLights);
43 return true;
44 }
46 void Tile::draw(unsigned int draw_mask) const
47 {
48 for(size_t i=0; i<meshes.size(); i++) {
49 if(mesh_side[i] & draw_mask) {
50 meshes[i]->draw();
51 }
52 }
53 }
55 int Tile::load_lights(const aiScene *scn)
56 {
57 int count = 0;
59 for(int i=0; i<(int)scn->mNumLights; i++) {
60 Light *lt;
61 aiLight *ailt = scn->mLights[i];
63 switch(ailt->mType) {
64 case aiLightSource_POINT:
65 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
66 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
67 ailt->mAttenuationQuadratic);
68 break;
70 case aiLightSource_DIRECTIONAL:
71 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
72 break;
74 default:
75 continue;
76 }
78 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
80 lights.push_back(lt);
81 count++;
82 }
84 return count;
85 }
87 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
88 {
89 int count = 0;
91 for(int i=0; i<(int)scn->mNumMeshes; i++) {
92 Mesh *mesh = new Mesh;
93 if(!mesh->create(scn, scn->mMeshes[i])) {
94 delete mesh;
95 continue;
96 }
98 Material mat;
99 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
100 mesh->set_material(mat);
102 // retrieve the node pointer
103 const char *name = "<unknown>";
105 auto iter = nmap.find(scn->mMeshes[i]);
106 if(iter != nmap.end()) {
107 aiNode *node = iter->second;
109 Matrix4x4 xform;
110 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
111 xform = *(Matrix4x4*)&node->mTransformation;
112 mesh->set_xform(xform);
114 name = node->mName.data;
115 mesh->set_name(name);
116 }
117 meshes.push_back(mesh);
119 printf(" mesh: \"%s\"", name);
121 // find which side is this mesh on
122 unsigned int side = 0;
123 if(strstr(name, "NORTH")) {
124 side |= TILE_NORTH;
125 }
126 if(strstr(name, "SOUTH")) {
127 side |= TILE_SOUTH;
128 }
129 if(strstr(name, "EAST")) {
130 side |= TILE_EAST;
131 }
132 if(strstr(name, "WEST")) {
133 side |= TILE_WEST;
134 }
135 if(!side) {
136 side = TILE_ALL;
137 }
138 printf(" (0x0%x)\n", side);
139 mesh_side.push_back(side);
141 count++;
142 }
143 return count;
144 }
146 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
147 {
148 unsigned int i;
150 for(i=0; i<node->mNumMeshes; i++) {
151 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
153 (*nmap)[m] = node;
154 }
156 for(i=0; i<node->mNumChildren; i++) {
157 build_nodemap(nmap, scn, node->mChildren[i]);
158 }
159 }