add: nearly complete particle system

This commit is contained in:
Szymon Szczubkowski 2024-02-08 07:00:43 +01:00
parent 383f7eaf90
commit ba05c16fea
4 changed files with 27 additions and 9 deletions

View File

@ -14,7 +14,7 @@ uniform vec3 cameraSide;
void main() void main()
{ {
float scale = 1.0f; float scale = 0.1f;
TexCoords = texCoords; TexCoords = texCoords;
ParticleColor = color; ParticleColor = color;
vec3 worldspacePos = cameraSide*vertex.x + cameraUp*vertex.y; vec3 worldspacePos = cameraSide*vertex.x + cameraUp*vertex.y;

View File

@ -6,9 +6,10 @@ ParticleGenerator::ParticleGenerator() {
void ParticleGenerator::Draw(glm::mat4 modelMatrix, GLuint programParticle, GLuint texture, glm::vec3 cameraDir) { void ParticleGenerator::Draw(glm::mat4 modelMatrix, GLuint programParticle, GLuint texture, glm::vec3 cameraDir) {
glEnable(GL_BLEND); glEnable(GL_BLEND);
glDepthMask(GL_FALSE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE); glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glUseProgram(programParticle); glUseProgram(programParticle);
std::sort(this->particles.begin(), this->particles.end());
for (Particle particle : this->particles) { for (Particle particle : this->particles) {
glUniform1i(glGetUniformLocation(programParticle, "sprite"), 0); glUniform1i(glGetUniformLocation(programParticle, "sprite"), 0);
@ -16,13 +17,14 @@ void ParticleGenerator::Draw(glm::mat4 modelMatrix, GLuint programParticle, GLui
glBindTexture(GL_TEXTURE_2D, texture); glBindTexture(GL_TEXTURE_2D, texture);
glUniformMatrix4fv(glGetUniformLocation(programParticle, "transformation"), 1, GL_FALSE, (float*)&(modelMatrix)); glUniformMatrix4fv(glGetUniformLocation(programParticle, "transformation"), 1, GL_FALSE, (float*)&(modelMatrix));
glUniform3f(glGetUniformLocation(programParticle, "position"), particle.position.x, particle.position.y, particle.position.z); glUniform3f(glGetUniformLocation(programParticle, "position"), particle.position.x, particle.position.y + (rand() % 10 - 5) / 500.f, particle.position.z + (rand() % 10 - 5) / 500.f);
glUniform4f(glGetUniformLocation(programParticle, "color"), particle.color.x, particle.color.y, particle.color.z, particle.color.w); glUniform4f(glGetUniformLocation(programParticle, "color"), particle.color.x, particle.color.y, particle.color.z, particle.color.w);
glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir, glm::vec3(0.f, 1.f, 0.f))); glm::vec3 cameraSide = glm::normalize(glm::cross(cameraDir, glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 cameraUp = glm::normalize(glm::cross(cameraSide, cameraDir));
glUniform3f(glGetUniformLocation(programParticle, "cameraSide"), cameraSide.x, cameraSide.y, cameraSide.z); glUniform3f(glGetUniformLocation(programParticle, "cameraSide"), cameraSide.x, cameraSide.y, cameraSide.z);
glUniform3f(glGetUniformLocation(programParticle, "cameraUp"), 0.f, 1.f, 0.f); glUniform3f(glGetUniformLocation(programParticle, "cameraUp"), cameraUp.x, cameraUp.y, cameraUp.z);
glBindVertexArray(this->VAO); glBindVertexArray(this->VAO);
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
@ -32,6 +34,7 @@ void ParticleGenerator::Draw(glm::mat4 modelMatrix, GLuint programParticle, GLui
glUseProgram(0); glUseProgram(0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_BLEND); glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
} }
void ParticleGenerator::init() { void ParticleGenerator::init() {
@ -56,7 +59,13 @@ void ParticleGenerator::init() {
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float))); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
glBindVertexArray(0); glBindVertexArray(0);
for (unsigned int i = 0; i < 1; i++) { for (unsigned int i = 0; i < 5; i++) {
this->particles.push_back(Particle()); this->particles.push_back(Particle());
} }
for (Particle particle : this->particles) {
particle.position.x = (rand() % 10 -5) / 500.f;
particle.position.y = (rand() % 10 -5) / 500.f;
particle.position.z = (rand() % 10 -5) / 500.f;
}
} }

View File

@ -1,11 +1,16 @@
#include <vector> #include <vector>
#include "glm.hpp" #include "glm.hpp"
#include "glew.h" #include "glew.h"
#include <algorithm>
struct Particle { struct Particle {
glm::vec3 position, velocity; glm::vec3 position, velocity;
glm::vec4 color; glm::vec4 color;
float life; float life;
bool operator<(Particle& part) {
return this->position.x > part.position.x;
}
}; };
class ParticleGenerator { class ParticleGenerator {

View File

@ -115,6 +115,7 @@ glm::mat4 createCameraMatrix();
void drawObjectSun(Core::RenderContext& context, glm::mat4 modelMatrix); void drawObjectSun(Core::RenderContext& context, glm::mat4 modelMatrix);
void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint albedo, GLuint ao, GLuint normal, GLuint roughness, GLuint metallic); void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint albedo, GLuint ao, GLuint normal, GLuint roughness, GLuint metallic);
ParticleGenerator* mainEngine;
void processInput(GLFWwindow* window) void processInput(GLFWwindow* window)
{ {
@ -144,7 +145,7 @@ void processInput(GLFWwindow* window)
}else if (glfwGetGamepadState(GLFW_JOYSTICK_1, &currentState)) { }else if (glfwGetGamepadState(GLFW_JOYSTICK_1, &currentState)) {
//station glm::vec3(0.f, 0.4f, 4.f) //station glm::vec3(0.f, 0.4f, 4.f)
if (engineIs == 1 && spaceshipPos.x > -0.7 && 0.3 > spaceshipPos.x && spaceshipPos.z > 3.7 && 4.3 > spaceshipPos.z && spaceshipPos.y > 0.1 && 0.7 > spaceshipPos.y) if (engineIs == 1 && spaceshipPos.x > -0.7 && 0.3 > spaceshipPos.x && spaceshipPos.z > 3.7 && 4.3 > spaceshipPos.z && spaceshipPos.y > -0.7 && 0.3 > spaceshipPos.y)
{ {
if (currentState.buttons[GLFW_GAMEPAD_BUTTON_A]==GLFW_PRESS) if (currentState.buttons[GLFW_GAMEPAD_BUTTON_A]==GLFW_PRESS)
engineIs = 2; engineIs = 2;
@ -249,7 +250,6 @@ unsigned int loadCubemap()
"textures/skybox/space_dn.png", "textures/skybox/space_dn.png",
"textures/skybox/space_bk.png", "textures/skybox/space_bk.png",
"textures/skybox/space_ft.png" "textures/skybox/space_ft.png"
}; };
int width, height, nrChannels; int width, height, nrChannels;
@ -407,7 +407,7 @@ void renderScene(GLFWwindow* window) {
drawObjectPBR(planetContext, glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(0, 0, -3.f)) * glm::eulerAngleY(-1.5f * time) * glm::scale(glm::vec3(0.1f)), texture::earth_albedo, texture::earth_normal, texture::earth_ao, texture::earth_roughness, texture::earth_metallic); drawObjectPBR(planetContext, glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(0, 0, -3.f)) * glm::eulerAngleY(-1.5f * time) * glm::scale(glm::vec3(0.1f)), texture::earth_albedo, texture::earth_normal, texture::earth_ao, texture::earth_roughness, texture::earth_metallic);
drawObjectPBR(planetContext, glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(0, 0, -3.f)) * glm::eulerAngleY( 2.f * time) * glm::translate(glm::vec3(0.4f, 0, 0.4f)) * glm::eulerAngleY(-0.5f * time) * glm::scale(glm::vec3(0.04f)), texture::moon_albedo, texture::moon_normal, texture::moon_ao, texture::moon_roughness, texture::moon_metallic); drawObjectPBR(planetContext, glm::eulerAngleY(time / 3) * glm::translate(glm::vec3(0, 0, -3.f)) * glm::eulerAngleY( 2.f * time) * glm::translate(glm::vec3(0.4f, 0, 0.4f)) * glm::eulerAngleY(-0.5f * time) * glm::scale(glm::vec3(0.04f)), texture::moon_albedo, texture::moon_normal, texture::moon_ao, texture::moon_roughness, texture::moon_metallic);
mainEngine->Draw(createPerspectiveMatrix() * createCameraMatrix() * glm::translate(glm::vec3(0.f, -0.22f, -0.05f)) * glm::translate(cameraPos + 0.2 * cameraDir + glm::vec3(0, 1, 0) * 0.05f), programParticle, texture::particle_fire, cameraDir);
//desired objects end here //desired objects end here
glUseProgram(0); glUseProgram(0);
glfwSwapBuffers(window); glfwSwapBuffers(window);
@ -431,7 +431,7 @@ void init(GLFWwindow* window)
programSun = shaderLoader.CreateProgram("shaders/sun.vert", "shaders/sun.frag"); programSun = shaderLoader.CreateProgram("shaders/sun.vert", "shaders/sun.frag");
programInstall = shaderLoader.CreateProgram("shaders/install.vert", "shaders/install.frag"); programInstall = shaderLoader.CreateProgram("shaders/install.vert", "shaders/install.frag");
//programParticle = shaderLoader.CreateProgram("shaders/part.vert", "shaders/part.frag"); programParticle = shaderLoader.CreateProgram("shaders/part.vert", "shaders/part.frag");
//load models here //load models here
@ -505,6 +505,10 @@ void init(GLFWwindow* window)
texture::station_roughness = Core::LoadTexture("textures/station2/ALUM_8L4.JPG"); texture::station_roughness = Core::LoadTexture("textures/station2/ALUM_8L4.JPG");
texture::station_albedo = Core::LoadTexture("textures/station2/Alum_panel1.jpg"); texture::station_albedo = Core::LoadTexture("textures/station2/Alum_panel1.jpg");
texture::station_metallic = Core::LoadTexture("textures/station2/Aluminium6.jpg"); texture::station_metallic = Core::LoadTexture("textures/station2/Aluminium6.jpg");
texture::particle_fire = Core::LoadTexture("textures/particles/fire.png");
mainEngine = new ParticleGenerator();
} }
void shutdown(GLFWwindow* window) void shutdown(GLFWwindow* window)