27#include "particles.hpp"
32#include "translation.hpp"
38using namespace Brenta;
58 glm::vec3 starting_position, glm::vec3 starting_velocity,
59 glm::vec3 starting_spread,
float starting_timeToLive,
int num_particles,
60 float spawn_rate,
float scale, std::string atlas_path,
int atlas_width,
61 int atlas_height,
int atlas_index,
Camera *camera)
75 this->camera = camera;
79 atlas_path, GL_REPEAT, GL_NEAREST, GL_NEAREST, GL_TRUE,
80 GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST,
false);
83 const GLchar *varyings[] = {
"outPosition",
"outVelocity",
"outTTL"};
84 Shader::New(varyings, 3,
"particle_update", GL_VERTEX_SHADER,
85 std::filesystem::absolute(
"engine/shaders/particle_update.vs"));
87 "particle_render", GL_VERTEX_SHADER,
88 std::filesystem::absolute(
"engine/shaders/particle_render.vs").
string(),
90 std::filesystem::absolute(
"engine/shaders/particle_render.gs").
string(),
92 std::filesystem::absolute(
"engine/shaders/particle_render.fs").
string());
95 glEnable(GL_PROGRAM_POINT_SIZE);
99 checkOpenGLError(
"vao bind");
105 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, this->
fbo[0].
id);
106 glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER,
107 this->num_particles * 2 *
sizeof(glm::vec3)
108 + this->num_particles *
sizeof(
float),
109 NULL, GL_DYNAMIC_COPY);
110 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, this->
fbo[1].
id);
111 glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER,
112 this->num_particles * 2 *
sizeof(glm::vec3)
113 + this->num_particles *
sizeof(
float),
114 NULL, GL_DYNAMIC_COPY);
115 checkOpenGLError(
"glBindBufferBase A");
118 glBindBuffer(GL_ARRAY_BUFFER, 0);
119 glBindVertexArray(0);
127 INFO(
"Deleted ParticleEmitter");
142 checkOpenGLError(
"settin update shader");
146 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0,
fbo[
current].
id);
147 checkOpenGLError(
"glBindBufferBase B");
149 glBindBuffer(GL_ARRAY_BUFFER,
fbo[!
current].
id);
150 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
151 2 *
sizeof(glm::vec3) +
sizeof(
float), (
void *) 0);
152 glEnableVertexAttribArray(0);
153 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
154 2 *
sizeof(glm::vec3) +
sizeof(
float),
155 (
void *)
sizeof(glm::vec3));
156 glEnableVertexAttribArray(1);
157 glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE,
158 2 *
sizeof(glm::vec3) +
sizeof(
float),
159 (
void *) (2 *
sizeof(glm::vec3)));
160 glEnableVertexAttribArray(2);
163 glEnable(GL_RASTERIZER_DISCARD);
164 glBeginTransformFeedback(GL_POINTS);
165 checkOpenGLError(
"glBeginTransformFeedback");
168 checkOpenGLError(
"glDrawTransformFeedback");
170 glEndTransformFeedback();
171 glDisable(GL_RASTERIZER_DISCARD);
173 glBindVertexArray(0);
174 glBindBuffer(GL_ARRAY_BUFFER, 0);
175 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
183 if (camera ==
nullptr)
185 ERROR(
"Camera not set or null for ParticleEmitter");
193 glBindBuffer(GL_ARRAY_BUFFER,
fbo[
current].
id);
194 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
195 2 *
sizeof(glm::vec3) +
sizeof(
float), (
void *) 0);
196 glEnableVertexAttribArray(0);
197 glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE,
198 2 *
sizeof(glm::vec3) +
sizeof(
float),
199 (
void *) (2 *
sizeof(glm::vec3)));
200 checkOpenGLError(
"glVertexAttribPointer");
201 glEnableVertexAttribArray(1);
219 GL_NEAREST, GL_TRUE, GL_NEAREST_MIPMAP_NEAREST,
223 checkOpenGLError(
"glDrawArrays");
225 glBindBuffer(GL_ARRAY_BUFFER, 0);
226 glBindVertexArray(0);
230void ParticleEmitter::checkOpenGLError(
const std::string &functionName)
233 while ((error = glGetError()) != GL_NO_ERROR)
235 std::cerr <<
"OpenGL Error after " << functionName <<
": " << error
241ParticleEmitter::Builder::set_starting_position(glm::vec3 starting_position)
243 this->starting_position = starting_position;
255ParticleEmitter::Builder::set_starting_spread(glm::vec3
starting_spread)
269ParticleEmitter::Builder::set_num_particles(
int num_particles)
276ParticleEmitter::Builder::set_spawn_rate(
float spawn_rate)
289ParticleEmitter::Builder::set_atlas_path(std::string atlas_path)
291 this->atlas_path = atlas_path;
296ParticleEmitter::Builder::set_atlas_width(
int atlas_width)
303ParticleEmitter::Builder::set_atlas_height(
int atlas_height)
310ParticleEmitter::Builder::set_atlas_index(
int atlas_index)
318 this->camera = camera;
glm::mat4 GetProjectionMatrix()
Get the projection matrix.
glm::mat4 GetViewMatrix()
Get the view matrix.
Builder pattern for ParticleEmitter.
int atlas_width
Atlas width.
float spawn_rate
Spawn rate of particles.
glm::vec3 starting_position
Starting position of a new particle.
ParticleEmitter()
Empty constructor.
float scale
Scale of particles.
int atlas_height
Atlas height.
int num_particles
Number of particles.
glm::vec3 starting_spread
Starting spread of a new particle.
~ParticleEmitter()
Destroy the ParticleEmitter object.
Types::Buffer fbo[2]
Feddback buffer objects.
void updateParticles(float deltaTime)
Update the particles.
void renderParticles()
Render the particles.
int atlas_index
Atlas index.
Types::VAO vao
Vertex array object.
int current
Current fbo index.
float starting_timeToLive
Time to live of a new particle.
glm::vec3 starting_velocity
Starting velocity of a new particle.
static int GetHeight()
Get the height of the window.
static int GetWidth()
Get the width of the window.
static void SetVec3(Types::ShaderName shader_name, const GLchar *name, float x, float y, float z)
Set a 3D vector in the shader.
static void SetInt(Types::ShaderName shader_name, const std::string &name, int value)
Set an integer in the shader.
static void New(std::string shader_name, GLenum type, std::string path, Args... args)
Create a new shader.
static void SetFloat(Types::ShaderName shader_name, const std::string &name, float value)
Set a float in the shader.
static void Use(Types::ShaderName shader_name)
Use the shader.
static void BindTexture(GLenum target, unsigned int texture, GLint wrapping=GL_REPEAT, GLint filtering_min=GL_NEAREST, GLint filtering_mag=GL_NEAREST, GLboolean hasMipmap=GL_TRUE, GLint mipmap_min=GL_LINEAR_MIPMAP_LINEAR, GLint mipmap_mag=GL_LINEAR)
Bind a texture.
static void ActiveTexture(GLenum texture)
Activate a texture unit.
static unsigned int LoadTexture(std::string path, GLint wrapping=GL_REPEAT, GLint filtering_min=GL_NEAREST, GLint filtering_mag=GL_NEAREST, GLboolean hasMipmap=GL_TRUE, GLint mipmap_min=GL_LINEAR_MIPMAP_LINEAR, GLint mipmap_mag=GL_LINEAR, bool flip=true)
Load a texture from a file.
Buffer wrapper around OpenGL buffer objects.
void Delete()
Delete the buffer object.
void setProjection(glm::mat4 projection)
Set the projection matrix.
void setView(glm::mat4 view)
Set the view matrix.
void setModel(glm::mat4 model)
Set the model matrix.
void setShader(Types::ShaderName shader_name)
Set the shader.
void Init()
Init Constructor.
void Unbind()
Unbind the VAO.