dungeon_crawler

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