dungeon_crawler
view prototype/src/tile.cc @ 70:f55ddab0f3a4
"fixed" the disappearing lights issue
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 09 Oct 2012 02:54:01 +0300 |
parents | aa86119e3295 |
children | 12a1dcfe91fa |
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 int tang_loc = rend->get_tangent_location();
146 for(size_t i=0; i<meshes.size(); i++) {
147 if(mesh_side[i] & draw_mask) {
148 meshes[i]->set_attrib_location(MESH_ATTR_TANGENT, tang_loc);
149 meshes[i]->draw();
150 }
151 }
152 }
154 void Tile::draw_lights(unsigned int draw_mask) const
155 {
156 for(size_t i=0; i<lights.size(); i++) {
157 if(light_side[i] & draw_mask) {
158 //printf("rendering light ...\n");
159 lights[i]->draw();
160 }
161 }
162 }
164 void Tile::draw_post(unsigned int draw_mask) const
165 {
166 // draw global particle systems (simulated once)
167 for(size_t i=0; i<psys_global.size(); i++) {
168 if(psys_side[i] & draw_mask) {
169 psys_draw(psys_global[i]);
170 }
171 }
172 }
174 /*
175 int Tile::load_lights(const aiScene *scn)
176 {
177 int count = 0;
179 for(int i=0; i<(int)scn->mNumLights; i++) {
180 Light *lt;
181 aiLight *ailt = scn->mLights[i];
183 switch(ailt->mType) {
184 case aiLightSource_POINT:
185 lt = new PointLight(Vector3(ailt->mPosition.x, ailt->mPosition.y, ailt->mPosition.z));
186 ((PointLight*)lt)->set_attenuation(ailt->mAttenuationConstant, ailt->mAttenuationLinear,
187 ailt->mAttenuationQuadratic);
188 break;
190 case aiLightSource_DIRECTIONAL:
191 lt = new PointLight(Vector3(ailt->mDirection.x, ailt->mDirection.y, ailt->mDirection.z));
192 break;
194 default:
195 continue;
196 }
198 lt->set_color(Color(ailt->mColorDiffuse.r, ailt->mColorDiffuse.g, ailt->mColorDiffuse.b, 1.0));
200 lights.push_back(lt);
201 count++;
202 }
204 return count;
205 }
206 */
208 int Tile::load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap)
209 {
210 int count = 0;
212 for(int i=0; i<(int)scn->mNumMeshes; i++) {
213 // ignore any lines or other crap
214 if(scn->mMeshes[i]->mPrimitiveTypes != aiPrimitiveType_TRIANGLE) {
215 continue;
216 }
218 Mesh *mesh = new Mesh;
219 if(!mesh->create(scn, scn->mMeshes[i])) {
220 delete mesh;
221 continue;
222 }
224 Material mat;
225 mat.load(scn->mMaterials[scn->mMeshes[i]->mMaterialIndex], tset->get_textures());
226 mesh->set_material(mat);
228 // retrieve the node pointer
229 const char *name = "<unknown>";
231 auto iter = nmap.find(scn->mMeshes[i]);
232 if(iter != nmap.end()) {
233 aiNode *node = iter->second;
235 Matrix4x4 xform;
236 //xform.rotate(Vector3(-M_PI / 2.0, 0, 0));
237 xform = *(Matrix4x4*)&node->mTransformation;
238 mesh->set_xform(xform);
240 name = node->mName.data;
241 mesh->set_name(name);
242 }
244 // find which side is this mesh on
245 unsigned int side = 0;
246 if(strstr(name, "NORTH")) {
247 side |= TILE_NORTH;
248 }
249 if(strstr(name, "SOUTH")) {
250 side |= TILE_SOUTH;
251 }
252 if(strstr(name, "EAST")) {
253 side |= TILE_EAST;
254 }
255 if(strstr(name, "WEST")) {
256 side |= TILE_WEST;
257 }
258 if(!side) {
259 side = TILE_ALL;
260 }
262 // what a sordid hack... if the name contains "LIGHT", then make a light out of this
263 // and destroy the mesh...
264 if(strstr(name, "LIGHT")) {
265 PointLight *lt = mesh_to_light(mesh);
266 if(!lt) {
267 fprintf(stderr, "failed to convert mesh %s to light\n", name);
268 } else {
269 lights.push_back(lt);
270 light_side.push_back(side);
271 }
272 delete mesh;
274 // ... ALSO add a fire particle system :) save me jebus
275 struct psys_emitter *ps = psys_create();
276 if(ps && psys_load_attr(&ps->attr, datafile_path("fire.psys").c_str()) == 0) {
277 Vector3 lpos = lt->get_position();
278 psys_set_pos(ps, v3_cons(lpos.x, lpos.y + 0.01, lpos.z), 0);
279 psys_global.push_back(ps);
280 psys_side.push_back(side);
281 } else {
282 fprintf(stderr, "failed to create global particle system\n");
283 }
285 // ... AND make an audio source out of each light source
286 if(cfg.sound) {
287 SampleSet *sampleset = tset->get_samples();
288 AudioSample *sample = sampleset->get("fire.ogg");
289 if(sample) {
290 AudioSourceDesc adesc = {side, lt->get_position(), sample, 1.0, 0.1};
291 ausrc.push_back(adesc);
292 }
293 }
295 } else {
296 meshes.push_back(mesh);
297 mesh_side.push_back(side);
298 count++;
299 }
300 }
301 return count;
302 }
304 static void build_nodemap(map<aiMesh*, aiNode*> *nmap, const aiScene *scn, aiNode *node)
305 {
306 unsigned int i;
308 for(i=0; i<node->mNumMeshes; i++) {
309 aiMesh *m = scn->mMeshes[node->mMeshes[i]];
311 (*nmap)[m] = node;
312 }
314 for(i=0; i<node->mNumChildren; i++) {
315 build_nodemap(nmap, scn, node->mChildren[i]);
316 }
317 }
319 static PointLight *mesh_to_light(Mesh *m)
320 {
321 Vector3 center = m->get_bsph_center();
322 float rad = m->get_bsph_radius();
324 PointLight *lt = new PointLight(center);
325 lt->set_radius(rad);
327 lt->set_color(m->get_material().kd);
329 return lt;
330 }