2024-07-30 16:57:32 +00:00
|
|
|
#include <glad/glad.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include "Graphics.h"
|
2024-07-31 13:40:16 +00:00
|
|
|
#include "Shader.h"
|
2024-07-30 16:57:32 +00:00
|
|
|
|
|
|
|
Graphics::Graphics() : window(nullptr), VAO(0), VBO(0) {}
|
|
|
|
|
|
|
|
Graphics::~Graphics()
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::Initialize(GLFWwindow* window)
|
|
|
|
{
|
|
|
|
this->window = window;
|
|
|
|
|
|
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
|
|
std::cerr << "Failed to initialize GLAD" << std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-31 13:40:16 +00:00
|
|
|
LoadShaders();
|
2024-07-30 16:57:32 +00:00
|
|
|
SetupBuffers();
|
|
|
|
}
|
|
|
|
|
2024-07-31 13:40:16 +00:00
|
|
|
void Graphics::LoadShaders()
|
|
|
|
{
|
|
|
|
shader = Shader(RESOURCES_PATH"shaders/shader.vs", RESOURCES_PATH"shaders/shader.fs");
|
|
|
|
}
|
|
|
|
|
2024-07-30 16:57:32 +00:00
|
|
|
void Graphics::SetupBuffers()
|
|
|
|
{
|
|
|
|
float vertices[] = {
|
2024-07-31 13:40:16 +00:00
|
|
|
0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,
|
|
|
|
-0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
|
|
|
|
0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f
|
2024-07-30 16:57:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
glGenVertexArrays(1, &VAO);
|
|
|
|
glGenBuffers(1, &VBO);
|
|
|
|
|
|
|
|
glBindVertexArray(VAO);
|
|
|
|
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
|
|
|
2024-07-31 13:40:16 +00:00
|
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
|
2024-07-30 16:57:32 +00:00
|
|
|
glEnableVertexAttribArray(0);
|
|
|
|
|
2024-07-31 13:40:16 +00:00
|
|
|
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
|
|
|
|
glEnableVertexAttribArray(1);
|
|
|
|
|
2024-07-30 16:57:32 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
glBindVertexArray(0);
|
|
|
|
}
|
|
|
|
|
2024-07-31 13:40:16 +00:00
|
|
|
void Graphics::FramebufferSizeCallback(GLFWwindow* window, int width, int height)
|
|
|
|
{
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
}
|
|
|
|
|
2024-07-30 16:57:32 +00:00
|
|
|
void Graphics::Render() {
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2024-07-31 13:40:16 +00:00
|
|
|
shader.Use();
|
2024-07-30 16:57:32 +00:00
|
|
|
glBindVertexArray(VAO);
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Graphics::Shutdown()
|
|
|
|
{
|
|
|
|
glDeleteVertexArrays(1, &VAO);
|
|
|
|
glDeleteBuffers(1, &VBO);
|
|
|
|
}
|