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