dungeon_crawler
changeset 46:f3030df27110
debugging the particles
author | John Tsiombikas <nuclear@mutantstargoat.com> |
---|---|
date | Thu, 13 Sep 2012 06:33:51 +0300 |
parents | dfd3a413ef9e |
children | d52711f2b9a1 |
files | prototype/data/fire.psys prototype/data/fire_particle.png prototype/src/level.cc prototype/src/light.cc prototype/src/light.h prototype/src/main.cc prototype/src/renderer.cc prototype/src/texman.cc prototype/src/tile.cc prototype/src/tile.h prototype/src/tileset.cc prototype/src/tileset.h |
diffstat | 12 files changed, 131 insertions(+), 18 deletions(-) [+] |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/prototype/data/fire.psys Thu Sep 13 06:33:51 2012 +0300 1.3 @@ -0,0 +1,27 @@ 1.4 +# fire particle system definition 1.5 +# 1.6 +# lines of key = value pairs. 1.7 +# animated attributes can be defined as key(time) = value. 1.8 +# time defaults to 0 if missing. time values can have an optional s suffix, 1.9 +# signifying seconds, otherwise they are assumed to be in milliseconds. 1.10 +# 1.11 +# string values enclosed in double quotes 1.12 +# vector values enclosed in square brackets [x y z] 1.13 +# randomized values have a pair of values/vectors separated by a tilde: 1.14 +# center ~ range. If the range is missing, it's assumed to be 0. 1.15 + 1.16 +texture = "fire_particle.png" 1.17 +rate = 20 1.18 +life = 1 1.19 +grav = [0 0.5 0] 1.20 +spawn_range = 0.025 1.21 + 1.22 +size = 0.1 1.23 + 1.24 +pcolor(0) = [0.91 0.89 0.65] 1.25 +pcolor(700) = [1.0 0.3 0.12] 1.26 +pcolor(1000) = [1.0 0.3 0.12] 1.27 + 1.28 +palpha(0) = 1 1.29 +palpha(700) = 1 1.30 +palpha(1000) = 0
2.1 Binary file prototype/data/fire_particle.png has changed
3.1 --- a/prototype/src/level.cc Wed Sep 12 06:04:20 2012 +0300 3.2 +++ b/prototype/src/level.cc Thu Sep 13 06:33:51 2012 +0300 3.3 @@ -100,8 +100,10 @@ 3.4 3.5 Vector3 Level::get_cell_pos(int x, int y) const 3.6 { 3.7 - float posx = (x - xsz / 2) * cell_size; 3.8 - float posy = (y - ysz / 2) * cell_size; 3.9 + float posx = (float)x * cell_size; 3.10 + float posy = (float)y * cell_size; 3.11 + posx -= cell_size * (float)xsz / 2.0f; 3.12 + posy -= cell_size * (float)ysz / 2.0f; 3.13 return Vector3(posx, 0, posy); 3.14 } 3.15 3.16 @@ -175,6 +177,28 @@ 3.17 } 3.18 } 3.19 3.20 +void Level::draw_post() const 3.21 +{ 3.22 + glMatrixMode(GL_MODELVIEW); 3.23 + 3.24 + for(int i=0; i<ysz; i++) { 3.25 + for(int j=0; j<xsz; j++) { 3.26 + const GridCell *cell = get_cell(j, i); 3.27 + if(cell) { 3.28 + Vector3 pos = get_cell_pos(j, i); 3.29 + glPushMatrix(); 3.30 + glTranslatef(pos.x, pos.y, pos.z); 3.31 + glScalef(cell_size, cell_size, cell_size); 3.32 + 3.33 + unsigned int dmask = get_cell_dirmask(j, i); 3.34 + 3.35 + cell->draw_post(dmask); 3.36 + glPopMatrix(); 3.37 + } 3.38 + } 3.39 + } 3.40 +} 3.41 + 3.42 void Level::draw_grid() const 3.43 { 3.44 float xlen = xsz * cell_size; 3.45 @@ -221,7 +245,7 @@ 3.46 tiles.push_back(tile); 3.47 3.48 /* instanciate any particle systems */ 3.49 - const struct psys_attributes **psattr = tile->get_unique_psys(); 3.50 + const struct psys_attributes * const *psattr = tile->get_unique_psys(); 3.51 int num_psattr = tile->get_unique_psys_count(); 3.52 3.53 for(int i=0; i<num_psattr; i++) { 3.54 @@ -233,9 +257,6 @@ 3.55 3.56 void GridCell::update(unsigned long msec, float dt) 3.57 { 3.58 - for(auto tile : tiles) { 3.59 - tile->update(msec, dt); 3.60 - } 3.61 for(auto ps : psys) { 3.62 psys_update(ps, (float)msec / 1000.0f); 3.63 }
4.1 --- a/prototype/src/light.cc Wed Sep 12 06:04:20 2012 +0300 4.2 +++ b/prototype/src/light.cc Thu Sep 13 06:33:51 2012 +0300 4.3 @@ -86,6 +86,11 @@ 4.4 this->pos = pos; 4.5 } 4.6 4.7 +const Vector3 &PointLight::get_position() const 4.8 +{ 4.9 + return pos; 4.10 +} 4.11 + 4.12 void PointLight::set_attenuation(float att_const, float att_lin, float att_quad) 4.13 { 4.14 atten[0] = att_const;
5.1 --- a/prototype/src/light.h Wed Sep 12 06:04:20 2012 +0300 5.2 +++ b/prototype/src/light.h Thu Sep 13 06:33:51 2012 +0300 5.3 @@ -43,6 +43,8 @@ 5.4 PointLight(const Vector3 &pos, const Color &col = 1.0); 5.5 5.6 void set_position(const Vector3 &pos); 5.7 + const Vector3 &get_position() const; 5.8 + 5.9 void set_attenuation(float att_const, float att_lin, float att_quad); 5.10 5.11 void set_radius(float rad); 5.12 @@ -64,6 +66,7 @@ 5.13 DirLight(const Vector3 &dir, const Color &col = 1.0); 5.14 5.15 void set_direction(const Vector3 &dir); 5.16 + const Vector3 &get_direction() const; 5.17 5.18 virtual void use(int id = 0) const; 5.19 };
6.1 --- a/prototype/src/main.cc Wed Sep 12 06:04:20 2012 +0300 6.2 +++ b/prototype/src/main.cc Thu Sep 13 06:33:51 2012 +0300 6.3 @@ -3,7 +3,9 @@ 6.4 #include <assert.h> 6.5 #include <unistd.h> 6.6 #include "opengl.h" 6.7 +#include "psys/psys.h" 6.8 #include "level.h" 6.9 +#include "texman.h" 6.10 #include "camera.h" 6.11 #include "datapath.h" 6.12 #include "tileset.h" 6.13 @@ -27,6 +29,7 @@ 6.14 void keyb_con(unsigned char key, int x, int y); 6.15 void mouse(int bn, int state, int x, int y); 6.16 void motion(int x, int y); 6.17 +unsigned int load_psys_tex(const char *fname, void *cls); 6.18 6.19 static TileSet *tileset; 6.20 static Level *level; 6.21 @@ -104,6 +107,8 @@ 6.22 return false; 6.23 } 6.24 6.25 + psys_texture_loader(load_psys_tex, 0, 0); 6.26 + 6.27 // load a tileset 6.28 tileset = new TileSet; 6.29 printf("loading tileset: %s\n", cfg.tileset_file); 6.30 @@ -238,6 +243,7 @@ 6.31 6.32 cam.input_move(dx, 0, dy); 6.33 6.34 + tileset->update_tiles(msec); 6.35 level->update(msec, dt); 6.36 6.37 last_upd = msec; 6.38 @@ -355,3 +361,9 @@ 6.39 glutPostRedisplay(); 6.40 } 6.41 } 6.42 + 6.43 +unsigned int load_psys_tex(const char *fname, void *cls) 6.44 +{ 6.45 + TextureSet *texset = tileset->get_textures(); 6.46 + return texset->get_texture(fname); 6.47 +}
7.1 --- a/prototype/src/renderer.cc Wed Sep 12 06:04:20 2012 +0300 7.2 +++ b/prototype/src/renderer.cc Thu Sep 13 06:33:51 2012 +0300 7.3 @@ -90,9 +90,10 @@ 7.4 glEnable(GL_LIGHTING); 7.5 level->draw(); 7.6 7.7 + glUseProgram(0); 7.8 + level->draw_post(); 7.9 + 7.10 glPopAttrib(); 7.11 - 7.12 - glUseProgram(0); 7.13 } 7.14 7.15 static unsigned int load_sdr(const char *vfname, const char *pfname)
8.1 --- a/prototype/src/texman.cc Wed Sep 12 06:04:20 2012 +0300 8.2 +++ b/prototype/src/texman.cc Thu Sep 13 06:33:51 2012 +0300 8.3 @@ -23,8 +23,13 @@ 8.4 const char *path, *slash; 8.5 if((slash = strrchr(fname, '/'))) { 8.6 path = slash + 1; 8.7 + } else { 8.8 + path = fname; 8.9 } 8.10 - path = datafile_path(path); 8.11 + if(!(path = datafile_path(path))) { 8.12 + fprintf(stderr, "can't find texture: %s\n", fname); 8.13 + return 0; 8.14 + } 8.15 8.16 printf("loading texture: %s\n", path); 8.17 unsigned int tex = load_texture(path);
9.1 --- a/prototype/src/tile.cc Wed Sep 12 06:04:20 2012 +0300 9.2 +++ b/prototype/src/tile.cc Thu Sep 13 06:33:51 2012 +0300 9.3 @@ -1,4 +1,5 @@ 9.4 #include <stdio.h> 9.5 +#include <ctype.h> 9.6 #include <map> 9.7 #include "opengl.h" 9.8 #include <assimp/cimport.h> 9.9 @@ -7,6 +8,7 @@ 9.10 #include "tile.h" 9.11 #include "tileset.h" 9.12 #include "renderer.h" 9.13 +#include "datapath.h" 9.14 9.15 using std::map; 9.16 9.17 @@ -18,6 +20,7 @@ 9.18 Tile::Tile(TileSet *tileset) 9.19 { 9.20 tset = tileset; 9.21 + last_upd = LONG_MIN; 9.22 } 9.23 9.24 Tile::~Tile() 9.25 @@ -28,18 +31,20 @@ 9.26 for(auto lt : lights) { 9.27 delete lt; 9.28 } 9.29 - for(auto ps : psattr) { 9.30 - psys_destroy_attr(ps); 9.31 - delete ps; 9.32 + for(auto psa : psattr) { 9.33 + psys_free_attr(psa); 9.34 + } 9.35 + for(auto ps : psys_global) { 9.36 + psys_free(ps); 9.37 } 9.38 } 9.39 9.40 -const struct psys_attributes **get_unique_psys() const 9.41 +const struct psys_attributes * const *Tile::get_unique_psys() const 9.42 { 9.43 return &psattr[0]; 9.44 } 9.45 9.46 -int get_unique_psys_count() const 9.47 +int Tile::get_unique_psys_count() const 9.48 { 9.49 return (int)psattr.size(); 9.50 } 9.51 @@ -85,7 +90,10 @@ 9.52 9.53 void Tile::update(unsigned long msec, float dt) 9.54 { 9.55 - // TODO particle system update 9.56 + // update particle systems 9.57 + for(auto ps : psys_global) { 9.58 + psys_update(ps, (float)msec / 1000.0f); 9.59 + } 9.60 } 9.61 9.62 void Tile::draw(unsigned int draw_mask) const 9.63 @@ -106,6 +114,16 @@ 9.64 } 9.65 } 9.66 9.67 +void Tile::draw_post(unsigned int draw_mask) const 9.68 +{ 9.69 + // draw global particle systems (simulated once) 9.70 + for(size_t i=0; i<psys_global.size(); i++) { 9.71 + if(psys_side[i] & draw_mask) { 9.72 + psys_draw(psys_global[i]); 9.73 + } 9.74 + } 9.75 +} 9.76 + 9.77 /* 9.78 int Tile::load_lights(const aiScene *scn) 9.79 { 9.80 @@ -214,6 +232,17 @@ 9.81 } 9.82 delete mesh; 9.83 9.84 + // ... ALSO add a fire particle system :) save me jebus 9.85 + struct psys_emitter *ps = psys_create(); 9.86 + if(ps && psys_load_attr(&ps->attr, datafile_path("fire.psys")) == 0) { 9.87 + Vector3 lpos = lt->get_position(); 9.88 + psys_set_pos(ps, v3_cons(lpos.x, lpos.y, lpos.z), 0); 9.89 + psys_global.push_back(ps); 9.90 + psys_side.push_back(side); 9.91 + } else { 9.92 + fprintf(stderr, "failed to create global particle system\n"); 9.93 + } 9.94 + 9.95 } else { 9.96 meshes.push_back(mesh); 9.97 mesh_side.push_back(side);
10.1 --- a/prototype/src/tile.h Wed Sep 12 06:04:20 2012 +0300 10.2 +++ b/prototype/src/tile.h Thu Sep 13 06:33:51 2012 +0300 10.3 @@ -23,12 +23,13 @@ 10.4 TileSet *tset; 10.5 10.6 std::vector<Mesh*> meshes; 10.7 - std::vector<unsigned int> mesh_side, light_side; 10.8 + std::vector<unsigned int> mesh_side, light_side, psys_side; 10.9 std::vector<Light*> lights; 10.10 // attributes for gridcell-unique particle systems 10.11 std::vector<struct psys_attributes*> psattr; 10.12 // global particle systems (simulated once, drawn for each tile instance). 10.13 std::vector<struct psys_emitter*> psys_global; 10.14 + long last_upd; 10.15 10.16 int load_lights(const aiScene *scn); 10.17 int load_meshes(const aiScene *scn, const std::map<aiMesh*, aiNode*> &nmap); 10.18 @@ -37,7 +38,7 @@ 10.19 Tile(TileSet *tileset = 0); 10.20 ~Tile(); 10.21 10.22 - const struct psys_attributes **get_unique_psys() const; 10.23 + const struct psys_attributes * const *get_unique_psys() const; 10.24 int get_unique_psys_count() const; 10.25 10.26 bool load(const char *fname); 10.27 @@ -46,7 +47,7 @@ 10.28 10.29 void draw(unsigned int drawmask) const; 10.30 void draw_lights(unsigned int drawmask) const; 10.31 - void draw_psys(unsigned int drawmask) const; 10.32 + void draw_post(unsigned int drawmask) const; 10.33 }; 10.34 10.35
11.1 --- a/prototype/src/tileset.cc Wed Sep 12 06:04:20 2012 +0300 11.2 +++ b/prototype/src/tileset.cc Thu Sep 13 06:33:51 2012 +0300 11.3 @@ -72,6 +72,13 @@ 11.4 return res != tiles.end() ? res->second : 0; 11.5 } 11.6 11.7 +void TileSet::update_tiles(unsigned long msec) 11.8 +{ 11.9 + for(auto tilepair : tiles) { 11.10 + tilepair.second->update(msec, 0.0f); 11.11 + } 11.12 +} 11.13 + 11.14 void set_active_tileset(TileSet *set) 11.15 { 11.16 active_tileset = set;
12.1 --- a/prototype/src/tileset.h Wed Sep 12 06:04:20 2012 +0300 12.2 +++ b/prototype/src/tileset.h Thu Sep 13 06:33:51 2012 +0300 12.3 @@ -21,6 +21,8 @@ 12.4 const TextureSet *get_textures() const; 12.5 12.6 Tile *get_tile(const char *name) const; 12.7 + 12.8 + void update_tiles(unsigned long msec); 12.9 }; 12.10 12.11 void set_active_tileset(TileSet *set);