dungeon_crawler

annotate 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
rev   line source
nuclear@1 1 #include <stdio.h>
nuclear@7 2 #include <map>
nuclear@1 3 #include "opengl.h"
nuclear@7 4 #include <assimp/cimport.h>
nuclear@7 5 #include <assimp/scene.h>
nuclear@7 6 #include <assimp/postprocess.h>
nuclear@1 7 #include "tile.h"
nuclear@11 8 #include "tileset.h"
nuclear@1 9
nuclear@7 10 using std::map;
nuclear@7 11
nuclear@7 12 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node);
nuclear@7 13
nuclear@11 14 Tile::Tile(TileSet *tileset)
nuclear@11 15 {
nuclear@11 16 tset = tileset;
nuclear@11 17 }
nuclear@7 18
nuclear@1 19 bool Tile::load(const char *fname)
nuclear@1 20 {
nuclear@5 21 if(!fname) {
nuclear@5 22 return false;
nuclear@5 23 }
nuclear@5 24
nuclear@3 25 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
nuclear@7 26 aiProcess_Triangulate |
nuclear@7 27 aiProcess_SortByPType |
nuclear@7 28 aiProcess_FlipUVs;
nuclear@4 29
nuclear@3 30 const aiScene *scn = aiImportFile(fname, proc_flags);
nuclear@3 31 if(!scn) {
nuclear@3 32 fprintf(stderr, "failed to load tile: %s\n", fname);
nuclear@3 33 return -1;
nuclear@3 34 }
nuclear@3 35
nuclear@7 36 map<aiMesh*, aiNode*> nodemap;
nuclear@7 37 build_nodemap(&nodemap, scn, scn->mRootNode);
nuclear@7 38
nuclear@3 39 load_lights(scn);
nuclear@7 40 load_meshes(scn, nodemap);
nuclear@3 41
nuclear@3 42 printf("loaded tile %s: %d meshes, %d lights\n", fname, scn->mNumMeshes, scn->mNumLights);
nuclear@1 43 return true;
nuclear@1 44 }
nuclear@1 45
nuclear@5 46 void Tile::draw(unsigned int draw_mask) const
nuclear@1 47 {
nuclear@4 48 for(size_t i=0; i<meshes.size(); i++) {
nuclear@4 49 if(mesh_side[i] & draw_mask) {
nuclear@5 50 meshes[i]->draw();
nuclear@4 51 }
nuclear@4 52 }
nuclear@4 53 }
nuclear@1 54
nuclear@4 55 int Tile::load_lights(const aiScene *scn)
nuclear@4 56 {
nuclear@4 57 int count = 0;
nuclear@4 58
nuclear@4 59 for(int i=0; i<(int)scn->mNumLights; i++) {
nuclear@4 60 Light *lt;
nuclear@4 61 aiLight *ailt = scn->mLights[i];
nuclear@4 62
nuclear@4 63 switch(ailt->mType) {
nuclear@4 64 case aiLightSource_POINT:
nuclear@4 65 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
nuclear@4 66 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
nuclear@4 67 ailt->mAttenuationQuadratic);
nuclear@4 68 break;
nuclear@4 69
nuclear@4 70 case aiLightSource_DIRECTIONAL:
nuclear@4 71 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
nuclear@4 72 break;
nuclear@4 73
nuclear@4 74 default:
nuclear@4 75 continue;
nuclear@4 76 }
nuclear@4 77
nuclear@4 78 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
nuclear@4 79
nuclear@4 80 lights.push_back(lt);
nuclear@4 81 count++;
nuclear@4 82 }
nuclear@4 83
nuclear@4 84 return count;
nuclear@1 85 }
nuclear@4 86
nuclear@7 87 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
nuclear@4 88 {
nuclear@4 89 int count = 0;
nuclear@4 90
nuclear@4 91 for(int i=0; i<(int)scn->mNumMeshes; i++) {
nuclear@4 92 Mesh *mesh = new Mesh;
nuclear@4 93 if(!mesh->create(scn, scn->mMeshes[i])) {
nuclear@4 94 delete mesh;
nuclear@4 95 continue;
nuclear@4 96 }
nuclear@4 97
nuclear@11 98 Material mat;
nuclear@11 99 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
nuclear@11 100 mesh->set_material(mat);
nuclear@11 101
nuclear@7 102 // retrieve the node pointer
nuclear@7 103 const char *name = "<unknown>";
nuclear@7 104
nuclear@7 105 auto iter = nmap.find(scn->mMeshes[i]);
nuclear@7 106 if(iter != nmap.end()) {
nuclear@7 107 aiNode *node = iter->second;
nuclear@7 108
nuclear@7 109 Matrix4x4 xform;
nuclear@7 110 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
nuclear@7 111 xform = *(Matrix4x4*)&node->mTransformation;
nuclear@7 112 mesh->set_xform(xform);
nuclear@7 113
nuclear@7 114 name = node->mName.data;
nuclear@7 115 mesh->set_name(name);
nuclear@7 116 }
nuclear@4 117 meshes.push_back(mesh);
nuclear@4 118
nuclear@9 119 printf(" mesh: \"%s\"", name);
nuclear@7 120
nuclear@4 121 // find which side is this mesh on
nuclear@7 122 unsigned int side = 0;
nuclear@7 123 if(strstr(name, "NORTH")) {
nuclear@7 124 side |= TILE_NORTH;
nuclear@7 125 }
nuclear@7 126 if(strstr(name, "SOUTH")) {
nuclear@7 127 side |= TILE_SOUTH;
nuclear@7 128 }
nuclear@7 129 if(strstr(name, "EAST")) {
nuclear@7 130 side |= TILE_EAST;
nuclear@7 131 }
nuclear@7 132 if(strstr(name, "WEST")) {
nuclear@7 133 side |= TILE_WEST;
nuclear@7 134 }
nuclear@7 135 if(!side) {
nuclear@4 136 side = TILE_ALL;
nuclear@4 137 }
nuclear@9 138 printf(" (0x0%x)\n", side);
nuclear@4 139 mesh_side.push_back(side);
nuclear@4 140
nuclear@4 141 count++;
nuclear@4 142 }
nuclear@4 143 return count;
nuclear@4 144 }
nuclear@7 145
nuclear@7 146 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
nuclear@7 147 {
nuclear@7 148 unsigned int i;
nuclear@7 149
nuclear@7 150 for(i=0; i<node->mNumMeshes; i++) {
nuclear@7 151 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
nuclear@7 152
nuclear@7 153 (*nmap)[m] = node;
nuclear@7 154 }
nuclear@7 155
nuclear@7 156 for(i=0; i<node->mNumChildren; i++) {
nuclear@7 157 build_nodemap(nmap, scn, node->mChildren[i]);
nuclear@7 158 }
nuclear@7 159 }