dungeon_crawler

annotate prototype/src/tile.cc @ 28:f5fb04fe12cd

moved compiler detection to the configure script
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 25 Aug 2012 20:20:56 +0300
parents 7b65bba15553
children ddb68dc4ba07
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@21 13 static PointLight *mesh_to_light(Mesh *m);
nuclear@7 14
nuclear@11 15 Tile::Tile(TileSet *tileset)
nuclear@11 16 {
nuclear@11 17 tset = tileset;
nuclear@11 18 }
nuclear@7 19
nuclear@1 20 bool Tile::load(const char *fname)
nuclear@1 21 {
nuclear@5 22 if(!fname) {
nuclear@5 23 return false;
nuclear@5 24 }
nuclear@5 25
nuclear@14 26 char *saved_fname = (char*)alloca(strlen(fname) + 1);
nuclear@14 27 strcpy(saved_fname, fname);
nuclear@14 28
nuclear@3 29 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
nuclear@7 30 aiProcess_Triangulate |
nuclear@7 31 aiProcess_SortByPType |
nuclear@7 32 aiProcess_FlipUVs;
nuclear@4 33
nuclear@3 34 const aiScene *scn = aiImportFile(fname, proc_flags);
nuclear@3 35 if(!scn) {
nuclear@3 36 fprintf(stderr, "failed to load tile: %s\n", fname);
nuclear@3 37 return -1;
nuclear@3 38 }
nuclear@3 39
nuclear@7 40 map<aiMesh*, aiNode*> nodemap;
nuclear@7 41 build_nodemap(&nodemap, scn, scn->mRootNode);
nuclear@7 42
nuclear@21 43 //load_lights(scn);
nuclear@7 44 load_meshes(scn, nodemap);
nuclear@3 45
nuclear@22 46 printf("loaded tile %s: %d meshes, %d lights\n", saved_fname, (int)meshes.size(), (int)lights.size());
nuclear@1 47 return true;
nuclear@1 48 }
nuclear@1 49
nuclear@5 50 void Tile::draw(unsigned int draw_mask) const
nuclear@1 51 {
nuclear@4 52 for(size_t i=0; i<meshes.size(); i++) {
nuclear@4 53 if(mesh_side[i] & draw_mask) {
nuclear@5 54 meshes[i]->draw();
nuclear@4 55 }
nuclear@4 56 }
nuclear@4 57 }
nuclear@1 58
nuclear@23 59 void Tile::draw_lights(unsigned int draw_mask) const
nuclear@23 60 {
nuclear@23 61 for(size_t i=0; i<lights.size(); i++) {
nuclear@23 62 if(light_side[i] & draw_mask) {
nuclear@23 63 lights[i]->draw();
nuclear@23 64 }
nuclear@23 65 }
nuclear@23 66 }
nuclear@23 67
nuclear@21 68 /*
nuclear@4 69 int Tile::load_lights(const aiScene *scn)
nuclear@4 70 {
nuclear@4 71 int count = 0;
nuclear@4 72
nuclear@4 73 for(int i=0; i<(int)scn->mNumLights; i++) {
nuclear@4 74 Light *lt;
nuclear@4 75 aiLight *ailt = scn->mLights[i];
nuclear@4 76
nuclear@4 77 switch(ailt->mType) {
nuclear@4 78 case aiLightSource_POINT:
nuclear@4 79 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
nuclear@4 80 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
nuclear@4 81 ailt->mAttenuationQuadratic);
nuclear@4 82 break;
nuclear@4 83
nuclear@4 84 case aiLightSource_DIRECTIONAL:
nuclear@4 85 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
nuclear@4 86 break;
nuclear@4 87
nuclear@4 88 default:
nuclear@4 89 continue;
nuclear@4 90 }
nuclear@4 91
nuclear@4 92 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
nuclear@4 93
nuclear@4 94 lights.push_back(lt);
nuclear@4 95 count++;
nuclear@4 96 }
nuclear@4 97
nuclear@4 98 return count;
nuclear@1 99 }
nuclear@21 100 */
nuclear@4 101
nuclear@7 102 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
nuclear@4 103 {
nuclear@4 104 int count = 0;
nuclear@4 105
nuclear@4 106 for(int i=0; i<(int)scn->mNumMeshes; i++) {
nuclear@4 107 Mesh *mesh = new Mesh;
nuclear@4 108 if(!mesh->create(scn, scn->mMeshes[i])) {
nuclear@4 109 delete mesh;
nuclear@4 110 continue;
nuclear@4 111 }
nuclear@4 112
nuclear@11 113 Material mat;
nuclear@11 114 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
nuclear@11 115 mesh->set_material(mat);
nuclear@11 116
nuclear@7 117 // retrieve the node pointer
nuclear@7 118 const char *name = "<unknown>";
nuclear@7 119
nuclear@7 120 auto iter = nmap.find(scn->mMeshes[i]);
nuclear@7 121 if(iter != nmap.end()) {
nuclear@7 122 aiNode *node = iter->second;
nuclear@7 123
nuclear@7 124 Matrix4x4 xform;
nuclear@7 125 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
nuclear@7 126 xform = *(Matrix4x4*)&node->mTransformation;
nuclear@7 127 mesh->set_xform(xform);
nuclear@7 128
nuclear@7 129 name = node->mName.data;
nuclear@7 130 mesh->set_name(name);
nuclear@7 131 }
nuclear@7 132
nuclear@4 133 // find which side is this mesh on
nuclear@7 134 unsigned int side = 0;
nuclear@7 135 if(strstr(name, "NORTH")) {
nuclear@7 136 side |= TILE_NORTH;
nuclear@7 137 }
nuclear@7 138 if(strstr(name, "SOUTH")) {
nuclear@7 139 side |= TILE_SOUTH;
nuclear@7 140 }
nuclear@7 141 if(strstr(name, "EAST")) {
nuclear@7 142 side |= TILE_EAST;
nuclear@7 143 }
nuclear@7 144 if(strstr(name, "WEST")) {
nuclear@7 145 side |= TILE_WEST;
nuclear@7 146 }
nuclear@7 147 if(!side) {
nuclear@4 148 side = TILE_ALL;
nuclear@4 149 }
nuclear@4 150
nuclear@21 151 // what a sordid hack... if the name contains "LIGHT", then make a light out of this
nuclear@21 152 // and destroy the mesh...
nuclear@21 153 if(strstr(name, "LIGHT")) {
nuclear@21 154 PointLight *lt = mesh_to_light(mesh);
nuclear@21 155 if(!lt) {
nuclear@21 156 fprintf(stderr, "failed to convert mesh %s to light\n", name);
nuclear@21 157 } else {
nuclear@21 158 lights.push_back(lt);
nuclear@21 159 light_side.push_back(side);
nuclear@21 160 }
nuclear@21 161 delete mesh;
nuclear@21 162
nuclear@21 163 } else {
nuclear@21 164 meshes.push_back(mesh);
nuclear@21 165 mesh_side.push_back(side);
nuclear@21 166 count++;
nuclear@21 167 }
nuclear@4 168 }
nuclear@4 169 return count;
nuclear@4 170 }
nuclear@7 171
nuclear@7 172 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
nuclear@7 173 {
nuclear@7 174 unsigned int i;
nuclear@7 175
nuclear@7 176 for(i=0; i<node->mNumMeshes; i++) {
nuclear@7 177 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
nuclear@7 178
nuclear@7 179 (*nmap)[m] = node;
nuclear@7 180 }
nuclear@7 181
nuclear@7 182 for(i=0; i<node->mNumChildren; i++) {
nuclear@7 183 build_nodemap(nmap, scn, node->mChildren[i]);
nuclear@7 184 }
nuclear@7 185 }
nuclear@21 186
nuclear@21 187 static PointLight *mesh_to_light(Mesh *m)
nuclear@21 188 {
nuclear@21 189 Vector3 center = m->get_bsph_center();
nuclear@21 190 float rad = m->get_bsph_radius();
nuclear@21 191
nuclear@21 192 PointLight *lt = new PointLight(center);
nuclear@21 193 lt->set_radius(rad);
nuclear@21 194
nuclear@21 195 lt->set_color(m->get_material().kd);
nuclear@21 196
nuclear@21 197 return lt;
nuclear@21 198 }