dungeon_crawler

annotate prototype/src/tile.cc @ 37:84a56fb24850

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