diff --git a/CMakeLists.txt b/CMakeLists.txt index 7cb22ef..ecadb36 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.24.2) project(Hohmann VERSION 1.0) -set(SOURCES src/main.cpp) +file(GLOB SOURCES src/*.cpp headers/*.hpp) add_executable(Hohmann ${SOURCES}) # GLFW submodule diff --git a/headers/shaders.hpp b/headers/shaders.hpp new file mode 100644 index 0000000..1dfc1fa --- /dev/null +++ b/headers/shaders.hpp @@ -0,0 +1,22 @@ +#include +#include +#include + +namespace Shaders { + std::string loadSourceFromFile(const char* file); + + unsigned int compile(GLenum shadertype, const char* shaderSource); + unsigned int compileFromFile(GLenum shadertype, const char* file); + unsigned int createProgram(std::vector shaders); + + class Shader { + public: + unsigned int id; + Shader(const char* vertPath, const char* fragPath); + + void use(); + void setBool(const std::string &name, bool val) const; + void setInt(const std::string &name, int val) const; + void setFloat(const std::string &name, float val) const; + }; +} diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl index e9478f5..af7e1c7 100644 --- a/shaders/fragment.glsl +++ b/shaders/fragment.glsl @@ -1,8 +1,7 @@ #version 330 core out vec4 FragColor; - -uniform vec4 inputColor; +in vec4 vertexColor; void main() { - FragColor = inputColor; + FragColor = vertexColor; } diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl index 27a18e0..ad2e13e 100644 --- a/shaders/vertex.glsl +++ b/shaders/vertex.glsl @@ -1,9 +1,10 @@ #version 330 core layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aColor; out vec4 vertexColor; void main() { gl_Position = vec4(aPos, 1.0); - vertexColor = vec4(1.0f, 1.0f, 1.0f, 0.0f); + vertexColor = vec4(aColor, 1.0); } diff --git a/src/main.cpp b/src/main.cpp index ec09097..d9cac04 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,7 @@ #include #include -#include "shaders.cpp" +#include "../headers/shaders.hpp" #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 @@ -13,10 +13,10 @@ #define FRAG_SHADER_SRC_FILE "shaders/fragment.glsl" float verts[] = { - 0.5f, 0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - -0.5f, -0.5f, 0.0f, - -0.5f, 0.5f, 0.0f + 0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 0.0f, + -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f }; unsigned int indices[] = { 0, 1, 3, @@ -36,20 +36,11 @@ void processInput(GLFWwindow *win) { struct RenderObj { unsigned int EBO; - unsigned int VBO; - unsigned int VAO; - unsigned int shaderProg; + Shaders::Shader shader; }; RenderObj preRenderCallback() { - // Compile a bunch of shaders etc - unsigned int vertShader = Shader::compileFromFile(GL_VERTEX_SHADER, VERT_SHADER_SRC_FILE); - unsigned int fragShader = Shader::compileFromFile(GL_FRAGMENT_SHADER, FRAG_SHADER_SRC_FILE); - unsigned int shaders[2] = {vertShader, fragShader}; - - // Create a shader program & link the shaders etc - unsigned int shaderProg; - shaderProg = Shader::createProgram( std::vector (std::begin(shaders), std::end(shaders)) ); + Shaders::Shader shader(VERT_SHADER_SRC_FILE, FRAG_SHADER_SRC_FILE); // Vertex buffer object unsigned int VBO; @@ -66,16 +57,21 @@ RenderObj preRenderCallback() { //glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW); // Set attrib pointers - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + // Pos + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); + // Color + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3*sizeof(float))); + glEnableVertexAttribArray(1); + unsigned int EBO; glGenBuffers(1, &EBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - return RenderObj {EBO, VBO, VAO, shaderProg}; + return RenderObj {EBO, shader}; } void renderCallback(RenderObj ro) { @@ -84,9 +80,9 @@ void renderCallback(RenderObj ro) { float time = glfwGetTime(); float gVal = (sin(time) / 1.5f) + 0.5f; - int vertColLocation = glGetUniformLocation(ro.shaderProg, "inputColor"); + int vertColLocation = glGetUniformLocation(ro.shader.id, "inputColor"); - glUseProgram(ro.shaderProg); + ro.shader.use(); glUniform4f(vertColLocation, gVal, gVal, gVal, 1.0f); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ro.EBO); diff --git a/src/shaders.cpp b/src/shaders.cpp index 7be0e55..8978a88 100644 --- a/src/shaders.cpp +++ b/src/shaders.cpp @@ -1,27 +1,30 @@ #include #include +#include #include #include #include -namespace Shader { - std::string loadSourceFromFile(const char* file) { - std::string content; - std::ifstream fh(file, std::ios::in); +#include "../headers/shaders.hpp" - if(!fh.is_open()) { - printf("Unable to read file \"%s\"!", file); - return ""; - } +namespace Shaders { + std::string loadSourceFromFile(const char* fp) { + std::string src; + std::ifstream fh; - std::string line = ""; - while(!fh.eof()) { - std::getline(fh, line); - content.append(line + "\n"); - } + fh.exceptions(std::ifstream::failbit | std::ifstream::badbit); + try { + fh.open(fp); + std::stringstream sstr; + sstr << fh.rdbuf(); + fh.close(); - fh.close(); - return content.c_str(); + src = sstr.str(); + return src; + } catch (std::ifstream::failure err) { + printf("[ERROR] Unable to read file \"%s\\n", fp); + exit(1); + } } unsigned int compile(GLenum shadertype, const char* shaderSource) { @@ -72,4 +75,27 @@ namespace Shader { return shaderProg; } + + Shader::Shader(const char* vertPath, const char* fragPath) { + unsigned int vertShader = compileFromFile(GL_VERTEX_SHADER, vertPath); + unsigned int fragShader = compileFromFile(GL_FRAGMENT_SHADER, fragPath); + unsigned int shaders[2] = {vertShader, fragShader}; + + // Shader program ID + id = Shaders::createProgram( std::vector (std::begin(shaders), std::end(shaders)) ); + } + + void Shader::use() { glUseProgram(id); } + + void Shader::setInt(const std::string &name, int val) const { + glUniform1i(glGetUniformLocation(id, name.c_str()), val); + } + + void Shader::setFloat(const std::string &name, float val) const { + glUniform1f(glGetUniformLocation(id, name.c_str()), val); + } + + void Shader::setBool(const std::string &name, bool val) const { + glUniform1i(glGetUniformLocation(id, name.c_str()), (int)val) ; + } } diff --git a/src/shaders.hpp b/src/shaders.hpp new file mode 100644 index 0000000..344098f --- /dev/null +++ b/src/shaders.hpp @@ -0,0 +1,25 @@ +#ifndef SHADERS_H +#define SHADERS_H +#include +#include +#include + +namespace Shaders { + std::string loadSourceFromFile(const char* file); + + unsigned int compile(GLenum shadertype, const char* shaderSource); + unsigned int compileFromFile(GLenum shadertype, const char* file); + unsigned int createProgram(std::vector shaders); + + class Shader { + public: + unsigned int id; + Shader(const char* vertPath, const char* fragPath); + + void use(); + void setBool(const std::string &name, bool val) const; + void setInt(const std::string &name, int val) const; + void setFloat(const std::string &name, float val) const; + }; +} +#endif