grafika_komputerowa/grk/project/SpriteRenderer.cpp

96 lines
3.2 KiB
C++
Raw Normal View History

2024-01-22 00:43:08 +01:00
#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<63> kamery
glm::vec3 cameraPosition = spaceship->cameraPos;
// Tw<54>rz macierz lookAt mi<6D>dzy kwadratem a kamer<65>
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<74>rz macierz widoku-projekcji
glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix();
// Kombinuj macierze transformacji
glm::mat4 transformation = viewProjectionMatrix * billboardModelMatrix * modelMatrix;
// Przeka<6B> 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<68>k<EFBFBD>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<6B>w wierzcho<68>k<EFBFBD>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<70>nij bufor wierzcho<68>k<EFBFBD>w
glBindBuffer(GL_ARRAY_BUFFER, this->VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// Wype<70>nij bufor indeks<6B>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);
}