#include "particle_batch.hpp" #include // TODO: use VAO's as soon as this is working int getDivisorForIndex(size_t index) { // 0 or 1? return (index == 0) ? 0 : 3; } namespace endofthejedi { ParticleBatch::ParticleBatch(size_t id, size_t numParticles, float particleSize, float halfAge) : m_id(id) , m_numParticles(numParticles) , m_particleRadius(particleSize) , m_halfAge(halfAge) , m_age(0.0) , m_maxVelocity(1.0) , m_center(glm::vec3(0.0f, 0.0f, 0.0f)) // 2d quad drawn as a triangle fan. // // TODO // it is transformed before uploading so it looks at the camera , m_data_geometry({ 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f, -0.5f, -0.5f}) { m_num_vertex_buffers = 4; //std::cout<<"[ParticleBatch] create for " << numParticles << " num particles" << std::endl; m_attr_locations.resize(m_num_vertex_buffers); m_data_vbos.resize(m_num_vertex_buffers); m_data_position.resize(m_numParticles); m_data_velocity.resize(m_numParticles); m_data_max_distance.resize(m_numParticles); for (size_t i=0; iprogram(), name); m_attr_locations[i] = loc; //std::cout<<"attr location for " << name << " (#" << i << ") is " << loc << std::endl; } } void ParticleBatch::setCenter(const glm::vec3 ¢er) { m_center = center; } void ParticleBatch::setMaxVelocity(float maxVelocity) { m_maxVelocity = maxVelocity; } void ParticleBatch::setParticle(size_t index, const glm::vec3 &p, const glm::vec3 &v, float maxDist) { if (index >= m_numParticles) { return; } //std::cout<<"[ParticleBatch] setParticle " << index << std::endl; m_data_position[index] = p; m_data_velocity[index] = v; m_data_max_distance[index] = maxDist; } void ParticleBatch::bind() { //std::cout<<"[ParticleBatch] bind" << std::endl; for (size_t i=0; ilocation("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); 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= 5.0*m_halfAge; } }