学习链接:中文---------英语原文
这一节主要讲粒子,还是不较好理解的,类似Unity的粒子系统,但这里是比较简单的。
ParticleGenerator.h:
#pragma once
#include "Shader.h"
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include "Texture2D.h"
#include "GameObject.h"
#include <vector>
struct Particle
{
glm::vec2 position, velocity;
glm::vec4 color;
GLfloat life;
Particle() :position(0.0f), velocity(0.0f), color(1.0f), life(0.0f) {}
};
class ParticleGenerator
{
public:
ParticleGenerator(Shader shader, Texture2D texture, GLuint amount);
void Update(GLfloat dt, GameObject& object, GLuint newParticles, glm::vec2 offset = glm::vec2(0.0f, 0.0f));
void Draw();
private:
std::vector<Particle> particles;
GLuint amount;
Shader shader;
Texture2D texture;
GLuint VAO;
void Init();
GLuint FirstUnusedParticle();
void RespawnParticle(Particle& particle, GameObject& object, glm::vec2 offset = glm::vec2(0.0f, 0.0f));
};
ParticleGenerator.cpp:
#include "ParticleGenerator.h"
ParticleGenerator::ParticleGenerator(Shader shader, Texture2D texture, GLuint amount) : shader(shader), texture(texture), amount(amount)
{
this->Init();
}
void ParticleGenerator::Update(GLfloat dt, GameObject& object, GLuint newParticles, glm::vec2 offset)
{
for (GLuint i = 0; i < newParticles; ++i)
{
int unusedParticle = this->FirstUnusedParticle();
this->RespawnParticle(this->particles[unusedParticle], object, offset);
}
for (GLuint 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.5;
}
}
}
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()
{
GLuint VBO;
GLfloat particleQuad[] =
{
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(particleQuad), particleQuad, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0);
glBindVertexArray(0);
for (GLuint i = 0; i < this->amount; i++)
{
this->particles.push_back(Particle());
}
}
GLuint lastUsedParticle = 0;
GLuint ParticleGenerator::FirstUnusedParticle()
{
for (GLuint i = lastUsedParticle; i < this->amount; i++)
{
if (this->particles[i].life <= 0.0f)
{
lastUsedParticle = i;
return i;
}
}
for (GLuint 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)
{
GLfloat random =((rand() % 100) - 50) / 10.0f;
GLfloat rColor = 0.5 + ((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;
}
Game.cpp:
#include "ResourceManager.h"
#include "Game.h"
#include "SpriteRenderer.h"
#include "BallObject.h"
#include "ParticleGenerator.h"
SpriteRenderer* renderer;
GameObject* player;
BallObject* ball;
ParticleGenerator* particles;
Game::Game(GLuint w, GLuint h) :state(GAME_ACTIVE), keys(), width(w), height(h) {}
Game::~Game()
{
delete renderer;
delete player;
delete ball;
delete particles;
}
void Game::Init()
{
ResourceManager::LoadShader("Breakout/Shaders/Sprite.vs", "Breakout/Shaders/Sprite.fs", nullptr, "sprite");
ResourceManager::LoadShader("Breakout/Shaders/Particle.vs", "Breakout/Shaders/Particle.fs", nullptr, "particle");
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(this->width), static_cast<GLfloat>(this->height), 0.0f, -1.0f, 1.0f);
ResourceManager::GetShader("sprite").Use().SetInteger("image", 0);
ResourceManager::GetShader("sprite").SetMatrix4("projection", projection);
ResourceManager::GetShader("particle").Use().SetInteger("sprite", 0);
ResourceManager::GetShader("particle").SetMatrix4("projection", projection);
ResourceManager::LoadTexture("Breakout/Images/background.jpg", GL_TRUE, "background");
ResourceManager::LoadTexture("Breakout/Images/block.png", GL_FALSE, "block");
ResourceManager::LoadTexture("Breakout/Images/block_solid.png", GL_FALSE, "block_solid");
ResourceManager::LoadTexture("Breakout/paddle.png", true, "paddle");
ResourceManager::LoadTexture("Breakout/face.png", true, "ball");
ResourceManager::LoadTexture("Breakout/particle.png", true, "particle");
renderer = new SpriteRenderer(ResourceManager::GetShader("sprite"));
particles = new ParticleGenerator(ResourceManager::GetShader("particle"), ResourceManager::GetTexture("particle"), 500);
GameLevel one; one.Load("Breakout/Levels/1.lvl", this->width, this->height * 0.5);
GameLevel two; two.Load("Breakout/Levels/2.lvl", this->width, this->height * 0.5);
GameLevel three; three.Load("Breakout/Levels/3.lvl", this->width, this->height * 0.5);
GameLevel four; four.Load("Breakout/Levels/4.lvl", this->width, this->height * 0.5);
this->levels.push_back(one);
this->levels.push_back(two);
this->levels.push_back(three);
this->levels.push_back(four);
this->currentLevel = 0;
glm::vec2 playerPos = glm::vec2(this->width / 2 - PLAYER_SIZE.x / 2, this->height - PLAYER_SIZE.y);
player = new GameObject(playerPos, PLAYER_SIZE, ResourceManager::GetTexture("paddle"));
glm::vec2 ballPos = playerPos + glm::vec2(PLAYER_SIZE.x / 2 - BALL_RADIUS, -BALL_RADIUS * 2);
ball = new BallObject(ballPos, BALL_RADIUS, INITIAL_BALL_VELOCITY, ResourceManager::GetTexture("ball"));
}
void Game::ProcessInput(GLfloat dt)
{
if (this->state == GAME_ACTIVE)
{
GLfloat velocity = PLAYER_VELOCITY * dt;
if (this->keys[GLFW_KEY_A])
{
if (player->position.x >= 0)
{
player->position.x -= velocity;
if (ball->stuck)
ball->position.x -= velocity;
}
}
if (this->keys[GLFW_KEY_D])
{
if (player->position.x <= this->width - player->size.x)
{
player->position.x += velocity;
if (ball->stuck)
ball->position.x += velocity;
}
}
if (this->keys[GLFW_KEY_SPACE])
ball->stuck = false;
}
}
void Game::Update(GLfloat dt)
{
ball->Move(dt, this->width);
this->DoCollisions();
particles->Update(dt, *ball, 2, glm::vec2(ball->radius / 2));
if (ball->position.y >= this->height)
{
this->ResetLevel();
this->ResetPlayer();
}
}
void Game::Render()
{
if (this->state == GAME_ACTIVE)
{
renderer->DrawSprite(ResourceManager::GetTexture("background"), glm::vec2(0, 0), glm::vec2(this->width, this->height), 0.0f);
this->levels[this->currentLevel].Draw(*renderer);
player->Draw(*renderer);
particles->Draw();
ball->Draw(*renderer);
}
}
Direction VectorDirection(glm::vec2 target)
{
glm::vec2 compass[] =
{
glm::vec2(0.0f,1.0f),
glm::vec2(1.0f,0.0f),
glm::vec2(0.0f,-1.0f),
glm::vec2(-1.0f,0.0f),
};
GLfloat max = 0.0f;
GLuint bestMatch = -1;
for (GLuint i = 0; i < 4; i++)
{
GLfloat dot = glm::dot(glm::normalize(target), compass[i]);
if (dot > max)
{
max = dot;
bestMatch = i;
}
}
return (Direction)bestMatch;
}
GLboolean CheckCollision(GameObject& one, GameObject& two)
{
bool collisionX = one.position.x + one.size.x >= two.position.x &&
two.position.x + two.size.x >= one.position.x;
bool collisionY = one.position.y + one.size.y >= two.position.y &&
two.position.y + two.size.y >= one.position.y;
return collisionX && collisionY;
}
Collision CheckCollision(BallObject& one, GameObject& two)
{
glm::vec2 center(one.position + one.radius);
glm::vec2 aabb_half_extents(two.size.x / 2, two.size.y / 2);
glm::vec2 aabb_center(two.position.x + aabb_half_extents.x, two.position.y + aabb_half_extents.y);
glm::vec2 difference = center - aabb_center;
glm::vec2 clamped = glm::clamp(difference, -aabb_half_extents, aabb_half_extents);
glm::vec2 closest = aabb_center + clamped;
difference = closest - center;
if (glm::length(difference) < one.radius)
return std::make_tuple(GL_TRUE, VectorDirection(difference), difference);
else
return std::make_tuple(GL_FALSE, UP, glm::vec2(0, 0));
}
void Game::DoCollisions()
{
for (GameObject& box : this->levels[this->currentLevel].bricks)
{
if (!box.destroyed)
{
Collision collision = CheckCollision(*ball, box);
if (std::get<0>(collision))
{
if (!box.isSoild)
box.destroyed = GL_TRUE;
Direction dir = std::get<1>(collision);
glm::vec2 diff_vector = std::get<2>(collision);
if (dir == LEFT || dir == RIGHT)
{
ball->velocity.x = -ball->velocity.x;
GLfloat penetration = ball->radius - std::abs(diff_vector.x);
if (dir == LEFT)
ball->position.x += penetration;
else
ball->position.x -= penetration;
}
else
{
ball->velocity.y = -ball->velocity.y;
GLfloat penetration = ball->radius - std::abs(diff_vector.y);
if (dir == UP)
ball->position.y -= penetration;
else
ball->position.y += penetration;
}
}
Collision result = CheckCollision(*ball, *player);
if (!ball->stuck && std::get<0>(result))
{
GLfloat centerBoard = player->position.x + player->size.x / 2;
GLfloat distance = (ball->position.x + ball->radius) - centerBoard;
GLfloat percentage = distance / (player->size.x / 2);
GLfloat strength = 2.0f;
glm::vec2 oldVelocity = ball->velocity;
ball->velocity.x = INITIAL_BALL_VELOCITY.x * percentage * strength;
ball->velocity.y = -1 * abs(ball->velocity.y);
ball->velocity = glm::normalize(ball->velocity) * glm::length(oldVelocity);
}
}
}
}
void Game::ResetLevel()
{
if (this->currentLevel == 0)
this->levels[0].Load("Breakout/Levels/1.lvl", this->width, this->height * 0.5f);
else if (this->currentLevel == 1)
this->levels[1].Load("Breakout/Levels/2.lvl", this->width, this->height * 0.5f);
else if (this->currentLevel == 2)
this->levels[1].Load("Breakout/Levels/3.lvl", this->width, this->height * 0.5f);
else if (this->currentLevel == 3)
this->levels[1].Load("Breakout/Levels/4.lvl", this->width, this->height * 0.5f);
}
void Game::ResetPlayer()
{
player->size = PLAYER_SIZE;
player->position = glm::vec2(this->width / 2 - PLAYER_SIZE.x / 2, this->height - PLAYER_SIZE.y);
ball->Reset(player->position + glm::vec2(PLAYER_SIZE.x / 2 - ball->radius, -(BALL_RADIUS * 2)), INITIAL_BALL_VELOCITY);
}
粒子顶点着色器:
#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.0f;
TexCoords = vertex.zw;
ParticleColor = color;
gl_Position = projection * vec4((vertex.xy * scale) + offset, 0.0, 1.0);
}
粒子片元着色器:
#version 330 core
in vec2 TexCoords;
in vec4 ParticleColor;
out vec4 color;
uniform sampler2D sprite;
void main()
{
color = (texture(sprite, TexCoords) * ParticleColor);
}```
|