From eae1ae479068767c2de789c3b1c5ee99eb847631 Mon Sep 17 00:00:00 2001 From: TheRedShip Date: Mon, 14 Oct 2024 01:43:29 +0200 Subject: [PATCH] ~ | First triangle --- Makefile | 4 ++-- includes/Shader.hpp | 2 +- shaders/frag.glsl | 4 ++-- srcs/RT.cpp | 30 ++++++++++++++++++++++++--- srcs/Shader.cpp | 50 ++++++++++++++++++++++----------------------- 5 files changed, 57 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index aed801c..0322413 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ SRCS_DIR := srcs OBJS_DIR := .objs ALL_SRCS := RT.cpp gl.cpp \ - Window.cpp + Window.cpp Shader.cpp \ SRCS := $(ALL_SRCS:%=$(SRCS_DIR)/%) @@ -38,7 +38,7 @@ SRCS := $(ALL_SRCS:%=$(SRCS_DIR)/%) OBJS := $(addprefix $(OBJS_DIR)/, $(SRCS:%.cpp=%.o)) -CC := g++ +CC := g++ -Wextra -Wall -Werror IFLAGS := -Ofast -I./includes -L./lib -lglfw3 -lopengl32 -lgdi32 -lcglm diff --git a/includes/Shader.hpp b/includes/Shader.hpp index f3a0c6e..643bd52 100644 --- a/includes/Shader.hpp +++ b/includes/Shader.hpp @@ -18,7 +18,7 @@ class Shader { public: - Shader(char *vertexPath, char *fragmentPath); + Shader(std::string vertexPath, std::string fragmentPath); Shader(Shader const &src); ~Shader(void); diff --git a/shaders/frag.glsl b/shaders/frag.glsl index 002dd51..56f44ac 100644 --- a/shaders/frag.glsl +++ b/shaders/frag.glsl @@ -3,5 +3,5 @@ out vec4 FragColor; void main() { - FragColor = vec4(1.0, 0., 0., 0.); -} \ No newline at end of file + FragColor = vec4(1.0, 0., 0., 1.); +} \ No newline at end of file diff --git a/srcs/RT.cpp b/srcs/RT.cpp index 73f0d0e..7a47ca9 100644 --- a/srcs/RT.cpp +++ b/srcs/RT.cpp @@ -15,18 +15,42 @@ int main(void) { Window window; - Shader shader("shaders/vertex.glsl", "shaders/frag.glsl"); - + shader.attach(); - + RT::Vec2f vertices[6] = { + { -0.5f, -1.0f }, { 1.0f, -1.0f }, { -1.0f, 1.0f }, + { 1.0f, -1.0f }, { 1.0f, 1.0f }, { -1.0f, 1.0f } + }; + + unsigned int VAO, VBO; + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + + // Bind the VAO + glBindVertexArray(VAO); + + // Bind the VBO and upload the vertex data to it + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + // Set vertex attributes + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + + // Unbind the VBO and VAO + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindVertexArray(0); while (!window.shouldClose()) { glClear(GL_COLOR_BUFFER_BIT); glUseProgram(shader.getProgram()); + glBindVertexArray(VAO); + + glDrawArrays(GL_TRIANGLES, 0, 6); window.display(); window.pollEvents(); diff --git a/srcs/Shader.cpp b/srcs/Shader.cpp index fa995a2..407b844 100644 --- a/srcs/Shader.cpp +++ b/srcs/Shader.cpp @@ -37,23 +37,10 @@ char* load_file(char const* path) return buffer; } -void Shader::checkCompileErrors(GLuint shader) +Shader::Shader(std::string vertexPath, std::string fragmentPath) { - GLint success; - GLchar infoLog[512]; - - glGetShaderiv(shader, GL_COMPILE_STATUS, &success); - if (!success) - { - glGetShaderInfoLog(shader, 512, NULL, infoLog); - std::cout << "ERROR::SHADER::COMPILATION_FAILED\n" << infoLog << std::endl; - } -} - -Shader::Shader(char *vertexPath, char *fragmentPath) -{ - const char *vertexCode = load_file(vertexPath); - const char *fragmentCode = load_file(fragmentPath); + const char *vertexCode = load_file(vertexPath.c_str()); + const char *fragmentCode = load_file(fragmentPath.c_str()); _vertex = glCreateShader(GL_VERTEX_SHADER); @@ -70,15 +57,6 @@ Shader::Shader(char *vertexPath, char *fragmentPath) checkCompileErrors(_fragment); } -void Shader::attach(void) -{ - _program = glCreateProgram(); - - glAttachShader(_program, _vertex); - glAttachShader(_program, _fragment); - glLinkProgram(_program); -} - Shader::Shader(Shader const &src) { *this = src; @@ -102,6 +80,28 @@ Shader::~Shader(void) glDeleteProgram(_program); } +void Shader::attach(void) +{ + _program = glCreateProgram(); + + glAttachShader(_program, _vertex); + glAttachShader(_program, _fragment); + glLinkProgram(_program); +} + +void Shader::checkCompileErrors(GLuint shader) +{ + GLint success; + GLchar infoLog[512]; + + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (!success) + { + glGetShaderInfoLog(shader, 512, NULL, infoLog); + std::cout << "ERROR::SHADER::COMPILATION_FAILED\n" << infoLog << std::endl; + } +} + GLuint Shader::getProgram(void) const { return (_program);