dungeon_crawler

annotate prototype/src/tile.cc @ 38:862461b686f4

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