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_time_to_live,
int num_particles,
60 float spawn_rate,
float scale, std::string atlas_path,
int atlas_width,
61 int atlas_height,
int atlas_index,
camera *cam)
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"};
85 varyings, 3,
"particle_update", GL_VERTEX_SHADER,
86 std::filesystem::absolute(
"engine/shaders/particle_update.vs"));
88 "particle_render", GL_VERTEX_SHADER,
89 std::filesystem::absolute(
"engine/shaders/particle_render.vs").
string(),
91 std::filesystem::absolute(
"engine/shaders/particle_render.gs").
string(),
93 std::filesystem::absolute(
"engine/shaders/particle_render.fs")
97 glEnable(GL_PROGRAM_POINT_SIZE);
101 check_opengl_error(
"vao bind");
107 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, this->
fbo[0].
id);
108 glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER,
109 this->num_particles * 2 *
sizeof(glm::vec3)
110 + this->num_particles *
sizeof(
float),
111 NULL, GL_DYNAMIC_COPY);
112 glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, this->
fbo[1].
id);
113 glBufferData(GL_TRANSFORM_FEEDBACK_BUFFER,
114 this->num_particles * 2 *
sizeof(glm::vec3)
115 + this->num_particles *
sizeof(
float),
116 NULL, GL_DYNAMIC_COPY);
117 check_opengl_error(
"glBindBufferBase A");
120 glBindBuffer(GL_ARRAY_BUFFER, 0);
121 glBindVertexArray(0);
129 INFO(
"Deleted ParticleEmitter");
144 check_opengl_error(
"settin update shader");
148 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0,
fbo[
current].
id);
149 check_opengl_error(
"glBindBufferBase B");
151 glBindBuffer(GL_ARRAY_BUFFER,
fbo[!
current].
id);
152 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
153 2 *
sizeof(glm::vec3) +
sizeof(
float), (
void *) 0);
154 glEnableVertexAttribArray(0);
155 glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,
156 2 *
sizeof(glm::vec3) +
sizeof(
float),
157 (
void *)
sizeof(glm::vec3));
158 glEnableVertexAttribArray(1);
159 glVertexAttribPointer(2, 1, GL_FLOAT, GL_FALSE,
160 2 *
sizeof(glm::vec3) +
sizeof(
float),
161 (
void *) (2 *
sizeof(glm::vec3)));
162 glEnableVertexAttribArray(2);
165 glEnable(GL_RASTERIZER_DISCARD);
166 glBeginTransformFeedback(GL_POINTS);
167 check_opengl_error(
"glBeginTransformFeedback");
170 check_opengl_error(
"glDrawTransformFeedback");
172 glEndTransformFeedback();
173 glDisable(GL_RASTERIZER_DISCARD);
175 glBindVertexArray(0);
176 glBindBuffer(GL_ARRAY_BUFFER, 0);
177 glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
185 if (this->cam ==
nullptr)
187 ERROR(
"Camera not set or null for ParticleEmitter");
195 glBindBuffer(GL_ARRAY_BUFFER,
fbo[
current].
id);
196 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,
197 2 *
sizeof(glm::vec3) +
sizeof(
float), (
void *) 0);
198 glEnableVertexAttribArray(0);
199 glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE,
200 2 *
sizeof(glm::vec3) +
sizeof(
float),
201 (
void *) (2 *
sizeof(glm::vec3)));
202 check_opengl_error(
"glVertexAttribPointer");
203 glEnableVertexAttribArray(1);
222 GL_NEAREST, GL_TRUE, GL_NEAREST_MIPMAP_NEAREST,
226 check_opengl_error(
"glDrawArrays");
228 glBindBuffer(GL_ARRAY_BUFFER, 0);
229 glBindVertexArray(0);
233void particle_emitter::check_opengl_error(
const std::string &function_name)
236 while ((error = glGetError()) != GL_NO_ERROR)
238 std::cerr <<
"OpenGL Error after " << function_name <<
": " << error
244particle_emitter::builder::set_starting_position(glm::vec3 starting_position)
246 this->starting_position = starting_position;
258particle_emitter::builder::set_starting_spread(glm::vec3
starting_spread)
272particle_emitter::builder::set_num_particles(
int num_particles)
279particle_emitter::builder::set_spawn_rate(
float spawn_rate)
292particle_emitter::builder::set_atlas_path(std::string atlas_path)
294 this->atlas_path = atlas_path;
299particle_emitter::builder::set_atlas_width(
int atlas_width)
306particle_emitter::builder::set_atlas_height(
int atlas_height)
313particle_emitter::builder::set_atlas_index(
int atlas_index)
glm::mat4 get_projection_matrix()
Get the projection matrix.
glm::mat4 get_view_matrix()
Get the view matrix.
Builder pattern for ParticleEmitter.
int current
Current fbo index.
void render_particles()
Render the particles.
glm::vec3 starting_velocity
Starting velocity of a new particle.
types::vao vao
Vertex array object.
~particle_emitter()
Destroy the ParticleEmitter object.
float starting_time_to_live
Time to live of a new particle.
particle_emitter()
Empty constructor.
types::buffer fbo[2]
Feddback buffer objects.
float scale
Scale of particles.
void update_particles(float deltaTime)
Update the particles.
float spawn_rate
Spawn rate of particles.
int atlas_index
Atlas index.
glm::vec3 starting_spread
Starting spread of a new particle.
glm::vec3 starting_position
Starting position of a new particle.
int num_particles
Number of particles.
int atlas_width
Atlas width.
int atlas_height
Atlas height.
static int get_width()
Get the width of the window.
static int get_height()
Get the height of the window.
static void create(std::string shader_name, GLenum type, std::string path, Args... args)
Create a new shader.
static void use(types::shader_name_t shader_name)
Use the shader.
static void set_int(types::shader_name_t shader_name, const std::string &name, int value)
Set an integer in the shader.
static void set_vec3(types::shader_name_t shader_name, const GLchar *name, float x, float y, float z)
Set a 3D vector in the shader.
static void set_float(types::shader_name_t shader_name, const std::string &name, float value)
Set a float in the shader.
static void active_texture(GLenum texture)
Activate a texture unit.
static unsigned int load_texture(std::string path, GLint wrapping=GL_REPEAT, GLint filtering_min=GL_NEAREST, GLint filtering_mag=GL_NEAREST, GLboolean has_mipmap=GL_TRUE, GLint mipmap_min=GL_LINEAR_MIPMAP_LINEAR, GLint mipmap_mag=GL_LINEAR, bool flip=true)
Load a texture from a file.
static void bind_texture(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.
Buffer wrapper around OpenGL buffer objects.
void destroy()
Delete the buffer object.
void set_projection(glm::mat4 projection)
Set the projection matrix.
void set_shader(types::shader_name_t shader_name)
Set the shader.
void set_model(glm::mat4 model)
Set the model matrix.
void set_view(glm::mat4 view)
Set the view matrix.
void unbind()
Unbind the VAO.
void init()
Init Constructor.