Skip to content
Snippets Groups Projects
Commit 1ade4848 authored by BATON Theau's avatar BATON Theau
Browse files

Add physic andpath scene

parent 6dc05282
Branches ParticleScene
No related tags found
No related merge requests found
Showing
with 486 additions and 98 deletions
......@@ -23,6 +23,6 @@ Pos=10,4
Size=674,754
[Window][Particle Generator Settings]
Pos=10,11
Size=362,377
Pos=3,8
Size=622,572
#pragma once
#include "PhysicGeneratorInterface.hpp"
#include <imgui.h>
#include "../../Particle/generator/PhysicsParticleGenerator.hpp"
namespace pg {
PhysicGeneratorInterface::PhysicGeneratorInterface(PhysicsParticleGenerator * generator)
: _generator(generator) {}
void PhysicGeneratorInterface::render(double) {
if(ImGui::CollapsingHeader("Generator Information")) {
ImGui::Text("Physic generator at %f / %f / %f (x, y, z) - variation : %f.", this->_generator->m_position.x, this->_generator->m_position.y, this->_generator->m_position.z, this->_generator->m_positionVariation);
ImGui::SeparatorText("Velocity");
ImGui::Text("X : %f - variation : [%f , %f].", this->_generator->m_velocity.x, this->_generator->m_velocityVariation.lower.x, this->_generator->m_velocityVariation.upper.x);
ImGui::Text("Y : %f - variation : [%f , %f].", this->_generator->m_velocity.y, this->_generator->m_velocityVariation.lower.y, this->_generator->m_velocityVariation.upper.y);
ImGui::Text("Z : %f - variation : [%f , %f].", this->_generator->m_velocity.z, this->_generator->m_velocityVariation.lower.z, this->_generator->m_velocityVariation.upper.z);
ImGui::SeparatorText("Acceleration");
ImGui::Text("X : %f - variation : [%f , %f].", this->_generator->m_acceleration.x, this->_generator->m_accelerationVariation.lower.x, this->_generator->m_accelerationVariation.upper.x);
ImGui::Text("Y : %f - variation : [%f , %f].", this->_generator->m_acceleration.y, this->_generator->m_accelerationVariation.lower.y, this->_generator->m_accelerationVariation.upper.y);
ImGui::Text("Z : %f - variation : [%f , %f].", this->_generator->m_acceleration.z, this->_generator->m_accelerationVariation.lower.z, this->_generator->m_accelerationVariation.upper.z);
ImGui::SeparatorText("Friction");
ImGui::Text("X : %f - variation : [0 , %f].", this->_generator->m_friction.x, this->_generator->m_frictionVariation.x);
ImGui::Text("Y : %f - variation : [0 , %f].", this->_generator->m_friction.y, this->_generator->m_frictionVariation.y);
ImGui::Text("Z : %f - variation : [0 , %f].", this->_generator->m_friction.z, this->_generator->m_frictionVariation.z);
}
if(ImGui::CollapsingHeader("Generator Position")) {
ImGui::DragFloat3("Generator Position (x, y, z)", &this->_generator->m_position[0], 0.1);
ImGui::InputFloat("Position Variation", &this->_generator->m_positionVariation, 0.f, 5.f, "%.4f");
}
if(ImGui::CollapsingHeader("Generation Settings")) {
ImGui::InputFloat3("Velocity (x, y, z)", &this->_generator->m_velocity[0], "%.4f");
ImGui::InputFloat3("Acceleration (x, y, z)", &this->_generator->m_acceleration[0], "%.4f");
ImGui::InputFloat3("Friction (x, y, z)", &this->_generator->m_friction[0], "%.4f");
}
if(ImGui::CollapsingHeader("Variation")) {
ImGui::SeparatorText("Velocity");
ImGui::InputFloat3("Upper Boundary (x, y, z)", &this->_generator->m_velocityVariation.upper[0], "%.4f");
ImGui::InputFloat3("Lower Boundary (x, y, z)", &this->_generator->m_velocityVariation.lower[0], "%.4f");
ImGui::SeparatorText("Acceleration");
ImGui::PushID(1);
ImGui::InputFloat3("Upper Boundary (x, y, z)", &this->_generator->m_accelerationVariation.upper[0], "%.4f");
ImGui::InputFloat3("Lower Boundary (x, y, z)", &this->_generator->m_accelerationVariation.lower[0], "%.4f");
ImGui::PopID();
ImGui::SeparatorText("Friction");
ImGui::InputFloat3("Variation", &this->_generator->m_frictionVariation[0], "%.4f");
}
}
}
#pragma once
#include "../Interface.hpp"
namespace pg {
class PhysicsParticleGenerator;
class PhysicGeneratorInterface : public Interface {
private:
PhysicsParticleGenerator * _generator;
public:
PhysicGeneratorInterface(PhysicsParticleGenerator *);
inline PhysicsParticleGenerator * getGenerator() const {return this->_generator;}
virtual void render(double);
};
}
\ No newline at end of file
......@@ -8,7 +8,7 @@
namespace pg {
GlobalInterface::GlobalInterface(Window & window, SceneManager & manager, const std::string & title)
: _window(window), _title(title), _manager(manager) {
: _title(title), _manager(manager), _historical() {
if(this->_title.empty()) {
this->_title = window.title();
}
......@@ -34,6 +34,8 @@ namespace pg {
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
this->_historical.count(current_time);
if(ImGui::Begin(this->_title.c_str())) {
if(ImGui::BeginTabBar("")) {
if(ImGui::BeginTabItem("Scene")) {
......@@ -41,8 +43,8 @@ namespace pg {
ImGui::EndTabItem();
}
if(ImGui::BeginTabItem("Window")) {
ImGui::Text("Window !");
if(ImGui::BeginTabItem("Performance")) {
this->_historical.render(current_time);
ImGui::EndTabItem();
}
ImGui::EndTabBar();
......
......@@ -4,14 +4,15 @@
#include "../System/Window.hpp"
#include "../Scene/SceneManager.hpp"
#include "../System/FrameHistorical.hpp"
namespace pg {
class GlobalInterface : public Interface {
private:
Window & _window;
std::string _title;
SceneManager & _manager;
FrameHistorical _historical;
public:
GlobalInterface(Window &, SceneManager &, const std::string & = "");
......
#include "PathSceneInterface.hpp"
#include <imgui.h>
#include "../../Scene/PathScene.hpp"
namespace pg {
PathSceneInterface::PathSceneInterface(PathScene * scene)
: _scene(scene),
_max(1024),
_spawnFrequence(500),
_spawnCount(5),
_lifetime(5),
_color(1.f, 1.f, 1.f, 1.f),
_enableRender(true),
_enableSpawning(true) {}
void PathSceneInterface::render(double current_time) {
ImGui::Text("Particles Number : %i / %i.", this->_scene->_particles.size(), this->_max);
ImGui::Checkbox("Enable Rendering", &this->_enableRender);
ImGui::Separator();
ImGui::Checkbox("Enable Spawning", &this->_enableSpawning);
ImGui::SliderInt("Spawning Number", &this->_spawnCount, 1, 1024);
ImGui::InputInt("Spawn Frequence (ms)", &this->_spawnFrequence, 25, 100);
ImGui::Separator();
ImGui::InputInt("Particles Lifetime (s)", &this->_lifetime, 1, 5);
ImGui::ColorEdit4("GlobalColor", &this->_color[0]);
if(ImGui::Button("Clear")) {
this->_scene->_particles.clear();
}
ImGui::SameLine();
if(ImGui::Button("Spawn")) {
std::vector<std::unique_ptr<pg::Particle>> newParticles = this->_scene->_generator.generate(1, static_cast<size_t>(current_time));
this->_scene->_particles.insert(this->_scene->_particles.begin(), std::make_move_iterator(newParticles.begin()), std::make_move_iterator(newParticles.end()));
}
}
}
\ No newline at end of file
#pragma once
#include "../Interface.hpp"
#include <glm/vec4.hpp>
namespace pg {
class PathScene;
class PathSceneInterface : public Interface {
private:
PathScene * _scene;
int _max;
int _spawnFrequence;
int _spawnCount;
int _lifetime;
glm::vec4 _color;
bool _enableRender;
bool _enableSpawning;
public:
PathSceneInterface(PathScene *);
inline int getMaxParticle() const {return this->_max;}
inline int getSpawnFrequence() const {return this->_spawnFrequence;}
inline int getSpawnCount() const {return this->_spawnCount;}
inline int getLifeTime() const {return this->_lifetime;}
inline const glm::vec4 & getColor() const {return this->_color;}
inline bool isRenderEnable() {return this->_enableRender;}
inline bool isSpawningEnable() {return this->_enableSpawning;}
inline void setSpawnFrequence(int freq) {this->_spawnFrequence = freq;}
inline void setSpawnCount(int count) {this->_spawnCount = count;}
inline void setGlobalColor(const glm::vec4 & color) {this->_color = color;}
inline void setRenderEnable(bool state) {this->_enableRender = state;}
inline void setEnableSpawning(bool state) {this->_enableSpawning = state;}
virtual void render(double);
};
}
\ No newline at end of file
#include "PhysicSceneInterface.hpp"
#include <imgui.h>
#include "../../Scene/PhysicScene.hpp"
namespace pg {
PhysicSceneInterface::PhysicSceneInterface(PhysicScene * scene)
: _scene(scene),
_generator(&scene->_generator),
_max(1024),
_spawnFrequence(500),
_spawnCount(5),
_lifetime(5),
_color(1.f, 1.f, 1.f, 1.f),
_enableRender(true),
_enableSpawning(true) {}
void PhysicSceneInterface::render(double current_time) {
ImGui::Text("Particles Number : %i / %i.", this->_scene->_particles.size(), this->_max);
//ImGui::Text("Frame Per Second : %f.", this->_fps);
ImGui::Checkbox("Enable Rendering", &this->_enableRender);
ImGui::Separator();
ImGui::Checkbox("Enable Spawning", &this->_enableSpawning);
ImGui::SliderInt("Spawning Number", &this->_spawnCount, 1, 1024);
ImGui::InputInt("Spawn Frequence (ms)", &this->_spawnFrequence, 25, 100);
ImGui::Separator();
ImGui::InputInt("Particles Lifetime (s)", &this->_lifetime, 1, 5);
ImGui::ColorEdit4("GlobalColor", &this->_color[0]);
if(ImGui::Button("Clear")) {
this->_scene->_particles.clear();
}
ImGui::SameLine();
if(ImGui::Button("Spawn")) {
std::vector<std::unique_ptr<pg::Particle>> newParticles = this->_scene->_generator.generate(1, static_cast<size_t>(current_time));
this->_scene->_particles.insert(this->_scene->_particles.begin(), std::make_move_iterator(newParticles.begin()), std::make_move_iterator(newParticles.end()));
}
this->_generator.render(current_time);
}
}
\ No newline at end of file
#pragma once
#include "../Interface.hpp"
#include "../Generator/PhysicGeneratorInterface.hpp"
#include <glm/vec4.hpp>
namespace pg {
class PhysicScene;
class PhysicSceneInterface : public Interface {
private:
PhysicScene * _scene;
PhysicGeneratorInterface _generator;
int _max;
int _spawnFrequence;
int _spawnCount;
int _lifetime;
glm::vec4 _color;
bool _enableRender;
bool _enableSpawning;
public:
PhysicSceneInterface(PhysicScene *);
inline int getMaxParticle() const {return this->_max;}
inline int getSpawnFrequence() const {return this->_spawnFrequence;}
inline int getSpawnCount() const {return this->_spawnCount;}
inline int getLifeTime() const {return this->_lifetime;}
inline const glm::vec4 & getColor() const {return this->_color;}
inline bool isRenderEnable() {return this->_enableRender;}
inline bool isSpawningEnable() {return this->_enableSpawning;}
inline void setSpawnFrequence(int freq) {this->_spawnFrequence = freq;}
inline void setSpawnCount(int count) {this->_spawnCount = count;}
inline void setGlobalColor(const glm::vec4 & color) {this->_color = color;}
inline void setRenderEnable(bool state) {this->_enableRender = state;}
inline void setEnableSpawning(bool state) {this->_enableSpawning = state;}
virtual void render(double);
};
}
\ No newline at end of file
......@@ -5,20 +5,19 @@
#include "../Scene/SceneManager.hpp"
namespace pg {
SceneManagerInterface::SceneManagerInterface(Window & window, SceneManager * manager)
: _manager(manager), _window(window) {
SceneManagerInterface::SceneManagerInterface(SceneManager * manager)
: _manager(manager) {
}
void SceneManagerInterface::render(double current_time) {
if(!this->_manager->scenes().empty()) {
std::string current = this->_manager->current() == nullptr ? this->_manager->scenes().front()->name() : this->_manager->current()->name();
if(ImGui::BeginCombo("Current Scene", current.c_str())) {
std::string name = this->_manager->current() == nullptr ? this->_manager->scenes().front()->name() : this->_manager->current()->name();
if(ImGui::BeginCombo("Current Scene", name.c_str())) {
for(auto & scene : this->_manager->_scenes) {
bool is_selected = (scene == this->_manager->current());
if(ImGui::Selectable(scene->name().c_str(), is_selected)) {
this->_manager->switchScene(this->_window, scene);
this->_manager->setScene(scene);
}
}
ImGui::EndCombo();
......
......@@ -9,10 +9,9 @@ namespace pg {
class SceneManagerInterface : public Interface {
private:
SceneManager * _manager;
Window & _window;
public:
SceneManagerInterface(Window &, SceneManager *);
SceneManagerInterface(SceneManager *);
virtual void render(double = 0.0);
};
......
#pragma once
#include "Interface.hpp"
#include <imgui.h>
namespace pg {
class TestInterface : public Interface {
public:
virtual void render(double) {
ImGui::Text("Hello Scene !");
}
};
}
\ No newline at end of file
......@@ -27,7 +27,7 @@ namespace pg
inline void setPosition(const ct::Point& position) {this->m_position = position;}
inline void translate(const ct::Point& position) {this->m_position += position;}
virtual void update(double) = 0;
virtual void update(double = 0.0) = 0;
inline friend bool operator==(const Particle & p1, const Particle & p2) {return p1.m_position == p2.m_position && p1.m_born == p2.m_born;}
inline friend bool operator!=(const Particle & p1, const Particle & p2) {return !(p1 == p2);}
......
#include "PathParticle.hpp"
#include <iostream>
namespace pg {
PathParticle::PathParticle(ct::CurveGenerator* generator, const ct::Curve& path, double u, double inc)
: Particle(), m_generator(generator), m_ctrlPoints(path), m_positionVariation(), m_u(u), m_increment(inc), m_limitor(0.0)
......@@ -19,9 +21,11 @@ namespace pg {
{
this->m_u += this->m_increment;
if(this->m_u > this->m_limitor && this->m_limitor != 0.0) {
if(this->m_u > this->m_limitor && this->m_limitor > 0.0) {
this->setBorn(0.0);
return;
}
this->setPosition(this->m_generator->operator()(this->m_ctrlPoints, this->m_u) + this->m_positionVariation);
}
}
......@@ -4,7 +4,7 @@
#include "../PathParticle.hpp"
namespace pg {
class PathConfig;
class PathGeneratorInterface;
class PathParticleGenerator : public ParticleGenerator {
private:
......@@ -30,6 +30,6 @@ namespace pg {
std::vector<std::unique_ptr<Particle>> generate(size_t count, size_t birth = 0) const override;
friend class PathConfig;
friend class PathGeneratorInterface;
};
}
\ No newline at end of file
......@@ -3,9 +3,9 @@
#include "ParticleGenerator.hpp"
#include "../PhysicsParticle.hpp"
namespace pg {
class PhysicConfig;
#include "../../Interface/Generator/PhysicGeneratorInterface.hpp"
namespace pg {
template <typename T>
struct Interval {
T upper;
......@@ -41,6 +41,6 @@ namespace pg {
std::vector<std::unique_ptr<Particle>> generate(size_t count, size_t birth = 0) const override;
friend class PhysicConfig;
friend class PhysicGeneratorInterface;
};
}
\ No newline at end of file
#include "PathScene.hpp"
#include <chrono>
#include <glm/gtc/matrix_transform.hpp>
namespace pg {
PathScene::PathScene(ct::CurveGenerator * generator, const ct::Curve & ctrlpoint)
: _ubo(0),
_program(),
_vao(),
_vbo(this->_vao, {layout::POSITION, layout::TEXTURE}, 1, GL_STATIC_DRAW),
_ebo(this->_vao, 1, GL_STATIC_DRAW),
_texture(),
_generator(generator, ctrlpoint),
_particles(),
_interface(this) {}
void PathScene::initialize() {
if(!this->_program.usable()) {
pg::Source vertices("../../res/shaders/Instanced-Fat.vert", pg::Source::Categorie::VERTEX);
pg::Source fragment("../../res/shaders/Instanced-Fat.frag", pg::Source::Categorie::FRAGMENT);
this->_program << vertices;
this->_program << fragment;
this->_program.link();
vertices.release();
fragment.release();
}
pg::error::OpenGLError::check();
std::vector<float> Vertices = {
-1.0f, -1.0f, 0.f, 0.0f, 0.0f,
1.0f, -1.0f, 0.f, 1.0f, 0.0f,
1.0f, 1.0f, 0.f, 1.0f, 1.0f,
-1.0f, 1.0f, 0.f, 0.0f, 1.0f
};
std::vector<unsigned int> Indices = {
0, 1, 2,
0, 2, 3
};
this->_vbo.set(Vertices);
this->_ebo.set(Indices);
pg::error::OpenGLError::check();
glCreateBuffers(1, &this->_ubo);
glBindBuffer(GL_UNIFORM_BUFFER, this->_ubo);
glBufferData(GL_UNIFORM_BUFFER, 1024 * sizeof(glm::mat4), nullptr, GL_DYNAMIC_DRAW);
GLuint uniforme_index = glGetUniformBlockIndex(this->_program.id(), "uModels_t");
glBindBufferBase(GL_UNIFORM_BUFFER, uniforme_index, this->_ubo);
pg::error::OpenGLError::check();
this->_texture.load("../../res/textures/smoke1.png");
this->_texture.bind(0);
pg::error::OpenGLError::check();
this->_generator.setPosition({0.f, 0.f, 0.f});
this->_generator.setPositionVariation(0.5);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.25f, 0.f, 0.15f, 1.0f);
}
void PathScene::update(double current_time) {
static auto start = std::chrono::high_resolution_clock::now();
auto end = std::chrono::high_resolution_clock::now();
pg::Particle::purge(this->_particles, static_cast<size_t>(current_time), this->_interface.getLifeTime());
if(duration_cast<std::chrono::milliseconds>(end - start).count() >= this->_interface.getSpawnFrequence()) {
start = std::chrono::high_resolution_clock::now();
if((this->_interface.getSpawnCount() + this->_particles.size()) <= this->_interface.getMaxParticle() && this->_interface.isSpawningEnable()) {
std::vector<std::unique_ptr<pg::Particle>> newParticles = this->_generator.generate(this->_interface.getSpawnCount(), static_cast<size_t>(current_time));
this->_particles.insert(this->_particles.begin(), std::make_move_iterator(newParticles.begin()), std::make_move_iterator(newParticles.end()));
}
}
if(duration_cast<std::chrono::milliseconds>(end - start).count() >= 10) {
for(auto& particle : this->_particles) {
particle->update(0.0);
}
}
}
void PathScene::render(const Camera & camera, double current_time) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const glm::mat4 VIEW_MATRIX = camera.getViewMatrix();
const glm::mat4 PROJECTION_MATRIX = camera.getViewFrustum().getProjectionMatrix();
std::vector<glm::mat4> models;
for(auto & particle : this->_particles) {
glm::mat4 model = glm::mat4(1.0);
model = glm::translate(model, glm::vec3(particle->getPosition()));
models.push_back(model);
}
pg::error::OpenGLError::check();
glBindBuffer(GL_UNIFORM_BUFFER, this->_ubo);
glBufferSubData(GL_UNIFORM_BUFFER, 0, models.size() * sizeof(glm::mat4), models.data());
pg::error::OpenGLError::check();
static GLint slot = 0;
this->_program.use();
this->_program.setUniform("uView", VIEW_MATRIX);
this->_program.setUniform("uProj", PROJECTION_MATRIX);
this->_program.setUniform("uSlot", slot);
this->_program.setUniform("uColor", this->_interface.getColor());
pg::error::OpenGLError::check();
this->_vao.bind();
this->_vbo.bind();
if(this->_interface.isRenderEnable()) {
glDrawElementsInstanced(GL_TRIANGLES, static_cast<GLsizei>(this->_ebo.size()), GL_UNSIGNED_INT, 0, static_cast<GLsizei>(this->_particles.size()));
}
pg::error::OpenGLError::check();
}
void PathScene::destroy() {
glDeleteBuffers(1, &this->_ubo);
this->_vbo.destroy();
this->_ebo.destroy();
this->_particles.clear();
pg::error::OpenGLError::check();
}
}
\ No newline at end of file
#pragma once
#include "Scene.hpp"
#include "../Renderer/Renderer.hpp"
#include "../Particle/generator/PathParticleGenerator.hpp"
#include "../Interface/Scene/PathSceneInterface.hpp"
#include "../System/Window.hpp"
namespace pg {
class PathScene : public Scene {
private:
GLuint _ubo;
pg::Program _program;
pg::VertexArray _vao;
pg::VerticeBuffer _vbo;
pg::ElementBuffer _ebo;
pg::Material _texture;
pg::PathParticleGenerator _generator;
std::vector<std::unique_ptr<pg::Particle>> _particles;
PathSceneInterface _interface;
public:
PathScene(ct::CurveGenerator *, const ct::Curve &);
virtual void initialize();
virtual void render(const Camera &, double);
virtual void update(double);
virtual void destroy();
virtual std::string name() const {return "Path Scene";}
virtual Interface & interface() {return this->_interface;}
friend class PathSceneInterface;
};
}
\ No newline at end of file
......@@ -6,7 +6,6 @@
namespace pg {
PhysicScene::PhysicScene()
: _ubo(0),
_camera({0, 0, 3}),
_program(),
_vao(),
_vbo(this->_vao, {layout::POSITION, layout::TEXTURE}, 1, GL_STATIC_DRAW),
......@@ -14,40 +13,9 @@ namespace pg {
_texture(),
_generator(),
_particles(),
_color(1.f, 1.f, 1.f, 1.f) {}
_interface(this) {}
void PhysicScene::initialize(Window & window) {
std::cout << "Physic Scene Init Start : ";
//? Camera
glfwSetWindowUserPointer(window.address(), (void *)&this->_camera);
glfwSetCursorPosCallback(window.address(), [](GLFWwindow* window, double xpos, double ypos) {
pg::OrbitCamera* camera = static_cast<pg::OrbitCamera*>(glfwGetWindowUserPointer(window));
camera->onMove(static_cast<unsigned int>(xpos), static_cast<unsigned int>(ypos));
});
glfwSetMouseButtonCallback(window.address(), [](GLFWwindow* window, int button, int action, int mods) {
pg::OrbitCamera* camera = static_cast<pg::OrbitCamera*>(glfwGetWindowUserPointer(window));
if (button == GLFW_MOUSE_BUTTON_RIGHT) {
if(action == GLFW_PRESS) {
camera->onMoveBegin();
}
else if(action == GLFW_RELEASE) {
camera->onMoveEnd();
}
}
});
glfwSetScrollCallback(window.address(), [](GLFWwindow* window, double xoffset, double yoffset) {
pg::OrbitCamera* camera = static_cast<pg::OrbitCamera*>(glfwGetWindowUserPointer(window));
camera->onScroll(static_cast<int>(yoffset));
});
std::cout << "Camera, ";
pg::error::OpenGLError::check();
//? Shaders
void PhysicScene::initialize() {
if(!this->_program.usable()) {
pg::Source vertices("../../res/shaders/Instanced-Fat.vert", pg::Source::Categorie::VERTEX);
pg::Source fragment("../../res/shaders/Instanced-Fat.frag", pg::Source::Categorie::FRAGMENT);
......@@ -61,10 +29,8 @@ namespace pg {
fragment.release();
}
std::cout << "Shader, ";
pg::error::OpenGLError::check();
//? Buffer
std::vector<float> Vertices = {
-1.0f, -1.0f, 0.f, 0.0f, 0.0f,
1.0f, -1.0f, 0.f, 1.0f, 0.0f,
......@@ -80,10 +46,8 @@ namespace pg {
this->_vbo.set(Vertices);
this->_ebo.set(Indices);
std::cout << "Buffer, ";
pg::error::OpenGLError::check();
//? UBO
glCreateBuffers(1, &this->_ubo);
glBindBuffer(GL_UNIFORM_BUFFER, this->_ubo);
glBufferData(GL_UNIFORM_BUFFER, 1024 * sizeof(glm::mat4), nullptr, GL_DYNAMIC_DRAW);
......@@ -91,24 +55,18 @@ namespace pg {
GLuint uniforme_index = glGetUniformBlockIndex(this->_program.id(), "uModels_t");
glBindBufferBase(GL_UNIFORM_BUFFER, uniforme_index, this->_ubo);
std::cout << "Uniforme Buffer, ";
pg::error::OpenGLError::check();
//? Texture
this->_texture.load("../../res/textures/smoke1.png");
this->_texture.bind(0);
std::cout << "Texture, ";
pg::error::OpenGLError::check();
//? Generator
this->_generator.setPosition({0.f, 0.f, 0.f});
this->_generator.setPositionVariation(0.5);
this->_generator.setVelocity({0, 0.01, 0});
this->_generator.setAcceleration({0.0, 0.0001, 0});
std::cout << "Generator." << std::endl;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.25f, 0.f, 0.15f, 1.0f);
......@@ -118,11 +76,11 @@ namespace pg {
static auto start = std::chrono::high_resolution_clock::now();
auto end = std::chrono::high_resolution_clock::now();
pg::Particle::purge(this->_particles, static_cast<size_t>(current_time), 5);
if(duration_cast<std::chrono::milliseconds>(end - start).count() >= 500) {
pg::Particle::purge(this->_particles, static_cast<size_t>(current_time), this->_interface.getLifeTime());
if(duration_cast<std::chrono::milliseconds>(end - start).count() >= this->_interface.getSpawnFrequence()) {
start = std::chrono::high_resolution_clock::now();
if((10 + this->_particles.size()) <= 1024) {
std::vector<std::unique_ptr<pg::Particle>> newParticles = this->_generator.generate(10, static_cast<size_t>(current_time));
if((this->_interface.getSpawnCount() + this->_particles.size()) <= this->_interface.getMaxParticle() && this->_interface.isSpawningEnable()) {
std::vector<std::unique_ptr<pg::Particle>> newParticles = this->_generator.generate(this->_interface.getSpawnCount(), static_cast<size_t>(current_time));
this->_particles.insert(this->_particles.begin(), std::make_move_iterator(newParticles.begin()), std::make_move_iterator(newParticles.end()));
}
}
......@@ -134,11 +92,11 @@ namespace pg {
}
}
void PhysicScene::render(double current_time) {
void PhysicScene::render(const Camera & camera, double current_time) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
const glm::mat4 VIEW_MATRIX = this->_camera.getViewMatrix();
const glm::mat4 PROJECTION_MATRIX = this->_camera.getViewFrustum().getProjectionMatrix();
const glm::mat4 VIEW_MATRIX = camera.getViewMatrix();
const glm::mat4 PROJECTION_MATRIX = camera.getViewFrustum().getProjectionMatrix();
std::vector<glm::mat4> models;
......@@ -162,14 +120,16 @@ namespace pg {
this->_program.setUniform("uView", VIEW_MATRIX);
this->_program.setUniform("uProj", PROJECTION_MATRIX);
this->_program.setUniform("uSlot", slot);
this->_program.setUniform("uColor", this->_color);
this->_program.setUniform("uColor", this->_interface.getColor());
pg::error::OpenGLError::check();
this->_vao.bind();
this->_vbo.bind();
if(this->_interface.isRenderEnable()) {
glDrawElementsInstanced(GL_TRIANGLES, static_cast<GLsizei>(this->_ebo.size()), GL_UNSIGNED_INT, 0, static_cast<GLsizei>(this->_particles.size()));
}
pg::error::OpenGLError::check();
}
......@@ -180,6 +140,8 @@ namespace pg {
this->_vbo.destroy();
this->_ebo.destroy();
this->_particles.clear();
pg::error::OpenGLError::check();
}
}
\ No newline at end of file
#pragma once
#include "../Renderer/Renderer.hpp"
#include "Scene.hpp"
#include "../Renderer/Renderer.hpp"
#include "../Particle/generator/PhysicsParticleGenerator.hpp"
#include "../Interface/TestInterface.hpp"
#include "../Interface/Scene/PhysicSceneInterface.hpp"
#include "../System/Window.hpp"
namespace pg {
class PhysicScene : public Scene {
private:
GLuint _ubo;
pg::OrbitCamera _camera;
pg::Program _program;
pg::VertexArray _vao;
pg::VerticeBuffer _vbo;
......@@ -19,18 +21,19 @@ namespace pg {
pg::PhysicsParticleGenerator _generator;
std::vector<std::unique_ptr<pg::Particle>> _particles;
glm::vec4 _color;
TestInterface _interface;
PhysicSceneInterface _interface;
public:
PhysicScene();
virtual void initialize(Window &);
virtual void initialize();
virtual void render(const Camera &, double);
virtual void update(double);
virtual void render(double);
virtual void destroy();
virtual std::string name() const {return "Physic Scene";}
virtual Interface & interface() {return this->_interface;}
friend class PhysicSceneInterface;
};
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment