dungeon_crawler

annotate prototype/src/tile.cc @ 47:d52711f2b9a1

started writting audio code
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 16 Sep 2012 08:16:50 +0300
parents dfd3a413ef9e
children aa9e28670ae2
rev   line source
nuclear@1 1 #include <stdio.h>
nuclear@46 2 #include <ctype.h>
nuclear@7 3 #include <map>
nuclear@1 4 #include "opengl.h"
nuclear@7 5 #include <assimp/cimport.h>
nuclear@7 6 #include <assimp/scene.h>
nuclear@7 7 #include <assimp/postprocess.h>
nuclear@1 8 #include "tile.h"
nuclear@11 9 #include "tileset.h"
nuclear@35 10 #include "renderer.h"
nuclear@46 11 #include "datapath.h"
nuclear@1 12
nuclear@7 13 using std::map;
nuclear@7 14
nuclear@7 15 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node);
nuclear@21 16 static PointLight *mesh_to_light(Mesh *m);
nuclear@7 17
nuclear@31 18 bool ass_obj_hack;
nuclear@31 19
nuclear@11 20 Tile::Tile(TileSet *tileset)
nuclear@11 21 {
nuclear@11 22 tset = tileset;
nuclear@46 23 last_upd = LONG_MIN;
nuclear@11 24 }
nuclear@7 25
nuclear@38 26 Tile::~Tile()
nuclear@38 27 {
nuclear@38 28 for(auto m : meshes) {
nuclear@38 29 delete m;
nuclear@38 30 }
nuclear@38 31 for(auto lt : lights) {
nuclear@38 32 delete lt;
nuclear@38 33 }
nuclear@46 34 for(auto psa : psattr) {
nuclear@46 35 psys_free_attr(psa);
nuclear@46 36 }
nuclear@46 37 for(auto ps : psys_global) {
nuclear@46 38 psys_free(ps);
nuclear@38 39 }
nuclear@38 40 }
nuclear@38 41
nuclear@46 42 const struct psys_attributes * const *Tile::get_unique_psys() const
nuclear@45 43 {
nuclear@45 44 return &psattr[0];
nuclear@45 45 }
nuclear@45 46
nuclear@46 47 int Tile::get_unique_psys_count() const
nuclear@45 48 {
nuclear@45 49 return (int)psattr.size();
nuclear@45 50 }
nuclear@45 51
nuclear@1 52 bool Tile::load(const char *fname)
nuclear@1 53 {
nuclear@5 54 if(!fname) {
nuclear@5 55 return false;
nuclear@5 56 }
nuclear@5 57
nuclear@14 58 char *saved_fname = (char*)alloca(strlen(fname) + 1);
nuclear@14 59 strcpy(saved_fname, fname);
nuclear@14 60
nuclear@3 61 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
nuclear@35 62 aiProcess_CalcTangentSpace |
nuclear@7 63 aiProcess_Triangulate |
nuclear@7 64 aiProcess_SortByPType |
nuclear@7 65 aiProcess_FlipUVs;
nuclear@4 66
nuclear@3 67 const aiScene *scn = aiImportFile(fname, proc_flags);
nuclear@3 68 if(!scn) {
nuclear@3 69 fprintf(stderr, "failed to load tile: %s\n", fname);
nuclear@3 70 return -1;
nuclear@3 71 }
nuclear@3 72
nuclear@7 73 map<aiMesh*, aiNode*> nodemap;
nuclear@7 74 build_nodemap(&nodemap, scn, scn->mRootNode);
nuclear@7 75
nuclear@31 76 if(strstr(fname, ".obj") == fname + strlen(fname) - 4) {
nuclear@31 77 ass_obj_hack = true;
nuclear@31 78 } else {
nuclear@31 79 ass_obj_hack = false;
nuclear@31 80 }
nuclear@31 81
nuclear@21 82 //load_lights(scn);
nuclear@7 83 load_meshes(scn, nodemap);
nuclear@3 84
nuclear@22 85 printf("loaded tile %s: %d meshes, %d lights\n", saved_fname, (int)meshes.size(), (int)lights.size());
nuclear@38 86
nuclear@38 87 aiReleaseImport(scn);
nuclear@1 88 return true;
nuclear@1 89 }
nuclear@1 90
nuclear@38 91 void Tile::update(unsigned long msec, float dt)
nuclear@38 92 {
nuclear@46 93 // update particle systems
nuclear@46 94 for(auto ps : psys_global) {
nuclear@46 95 psys_update(ps, (float)msec / 1000.0f);
nuclear@46 96 }
nuclear@38 97 }
nuclear@38 98
nuclear@5 99 void Tile::draw(unsigned int draw_mask) const
nuclear@1 100 {
nuclear@4 101 for(size_t i=0; i<meshes.size(); i++) {
nuclear@4 102 if(mesh_side[i] & draw_mask) {
nuclear@5 103 meshes[i]->draw();
nuclear@4 104 }
nuclear@4 105 }
nuclear@4 106 }
nuclear@1 107
nuclear@23 108 void Tile::draw_lights(unsigned int draw_mask) const
nuclear@23 109 {
nuclear@23 110 for(size_t i=0; i<lights.size(); i++) {
nuclear@23 111 if(light_side[i] & draw_mask) {
nuclear@23 112 lights[i]->draw();
nuclear@23 113 }
nuclear@23 114 }
nuclear@23 115 }
nuclear@23 116
nuclear@46 117 void Tile::draw_post(unsigned int draw_mask) const
nuclear@46 118 {
nuclear@46 119 // draw global particle systems (simulated once)
nuclear@46 120 for(size_t i=0; i<psys_global.size(); i++) {
nuclear@46 121 if(psys_side[i] & draw_mask) {
nuclear@46 122 psys_draw(psys_global[i]);
nuclear@46 123 }
nuclear@46 124 }
nuclear@46 125 }
nuclear@46 126
nuclear@21 127 /*
nuclear@4 128 int Tile::load_lights(const aiScene *scn)
nuclear@4 129 {
nuclear@4 130 int count = 0;
nuclear@4 131
nuclear@4 132 for(int i=0; i<(int)scn->mNumLights; i++) {
nuclear@4 133 Light *lt;
nuclear@4 134 aiLight *ailt = scn->mLights[i];
nuclear@4 135
nuclear@4 136 switch(ailt->mType) {
nuclear@4 137 case aiLightSource_POINT:
nuclear@4 138 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
nuclear@4 139 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
nuclear@4 140 ailt->mAttenuationQuadratic);
nuclear@4 141 break;
nuclear@4 142
nuclear@4 143 case aiLightSource_DIRECTIONAL:
nuclear@4 144 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
nuclear@4 145 break;
nuclear@4 146
nuclear@4 147 default:
nuclear@4 148 continue;
nuclear@4 149 }
nuclear@4 150
nuclear@4 151 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
nuclear@4 152
nuclear@4 153 lights.push_back(lt);
nuclear@4 154 count++;
nuclear@4 155 }
nuclear@4 156
nuclear@4 157 return count;
nuclear@1 158 }
nuclear@21 159 */
nuclear@4 160
nuclear@7 161 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
nuclear@4 162 {
nuclear@4 163 int count = 0;
nuclear@4 164
nuclear@41 165 int attr_loc = rend->get_tangent_location();
nuclear@35 166 if(attr_loc == -1) {
nuclear@35 167 fprintf(stderr, "warning: failed to retrieve tangent attribute location while loading tile\n");
nuclear@35 168 }
nuclear@35 169
nuclear@4 170 for(int i=0; i<(int)scn->mNumMeshes; i++) {
nuclear@37 171 // ignore any lines or other crap
nuclear@37 172 if(scn->mMeshes[i]->mPrimitiveTypes != aiPrimitiveType_TRIANGLE) {
nuclear@37 173 continue;
nuclear@37 174 }
nuclear@37 175
nuclear@4 176 Mesh *mesh = new Mesh;
nuclear@4 177 if(!mesh->create(scn, scn->mMeshes[i])) {
nuclear@4 178 delete mesh;
nuclear@4 179 continue;
nuclear@4 180 }
nuclear@35 181 if(attr_loc != -1) {
nuclear@35 182 mesh->set_attrib_location(MESH_ATTR_TANGENT, attr_loc);
nuclear@35 183 }
nuclear@4 184
nuclear@11 185 Material mat;
nuclear@11 186 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
nuclear@11 187 mesh->set_material(mat);
nuclear@11 188
nuclear@7 189 // retrieve the node pointer
nuclear@7 190 const char *name = "<unknown>";
nuclear@7 191
nuclear@7 192 auto iter = nmap.find(scn->mMeshes[i]);
nuclear@7 193 if(iter != nmap.end()) {
nuclear@7 194 aiNode *node = iter->second;
nuclear@7 195
nuclear@7 196 Matrix4x4 xform;
nuclear@7 197 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
nuclear@7 198 xform = *(Matrix4x4*)&node->mTransformation;
nuclear@7 199 mesh->set_xform(xform);
nuclear@7 200
nuclear@7 201 name = node->mName.data;
nuclear@7 202 mesh->set_name(name);
nuclear@7 203 }
nuclear@7 204
nuclear@4 205 // find which side is this mesh on
nuclear@7 206 unsigned int side = 0;
nuclear@7 207 if(strstr(name, "NORTH")) {
nuclear@7 208 side |= TILE_NORTH;
nuclear@7 209 }
nuclear@7 210 if(strstr(name, "SOUTH")) {
nuclear@7 211 side |= TILE_SOUTH;
nuclear@7 212 }
nuclear@7 213 if(strstr(name, "EAST")) {
nuclear@7 214 side |= TILE_EAST;
nuclear@7 215 }
nuclear@7 216 if(strstr(name, "WEST")) {
nuclear@7 217 side |= TILE_WEST;
nuclear@7 218 }
nuclear@7 219 if(!side) {
nuclear@4 220 side = TILE_ALL;
nuclear@4 221 }
nuclear@4 222
nuclear@21 223 // what a sordid hack... if the name contains "LIGHT", then make a light out of this
nuclear@21 224 // and destroy the mesh...
nuclear@21 225 if(strstr(name, "LIGHT")) {
nuclear@21 226 PointLight *lt = mesh_to_light(mesh);
nuclear@21 227 if(!lt) {
nuclear@21 228 fprintf(stderr, "failed to convert mesh %s to light\n", name);
nuclear@21 229 } else {
nuclear@21 230 lights.push_back(lt);
nuclear@21 231 light_side.push_back(side);
nuclear@21 232 }
nuclear@21 233 delete mesh;
nuclear@21 234
nuclear@46 235 // ... ALSO add a fire particle system :) save me jebus
nuclear@46 236 struct psys_emitter *ps = psys_create();
nuclear@46 237 if(ps && psys_load_attr(&ps->attr, datafile_path("fire.psys")) == 0) {
nuclear@46 238 Vector3 lpos = lt->get_position();
nuclear@46 239 psys_set_pos(ps, v3_cons(lpos.x, lpos.y, lpos.z), 0);
nuclear@46 240 psys_global.push_back(ps);
nuclear@46 241 psys_side.push_back(side);
nuclear@46 242 } else {
nuclear@46 243 fprintf(stderr, "failed to create global particle system\n");
nuclear@46 244 }
nuclear@46 245
nuclear@21 246 } else {
nuclear@21 247 meshes.push_back(mesh);
nuclear@21 248 mesh_side.push_back(side);
nuclear@21 249 count++;
nuclear@21 250 }
nuclear@4 251 }
nuclear@4 252 return count;
nuclear@4 253 }
nuclear@7 254
nuclear@7 255 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
nuclear@7 256 {
nuclear@7 257 unsigned int i;
nuclear@7 258
nuclear@7 259 for(i=0; i<node->mNumMeshes; i++) {
nuclear@7 260 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
nuclear@7 261
nuclear@7 262 (*nmap)[m] = node;
nuclear@7 263 }
nuclear@7 264
nuclear@7 265 for(i=0; i<node->mNumChildren; i++) {
nuclear@7 266 build_nodemap(nmap, scn, node->mChildren[i]);
nuclear@7 267 }
nuclear@7 268 }
nuclear@21 269
nuclear@21 270 static PointLight *mesh_to_light(Mesh *m)
nuclear@21 271 {
nuclear@21 272 Vector3 center = m->get_bsph_center();
nuclear@21 273 float rad = m->get_bsph_radius();
nuclear@21 274
nuclear@21 275 PointLight *lt = new PointLight(center);
nuclear@21 276 lt->set_radius(rad);
nuclear@21 277
nuclear@21 278 lt->set_color(m->get_material().kd);
nuclear@21 279
nuclear@21 280 return lt;
nuclear@21 281 }