KlassischeKeplerKriege/game/renderer_polygon_3d/particle_batch.cpp
2016-09-30 20:19:10 +02:00

195 lines
5.7 KiB
C++

#include "particle_batch.hpp"
#include <iostream>
// TODO: use VAO's as soon as this is working
int getDivisorForIndex(int index)
{
// 0 or 1?
return (index == 0) ? 0 : 3;
}
namespace endofthejedi {
ParticleBatch::ParticleBatch(size_t id, size_t numParticles, float particleSize, float maxAge)
: m_id(id)
, m_numParticles(numParticles)
, m_particleRadius(particleSize)
, m_maxAge(maxAge)
, m_age(0.0)
// 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})
{
//std::cout<<"[ParticleBatch] create for " << numParticles << " num particles" << std::endl;
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);
std::string vss_particles = "../data/shader/particle.vert";
std::string fss_particles = "../data/shader/particle.frag";
m_shader.init();
m_shader.loadFile(vss_particles, GL_VERTEX_SHADER);
m_shader.loadFile(fss_particles, GL_FRAGMENT_SHADER);
const char *names[] = {
"in_vertex",
"in_position",
"in_velocity",
"XXXunusedXXX",
"XXXunusedXXX"
};
for (int i=0; i<5; i++) {
const char *name = names[i];
GLint loc = glGetAttribLocation(m_shader.program(), name);
m_attr_locations[i] = loc;
//std::cout<<"attr location " << i << " " << loc << " " << name << std::endl;
}
}
ParticleBatch::~ParticleBatch()
{
// TODO: find out if stuff must be deallocated
glDeleteBuffers(5, m_data_vbos);
}
void ParticleBatch::setParticle(size_t index, const glm::vec2 &p, const glm::vec2 &v)
{
if (index >= m_numParticles) {
return;
}
//std::cout<<"[ParticleBatch] setParticle " << index << std::endl;
m_data_position[index] = p;
m_data_velocity[index] = v;
m_data_kind[index] = 0.0;
m_data_max_age[index] = 0.0;
}
void ParticleBatch::bind()
{
//std::cout<<"[ParticleBatch] bind" << std::endl;
for (size_t i=0; i<5; i++) {
//std::cout<<"vbo #" << i << ": " << m_data_vbos[i] << std::endl;
glEnableVertexAttribArray(m_attr_locations[i]);
glBindBuffer(GL_ARRAY_BUFFER, m_data_vbos[i]);
glVertexAttribDivisor(m_attr_locations[i], getDivisorForIndex(i));
// TODO: i or index? at first argument?
glVertexAttribPointer(
m_attr_locations[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()
{
//std::cout<<"[ParticleBatch] upload to vbo's " << std::endl;
glGenBuffers(5, m_data_vbos); // Generate buffer
for (size_t i=0; i<5; i++) {
size_t bufferDataSize = dataSizeForIndex(i) * m_numParticles * sizeof(float);
glEnableVertexAttribArray(i);
glBindBuffer(GL_ARRAY_BUFFER, m_data_vbos[i]);
// fill buffer with the loaded mesh position data
glBufferData(
GL_ARRAY_BUFFER, // Buffer target
bufferDataSize, // Buffer data size
dataSourceForIndex(i), // Buffer data pointer
GL_STATIC_DRAW); // Usage - Data never changes;
}
}
void ParticleBatch::render()
{
//std::cout<<"[ParticleBatch] render " << std::endl;
m_shader.bind();
glUniform1f(m_shader.location("time"), m_age);
glUniform1f(m_shader.location("maxAge"), m_maxAge);
glUniform1f(m_shader.location("size"), m_particleRadius);
bind();
//glDrawArrays(GL_TRIANGLE_FAN, 0, 4*m_numParticles);
//glDrawArraysInstanced( GLenum mode, GLint first, GLsizei count, GLsizei primcount);
// 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);
#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:
case 2:
return 2;
case 3:
case 4:
return 1;
default:
std::cerr << "bad" << std::endl;
exit(-1);
}
}
void *ParticleBatch::dataSourceForIndex(int i)
{
switch(i) {
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();
default:
std::cerr << "bad" << std::endl;
exit(-1);
}
}
void ParticleBatch::tick(float dt)
{
m_age += dt;
}
bool ParticleBatch::done() const
{
return m_age >= m_maxAge;
}
}