dungeon_crawler

annotate prototype/src/tile.cc @ 35:d0e93b4d9ec9

normal mapping
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 28 Aug 2012 06:28:22 +0300
parents ddb68dc4ba07
children 84a56fb24850
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@4 122 Mesh *mesh = new Mesh;
nuclear@4 123 if(!mesh->create(scn, scn->mMeshes[i])) {
nuclear@4 124 delete mesh;
nuclear@4 125 continue;
nuclear@4 126 }
nuclear@35 127 if(attr_loc != -1) {
nuclear@35 128 mesh->set_attrib_location(MESH_ATTR_TANGENT, attr_loc);
nuclear@35 129 }
nuclear@4 130
nuclear@11 131 Material mat;
nuclear@11 132 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
nuclear@11 133 mesh->set_material(mat);
nuclear@11 134
nuclear@7 135 // retrieve the node pointer
nuclear@7 136 const char *name = "<unknown>";
nuclear@7 137
nuclear@7 138 auto iter = nmap.find(scn->mMeshes[i]);
nuclear@7 139 if(iter != nmap.end()) {
nuclear@7 140 aiNode *node = iter->second;
nuclear@7 141
nuclear@7 142 Matrix4x4 xform;
nuclear@7 143 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
nuclear@7 144 xform = *(Matrix4x4*)&node->mTransformation;
nuclear@7 145 mesh->set_xform(xform);
nuclear@7 146
nuclear@7 147 name = node->mName.data;
nuclear@7 148 mesh->set_name(name);
nuclear@7 149 }
nuclear@7 150
nuclear@4 151 // find which side is this mesh on
nuclear@7 152 unsigned int side = 0;
nuclear@7 153 if(strstr(name, "NORTH")) {
nuclear@7 154 side |= TILE_NORTH;
nuclear@7 155 }
nuclear@7 156 if(strstr(name, "SOUTH")) {
nuclear@7 157 side |= TILE_SOUTH;
nuclear@7 158 }
nuclear@7 159 if(strstr(name, "EAST")) {
nuclear@7 160 side |= TILE_EAST;
nuclear@7 161 }
nuclear@7 162 if(strstr(name, "WEST")) {
nuclear@7 163 side |= TILE_WEST;
nuclear@7 164 }
nuclear@7 165 if(!side) {
nuclear@4 166 side = TILE_ALL;
nuclear@4 167 }
nuclear@4 168
nuclear@21 169 // what a sordid hack... if the name contains "LIGHT", then make a light out of this
nuclear@21 170 // and destroy the mesh...
nuclear@21 171 if(strstr(name, "LIGHT")) {
nuclear@21 172 PointLight *lt = mesh_to_light(mesh);
nuclear@21 173 if(!lt) {
nuclear@21 174 fprintf(stderr, "failed to convert mesh %s to light\n", name);
nuclear@21 175 } else {
nuclear@21 176 lights.push_back(lt);
nuclear@21 177 light_side.push_back(side);
nuclear@21 178 }
nuclear@21 179 delete mesh;
nuclear@21 180
nuclear@21 181 } else {
nuclear@21 182 meshes.push_back(mesh);
nuclear@21 183 mesh_side.push_back(side);
nuclear@21 184 count++;
nuclear@21 185 }
nuclear@4 186 }
nuclear@4 187 return count;
nuclear@4 188 }
nuclear@7 189
nuclear@7 190 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
nuclear@7 191 {
nuclear@7 192 unsigned int i;
nuclear@7 193
nuclear@7 194 for(i=0; i<node->mNumMeshes; i++) {
nuclear@7 195 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
nuclear@7 196
nuclear@7 197 (*nmap)[m] = node;
nuclear@7 198 }
nuclear@7 199
nuclear@7 200 for(i=0; i<node->mNumChildren; i++) {
nuclear@7 201 build_nodemap(nmap, scn, node->mChildren[i]);
nuclear@7 202 }
nuclear@7 203 }
nuclear@21 204
nuclear@21 205 static PointLight *mesh_to_light(Mesh *m)
nuclear@21 206 {
nuclear@21 207 Vector3 center = m->get_bsph_center();
nuclear@21 208 float rad = m->get_bsph_radius();
nuclear@21 209
nuclear@21 210 PointLight *lt = new PointLight(center);
nuclear@21 211 lt->set_radius(rad);
nuclear@21 212
nuclear@21 213 lt->set_color(m->get_material().kd);
nuclear@21 214
nuclear@21 215 return lt;
nuclear@21 216 }