From d493d60fe5f612822a1746074b0e59be3bcd7f5c Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Wed, 25 Oct 2023 19:46:44 -0500 Subject: [PATCH] Consolidated all shader loading inside its own util. --- OpenGLTemplate/fragShader.glsl | 4 +- OpenGLTemplate/main.cpp | 80 +++++++++------------------------- OpenGLTemplate/utils.h | 58 ++++++++++++++++++++++-- OpenGLTemplate/vertShader.glsl | 13 +++++- 4 files changed, 89 insertions(+), 66 deletions(-) diff --git a/OpenGLTemplate/fragShader.glsl b/OpenGLTemplate/fragShader.glsl index c5a70ca..1da04d5 100644 --- a/OpenGLTemplate/fragShader.glsl +++ b/OpenGLTemplate/fragShader.glsl @@ -3,9 +3,9 @@ out vec4 color; void main(void) { color=vec4( - (gl_FragCoord.x - 300) / 5.f, + (gl_FragCoord.x) / 600.f, 0.0, - 5 - (gl_FragCoord.x - 300) / 5.f, + 1-(gl_FragCoord.x) / 600.f, 1.0 ); } \ No newline at end of file diff --git a/OpenGLTemplate/main.cpp b/OpenGLTemplate/main.cpp index 70448bc..b28e99c 100644 --- a/OpenGLTemplate/main.cpp +++ b/OpenGLTemplate/main.cpp @@ -1,85 +1,45 @@ #include #include -#include #include "utils.h" -#include "ShaderError.h" #define numVAOs 1 GLuint renderingProgram; GLuint vao[numVAOs]; -GLuint createShaderProgram(){ - GLint vertCompiled; - GLint fragCompiled; - GLint linked; - // catch errors while compiling shaders - - GLuint vShader=glCreateShader(GL_VERTEX_SHADER); - GLuint fShader=glCreateShader(GL_FRAGMENT_SHADER); - - std::string vertShaderStr=utils::readShaderSource("vertShader.glsl"); - std::string fragShaderStr=utils::readShaderSource("fragShader.glsl"); - - const char*vertShaderSrc=vertShaderStr.c_str(); - const char*fragShaderSrc=fragShaderStr.c_str(); - - glShaderSource(vShader,1,&vertShaderSrc,NULL); - glShaderSource(fShader,1,&fragShaderSrc,NULL); - - glCompileShader(vShader); - ErrorCheck::checkOpenGLError(); - glGetShaderiv(vShader,GL_COMPILE_STATUS,&vertCompiled); - if(vertCompiled!=1){ - std::cout<<"Vertex Compilation Failed!"<1||x<-1){inc*=-1;} + + GLuint offsetLoc=glGetUniformLocation(renderingProgram,"offset"); + glProgramUniform1f(renderingProgram,offsetLoc,x); + + glDrawArrays(GL_TRIANGLES,0,3); } int main(void) { if (!glfwInit()) { exit(EXIT_FAILURE); } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - GLFWwindow* window = glfwCreateWindow(600, 600, "My First OpenGL Program!", NULL, NULL); + GLFWwindow* window = glfwCreateWindow(600, 600, "Hello Triangle!", NULL, NULL); glfwMakeContextCurrent(window); if (glewInit() != GLEW_OK) { exit(EXIT_FAILURE); } glfwSwapInterval(1); diff --git a/OpenGLTemplate/utils.h b/OpenGLTemplate/utils.h index b612af0..c87e4e8 100644 --- a/OpenGLTemplate/utils.h +++ b/OpenGLTemplate/utils.h @@ -2,9 +2,10 @@ #include #include #include +#include "ShaderError.h" -namespace utils{ - inline std::string readShaderSource(const char *filePath){ +class utils{ + inline static std::string readShaderSource(const char *filePath){ std::string content; std::ifstream fileStream(filePath, std::ios::in); std::string line = ""; @@ -19,4 +20,55 @@ namespace utils{ fileStream.close(); return content; } -} \ No newline at end of file + inline static void CompileShader(GLuint shader,std::string type){ + GLint compiled; + glCompileShader(shader); + ErrorCheck::checkOpenGLError(); + glGetShaderiv(shader,GL_COMPILE_STATUS,&compiled); + if(compiled!=1){ + std::cout<