dungeon_crawler

view prototype/src/tile.cc @ 5:252a00508411

more stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 12 Aug 2012 07:07:57 +0300
parents 158de53b4e18
children 8fb37db44fd8
line source
1 #include <stdio.h>
2 #include "opengl.h"
3 #include <assimp/assimp.h>
4 #include <assimp/aiScene.h>
5 #include <assimp/aiPostProcess.h>
6 #include "tile.h"
8 bool Tile::load(const char *fname)
9 {
10 if(!fname) {
11 return false;
12 }
14 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
15 aiProcess_PreTransformVertices | aiProcess_Triangulate |
16 aiProcess_GenNormals | aiProcess_SortByPType | aiProcess_FlipUVs;
18 const aiScene *scn = aiImportFile(fname, proc_flags);
19 if(!scn) {
20 fprintf(stderr, "failed to load tile: %s\n", fname);
21 return -1;
22 }
24 load_lights(scn);
26 load_meshes(scn);
28 printf("loaded tile %s: %d meshes, %d lights\n", fname, scn->mNumMeshes, scn->mNumLights);
29 return true;
30 }
32 void Tile::draw(unsigned int draw_mask) const
33 {
34 for(size_t i=0; i<meshes.size(); i++) {
35 if(mesh_side[i] & draw_mask) {
36 meshes[i]->draw();
37 }
38 }
39 }
41 int Tile::load_lights(const aiScene *scn)
42 {
43 int count = 0;
45 for(int i=0; i<(int)scn->mNumLights; i++) {
46 Light *lt;
47 aiLight *ailt = scn->mLights[i];
49 switch(ailt->mType) {
50 case aiLightSource_POINT:
51 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
52 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
53 ailt->mAttenuationQuadratic);
54 break;
56 case aiLightSource_DIRECTIONAL:
57 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
58 break;
60 default:
61 continue;
62 }
64 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
66 lights.push_back(lt);
67 count++;
68 }
70 return count;
71 }
73 int Tile::load_meshes(const aiScene *scn)
74 {
75 int count = 0;
77 for(int i=0; i<(int)scn->mNumMeshes; i++) {
78 Mesh *mesh = new Mesh;
79 if(!mesh->create(scn, scn->mMeshes[i])) {
80 delete mesh;
81 continue;
82 }
84 meshes.push_back(mesh);
86 // find which side is this mesh on
87 const char *name = mesh->get_name();
88 printf("name: %s ... ", name);
89 unsigned int side;
91 if(strstr(name, "NORTH") == name) {
92 side = TILE_NORTH;
93 } else if(strstr(name, "SOUTH") == name) {
94 side = TILE_SOUTH;
95 } else if(strstr(name, "EAST") == name) {
96 side = TILE_EAST;
97 } else if(strstr(name, "WEST") == name) {
98 side = TILE_WEST;
99 } else {
100 side = TILE_ALL;
101 }
102 printf("side: %x\n", side);
103 mesh_side.push_back(side);
105 count++;
106 }
107 return count;
108 }