dungeon_crawler
view prototype/src/tile.cc @ 4:158de53b4e18
tile work
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 11 Aug 2012 05:44:52 +0300 |
parents | 31e53fd79c2d |
children | 252a00508411 |
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 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
11 aiProcess_PreTransformVertices | aiProcess_Triangulate |
12 aiProcess_GenNormals | aiProcess_SortByPType | aiProcess_FlipUVs;
14 const aiScene *scn = aiImportFile(fname, proc_flags);
15 if(!scn) {
16 fprintf(stderr, "failed to load tile: %s\n", fname);
17 return -1;
18 }
20 load_lights(scn);
22 load_meshes(scn);
24 printf("loaded tile %s: %d meshes, %d lights\n", fname, scn->mNumMeshes, scn->mNumLights);
25 return true;
26 }
28 void Tile::draw(unsigned int drawmask) const
29 {
30 for(size_t i=0; i<meshes.size(); i++) {
31 if(mesh_side[i] & draw_mask) {
32 meshes[i].draw();
33 }
34 }
35 }
37 int Tile::load_lights(const aiScene *scn)
38 {
39 int count = 0;
41 for(int i=0; i<(int)scn->mNumLights; i++) {
42 Light *lt;
43 aiLight *ailt = scn->mLights[i];
45 switch(ailt->mType) {
46 case aiLightSource_POINT:
47 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
48 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
49 ailt->mAttenuationQuadratic);
50 break;
52 case aiLightSource_DIRECTIONAL:
53 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
54 break;
56 default:
57 continue;
58 }
60 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
62 lights.push_back(lt);
63 count++;
64 }
66 return count;
67 }
69 int Tile::load_meshes(const aiScene *scn)
70 {
71 int count = 0;
73 for(int i=0; i<(int)scn->mNumMeshes; i++) {
74 Mesh *mesh = new Mesh;
75 if(!mesh->create(scn, scn->mMeshes[i])) {
76 delete mesh;
77 continue;
78 }
80 meshes.push_back(mesh);
82 // find which side is this mesh on
83 const char *name = mesh->get_name();
84 unsigned int side;
86 if(strstr(name, "NORTH") == name) {
87 side = TILE_NORTH;
88 } else if(strstr(name, "SOUTH") == name) {
89 side = TILE_SOUTH;
90 } else if(strstr(name, "EAST") == name) {
91 side = TILE_EAST;
92 } else if(strstr(name, "WEST") == name) {
93 side = TILE_WEST;
94 } else {
95 side = TILE_ALL;
96 }
97 mesh_side.push_back(side);
99 count++;
100 }
101 return count;
102 }