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
|
|
|
|
|
2016-10-02 15:22:47 +00:00
|
|
|
int getDivisorForIndex(size_t index)
|
2016-09-28 22:50:14 +00:00
|
|
|
{
|
2016-09-30 18:19:10 +00:00
|
|
|
// 0 or 1?
|
|
|
|
return (index == 0) ? 0 : 3;
|
2016-09-28 22:50:14 +00:00
|
|
|
}
|
|
|
|
|
2016-09-28 19:26:11 +00:00
|
|
|
namespace endofthejedi {
|
2016-09-30 19:48:15 +00:00
|
|
|
ParticleBatch::ParticleBatch(size_t id, size_t numParticles, float particleSize, float halfAge)
|
2016-09-30 18:19:10 +00:00
|
|
|
: m_id(id)
|
|
|
|
, m_numParticles(numParticles)
|
2016-09-28 19:26:11 +00:00
|
|
|
, m_particleRadius(particleSize)
|
2016-09-30 19:48:15 +00:00
|
|
|
, m_halfAge(halfAge)
|
2016-09-29 06:28:56 +00:00
|
|
|
, m_age(0.0)
|
2016-09-30 19:24:06 +00:00
|
|
|
, m_maxVelocity(1.0)
|
|
|
|
, m_center(glm::vec3(0.0f, 0.0f, 0.0f))
|
2016-09-28 22:50:14 +00:00
|
|
|
|
2016-10-02 15:22:47 +00:00
|
|
|
// 2d quad drawn as a triangle fan.
|
|
|
|
//
|
|
|
|
// TODO
|
|
|
|
// it is transformed before uploading so it looks at the camera
|
|
|
|
, m_data_geometry({
|
2016-10-02 18:10:43 +00:00
|
|
|
0.5f, -0.5f,
|
|
|
|
0.5f, 0.5f,
|
|
|
|
-0.5f, 0.5f,
|
|
|
|
-0.5f, -0.5f})
|
2016-09-28 19:26:11 +00:00
|
|
|
{
|
2016-10-02 15:22:47 +00:00
|
|
|
m_num_vertex_buffers = 4;
|
|
|
|
|
2016-09-29 07:05:35 +00:00
|
|
|
//std::cout<<"[ParticleBatch] create for " << numParticles << " num particles" << std::endl;
|
2016-10-02 15:22:47 +00:00
|
|
|
m_attr_locations.resize(m_num_vertex_buffers);
|
|
|
|
m_data_vbos.resize(m_num_vertex_buffers);
|
2016-09-28 19:26:11 +00:00
|
|
|
|
2016-09-28 22:50:14 +00:00
|
|
|
m_data_position.resize(m_numParticles);
|
|
|
|
m_data_velocity.resize(m_numParticles);
|
2016-10-02 15:22:47 +00:00
|
|
|
m_data_max_distance.resize(m_numParticles);
|
|
|
|
|
|
|
|
for (size_t i=0; i<m_num_vertex_buffers; i++) {
|
|
|
|
m_attr_locations[i] = -1;
|
|
|
|
m_data_vbos[i] = 0;
|
|
|
|
}
|
2016-10-02 09:00:03 +00:00
|
|
|
}
|
2016-09-28 19:26:11 +00:00
|
|
|
|
2016-10-02 09:00:03 +00:00
|
|
|
ParticleBatch::~ParticleBatch()
|
|
|
|
{
|
|
|
|
// TODO: find out if stuff must be deallocated
|
2016-10-02 15:22:47 +00:00
|
|
|
glDeleteBuffers(m_num_vertex_buffers, m_data_vbos.data());
|
2016-10-02 09:00:03 +00:00
|
|
|
}
|
2016-09-30 18:19:10 +00:00
|
|
|
|
2016-10-02 09:00:03 +00:00
|
|
|
void ParticleBatch::setup(Shader *shader)
|
|
|
|
{
|
2016-10-02 15:22:47 +00:00
|
|
|
static const char *names[] = {
|
|
|
|
"in_geometry",
|
2016-09-30 18:19:10 +00:00
|
|
|
"in_position",
|
|
|
|
"in_velocity",
|
2016-10-02 15:22:47 +00:00
|
|
|
"in_maxDist"
|
2016-09-30 18:19:10 +00:00
|
|
|
};
|
|
|
|
|
2016-10-02 15:22:47 +00:00
|
|
|
for (size_t i=0; i<m_num_vertex_buffers; i++) {
|
2016-09-30 18:19:10 +00:00
|
|
|
const char *name = names[i];
|
2016-10-02 09:00:03 +00:00
|
|
|
GLint loc = glGetAttribLocation(shader->program(), name);
|
2016-09-30 18:19:10 +00:00
|
|
|
m_attr_locations[i] = loc;
|
2016-10-02 15:22:47 +00:00
|
|
|
//std::cout<<"attr location for " << name << " (#" << i << ") is " << loc << std::endl;
|
2016-09-30 18:19:10 +00:00
|
|
|
}
|
2016-09-28 19:26:11 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 19:24:06 +00:00
|
|
|
void ParticleBatch::setCenter(const glm::vec3 ¢er)
|
|
|
|
{
|
|
|
|
m_center = center;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleBatch::setMaxVelocity(float maxVelocity)
|
|
|
|
{
|
|
|
|
m_maxVelocity = maxVelocity;
|
|
|
|
}
|
|
|
|
|
2016-10-02 15:22:47 +00:00
|
|
|
void ParticleBatch::setParticle(size_t index, const glm::vec3 &p, const glm::vec3 &v, float maxDist)
|
2016-09-28 19:26:11 +00:00
|
|
|
{
|
|
|
|
if (index >= m_numParticles) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//std::cout<<"[ParticleBatch] setParticle " << index << std::endl;
|
|
|
|
|
2016-10-02 15:22:47 +00:00
|
|
|
m_data_position[index] = p;
|
|
|
|
m_data_velocity[index] = v;
|
|
|
|
m_data_max_distance[index] = maxDist;
|
2016-09-28 19:26:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleBatch::bind()
|
|
|
|
{
|
|
|
|
//std::cout<<"[ParticleBatch] bind" << std::endl;
|
|
|
|
|
2016-10-02 15:22:47 +00:00
|
|
|
for (size_t i=0; i<m_num_vertex_buffers; i++) {
|
2016-09-28 19:26:11 +00:00
|
|
|
//std::cout<<"vbo #" << i << ": " << m_data_vbos[i] << std::endl;
|
2016-09-30 18:19:10 +00:00
|
|
|
glEnableVertexAttribArray(m_attr_locations[i]);
|
2016-09-28 19:26:11 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, m_data_vbos[i]);
|
|
|
|
|
2016-09-30 18:19:10 +00:00
|
|
|
glVertexAttribDivisor(m_attr_locations[i], getDivisorForIndex(i));
|
2016-09-28 22:50:14 +00:00
|
|
|
|
2016-09-28 19:26:11 +00:00
|
|
|
// TODO: i or index? at first argument?
|
|
|
|
glVertexAttribPointer(
|
2016-09-30 18:19:10 +00:00
|
|
|
m_attr_locations[i],
|
2016-09-28 19:26:11 +00:00
|
|
|
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-29 07:05:35 +00:00
|
|
|
//std::cout<<"[ParticleBatch] upload to vbo's " << std::endl;
|
2016-09-28 19:26:11 +00:00
|
|
|
|
2016-10-02 15:22:47 +00:00
|
|
|
glGenBuffers(m_num_vertex_buffers, m_data_vbos.data()); // Generate buffer
|
2016-09-28 19:26:11 +00:00
|
|
|
|
2016-10-02 15:22:47 +00:00
|
|
|
for (size_t i=0; i<m_num_vertex_buffers; 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-02 09:00:03 +00:00
|
|
|
void ParticleBatch::render(Shader *shader)
|
2016-09-28 19:26:11 +00:00
|
|
|
{
|
|
|
|
//std::cout<<"[ParticleBatch] render " << std::endl;
|
2016-10-02 09:00:03 +00:00
|
|
|
glUniform1f(shader->location("age"), m_age);
|
|
|
|
glUniform1f(shader->location("maxVelocity"), m_maxVelocity);
|
|
|
|
glUniform1f(shader->location("halfAge"), m_halfAge);
|
|
|
|
glUniform1f(shader->location("size"), m_particleRadius);
|
|
|
|
glUniform3f(shader->location("explCenter"), m_center.x, m_center.y, m_center.z);
|
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);
|
2016-09-30 18:19:10 +00:00
|
|
|
// XXX the 3 is a magical number.
|
|
|
|
// I dont know why this works
|
|
|
|
// without it, it will render 1/3 of the particles
|
|
|
|
glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, 3*m_numParticles);
|
2016-09-28 19:26:11 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
for (size_t index=0; index<m_numParticles; index++) {
|
2016-10-02 15:22:47 +00:00
|
|
|
for (size_t i=0; i<4; i++) {
|
2016-09-28 19:26:11 +00:00
|
|
|
glm::vec2 p = m_data_position[index] + triangles[i];
|
|
|
|
//glColor3f(1.0, 0.0, 0.0);
|
|
|
|
glVertex2f(p.x, p.y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glEnd();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-10-02 15:22:47 +00:00
|
|
|
size_t ParticleBatch::dataSizeForIndex(size_t index)
|
2016-09-28 19:26:11 +00:00
|
|
|
{
|
2016-10-02 15:22:47 +00:00
|
|
|
switch(index) {
|
2016-09-28 19:26:11 +00:00
|
|
|
case 0:
|
2016-09-30 19:24:06 +00:00
|
|
|
return 2;
|
|
|
|
|
2016-09-28 19:26:11 +00:00
|
|
|
case 1:
|
2016-09-28 22:50:14 +00:00
|
|
|
case 2:
|
2016-09-30 19:24:06 +00:00
|
|
|
return 3;
|
2016-09-28 19:26:11 +00:00
|
|
|
|
|
|
|
case 3:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
default:
|
2016-10-02 15:22:47 +00:00
|
|
|
std::cerr << "dataSizeForIndex() bad" << std::endl;
|
2016-09-28 22:50:14 +00:00
|
|
|
exit(-1);
|
2016-09-28 19:26:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-02 15:22:47 +00:00
|
|
|
void *ParticleBatch::dataSourceForIndex(size_t index)
|
2016-09-28 19:26:11 +00:00
|
|
|
{
|
2016-10-02 15:22:47 +00:00
|
|
|
switch(index) {
|
|
|
|
case 0: return (void *) m_data_geometry.data();
|
2016-09-28 22:50:14 +00:00
|
|
|
case 1: return (void *) m_data_position.data();
|
|
|
|
case 2: return (void *) m_data_velocity.data();
|
2016-10-02 15:22:47 +00:00
|
|
|
case 3: return (void *) m_data_max_distance.data();
|
2016-09-28 19:26:11 +00:00
|
|
|
default:
|
2016-10-02 15:22:47 +00:00
|
|
|
std::cerr << "dataSourceForIndex() 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
|
|
|
|
{
|
2016-09-30 19:48:15 +00:00
|
|
|
return m_age >= 5.0*m_halfAge;
|
2016-09-29 06:28:56 +00:00
|
|
|
}
|
2016-09-28 19:26:11 +00:00
|
|
|
}
|