sgltk 0.6
Simple OpenGL Tool Kit
Loading...
Searching...
No Matches
particle.cpp
1#include "particle.h"
2
3using namespace sgltk;
4
5Particles::Particles() {
6 timer.start();
7}
8
9Particles::~Particles() {
10}
11
12void Particles::resize(unsigned int number) {
13 this->number = number;
14 position.resize(number);
15 velocity.resize(number);
16 lifetime.resize(number);
17 indices.resize(number);
18 for(unsigned int i = 0; i < number; i++) {
19 indices[i] = i;
20 }
21 pos_buf = mesh.attach_vertex_buffer(position, GL_STREAM_DRAW);
22 vel_buf = mesh.attach_vertex_buffer(velocity, GL_STREAM_DRAW);
23 life_buf = mesh.attach_vertex_buffer(lifetime, GL_STREAM_DRAW);
24 mesh.attach_index_buffer(indices);
25 mesh.set_vertex_attribute("position_in", pos_buf, 3, GL_FLOAT, 0, 0);
26 mesh.set_vertex_attribute("velocity_in", vel_buf, 3, GL_FLOAT, 0, 0);
27 mesh.set_vertex_attribute("lifetime_in", life_buf, 2, GL_FLOAT, 0, 0);
28}
29
31 mesh.setup_shader(shader);
32}
33
35 mesh.setup_camera(camera);
36}
37
38void Particles::setup_camera(glm::mat4 *view_matrix,
39 glm::mat4 *projection_matrix) {
40
41 mesh.setup_camera(view_matrix, projection_matrix);
42}
43
44void Particles::attach_texture(const std::string& name,
45 const Texture& texture) {
46
47 mesh.attach_texture(name, texture);
48}
49
50bool Particles::add_particle(glm::vec3 position,
51 glm::vec3 velocity,
52 float lifetime) {
53
54 float time = (float)timer.get_time_s();
55 for(unsigned int i = 0; i < number; i++) {
56 if(this->lifetime[i][0] + this->lifetime[i][1] < time) {
57 this->position[i] = position;
58 this->velocity[i] = velocity;
59 this->lifetime[i] = glm::vec2(time, lifetime);
60 break;
61 }
62 }
63 return true;
64}
65
67 glm::vec3 velocity,
68 float lifetime) {
69
70 float time = (float)timer.get_time_s();
71 for(unsigned int i = 0; i < number; i++) {
72 if(this->lifetime[i][0] + this->lifetime[i][1] < time) {
73 this->position[i] = position;
74 this->velocity[i] = velocity;
75 this->lifetime[i] = glm::vec2(time, lifetime);
76 mesh.replace_partial_data(pos_buf, i * sizeof(glm::vec3), &this->position[i], 1);
77 mesh.replace_partial_data(vel_buf, i * sizeof(glm::vec3), &this->velocity[i], 1);
78 mesh.replace_partial_data(life_buf, i * sizeof(glm::vec2), &this->lifetime[i], 1);
79 mesh.set_vertex_attribute("position_in", pos_buf, 3, GL_FLOAT, 0, 0);
80 mesh.set_vertex_attribute("velocity_in", vel_buf, 3, GL_FLOAT, 0, 0);
81 mesh.set_vertex_attribute("lifetime_in", life_buf, 2, GL_FLOAT, 0, 0);
82 break;
83 }
84 }
85 return true;
86}
87
89 mesh.replace_buffer_data(pos_buf, position);
90 mesh.replace_buffer_data(vel_buf, velocity);
91 mesh.replace_buffer_data(life_buf, lifetime);
92 mesh.set_vertex_attribute("position_in", pos_buf, 3, GL_FLOAT, 0, 0);
93 mesh.set_vertex_attribute("velocity_in", vel_buf, 3, GL_FLOAT, 0, 0);
94 mesh.set_vertex_attribute("lifetime_in", life_buf, 2, GL_FLOAT, 0, 0);
95}
96
98 if(!mesh.shader) {
99 App::error_string.push_back("No shader set.");
100 }
101 mesh.shader->bind();
102 mesh.shader->set_uniform("time", static_cast<float>(timer.get_time_s()));
103 mesh.draw(GL_POINTS);
104}
static std::vector< std::string > error_string
A list of all error strings.
Definition app.h:151
Manages cameras.
Definition camera.h:12
bool add_particle_immediately(glm::vec3 position, glm::vec3 velocity, float lifetime)
Adds a new particle to the system if the particle buffer has an empty space and immediately updates t...
Definition particle.cpp:66
void setup_camera(sgltk::Camera *camera)
Sets up the view and projection matrices that will be used to render the particles.
Definition particle.cpp:34
void draw()
Draws the particles.
Definition particle.cpp:97
void setup_shader(sgltk::Shader *shader)
Specifies the shader to use to render the particles.
Definition particle.cpp:30
void attach_texture(const std::string &name, const sgltk::Texture &texture)
Attaches a texture to the particles.
Definition particle.cpp:44
bool add_particle(glm::vec3 position, glm::vec3 velocity, float lifetime)
Adds a new particle to the system if the particle buffer has an empty space.
Definition particle.cpp:50
void update_all()
Updates the particle buffers.
Definition particle.cpp:88
void resize(unsigned int number)
Sets the number of the particles in the system.
Definition particle.cpp:12
Manager shaders.
Definition shader.h:12
Manages textures.
Definition texture.h:13