2024-01-22 00:43:08 +01:00
|
|
|
|
#include "SpriteRenderer.h"
|
2024-01-24 00:35:11 +01:00
|
|
|
|
#include <iostream>
|
2024-01-22 00:43:08 +01:00
|
|
|
|
|
|
|
|
|
Core::SpriteRenderer::SpriteRenderer()
|
|
|
|
|
{
|
|
|
|
|
this->initRenderData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Core::SpriteRenderer::~SpriteRenderer()
|
|
|
|
|
{
|
|
|
|
|
glDeleteVertexArrays(1, &this ->VAO);
|
|
|
|
|
glDeleteBuffers(1, &this->VBO);
|
|
|
|
|
glDeleteBuffers(1, &this->EBO);
|
|
|
|
|
}
|
2024-02-01 23:27:42 +01:00
|
|
|
|
void Core::SpriteRenderer::DrawHUDBar(const glm::vec3 color, const glm::mat4 modelMatrix, const float progress, GLuint program) {
|
|
|
|
|
|
|
|
|
|
Spaceship* spaceship = Spaceship::getInstance();
|
|
|
|
|
|
|
|
|
|
glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix();
|
|
|
|
|
|
|
|
|
|
// Kombinuj macierze transformacji
|
|
|
|
|
glm::mat4 transformation = viewProjectionMatrix * 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(modelMatrix));
|
|
|
|
|
glUniform3fv(glGetUniformLocation(program, "activeColor"), 1, glm::value_ptr(color));
|
|
|
|
|
glUniform1f(glGetUniformLocation(program, "progress"), progress);
|
|
|
|
|
|
|
|
|
|
glBindVertexArray(this->VAO);
|
|
|
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
|
|
|
|
glBindVertexArray(0);
|
|
|
|
|
};
|
2024-02-01 15:11:52 +01:00
|
|
|
|
void Core::SpriteRenderer::DrawSpriteBar(const glm::vec3 color, const glm::mat4 modelMatrix,const float progress, GLuint program) {
|
|
|
|
|
|
|
|
|
|
Spaceship* spaceship = Spaceship::getInstance();
|
|
|
|
|
|
|
|
|
|
// Pobierz pozycj<63> kamery
|
|
|
|
|
glm::vec3 cameraPosition = spaceship->cameraPos;
|
|
|
|
|
|
|
|
|
|
glm::vec3 spritePosition = glm::vec3(modelMatrix[3]);
|
|
|
|
|
|
|
|
|
|
// Oblicz wektor skierowany od sprita do kamery
|
|
|
|
|
glm::vec3 spriteToCamera = glm::normalize(cameraPosition - spritePosition);
|
|
|
|
|
|
|
|
|
|
// Oblicz k<>t rotacji w stopniach wok<6F><6B> osi Y
|
|
|
|
|
float angle = glm::degrees(atan2(spriteToCamera.x, spriteToCamera.z));
|
|
|
|
|
|
|
|
|
|
// Utw<74>rz macierz rotacji wok<6F> osi Y
|
|
|
|
|
glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), glm::radians(angle), glm::vec3(0.0f, 1.0f, 0.0f));
|
|
|
|
|
// Utw<74>rz macierz widoku-projekcji
|
|
|
|
|
glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix();
|
|
|
|
|
|
|
|
|
|
// Kombinuj macierze transformacji
|
|
|
|
|
glm::mat4 transformation = viewProjectionMatrix * modelMatrix * rotationMatrix;
|
|
|
|
|
|
|
|
|
|
// 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(modelMatrix));
|
|
|
|
|
glUniform3fv(glGetUniformLocation(program, "activeColor"), 1, glm::value_ptr(color));
|
|
|
|
|
glUniform1f(glGetUniformLocation(program, "progress"), progress);
|
|
|
|
|
|
|
|
|
|
glBindVertexArray(this->VAO);
|
|
|
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
|
|
|
|
glBindVertexArray(0);
|
|
|
|
|
};
|
2024-01-22 00:43:08 +01:00
|
|
|
|
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;
|
|
|
|
|
|
2024-01-24 00:35:11 +01:00
|
|
|
|
glm::vec3 spritePosition = glm::vec3(modelMatrix[3]);
|
2024-01-22 00:43:08 +01:00
|
|
|
|
|
2024-01-24 00:35:11 +01:00
|
|
|
|
// Oblicz wektor skierowany od sprita do kamery
|
|
|
|
|
glm::vec3 spriteToCamera = glm::normalize(cameraPosition - spritePosition);
|
2024-01-22 00:43:08 +01:00
|
|
|
|
|
2024-01-24 00:35:11 +01:00
|
|
|
|
// Oblicz k<>t rotacji w stopniach wok<6F><6B> osi Y
|
|
|
|
|
float angle = glm::degrees(atan2(spriteToCamera.x, spriteToCamera.z));
|
|
|
|
|
|
|
|
|
|
// Utw<74>rz macierz rotacji wok<6F> osi Y
|
|
|
|
|
glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), glm::radians(angle), glm::vec3(0.0f, 1.0f, 0.0f));
|
2024-01-22 00:43:08 +01:00
|
|
|
|
// Utw<74>rz macierz widoku-projekcji
|
|
|
|
|
glm::mat4 viewProjectionMatrix = Core::createPerspectiveMatrix() * spaceship->createCameraMatrix();
|
|
|
|
|
|
|
|
|
|
// Kombinuj macierze transformacji
|
2024-01-24 00:35:11 +01:00
|
|
|
|
glm::mat4 transformation = viewProjectionMatrix * modelMatrix * rotationMatrix;
|
2024-01-22 00:43:08 +01:00
|
|
|
|
|
|
|
|
|
// Przeka<6B> macierze do shadera
|
|
|
|
|
glUniformMatrix4fv(glGetUniformLocation(program, "transformation"), 1, GL_FALSE, glm::value_ptr(transformation));
|
2024-01-24 00:35:11 +01:00
|
|
|
|
glUniformMatrix4fv(glGetUniformLocation(program, "modelMatrix"), 1, GL_FALSE, glm::value_ptr(modelMatrix));
|
2024-01-22 00:43:08 +01:00
|
|
|
|
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);
|
|
|
|
|
}
|