dungeon_crawler
view prototype/src/tile.cc @ 51:d57df51f6b50
- fixed audio panning (listener direction)
- particles had no fog
- sound sources were not destroyed properly
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 18 Sep 2012 09:40:56 +0300 |
parents | c40efa9cf844 |
children | 1ea56011c1ff |
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"
12 #include "cfg.h"
14 using std::map;
16 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node);
17 static PointLight *mesh_to_light(Mesh *m);
19 bool ass_obj_hack;
21 Tile::Tile(TileSet *tileset)
22 {
23 tset = tileset;
24 last_upd = LONG_MIN;
26 memset(samples, 0, sizeof samples);
27 }
29 Tile::~Tile()
30 {
31 for(auto m : meshes) {
32 delete m;
33 }
34 for(auto lt : lights) {
35 delete lt;
36 }
37 for(auto psa : psattr) {
38 psys_free_attr(psa);
39 }
40 for(auto ps : psys_global) {
41 psys_free(ps);
42 }
43 }
45 AudioSample *Tile::get_sample(int sidx) const
46 {
47 if(sidx >= 0 && sidx < MAX_TILE_SAMPLES) {
48 return samples[sidx];
49 }
50 return 0;
51 }
53 int Tile::get_audio_source_count() const
54 {
55 return (int)ausrc.size();
56 }
58 const Tile::AudioSourceDesc &Tile::get_audio_source(int idx) const
59 {
60 if(idx < 0 || idx >= (int)ausrc.size()) {
61 static AudioSourceDesc d = { 0, Vector3(), 0 };
62 return d;
63 }
64 return ausrc[idx];
65 }
67 int Tile::get_unique_psys_count() const
68 {
69 return (int)psattr.size();
70 }
72 struct psys_attributes *Tile::get_unique_psys(int idx)
73 {
74 if(idx < 0 || idx >= (int)psattr.size()) {
75 return 0;
76 }
77 return psattr[idx];
78 }
80 const struct psys_attributes *Tile::get_unique_psys(int idx) const
81 {
82 if(idx < 0 || idx >= (int)psattr.size()) {
83 return 0;
84 }
85 return psattr[idx];
86 }
88 bool Tile::load(const char *fname)
89 {
90 if(!fname) {
91 return false;
92 }
94 char *saved_fname = (char*)alloca(strlen(fname) + 1);
95 strcpy(saved_fname, fname);
97 unsigned int proc_flags = aiProcess_JoinIdenticalVertices |
98 aiProcess_CalcTangentSpace |
99 aiProcess_Triangulate |
100 aiProcess_SortByPType |
101 aiProcess_FlipUVs;
103 const aiScene *scn = aiImportFile(fname, proc_flags);
104 if(!scn) {
105 fprintf(stderr, "failed to load tile: %s\n", fname);
106 return -1;
107 }
109 map<aiMesh*, aiNode*> nodemap;
110 build_nodemap(&nodemap, scn, scn->mRootNode);
112 if(strstr(fname, ".obj") == fname + strlen(fname) - 4) {
113 ass_obj_hack = true;
114 } else {
115 ass_obj_hack = false;
116 }
118 //load_lights(scn);
119 load_meshes(scn, nodemap);
121 printf("loaded tile %s: %d meshes, %d lights\n", saved_fname, (int)meshes.size(), (int)lights.size());
123 aiReleaseImport(scn);
125 // XXX get the default audio samples for now
126 if(cfg.sound) {
127 SampleSet *sampleset = tset->get_samples();
128 samples[TILE_SAMPLE_WALK] = sampleset->get("walk_stone.ogg");
129 samples[TILE_SAMPLE_RUN] = sampleset->get("run_stone.ogg");
130 }
131 return true;
132 }
134 void Tile::update(unsigned long msec, float dt)
135 {
136 // update particle systems
137 for(auto ps : psys_global) {
138 psys_update(ps, (float)msec / 1000.0f);
139 }
140 }
142 void Tile::draw(unsigned int draw_mask) const
143 {
144 for(size_t i=0; i<meshes.size(); i++) {
145 if(mesh_side[i] & draw_mask) {
146 meshes[i]->draw();
147 }
148 }
149 }
151 void Tile::draw_lights(unsigned int draw_mask) const
152 {
153 for(size_t i=0; i<lights.size(); i++) {
154 if(light_side[i] & draw_mask) {
155 lights[i]->draw();
156 }
157 }
158 }
160 void Tile::draw_post(unsigned int draw_mask) const
161 {
162 // draw global particle systems (simulated once)
163 for(size_t i=0; i<psys_global.size(); i++) {
164 if(psys_side[i] & draw_mask) {
165 psys_draw(psys_global[i]);
166 }
167 }
168 }
170 /*
171 int Tile::load_lights(const aiScene *scn)
172 {
173 int count = 0;
175 for(int i=0; i<(int)scn->mNumLights; i++) {
176 Light *lt;
177 aiLight *ailt = scn->mLights[i];
179 switch(ailt->mType) {
180 case aiLightSource_POINT:
181 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
182 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
183 ailt->mAttenuationQuadratic);
184 break;
186 case aiLightSource_DIRECTIONAL:
187 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
188 break;
190 default:
191 continue;
192 }
194 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
196 lights.push_back(lt);
197 count++;
198 }
200 return count;
201 }
202 */
204 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
205 {
206 int count = 0;
208 int attr_loc = rend->get_tangent_location();
209 if(attr_loc == -1) {
210 fprintf(stderr, "warning: failed to retrieve tangent attribute location while loading tile\n");
211 }
213 for(int i=0; i<(int)scn->mNumMeshes; i++) {
214 // ignore any lines or other crap
215 if(scn->mMeshes[i]->mPrimitiveTypes != aiPrimitiveType_TRIANGLE) {
216 continue;
217 }
219 Mesh *mesh = new Mesh;
220 if(!mesh->create(scn, scn->mMeshes[i])) {
221 delete mesh;
222 continue;
223 }
224 if(attr_loc != -1) {
225 mesh->set_attrib_location(MESH_ATTR_TANGENT, attr_loc);
226 }
228 Material mat;
229 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
230 mesh->set_material(mat);
232 // retrieve the node pointer
233 const char *name = "<unknown>";
235 auto iter = nmap.find(scn->mMeshes[i]);
236 if(iter != nmap.end()) {
237 aiNode *node = iter->second;
239 Matrix4x4 xform;
240 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
241 xform = *(Matrix4x4*)&node->mTransformation;
242 mesh->set_xform(xform);
244 name = node->mName.data;
245 mesh->set_name(name);
246 }
248 // find which side is this mesh on
249 unsigned int side = 0;
250 if(strstr(name, "NORTH")) {
251 side |= TILE_NORTH;
252 }
253 if(strstr(name, "SOUTH")) {
254 side |= TILE_SOUTH;
255 }
256 if(strstr(name, "EAST")) {
257 side |= TILE_EAST;
258 }
259 if(strstr(name, "WEST")) {
260 side |= TILE_WEST;
261 }
262 if(!side) {
263 side = TILE_ALL;
264 }
266 // what a sordid hack... if the name contains "LIGHT", then make a light out of this
267 // and destroy the mesh...
268 if(strstr(name, "LIGHT")) {
269 PointLight *lt = mesh_to_light(mesh);
270 if(!lt) {
271 fprintf(stderr, "failed to convert mesh %s to light\n", name);
272 } else {
273 lights.push_back(lt);
274 light_side.push_back(side);
275 }
276 delete mesh;
278 // ... ALSO add a fire particle system :) save me jebus
279 struct psys_emitter *ps = psys_create();
280 if(ps && psys_load_attr(&ps->attr, datafile_path("fire.psys")) == 0) {
281 Vector3 lpos = lt->get_position();
282 psys_set_pos(ps, v3_cons(lpos.x, lpos.y, lpos.z), 0);
283 psys_global.push_back(ps);
284 psys_side.push_back(side);
285 } else {
286 fprintf(stderr, "failed to create global particle system\n");
287 }
289 // ... AND make an audio source out of each light source
290 if(cfg.sound) {
291 SampleSet *sampleset = tset->get_samples();
292 AudioSample *sample = sampleset->get("fire.ogg");
293 if(sample) {
294 AudioSourceDesc adesc = {side, lt->get_position(), sample, 1.0, 0.1};
295 ausrc.push_back(adesc);
296 }
297 }
299 } else {
300 meshes.push_back(mesh);
301 mesh_side.push_back(side);
302 count++;
303 }
304 }
305 return count;
306 }
308 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
309 {
310 unsigned int i;
312 for(i=0; i<node->mNumMeshes; i++) {
313 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
315 (*nmap)[m] = node;
316 }
318 for(i=0; i<node->mNumChildren; i++) {
319 build_nodemap(nmap, scn, node->mChildren[i]);
320 }
321 }
323 static PointLight *mesh_to_light(Mesh *m)
324 {
325 Vector3 center = m->get_bsph_center();
326 float rad = m->get_bsph_radius();
328 PointLight *lt = new PointLight(center);
329 lt->set_radius(rad);
331 lt->set_color(m->get_material().kd);
333 return lt;
334 }