dungeon_crawler

annotate prototype/src/tile.cc @ 45:dfd3a413ef9e

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