goat3dgfx

annotate src/psyspp.cc @ 9:25b911c7c35c

fixed some line endings fixed the cubemap2.jpg file in examples/cubemap/data which was resized improperly causing seams...
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 18 Nov 2013 04:10:19 +0200
parents
children 7d6b667821cf
rev   line source
nuclear@0 1 #include <limits.h>
nuclear@0 2 #include "psyspp.h"
nuclear@0 3 #include "sdrman.h"
nuclear@0 4 #include "logger.h"
nuclear@0 5 #include "mesh.h" // just for the attrib enums
nuclear@0 6 #include "unistate.h"
nuclear@0 7 #include "datapath.h"
nuclear@0 8 #include "texman.h"
nuclear@0 9 #include "texgen.h"
nuclear@0 10
nuclear@0 11 static void pdraw_start(const psys_emitter *em, void *cls);
nuclear@0 12 static void pdraw(const psys_emitter *em, const psys_particle *part, void *cls);
nuclear@0 13 static void pdraw_end(const psys_emitter *em, void *cls);
nuclear@0 14
nuclear@0 15 static unsigned int psys_load_texture(const char *fname, void *cls);
nuclear@0 16
nuclear@0 17 ParticleSystemAttributes::ParticleSystemAttributes()
nuclear@0 18 {
nuclear@0 19 tex = 0;
nuclear@0 20 own_psattr = true;
nuclear@0 21 psattr = new psys_attributes;
nuclear@0 22 psys_init_attr(psattr);
nuclear@0 23 }
nuclear@0 24
nuclear@0 25 ParticleSystemAttributes::ParticleSystemAttributes(psys_attributes *psattr)
nuclear@0 26 {
nuclear@0 27 tex = 0;
nuclear@0 28 own_psattr = false;
nuclear@0 29 this->psattr = psattr;
nuclear@0 30 }
nuclear@0 31
nuclear@0 32 ParticleSystemAttributes::~ParticleSystemAttributes()
nuclear@0 33 {
nuclear@0 34 if(own_psattr) {
nuclear@0 35 psys_destroy_attr(psattr);
nuclear@0 36 delete psattr;
nuclear@0 37 }
nuclear@0 38 }
nuclear@0 39
nuclear@0 40 ParticleSystemAttributes::ParticleSystemAttributes(const ParticleSystemAttributes &rhs)
nuclear@0 41 {
nuclear@0 42 own_psattr = true;
nuclear@0 43 tex = rhs.tex;
nuclear@0 44 psattr = new psys_attributes;
nuclear@0 45 psys_init_attr(psattr);
nuclear@0 46
nuclear@0 47 psys_copy_attr(psattr, rhs.psattr);
nuclear@0 48 }
nuclear@0 49
nuclear@0 50 ParticleSystemAttributes &ParticleSystemAttributes::operator =(const ParticleSystemAttributes &rhs)
nuclear@0 51 {
nuclear@0 52 if(&rhs != this) {
nuclear@0 53 tex = rhs.tex;
nuclear@0 54 psys_copy_attr(psattr, rhs.psattr);
nuclear@0 55 }
nuclear@0 56 return *this;
nuclear@0 57 }
nuclear@0 58
nuclear@0 59 bool ParticleSystemAttributes::load(const char *fname)
nuclear@0 60 {
nuclear@0 61 psys_texture_loader(psys_load_texture, 0, this);
nuclear@0 62 return psys_load_attr(psattr, datafile_path(fname).c_str()) != -1;
nuclear@0 63 }
nuclear@0 64
nuclear@0 65 bool ParticleSystemAttributes::load(FILE *fp)
nuclear@0 66 {
nuclear@0 67 psys_texture_loader(psys_load_texture, 0, this);
nuclear@0 68 return psys_load_attr_stream(psattr, fp) != -1;
nuclear@0 69 }
nuclear@0 70
nuclear@0 71 bool ParticleSystemAttributes::save(const char *fname) const
nuclear@0 72 {
nuclear@0 73 return psys_save_attr(psattr, fname) != -1;
nuclear@0 74 }
nuclear@0 75
nuclear@0 76 bool ParticleSystemAttributes::save(FILE *fp) const
nuclear@0 77 {
nuclear@0 78 return psys_save_attr_stream(psattr, fp) != -1;
nuclear@0 79 }
nuclear@0 80
nuclear@0 81 void ParticleSystemAttributes::set_part_color(const Vector3 &color, float t)
nuclear@0 82 {
nuclear@0 83 psys_set_value3(&psattr->part_attr.color, (anm_time_t)(t * 1000.0), v3_cons(color.x, color.y, color.z));
nuclear@0 84 }
nuclear@0 85
nuclear@0 86 void ParticleSystemAttributes::set_part_alpha(float alpha, float t)
nuclear@0 87 {
nuclear@0 88 psys_set_value(&psattr->part_attr.alpha, (anm_time_t)(t * 1000.0), alpha);
nuclear@0 89 }
nuclear@0 90
nuclear@0 91 void ParticleSystemAttributes::set_part_scale(float size, float t)
nuclear@0 92 {
nuclear@0 93 psys_set_value(&psattr->part_attr.size, (anm_time_t)(t * 1000.0), size);
nuclear@0 94 }
nuclear@0 95
nuclear@0 96
nuclear@0 97 // emmiter attributes
nuclear@0 98 void ParticleSystemAttributes::set_texture(Texture *tex)
nuclear@0 99 {
nuclear@0 100 this->tex = tex;
nuclear@0 101 psattr->tex = tex->get_id();
nuclear@0 102 }
nuclear@0 103
nuclear@0 104 Texture *ParticleSystemAttributes::get_texture() const
nuclear@0 105 {
nuclear@0 106 return tex;
nuclear@0 107 }
nuclear@0 108
nuclear@0 109 void ParticleSystemAttributes::set_spawn_range(const Vector3 &range, long tm)
nuclear@0 110 {
nuclear@0 111 psys_set_value3(&psattr->spawn_range, ANM_MSEC2TM(tm), v3_cons(range.x, range.y, range.z));
nuclear@0 112 }
nuclear@0 113
nuclear@0 114 void ParticleSystemAttributes::set_spawn_rate(float rate, long tm)
nuclear@0 115 {
nuclear@0 116 psys_set_value(&psattr->rate, ANM_MSEC2TM(tm), rate);
nuclear@0 117 }
nuclear@0 118
nuclear@0 119 float ParticleSystemAttributes::get_spawn_rate(long tm) const
nuclear@0 120 {
nuclear@0 121 return psys_get_value(&psattr->rate, ANM_MSEC2TM(tm));
nuclear@0 122 }
nuclear@0 123
nuclear@0 124 void ParticleSystemAttributes::set_life(float life, float range, long tm)
nuclear@0 125 {
nuclear@0 126 psys_set_anm_rnd(&psattr->life, ANM_MSEC2TM(tm), life, range);
nuclear@0 127 }
nuclear@0 128
nuclear@0 129 void ParticleSystemAttributes::set_size(float sz, float range, long tm)
nuclear@0 130 {
nuclear@0 131 psys_set_anm_rnd(&psattr->size, ANM_MSEC2TM(tm), sz, range);
nuclear@0 132 }
nuclear@0 133
nuclear@0 134 void ParticleSystemAttributes::set_spawn_dir(const Vector3 &dir, const Vector3 &range, long tm)
nuclear@0 135 {
nuclear@0 136 psys_set_anm_rnd3(&psattr->dir, ANM_MSEC2TM(tm), v3_cons(dir.x, dir.y, dir.z), v3_cons(range.x, range.y, range.z));
nuclear@0 137 }
nuclear@0 138
nuclear@0 139 void ParticleSystemAttributes::set_gravity(const Vector3 &grav, long tm)
nuclear@0 140 {
nuclear@0 141 psys_set_value3(&psattr->grav, ANM_MSEC2TM(tm), v3_cons(grav.x, grav.y, grav.z));
nuclear@0 142 }
nuclear@0 143
nuclear@0 144 void ParticleSystemAttributes::set_drag(float drag)
nuclear@0 145 {
nuclear@0 146 psattr->drag = drag;
nuclear@0 147 }
nuclear@0 148
nuclear@0 149 void ParticleSystemAttributes::set_particle_limit(int lim)
nuclear@0 150 {
nuclear@0 151 psattr->max_particles = lim;
nuclear@0 152 }
nuclear@0 153
nuclear@0 154
nuclear@0 155 // ---- ParticleSystem ----
nuclear@0 156
nuclear@0 157 ParticleSystem::ParticleSystem()
nuclear@0 158 : attr(&psys.attr)
nuclear@0 159 {
nuclear@0 160 psys_init(&psys);
nuclear@0 161 psys_draw_func(&psys, pdraw, pdraw_start, pdraw_end, (void*)this);
nuclear@0 162 start_time = LONG_MIN;
nuclear@0 163 last_upd_time = LONG_MIN;
nuclear@0 164 }
nuclear@0 165
nuclear@0 166 ParticleSystem::~ParticleSystem()
nuclear@0 167 {
nuclear@0 168 psys_destroy(&psys);
nuclear@0 169 }
nuclear@0 170
nuclear@0 171 void ParticleSystem::set_start_time(long tm)
nuclear@0 172 {
nuclear@0 173 start_time = tm;
nuclear@0 174 }
nuclear@0 175
nuclear@0 176 bool ParticleSystem::is_active() const
nuclear@0 177 {
nuclear@0 178 float rate = attr.get_spawn_rate(last_upd_time);
nuclear@0 179 return psys.pcount > 0 || last_upd_time == 0;// || rate > 0.0;
nuclear@0 180 }
nuclear@0 181
nuclear@0 182 ParticleSystemAttributes *ParticleSystem::get_attr()
nuclear@0 183 {
nuclear@0 184 return &attr;
nuclear@0 185 }
nuclear@0 186
nuclear@0 187 const ParticleSystemAttributes *ParticleSystem::get_attr() const
nuclear@0 188 {
nuclear@0 189 return &attr;
nuclear@0 190 }
nuclear@0 191
nuclear@0 192 void ParticleSystem::set_attr(const ParticleSystemAttributes &pattr)
nuclear@0 193 {
nuclear@0 194 attr = pattr;
nuclear@0 195 }
nuclear@0 196
nuclear@0 197 bool ParticleSystem::load(const char *fname)
nuclear@0 198 {
nuclear@0 199 psys_texture_loader(psys_load_texture, 0, &attr);
nuclear@0 200 return attr.load(fname);
nuclear@0 201 }
nuclear@0 202
nuclear@0 203 bool ParticleSystem::save(const char *fname) const
nuclear@0 204 {
nuclear@0 205 return attr.save(fname);
nuclear@0 206 }
nuclear@0 207
nuclear@0 208 void ParticleSystem::update(long tm)
nuclear@0 209 {
nuclear@0 210 if(start_time > LONG_MIN) {
nuclear@0 211 tm -= start_time;
nuclear@0 212 }
nuclear@0 213
nuclear@0 214 Matrix4x4 xform;
nuclear@0 215 get_xform(tm, &xform);
nuclear@0 216
nuclear@0 217 Vector3 pos = Vector3(0, 0, 0).transformed(xform);
nuclear@0 218
nuclear@0 219 psys_set_pos(&psys, v3_cons(pos.x, pos.y, pos.z), 0);
nuclear@0 220 psys_update(&psys, (double)tm / 1000.0);
nuclear@0 221
nuclear@0 222 last_upd_time = tm;
nuclear@0 223 }
nuclear@0 224
nuclear@0 225 void ParticleSystem::draw() const
nuclear@0 226 {
nuclear@0 227 psys_draw(&psys);
nuclear@0 228 }
nuclear@0 229
nuclear@0 230 // ---- particle drawing ----
nuclear@0 231 struct PVertex {
nuclear@0 232 Vector4 color;
nuclear@0 233 Vector3 pos;
nuclear@0 234 Vector2 texcoord;
nuclear@0 235 };
nuclear@0 236
nuclear@0 237
nuclear@0 238 #define USE_VBO
nuclear@0 239 #define USE_IBO
nuclear@0 240
nuclear@0 241
nuclear@0 242 #define MAX_DRAW_PART 256
nuclear@0 243 #define MAX_PVERTS (MAX_DRAW_PART * 4)
nuclear@0 244 static PVertex *pvarr, *pvptr;
nuclear@0 245
nuclear@0 246 // double-buffered vbo set, write on one, while drawing from the other
nuclear@0 247
nuclear@0 248 #ifdef USE_VBO
nuclear@0 249 static unsigned int vbo[2];
nuclear@0 250 static int cur_buf; // current write vbo
nuclear@0 251 #endif
nuclear@0 252 static int num_buffered; // number of particles bufferd, will flush when >= MAX_DRAW_PART
nuclear@0 253
nuclear@0 254 // ok so the index array is constant, regardless of the particle system
nuclear@0 255 // so this is a static index buffer created in init_particle_draw which should
nuclear@0 256 // be called once.
nuclear@0 257 #define MAX_PVIDX (MAX_DRAW_PART * 6)
nuclear@0 258 #ifdef USE_IBO
nuclear@0 259 static unsigned int ibo;
nuclear@0 260 #endif
nuclear@0 261 unsigned int *pvidx;
nuclear@0 262
nuclear@0 263 static ShaderProg *psdr; // particle shader
nuclear@0 264 static Texture2D *blank_tex;
nuclear@0 265
nuclear@0 266 static inline void init_particle_draw()
nuclear@0 267 {
nuclear@0 268 static bool done_init;
nuclear@0 269 if(done_init) {
nuclear@0 270 return; // once
nuclear@0 271 }
nuclear@0 272
nuclear@0 273 pvidx = new unsigned int[MAX_PVIDX];
nuclear@0 274 unsigned int *ptr = pvidx;
nuclear@0 275
nuclear@0 276 static const unsigned int idxoffs[] = { 0, 1, 2, 0, 2, 3 };
nuclear@0 277
nuclear@0 278 for(int i=0; i<MAX_DRAW_PART; i++) {
nuclear@0 279 for(int j=0; j<6; j++) {
nuclear@0 280 *ptr++ = i * 4 + idxoffs[j];
nuclear@0 281 }
nuclear@0 282 }
nuclear@0 283
nuclear@0 284 #ifdef USE_IBO
nuclear@0 285 glGenBuffers(1, &ibo);
nuclear@0 286 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
nuclear@0 287 glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_PVIDX * sizeof *pvidx, pvidx, GL_STATIC_DRAW);
nuclear@0 288 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
nuclear@0 289
nuclear@0 290 delete [] pvidx;
nuclear@0 291 #endif
nuclear@0 292
nuclear@0 293 #ifdef USE_VBO
nuclear@0 294 // create the stream vertex buffers (double buffering)
nuclear@0 295 glGenBuffers(2, vbo);
nuclear@0 296 for(int i=0; i<2; i++) {
nuclear@0 297 glBindBuffer(GL_ARRAY_BUFFER, vbo[i]);
nuclear@0 298 glBufferData(GL_ARRAY_BUFFER, MAX_PVERTS * sizeof(PVertex), 0, GL_STREAM_DRAW);
nuclear@0 299 }
nuclear@0 300 glBindBuffer(GL_ARRAY_BUFFER, 0);
nuclear@0 301 #else
nuclear@0 302 pvarr = new PVertex[MAX_PVERTS];
nuclear@0 303 pvptr = pvarr;
nuclear@0 304 #endif
nuclear@0 305
nuclear@0 306 // load shader program
nuclear@0 307 if(!(psdr = get_sdrprog("psdr.v.glsl", "psdr.p.glsl"))) {
nuclear@0 308 error_log("failed to load particle shader!\n");
nuclear@0 309 }
nuclear@0 310
nuclear@0 311 // create empty texture
nuclear@0 312 Image *img = texgen_solid(8, 8, Vector4(1, 1, 1, 1));
nuclear@0 313 blank_tex = new Texture2D;
nuclear@0 314 blank_tex->set_image(*img);
nuclear@0 315 delete img;
nuclear@0 316
nuclear@0 317 done_init = true;
nuclear@0 318 }
nuclear@0 319
nuclear@0 320 static void pdraw_flush()
nuclear@0 321 {
nuclear@0 322 #ifdef USE_VBO
nuclear@0 323 // assuming vbo[cur_buf] is bound
nuclear@0 324 glUnmapBuffer(GL_ARRAY_BUFFER);
nuclear@0 325 #endif
nuclear@0 326
nuclear@0 327 // draw from the bound buffer 6 indices per particle
nuclear@0 328 #ifdef USE_IBO
nuclear@0 329 glDrawElements(GL_TRIANGLES, num_buffered * 6, GL_UNSIGNED_INT, 0);
nuclear@0 330 #else
nuclear@0 331 glDrawElements(GL_TRIANGLES, num_buffered * 6, GL_UNSIGNED_INT, pvidx);
nuclear@0 332 #endif
nuclear@0 333 num_buffered = 0;
nuclear@0 334
nuclear@0 335 #ifdef USE_VBO
nuclear@0 336 // map the next buffer (write buffer) while the previous is drawing
nuclear@0 337 cur_buf = (cur_buf + 1) & 1;
nuclear@0 338 glBindBuffer(GL_ARRAY_BUFFER, vbo[cur_buf]);
nuclear@0 339 pvarr = (PVertex*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
nuclear@0 340 #endif
nuclear@0 341 pvptr = pvarr;
nuclear@0 342 }
nuclear@0 343
nuclear@0 344 static void pdraw_start(const psys_emitter *em, void *cls)
nuclear@0 345 {
nuclear@0 346 ParticleSystem *ps = (ParticleSystem*)cls;
nuclear@0 347
nuclear@0 348 init_particle_draw();
nuclear@0 349
nuclear@0 350 num_buffered = 0;
nuclear@0 351
nuclear@0 352 #ifdef USE_IBO
nuclear@0 353 // bind the particle index buffer which is static
nuclear@0 354 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
nuclear@0 355 #endif
nuclear@0 356
nuclear@0 357 #ifdef USE_VBO
nuclear@0 358 // map the current write buffer
nuclear@0 359 glBindBuffer(GL_ARRAY_BUFFER, vbo[cur_buf]);
nuclear@0 360 pvarr = (PVertex*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
nuclear@0 361 #endif
nuclear@0 362 pvptr = pvarr;
nuclear@0 363
nuclear@0 364 Texture *tex = ps->get_attr()->get_texture();
nuclear@0 365 if(tex) {
nuclear@0 366 tex->bind();
nuclear@0 367 } else {
nuclear@0 368 blank_tex->bind();
nuclear@0 369 }
nuclear@0 370 psdr->bind();
nuclear@0 371
nuclear@0 372 glDisable(GL_DEPTH_TEST);
nuclear@0 373 glDisable(GL_CULL_FACE);
nuclear@0 374 glEnable(GL_BLEND);
nuclear@0 375 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
nuclear@0 376 glDepthMask(0);
nuclear@0 377
nuclear@0 378 glEnableVertexAttribArray(MESH_ATTR_VERTEX);
nuclear@0 379 glEnableVertexAttribArray(MESH_ATTR_COLOR);
nuclear@0 380 glEnableVertexAttribArray(MESH_ATTR_TEXCOORD);
nuclear@0 381
nuclear@0 382 #ifdef USE_VBO
nuclear@0 383 glVertexAttribPointer(MESH_ATTR_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof *pvarr, (void*)((char*)&pvarr->pos - (char*)pvarr));
nuclear@0 384 glVertexAttribPointer(MESH_ATTR_COLOR, 4, GL_FLOAT, GL_TRUE, sizeof *pvarr, (void*)((char*)&pvarr->color - (char*)pvarr));
nuclear@0 385 glVertexAttribPointer(MESH_ATTR_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof *pvarr, (void*)((char*)&pvarr->texcoord - (char*)pvarr));
nuclear@0 386 #else
nuclear@0 387 glVertexAttribPointer(MESH_ATTR_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof *pvarr, (void*)&pvarr->pos);
nuclear@0 388 glVertexAttribPointer(MESH_ATTR_COLOR, 4, GL_FLOAT, GL_TRUE, sizeof *pvarr, (void*)&pvarr->color);
nuclear@0 389 glVertexAttribPointer(MESH_ATTR_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof *pvarr, (void*)&pvarr->texcoord);
nuclear@0 390 #endif
nuclear@0 391 }
nuclear@0 392
nuclear@0 393 static void pdraw(const psys_emitter *em, const psys_particle *part, void *cls)
nuclear@0 394 {
nuclear@0 395 ParticleSystem *ps = (ParticleSystem*)cls;
nuclear@0 396
nuclear@0 397 static const Vector3 pv[] = {
nuclear@0 398 Vector3(-0.5, -0.5, 0),
nuclear@0 399 Vector3(0.5, -0.5, 0),
nuclear@0 400 Vector3(0.5, 0.5, 0),
nuclear@0 401 Vector3(-0.5, 0.5, 0)
nuclear@0 402 };
nuclear@0 403 static const Vector2 tex[] = {
nuclear@0 404 Vector2(0, 0), Vector2(0, 1), Vector2(1, 1), Vector2(1, 0)
nuclear@0 405 };
nuclear@0 406 Vector4 color = Vector4(part->color.x, part->color.y, part->color.z, part->alpha);
nuclear@0 407
nuclear@0 408 for(int i=0; i<4; i++) {
nuclear@0 409 pvptr->color = color;
nuclear@0 410 pvptr->pos = pv[i] * part->size + part->pos;
nuclear@0 411 pvptr->texcoord = tex[i];
nuclear@0 412 pvptr++;
nuclear@0 413 }
nuclear@0 414 // XXX we don't need billboarding for this game, so don't bother
nuclear@0 415
nuclear@0 416 // if we reached the maximum number of buffered particles, draw them
nuclear@0 417 if(++num_buffered >= MAX_DRAW_PART) {
nuclear@0 418 pdraw_flush(); // this will reset the counter
nuclear@0 419 }
nuclear@0 420 }
nuclear@0 421
nuclear@0 422 static void pdraw_end(const psys_emitter *em, void *cls)
nuclear@0 423 {
nuclear@0 424 // if we have leftover particles buffered, draw them before returning
nuclear@0 425 if(num_buffered) {
nuclear@0 426 pdraw_flush();
nuclear@0 427 }
nuclear@0 428
nuclear@0 429 // cleanup
nuclear@0 430 glDisableVertexAttribArray(MESH_ATTR_VERTEX);
nuclear@0 431 glDisableVertexAttribArray(MESH_ATTR_COLOR);
nuclear@0 432 glDisableVertexAttribArray(MESH_ATTR_TEXCOORD);
nuclear@0 433
nuclear@0 434 #ifdef USE_VBO
nuclear@0 435 glUnmapBuffer(GL_ARRAY_BUFFER);
nuclear@0 436 glBindBuffer(GL_ARRAY_BUFFER, 0);
nuclear@0 437 #endif
nuclear@0 438 #ifdef USE_IBO
nuclear@0 439 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
nuclear@0 440 #endif
nuclear@0 441
nuclear@0 442 glDepthMask(1);
nuclear@0 443 glDisable(GL_BLEND);
nuclear@0 444
nuclear@0 445 glEnable(GL_DEPTH_TEST);
nuclear@0 446 glEnable(GL_CULL_FACE);
nuclear@0 447 }
nuclear@0 448
nuclear@0 449 static unsigned int psys_load_texture(const char *fname, void *cls)
nuclear@0 450 {
nuclear@0 451 ParticleSystemAttributes *attr = (ParticleSystemAttributes*)cls;
nuclear@0 452
nuclear@0 453 Texture *tex = texset.get(fname);
nuclear@0 454 if(tex) {
nuclear@0 455 attr->set_texture(tex);
nuclear@0 456 return tex->get_id();
nuclear@0 457 }
nuclear@0 458 return 0;
nuclear@0 459 }