2016-09-28 19:26:11 +00:00
|
|
|
#include "particle_batch.hpp"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
2016-09-28 22:50:14 +00:00
|
|
|
// TODO: use VAO's as soon as this is working
|
|
|
|
|
|
|
|
int getDivisorForIndex(int index)
|
|
|
|
{
|
|
|
|
return (index == 0) ? 1 : 4;
|
|
|
|
}
|
|
|
|
|
2016-09-28 19:26:11 +00:00
|
|
|
namespace endofthejedi {
|
2016-09-28 22:50:14 +00:00
|
|
|
ParticleBatch::ParticleBatch(size_t numParticles, float particleSize, float maxAge)
|
2016-09-28 19:26:11 +00:00
|
|
|
: m_numParticles(numParticles)
|
|
|
|
, m_particleRadius(particleSize)
|
2016-09-28 22:50:14 +00:00
|
|
|
, m_maxAge(maxAge)
|
2016-09-29 06:28:56 +00:00
|
|
|
, m_age(0.0)
|
2016-09-28 22:50:14 +00:00
|
|
|
|
|
|
|
// 2d quad drawn as a triangle fan
|
|
|
|
, m_data_quad({
|
|
|
|
1.0f, -1.0f,
|
|
|
|
1.0f, 1.0f,
|
|
|
|
-1.0f, 1.0f,
|
|
|
|
-1.0f, -1.0f})
|
2016-09-28 19:26:11 +00:00
|
|
|
{
|
|
|
|
std::cout<<"[ParticleBatch] create for " << numParticles << " num particles" << std::endl;
|
|
|
|
|
2016-09-28 22:50:14 +00:00
|
|
|
m_data_position.resize(m_numParticles);
|
|
|
|
m_data_velocity.resize(m_numParticles);
|
|
|
|
m_data_kind.resize(m_numParticles);
|
|
|
|
m_data_max_age.resize(m_numParticles);
|
2016-09-28 19:26:11 +00:00
|
|
|
|
|
|
|
std::string vss_particles =
|
2016-09-29 02:24:14 +00:00
|
|
|
#include "particle.vert"
|
2016-09-28 19:26:11 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
std::string fss_particles =
|
2016-09-29 02:24:14 +00:00
|
|
|
#include "particle.frag"
|
2016-09-28 19:26:11 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
m_shader.init();
|
|
|
|
m_shader.load(vss_particles.c_str(), GL_VERTEX_SHADER);
|
|
|
|
m_shader.load(fss_particles.c_str(), GL_FRAGMENT_SHADER);
|
|
|
|
}
|
|
|
|
|
|
|
|
ParticleBatch::~ParticleBatch()
|
|
|
|
{
|
|
|
|
// TODO: find out if stuff must be deallocated
|
|
|
|
}
|
|
|
|
|
2016-09-28 22:50:14 +00:00
|
|
|
void ParticleBatch::setParticle(size_t index, const glm::vec2 &p, const glm::vec2 &v)
|
2016-09-28 19:26:11 +00:00
|
|
|
{
|
|
|
|
if (index >= m_numParticles) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//std::cout<<"[ParticleBatch] setParticle " << index << std::endl;
|
|
|
|
|
2016-09-28 22:50:14 +00:00
|
|
|
m_data_position[index] = p;
|
|
|
|
m_data_velocity[index] = v;
|
|
|
|
m_data_kind[index] = 0.0;
|
|
|
|
m_data_max_age[index] = 0.0;
|
2016-09-28 19:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleBatch::bind()
|
|
|
|
{
|
|
|
|
//std::cout<<"[ParticleBatch] bind" << std::endl;
|
|
|
|
|
2016-09-28 22:50:14 +00:00
|
|
|
for (size_t i=0; i<5; i++) {
|
2016-09-28 19:26:11 +00:00
|
|
|
//std::cout<<"vbo #" << i << ": " << m_data_vbos[i] << std::endl;
|
|
|
|
glEnableVertexAttribArray(i);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, m_data_vbos[i]);
|
|
|
|
|
2016-09-28 22:50:14 +00:00
|
|
|
if (i != 0) {
|
|
|
|
glVertexAttribDivisor(i, getDivisorForIndex(i-1));
|
|
|
|
}
|
|
|
|
|
2016-09-28 19:26:11 +00:00
|
|
|
// TODO: i or index? at first argument?
|
|
|
|
glVertexAttribPointer(
|
|
|
|
i,
|
|
|
|
dataSizeForIndex(i),
|
|
|
|
GL_FLOAT, // Data is floating point type
|
|
|
|
GL_FALSE, // No fixed point scaling
|
|
|
|
0, // stride: no
|
|
|
|
NULL); // No offset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleBatch::upload()
|
|
|
|
{
|
2016-09-28 22:50:14 +00:00
|
|
|
std::cout<<"[ParticleBatch] upload to vbo's " << std::endl;
|
2016-09-28 19:26:11 +00:00
|
|
|
|
|
|
|
glGenBuffers(4, m_data_vbos); // Generate buffer
|
|
|
|
|
|
|
|
for (size_t i=0; i<4; i++) {
|
2016-09-28 22:50:14 +00:00
|
|
|
size_t bufferDataSize = dataSizeForIndex(i) * m_numParticles * sizeof(float);
|
|
|
|
|
2016-09-28 19:26:11 +00:00
|
|
|
glEnableVertexAttribArray(i);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, m_data_vbos[i]);
|
|
|
|
|
|
|
|
// fill buffer with the loaded mesh position data
|
|
|
|
glBufferData(
|
2016-09-28 22:50:14 +00:00
|
|
|
GL_ARRAY_BUFFER, // Buffer target
|
|
|
|
bufferDataSize, // Buffer data size
|
|
|
|
dataSourceForIndex(i), // Buffer data pointer
|
|
|
|
GL_STATIC_DRAW); // Usage - Data never changes;
|
2016-09-28 19:26:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleBatch::render()
|
|
|
|
{
|
|
|
|
//std::cout<<"[ParticleBatch] render " << std::endl;
|
|
|
|
|
|
|
|
m_shader.bind();
|
|
|
|
|
2016-09-29 06:28:56 +00:00
|
|
|
// TODO: does that work?
|
2016-09-28 22:50:14 +00:00
|
|
|
glBindAttribLocation(m_shader.program(), 0, "in_vertex");
|
|
|
|
glBindAttribLocation(m_shader.program(), 1, "in_position");
|
|
|
|
glBindAttribLocation(m_shader.program(), 2, "in_velocity");
|
|
|
|
|
2016-09-29 06:28:56 +00:00
|
|
|
glUniform1f(m_shader.location("time"), m_age);
|
2016-09-28 22:50:14 +00:00
|
|
|
glUniform1f(m_shader.location("maxAge"), m_maxAge);
|
|
|
|
glUniform1f(m_shader.location("size"), m_particleRadius);
|
2016-09-28 19:26:11 +00:00
|
|
|
|
|
|
|
bind();
|
|
|
|
|
2016-09-28 22:50:14 +00:00
|
|
|
//glDrawArrays(GL_TRIANGLE_FAN, 0, 4*m_numParticles);
|
|
|
|
|
|
|
|
//glDrawArraysInstanced( GLenum mode, GLint first, GLsizei count, GLsizei primcount);
|
|
|
|
glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, 4*m_numParticles);
|
2016-09-28 19:26:11 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
for (size_t index=0; index<m_numParticles; index++) {
|
|
|
|
for (int i=0; i<4; i++) {
|
|
|
|
glm::vec2 p = m_data_position[index] + triangles[i];
|
|
|
|
//glColor3f(1.0, 0.0, 0.0);
|
|
|
|
glVertex2f(p.x, p.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glEnd();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ParticleBatch::dataSizeForIndex(int i)
|
|
|
|
{
|
|
|
|
switch(i) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
2016-09-28 22:50:14 +00:00
|
|
|
case 2:
|
2016-09-28 19:26:11 +00:00
|
|
|
return 2;
|
|
|
|
|
|
|
|
case 3:
|
2016-09-28 22:50:14 +00:00
|
|
|
case 4:
|
2016-09-28 19:26:11 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
default:
|
|
|
|
std::cerr << "bad" << std::endl;
|
2016-09-28 22:50:14 +00:00
|
|
|
exit(-1);
|
2016-09-28 19:26:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ParticleBatch::dataSourceForIndex(int i)
|
|
|
|
{
|
|
|
|
switch(i) {
|
2016-09-28 22:50:14 +00:00
|
|
|
case 0: return (void *) m_data_quad.data();
|
|
|
|
case 1: return (void *) m_data_position.data();
|
|
|
|
case 2: return (void *) m_data_velocity.data();
|
|
|
|
case 3: return (void *) m_data_kind.data();
|
|
|
|
case 4: return (void *) m_data_max_age.data();
|
2016-09-28 19:26:11 +00:00
|
|
|
default:
|
|
|
|
std::cerr << "bad" << std::endl;
|
2016-09-28 22:50:14 +00:00
|
|
|
exit(-1);
|
2016-09-28 19:26:11 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-29 06:28:56 +00:00
|
|
|
|
|
|
|
void ParticleBatch::tick(float dt)
|
|
|
|
{
|
|
|
|
m_age += dt;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParticleBatch::done() const
|
|
|
|
{
|
|
|
|
return m_age >= m_maxAge;
|
|
|
|
}
|
2016-09-28 19:26:11 +00:00
|
|
|
}
|