diff --git a/grk/project/SpriteRenderer.cpp b/grk/project/SpriteRenderer.cpp new file mode 100644 index 0000000..b91d26e --- /dev/null +++ b/grk/project/SpriteRenderer.cpp @@ -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); +} \ No newline at end of file diff --git a/grk/project/SpriteRenderer.h b/grk/project/SpriteRenderer.h new file mode 100644 index 0000000..e83640a --- /dev/null +++ b/grk/project/SpriteRenderer.h @@ -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(); + + + + }; +} + diff --git a/grk/project/grk-project.vcxproj b/grk/project/grk-project.vcxproj index 16a703f..abb491b 100644 --- a/grk/project/grk-project.vcxproj +++ b/grk/project/grk-project.vcxproj @@ -13,6 +13,7 @@ + @@ -26,10 +27,12 @@ + + @@ -53,6 +56,8 @@ + + diff --git a/grk/project/grk-project.vcxproj.filters b/grk/project/grk-project.vcxproj.filters index c8486a9..8109db6 100644 --- a/grk/project/grk-project.vcxproj.filters +++ b/grk/project/grk-project.vcxproj.filters @@ -57,6 +57,9 @@ Source Files + + Source Files + @@ -113,6 +116,12 @@ Header Files + + Header Files + + + Header Files + @@ -139,5 +148,13 @@ Shader Files + + + + Shader Files + + + Shader Files + \ No newline at end of file diff --git a/grk/project/shaders/shader_sprite.frag b/grk/project/shaders/shader_sprite.frag new file mode 100644 index 0000000..8cf8107 --- /dev/null +++ b/grk/project/shaders/shader_sprite.frag @@ -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); + +} \ No newline at end of file diff --git a/grk/project/shaders/shader_sprite.vert b/grk/project/shaders/shader_sprite.vert new file mode 100644 index 0000000..bb00932 --- /dev/null +++ b/grk/project/shaders/shader_sprite.vert @@ -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; +} \ No newline at end of file diff --git a/grk/project/textures/blinky1.png b/grk/project/textures/blinky1.png new file mode 100644 index 0000000..83ca8e6 Binary files /dev/null and b/grk/project/textures/blinky1.png differ