Add sprite renderer and sprite shaders

This commit is contained in:
s473577 2024-01-22 00:43:08 +01:00 committed by Qumpell
parent 449a49102b
commit 6b60c55af7
7 changed files with 182 additions and 0 deletions

View File

@ -0,0 +1,96 @@
#include "SpriteRenderer.h"
Core::SpriteRenderer::SpriteRenderer()
{
this->initRenderData();
}
Core::SpriteRenderer::~SpriteRenderer()
{
glDeleteVertexArrays(1, &this ->VAO);
glDeleteBuffers(1, &this->VBO);
glDeleteBuffers(1, &this->EBO);
}
void Core::SpriteRenderer::DrawSprite(GLuint spriteTexture, const glm::mat4 modelMatrix, GLuint program) {
Spaceship* spaceship = Spaceship::getInstance();
// Pobierz pozycjê kamery
glm::vec3 cameraPosition = spaceship->cameraPos;
// Twórz macierz lookAt miêdzy kwadratem a kamer¹
glm::mat4 billboardModelMatrix = glm::lookAt(glm::vec3(modelMatrix[3]), cameraPosition, glm::vec3(0.0f, 1.0f, 0.0f));
// Reset the rotation part of the matrix to make it face the camera
//billboardModelMatrix[0][0] = 1.0f;
//billboardModelMatrix[0][1] = 0.0f;
//billboardModelMatrix[0][2] = 0.0f;
billboardModelMatrix[1][0] = 0.0f;
billboardModelMatrix[1][1] = 1.0f;
billboardModelMatrix[1][2] = 0.0f;
billboardModelMatrix[2][0] = 0.0f;
billboardModelMatrix[2][1] = 0.0f;
billboardModelMatrix[2][2] = 1.0f;
// Utwórz macierz widoku-projekcji
glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix();
// Kombinuj macierze transformacji
glm::mat4 transformation = viewProjectionMatrix * billboardModelMatrix * modelMatrix;
// Przeka¿ macierze do shadera
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, glm::value_ptr(transformation));
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, glm::value_ptr(billboardModelMatrix));
Core::SetActiveTexture(spriteTexture, "colorTexture", program, 0);
glBindVertexArray(this->VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
void Core::SpriteRenderer::initRenderData()
{
// Definicja wierzcho³ków kwadratu
float vertices[] = {
// Postions //colors // texture coordiantes
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Left down
-0.5f, 0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,1.0f, // Left up
0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,// Right up
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,0.0f// Right down
};
// Definicja indeksów wierzcho³ków dla kwadratu
unsigned int indices[] = {
0, 2, 1, // Upper triangle
0, 3, 2 // Lower triangle
};
glGenVertexArrays(1, &this->VAO);
glGenBuffers(1, &this->VBO);
glGenBuffers(1, &this->EBO);
glBindVertexArray(this->VAO);
// Wype³nij bufor wierzcho³ków
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Wype³nij bufor indeksów
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(1);
// Konfiguracja atrybutu koordynatow kolorow
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(2);
// Konfiguracja atrybutu koordynatow tekstury
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glBindVertexArray(0);
}

View File

@ -0,0 +1,27 @@
#include "src/Render_Utils.h"
#include "src/Texture.h"
#include "Spaceship.h"
#include "./Enemy.h"
#pragma once
namespace Core {
class SpriteRenderer
{
public:
SpriteRenderer();
~SpriteRenderer();
void DrawSprite(GLuint spriteTexture, const glm::mat4 modelMatrix, GLuint program);
private:
unsigned int VAO;
unsigned int VBO;
unsigned int EBO;
// Initializes and configures the quad's buffer and vertex attributes
void initRenderData();
};
}

View File

@ -13,6 +13,7 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="GameObject.cpp" /> <ClCompile Include="GameObject.cpp" />
<ClCompile Include="Planet.cpp" /> <ClCompile Include="Planet.cpp" />
<ClCompile Include="SpriteRenderer.cpp" />
<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" />
@ -26,10 +27,12 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="Bullet.h" /> <ClInclude Include="Bullet.h" />
<ClInclude Include="Enemy.h" />
<ClInclude Include="GameObject.h" /> <ClInclude Include="GameObject.h" />
<ClInclude Include="GameUtils.h" /> <ClInclude Include="GameUtils.h" />
<ClInclude Include="Planet.h" /> <ClInclude Include="Planet.h" />
<ClInclude Include="Spaceship.h" /> <ClInclude Include="Spaceship.h" />
<ClInclude Include="SpriteRenderer.h" />
<ClInclude Include="src\Camera.h" /> <ClInclude Include="src\Camera.h" />
<ClInclude Include="src\ex_9_1.hpp" /> <ClInclude Include="src\ex_9_1.hpp" />
<ClInclude Include="src\objload.h" /> <ClInclude Include="src\objload.h" />
@ -53,6 +56,8 @@
<None Include="shaders\shader_8_sun.vert" /> <None Include="shaders\shader_8_sun.vert" />
<None Include="shaders\shader_skybox.frag" /> <None Include="shaders\shader_skybox.frag" />
<None Include="shaders\shader_skybox.vert" /> <None Include="shaders\shader_skybox.vert" />
<None Include="shaders\shader_sprite.frag" />
<None Include="shaders\shader_sprite.vert" />
<None Include="shaders\test.frag" /> <None Include="shaders\test.frag" />
<None Include="shaders\test.vert" /> <None Include="shaders\test.vert" />
</ItemGroup> </ItemGroup>

View File

@ -57,6 +57,9 @@
<ClCompile Include="Planet.cpp"> <ClCompile Include="Planet.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="SpriteRenderer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\objload.h"> <ClInclude Include="src\objload.h">
@ -113,6 +116,12 @@
<ClInclude Include="Bullet.h"> <ClInclude Include="Bullet.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="Enemy.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="SpriteRenderer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="shaders\shader_8_sun.vert"> <None Include="shaders\shader_8_sun.vert">
@ -139,5 +148,13 @@
<None Include="shaders\shader_skybox.vert"> <None Include="shaders\shader_skybox.vert">
<Filter>Shader Files</Filter> <Filter>Shader Files</Filter>
</None> </None>
<None Include="fireball.frag" />
<None Include="fireball.vert" />
<None Include="shaders\shader_sprite.frag">
<Filter>Shader Files</Filter>
</None>
<None Include="shaders\shader_sprite.vert">
<Filter>Shader Files</Filter>
</None>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -0,0 +1,16 @@
#version 430 core
uniform sampler2D colorTexture;
in vec3 worldPos;
in vec2 texCoord;
in vec3 color;
out vec4 outColor;
void main()
{
outColor = texture(colorTexture, texCoord);
outColor *= vec4(color, 1.0);
}

View File

@ -0,0 +1,21 @@
#version 430 core
layout(location = 0) in vec3 vertexPosition;
layout(location = 1) in vec3 aColor;
layout(location = 2) in vec2 vertexTexCoord;
uniform mat4 transformation;
uniform mat4 modelMatrix;
out vec3 worldPos;
out vec2 texCoord;
out vec3 color;
void main()
{
worldPos = (modelMatrix* vec4(vertexPosition,1)).xyz;
gl_Position = transformation * vec4(worldPos, 1.0);
texCoord = vertexTexCoord;
texCoord.y = 1.0 - texCoord.y; // so that it is turned correctly
color = aColor;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB