Particle
This commit is contained in:
11
particle.fs
Normal file
11
particle.fs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#version 330 core
|
||||||
|
in vec2 TexCoords;
|
||||||
|
in vec4 ParticleColor;
|
||||||
|
out vec4 color;
|
||||||
|
|
||||||
|
uniform sampler2D sprite;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
color = (texture(sprite, TexCoords) * ParticleColor);
|
||||||
|
}
|
||||||
17
particle.vs
Normal file
17
particle.vs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
#version 330 core
|
||||||
|
layout (location = 0) in vec4 vertex;
|
||||||
|
|
||||||
|
out vec2 TexCoords;
|
||||||
|
out vec4 ParticleColor;
|
||||||
|
|
||||||
|
uniform mat4 projection;
|
||||||
|
uniform vec2 offset;
|
||||||
|
uniform vec4 color;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
float scale = 10.0;
|
||||||
|
TexCoords = vertex.zw;
|
||||||
|
ParticleColor = color;
|
||||||
|
gl_Position = projection * vec4((vertex.xy * scale) + offset, 0.0, 1.0);
|
||||||
|
}
|
||||||
11
src/game.cpp
11
src/game.cpp
@@ -1,9 +1,11 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
#include "particleGenerator.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
typedef std::tuple<bool, Direction,glm::vec2> Collision;
|
typedef std::tuple<bool, Direction,glm::vec2> Collision;
|
||||||
|
|
||||||
|
ParticleGenerator* Particles;
|
||||||
SpriteRenderer* Renderer;
|
SpriteRenderer* Renderer;
|
||||||
|
|
||||||
// Initial size of the player paddle
|
// Initial size of the player paddle
|
||||||
@@ -86,6 +88,7 @@ Game::~Game()
|
|||||||
delete Renderer;
|
delete Renderer;
|
||||||
delete player;
|
delete player;
|
||||||
delete ball;
|
delete ball;
|
||||||
|
delete Particles;
|
||||||
}
|
}
|
||||||
|
|
||||||
Game::Game(unsigned int width, unsigned int height)
|
Game::Game(unsigned int width, unsigned int height)
|
||||||
@@ -98,10 +101,12 @@ void Game::Init()
|
|||||||
{
|
{
|
||||||
// Load shaders
|
// Load shaders
|
||||||
ResourceManager::LoadShader("shader.vs", "shader.fs", nullptr, "sprite");
|
ResourceManager::LoadShader("shader.vs", "shader.fs", nullptr, "sprite");
|
||||||
|
ResourceManager::LoadShader("particle.vs", "particle.fs", nullptr, "particle");
|
||||||
// configure shaders
|
// configure shaders
|
||||||
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(this->Width), static_cast<float>(this->Height), 0.0f, -1.0f, 1.0f);
|
glm::mat4 projection = glm::ortho(0.0f, static_cast<float>(this->Width), static_cast<float>(this->Height), 0.0f, -1.0f, 1.0f);
|
||||||
ResourceManager::GetShader("sprite").Use().SetInteger("image", 0);
|
ResourceManager::GetShader("sprite").Use().SetInteger("image", 0);
|
||||||
ResourceManager::GetShader("sprite").SetMatrix4("projection", projection);
|
ResourceManager::GetShader("sprite").SetMatrix4("projection", projection);
|
||||||
|
ResourceManager::GetShader("particle").Use().SetMatrix4("projection", projection);
|
||||||
// set render-specific controls
|
// set render-specific controls
|
||||||
Renderer = new SpriteRenderer(ResourceManager::GetShader("sprite"));
|
Renderer = new SpriteRenderer(ResourceManager::GetShader("sprite"));
|
||||||
// load textures
|
// load textures
|
||||||
@@ -110,6 +115,7 @@ void Game::Init()
|
|||||||
ResourceManager::LoadTexture("textures/block.png", false, "block");
|
ResourceManager::LoadTexture("textures/block.png", false, "block");
|
||||||
ResourceManager::LoadTexture("textures/block_solid.png", false, "block_solid");
|
ResourceManager::LoadTexture("textures/block_solid.png", false, "block_solid");
|
||||||
ResourceManager::LoadTexture("textures/paddle.png", true, "paddle");
|
ResourceManager::LoadTexture("textures/paddle.png", true, "paddle");
|
||||||
|
ResourceManager::LoadTexture("textures/particle.png", true, "particle");
|
||||||
// Load levels
|
// Load levels
|
||||||
GameLevel one; one.Load("levels/one.lvl", this->Width, this->Height / 2);
|
GameLevel one; one.Load("levels/one.lvl", this->Width, this->Height / 2);
|
||||||
GameLevel two; two.Load("levels/two.lvl", this->Width, this->Height / 2);
|
GameLevel two; two.Load("levels/two.lvl", this->Width, this->Height / 2);
|
||||||
@@ -129,6 +135,8 @@ void Game::Init()
|
|||||||
|
|
||||||
ball = new BallObject(ballPos, BALL_RADIUS, INITIAL_BALL_VELOCITY, ResourceManager::GetTexture("face"));
|
ball = new BallObject(ballPos, BALL_RADIUS, INITIAL_BALL_VELOCITY, ResourceManager::GetTexture("face"));
|
||||||
|
|
||||||
|
Particles = new ParticleGenerator(ResourceManager::GetShader("particle"), ResourceManager::GetTexture("particle"), 500);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -227,6 +235,8 @@ void Game::Update(float dt)
|
|||||||
ball->Move(dt, this->Width);
|
ball->Move(dt, this->Width);
|
||||||
this->DoCollisions();
|
this->DoCollisions();
|
||||||
|
|
||||||
|
Particles->Update(dt, *ball, 2, glm::vec2(ball->Radius /2.0f));
|
||||||
|
|
||||||
if(ball->Position.y >= this->Height)
|
if(ball->Position.y >= this->Height)
|
||||||
{
|
{
|
||||||
this->ResetLevel();
|
this->ResetLevel();
|
||||||
@@ -246,6 +256,7 @@ void Game::Render()
|
|||||||
this->Levels[this->Level].Draw(*Renderer);
|
this->Levels[this->Level].Draw(*Renderer);
|
||||||
|
|
||||||
player->Draw(*Renderer);
|
player->Draw(*Renderer);
|
||||||
|
Particles->Draw();
|
||||||
ball->Draw(*Renderer);
|
ball->Draw(*Renderer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
src/particle.h
Normal file
11
src/particle.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
|
||||||
|
struct Particle {
|
||||||
|
glm::vec2 Position, Velocity;
|
||||||
|
glm::vec4 Color;
|
||||||
|
float Life;
|
||||||
|
|
||||||
|
Particle(): Position(0.0f), Velocity(0.0f), Color(1.0f), Life(0.0f){}
|
||||||
|
};
|
||||||
111
src/particleGenerator.cpp
Normal file
111
src/particleGenerator.cpp
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
#include "particleGenerator.h"
|
||||||
|
#include <glad/glad.h>
|
||||||
|
ParticleGenerator::ParticleGenerator (Shader shader, Texture2D texture, unsigned int amount)
|
||||||
|
: shader(shader), texture(texture), amount(amount)
|
||||||
|
{
|
||||||
|
this->init();
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParticleGenerator::Update (float dt, GameObject& object, unsigned int newParticles, glm::vec2 offset)
|
||||||
|
{
|
||||||
|
for (unsigned int i = 0; i < newParticles; ++i)
|
||||||
|
{
|
||||||
|
int unusedParticle = this->firstUnusedParticles();
|
||||||
|
this->respawnParticle(this->particles[unusedParticle], object, offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < this->amount; ++i)
|
||||||
|
{
|
||||||
|
Particle& p = this->particles[i];
|
||||||
|
p.Life -= dt;
|
||||||
|
if(p.Life > 0.0f)
|
||||||
|
{
|
||||||
|
p.Position -= p.Velocity * dt;
|
||||||
|
p.Color.a -= dt * 2.5f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParticleGenerator::Draw()
|
||||||
|
{
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
|
||||||
|
this->shader.Use();
|
||||||
|
for (Particle particle : this->particles)
|
||||||
|
{
|
||||||
|
if( particle.Life > 0.0f)
|
||||||
|
{
|
||||||
|
this->shader.SetVector2f("offset", particle.Position);
|
||||||
|
this->shader.SetVector4f("color", particle.Color);
|
||||||
|
this->texture.Bind();
|
||||||
|
glBindVertexArray(this->VAO);
|
||||||
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
glBindVertexArray(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParticleGenerator::init()
|
||||||
|
{
|
||||||
|
unsigned int VBO;
|
||||||
|
float particle_quad[] = {
|
||||||
|
0.0f, 1.0f, 0.0f, 1.0f,
|
||||||
|
1.0f, 0.0f, 1.0f, 0.0f,
|
||||||
|
0.0f, 0.0f, 0.0f, 0.0f,
|
||||||
|
|
||||||
|
0.0f, 1.0f, 0.0f, 1.0f,
|
||||||
|
1.0f, 1.0f, 1.0f, 1.0f,
|
||||||
|
1.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,4,GL_FLOAT,GL_FALSE, 4 * sizeof(float), (void*)0);
|
||||||
|
glBindVertexArray(0);
|
||||||
|
|
||||||
|
for(unsigned int i =0; i < this->amount; ++i)
|
||||||
|
this->particles.push_back(Particle());
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int lastUsedParticle = 0;
|
||||||
|
unsigned int ParticleGenerator::firstUnusedParticles()
|
||||||
|
{
|
||||||
|
for(unsigned int i = lastUsedParticle; i < this->amount; ++i)
|
||||||
|
{
|
||||||
|
if( this->particles[i].Life <= 0.0f)
|
||||||
|
{
|
||||||
|
lastUsedParticle = i;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(unsigned int i = 0; i < lastUsedParticle; ++i)
|
||||||
|
{
|
||||||
|
if(this->particles[i].Life <= 0.0f)
|
||||||
|
{
|
||||||
|
lastUsedParticle = i;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastUsedParticle = 0;
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParticleGenerator::respawnParticle(Particle& particle, GameObject& object , glm::vec2 offset)
|
||||||
|
{
|
||||||
|
float random = ((rand()%100) - 50) / 10.0f;
|
||||||
|
float rColor = 0.5f + ((rand() % 100) / 100.0f);
|
||||||
|
particle.Position = object.Position + random + offset;
|
||||||
|
particle.Color = glm::vec4(rColor, rColor, rColor, 1.0f);
|
||||||
|
particle.Life = 1.0f;
|
||||||
|
particle.Velocity = object.Velocity * 0.1f;
|
||||||
|
}
|
||||||
29
src/particleGenerator.h
Normal file
29
src/particleGenerator.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "particle.h"
|
||||||
|
#include "gameobject.h"
|
||||||
|
#include <list>
|
||||||
|
#include <vector>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
class ParticleGenerator
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ParticleGenerator (Shader shader, Texture2D texture, unsigned int amount);
|
||||||
|
void Update (float dt, GameObject& gameobject, unsigned int newParticles, glm::vec2 offset = glm::vec2(0.0f, 0.0f));
|
||||||
|
|
||||||
|
void Draw();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<Particle> particles;
|
||||||
|
unsigned int amount;
|
||||||
|
Shader shader;
|
||||||
|
Texture2D texture;
|
||||||
|
unsigned int VAO;
|
||||||
|
|
||||||
|
void init();
|
||||||
|
|
||||||
|
unsigned int firstUnusedParticles();
|
||||||
|
|
||||||
|
void respawnParticle(Particle& particle, GameObject& object , glm::vec2 offset = glm::vec2(0.0f, 0.0f));
|
||||||
|
|
||||||
|
};
|
||||||
BIN
textures/particle.png
LFS
Normal file
BIN
textures/particle.png
LFS
Normal file
Binary file not shown.
Reference in New Issue
Block a user