add in-progress particle system

This commit is contained in:
Szymon Szczubkowski 2024-02-04 23:39:37 +01:00
parent 80db86ab11
commit f50f415191
9 changed files with 142 additions and 9 deletions

View File

@ -14,6 +14,7 @@
<ClCompile Include="src\Box.cpp" /> <ClCompile Include="src\Box.cpp" />
<ClCompile Include="src\Camera.cpp" /> <ClCompile Include="src\Camera.cpp" />
<ClCompile Include="src\main.cpp" /> <ClCompile Include="src\main.cpp" />
<ClCompile Include="src\Particle.cpp" />
<ClCompile Include="src\Render_Utils.cpp" /> <ClCompile Include="src\Render_Utils.cpp" />
<ClCompile Include="src\Shader_Loader.cpp" /> <ClCompile Include="src\Shader_Loader.cpp" />
<ClCompile Include="src\SOIL\image_DXT.c" /> <ClCompile Include="src\SOIL\image_DXT.c" />
@ -25,6 +26,7 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="src\Camera.h" /> <ClInclude Include="src\Camera.h" />
<ClInclude Include="src\objload.h" /> <ClInclude Include="src\objload.h" />
<ClInclude Include="src\Particle.h" />
<ClInclude Include="src\projekt.hpp" /> <ClInclude Include="src\projekt.hpp" />
<ClInclude Include="src\Render_Utils.h" /> <ClInclude Include="src\Render_Utils.h" />
<ClInclude Include="src\Shader_Loader.h" /> <ClInclude Include="src\Shader_Loader.h" />

View File

@ -51,6 +51,9 @@
<ClCompile Include="src\SOIL\image_helper.c"> <ClCompile Include="src\SOIL\image_helper.c">
<Filter>Source Files\SOIL</Filter> <Filter>Source Files\SOIL</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Particle.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\objload.h"> <ClInclude Include="src\objload.h">
@ -89,6 +92,9 @@
<ClInclude Include="src\projekt.hpp"> <ClInclude Include="src\projekt.hpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\Particle.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="shaders\pbr.frag"> <None Include="shaders\pbr.frag">

View File

@ -0,0 +1,12 @@
#version 430 core
in vec2 TexCoords;
in vec4 ParticleColor;
out vec4 FragColor;
uniform sampler2D sprite;
void main()
{
FragColor = (texture(sprite, TexCoords) * ParticleColor);
FragColor = texture(sprite, TexCoords);
}

View File

@ -0,0 +1,22 @@
#version 430 core
layout (location = 0) in vec3 vertex;
layout (location = 1) in vec2 texCoords;
out vec2 TexCoords;
out vec4 ParticleColor;
uniform mat4 transformation;
uniform vec3 position;
uniform vec4 color;
uniform vec3 cameraUp;
uniform vec3 cameraSide;
void main()
{
float scale = 1.0f;
TexCoords = texCoords;
ParticleColor = color;
vec3 worldspacePos = cameraSide*vertex.x + cameraUp*vertex.y;
gl_Position = transformation * vec4((worldspacePos.xyz * scale) + position, 1.0);
}

62
cw_8/src/Particle.cpp Normal file
View File

@ -0,0 +1,62 @@
#include "Particle.h"
ParticleGenerator::ParticleGenerator() {
this->init();
}
void ParticleGenerator::Draw(glm::mat4 modelMatrix, GLuint programParticle, GLuint texture, glm::vec3 cameraDir) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glUseProgram(programParticle);
for (Particle particle : this->particles) {
glUniform1i(glGetUniformLocation(programParticle, "sprite"), 0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniformMatrix4fv(glGetUniformLocation(programParticle, "transformation"), 1, GL_FALSE, (float*)&(modelMatrix));
glUniform3f(glGetUniformLocation(programParticle, "position"), particle.position.x, particle.position.y, particle.position.z);
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)));
glUniform3f(glGetUniformLocation(programParticle, "cameraSide"), cameraSide.x, cameraSide.y, cameraSide.z);
glUniform3f(glGetUniformLocation(programParticle, "cameraUp"), 0.f, 1.f, 0.f);
glBindVertexArray(this->VAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}
glUseProgram(0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_BLEND);
}
void ParticleGenerator::init() {
unsigned int VBO;
float particle_quad[] = {
0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f, 0.0f
};
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(this->VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(particle_quad), particle_quad, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
glBindVertexArray(0);
for (unsigned int i = 0; i < 1; i++) {
this->particles.push_back(Particle());
}
}

19
cw_8/src/Particle.h Normal file
View File

@ -0,0 +1,19 @@
#include <vector>
#include "glm.hpp"
#include "glew.h"
struct Particle {
glm::vec3 position, velocity;
glm::vec4 color;
float life;
};
class ParticleGenerator {
public:
void Draw(glm::mat4 modelMatrix, GLuint programParticle, GLuint texture, glm::vec3 cameraDir);
ParticleGenerator();
private:
std::vector<Particle> particles;
GLuint VAO;
void init();
};

View File

@ -15,8 +15,8 @@ GLuint Core::LoadTexture( const char * filepath )
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
int w, h; int w, h;
unsigned char* image = SOIL_load_image(filepath, &w, &h, 0, SOIL_LOAD_RGBA); unsigned char* image = SOIL_load_image(filepath, &w, &h, 0, SOIL_LOAD_RGBA);

View File

@ -9,6 +9,7 @@
#include "Render_Utils.h" #include "Render_Utils.h"
#include "Texture.h" #include "Texture.h"
#include "Particle.h"
GLuint program; GLuint program;
GLuint programSun; GLuint programSun;
@ -72,6 +73,8 @@ namespace texture {
GLuint station_ao; GLuint station_ao;
GLuint station_roughness; GLuint station_roughness;
GLuint station_metallic; GLuint station_metallic;
GLuint particle_fire;
} }
float aspectRatio = 1.f; float aspectRatio = 1.f;
@ -100,15 +103,19 @@ 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* Particles;
void init(GLFWwindow* window) void init(GLFWwindow* window)
{ {
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
//load shaders here
program = shaderLoader.CreateProgram("shaders/pbr.vert", "shaders/pbr.frag"); program = shaderLoader.CreateProgram("shaders/pbr.vert", "shaders/pbr.frag");
programSun = shaderLoader.CreateProgram("shaders/sun.vert", "shaders/sun.frag"); programSun = shaderLoader.CreateProgram("shaders/sun.vert", "shaders/sun.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
@ -120,7 +127,6 @@ void init(GLFWwindow* window)
loadModelToContext("./models/station.obj", stationContext); loadModelToContext("./models/station.obj", stationContext);
//load textures here //load textures here
texture::sun = Core::LoadTexture("textures/sun/sun.png"); texture::sun = Core::LoadTexture("textures/sun/sun.png");
@ -166,19 +172,20 @@ void init(GLFWwindow* window)
texture::station_roughness = Core::LoadTexture("textures/station/roughness.jpg"); texture::station_roughness = Core::LoadTexture("textures/station/roughness.jpg");
texture::station_ao = Core::LoadTexture("textures/station/ao.jpg"); texture::station_ao = Core::LoadTexture("textures/station/ao.jpg");
texture::station_metallic = Core::LoadTexture("textures/station/metallic.jpg"); texture::station_metallic = Core::LoadTexture("textures/station/metallic.jpg");
texture::particle_fire = Core::LoadTexture("textures/particles/fire.png");
Particles = new ParticleGenerator();
} }
void renderScene(GLFWwindow* window){ void renderScene(GLFWwindow* window){
glClearColor(0.0f, 0.3f, 0.3f, 1.0f); glClearColor(0.0f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(program);
float time = glfwGetTime(); float time = glfwGetTime();
updateDeltaTime(time); updateDeltaTime(time);
//desired objects go here //desired objects go here
drawObjectSun(sunContext, glm::scale(glm::vec3(0.1f)) * glm::eulerAngleY(time/10)); //drawObjectSun(sunContext, glm::scale(glm::vec3(0.1f)) * glm::eulerAngleY(time/10));
glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f))); glm::vec3 spaceshipSide = glm::normalize(glm::cross(spaceshipDir, glm::vec3(0.f, 1.f, 0.f)));
glm::vec3 spaceshipUp = glm::normalize(glm::cross(spaceshipSide, spaceshipDir)); glm::vec3 spaceshipUp = glm::normalize(glm::cross(spaceshipSide, spaceshipDir));
@ -207,7 +214,6 @@ void renderScene(GLFWwindow* window){
drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 3.f, 0.f)) * glm::scale(glm::vec3(0.1f)) * glm::eulerAngleY(time), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic); drawObjectPBR(stationContext, glm::translate(glm::vec3(0.f, 3.f, 0.f)) * glm::scale(glm::vec3(0.1f)) * glm::eulerAngleY(time), texture::station_albedo, texture::station_normal, texture::station_ao, texture::station_roughness, texture::station_metallic);
//desired objects end here //desired objects end here
glUseProgram(0);
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
@ -233,6 +239,8 @@ void processInput(GLFWwindow* window)
void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint albedo, GLuint normal, GLuint ao, GLuint roughness, GLuint metallic) { void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint albedo, GLuint normal, GLuint ao, GLuint roughness, GLuint metallic) {
glUseProgram(program);
glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix(); glm::mat4 viewProjectionMatrix = createPerspectiveMatrix() * createCameraMatrix();
glm::mat4 transformation = viewProjectionMatrix * modelMatrix; glm::mat4 transformation = viewProjectionMatrix * modelMatrix;
@ -249,6 +257,8 @@ void drawObjectPBR(Core::RenderContext& context, glm::mat4 modelMatrix, GLuint a
Core::SetActiveTexture(metallic, "metallicMap", program, 4); Core::SetActiveTexture(metallic, "metallicMap", program, 4);
Core::DrawContext(context); Core::DrawContext(context);
glUseProgram(0);
} }
void drawObjectSun(Core::RenderContext& context, glm::mat4 modelMatrix) { void drawObjectSun(Core::RenderContext& context, glm::mat4 modelMatrix) {

Binary file not shown.

After

Width:  |  Height:  |  Size: 140 KiB