1
0

Rendering sprites

This commit is contained in:
2022-10-19 17:30:18 +02:00
parent e211fb13c3
commit d1963f08c6
8 changed files with 138 additions and 4 deletions

1
.gitattributes vendored Normal file
View File

@@ -0,0 +1 @@
*.png filter=lfs diff=lfs merge=lfs -text

11
shader.fs Normal file
View File

@@ -0,0 +1,11 @@
#version 330 core
in vec2 TexCoords;
out vec4 color;
uniform sampler2D image;
uniform vec3 spriteColor;
void main()
{
color = vec4(spriteColor, 1.0) * texture(image, TexCoords);
}

13
shader.vs Normal file
View File

@@ -0,0 +1,13 @@
#version 330 core
layout (location=0) in vec4 vertex;
out vec2 TexCoords;
uniform mat4 model;
uniform mat4 projection;
void main()
{
TexCoords = vertex.zw;
gl_Position = projection * model * vec4(vertex.xy, 0.0, 1.0);
}

View File

@@ -1,18 +1,32 @@
#include "game.h"
#include <iostream>
SpriteRenderer* Renderer;
Game::~Game()
{
delete Renderer;
}
Game::Game(unsigned int width, unsigned int height)
: State(GAME_ACTIVE), Keys(), Width(width), Height(height)
{
}
void Game::Init()
{
// Load shaders
ResourceManager::LoadShader("shader.vs", "shader.fs", nullptr, "sprite");
// 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);
ResourceManager::GetShader("sprite").Use().SetInteger("image", 0);
ResourceManager::GetShader("sprite").SetMatrix4("projection", projection);
// set render-specific controls
Renderer = new SpriteRenderer(ResourceManager::GetShader("sprite"));
// load textures
ResourceManager::LoadTexture("textures/awesomeface.png", true, "face");
}
@@ -29,5 +43,5 @@ void Game::Update(float dt)
void Game::Render()
{
Renderer->DrawSprite(ResourceManager::GetTexture("face"), glm::vec2(200.0f, 200.0f), glm::vec2(300.0f, 400.0f), 45.0f, glm::vec3(0.0f, 1.0f,0.0f));
}

View File

@@ -1,4 +1,7 @@
#pragma once
#include "spriterenderer.h"
#include "resourcemanager.h"
// Represent the curreent state of the game
enum GameState {
GAME_ACTIVE,
@@ -10,7 +13,7 @@ class Game {
// game state
GameState State;
bool Keys[1024];
unsigned int width, height;
unsigned int Width, Height;
// constructor / destructor
Game(unsigned int width, unsigned int height);
~Game();

61
src/spriterenderer.cpp Normal file
View File

@@ -0,0 +1,61 @@
#include "spriterenderer.h"
SpriteRenderer::SpriteRenderer(const Shader& shader)
{
this->shader = shader;
this->initRenderData();
}
SpriteRenderer::~SpriteRenderer()
{
glDeleteVertexArrays(1, &this->quadVAO);
}
void SpriteRenderer::initRenderData()
{
unsigned int VBO;
float vertices [] = {
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->quadVAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindVertexArray(this->quadVAO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
void SpriteRenderer::DrawSprite(const Texture2D& texture, glm::vec2 position, glm::vec2 size, float rotate, glm::vec3 color )
{
// prepare transformations
this->shader.Use();
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, glm::vec3(position, 0.0f));
model = glm::translate(model, glm::vec3(0.5f * size.x , 0.5f * size.y, 0.0f));
model = glm::rotate(model, glm::radians(rotate), glm::vec3(0.0f,0.0f, 1.0f));
model = glm::translate(model, glm::vec3(-0.5 * size.x ,-0.5 * size.y, 0.0f));
model = glm::scale(model, glm::vec3(size, 1.0f));
this->shader.SetMatrix4("model", model);
this->shader.SetVector3f("spriteColor", color);
glActiveTexture(GL_TEXTURE0);
texture.Bind();
glBindVertexArray(this->quadVAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
}

28
src/spriterenderer.h Normal file
View File

@@ -0,0 +1,28 @@
#pragma once
#include <glad/glad.h>
#include "glm/glm.hpp"
#include "shader.h"
#include "texture2d.h"
class SpriteRenderer
{
public:
SpriteRenderer(const Shader& shader);
~SpriteRenderer();
void DrawSprite(
const Texture2D& texture,
glm::vec2 position,
glm::vec2 size = glm::vec2(10.0f, 10.0f),
float rotate = 0.0f,
glm::vec3 color = glm::vec3(1.0f)
);
private:
Shader shader;
unsigned int quadVAO;
void initRenderData();
};

BIN
textures/awesomeface.png LFS Normal file

Binary file not shown.