dungeon_crawler

annotate prototype/src/tile.cc @ 31:ddb68dc4ba07

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