From 58cca1db34f96fe32e58cde9100202c3972435ca Mon Sep 17 00:00:00 2001 From: Nigel Date: Fri, 11 Feb 2022 22:24:15 +0100 Subject: [PATCH] Shader exercises complete! --- Makefile | 2 +- shader.fs | 11 ++++ shader.vs | 15 ++++++ src/main.cpp | 93 +++++++------------------------- src/shader.h | 133 ++++++++++++++++++++++++++++++++++++++++++++++ src/vertex.shader | 7 --- 6 files changed, 180 insertions(+), 81 deletions(-) create mode 100644 shader.fs create mode 100644 shader.vs create mode 100644 src/shader.h delete mode 100644 src/vertex.shader diff --git a/Makefile b/Makefile index f04486f..b5144f4 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CFLAGS = -std=c++17 -O2 LDFLAGS = -lglfw3 -lGL -ldl -lpthread -lX11 -lXxf86vm -lXrandr -lXi OUT_NAME = LearnOpenGL -build: src/main.cpp +build: src/main.cpp src/shader.h mkdir -p build/ $(CC) $(CFLAGS) -Ilib/GLAD/include -Ilib/glfw/include -o build/$(OUT_NAME) lib/GLAD/src/glad.c src/main.cpp $(LDFLAGS) diff --git a/shader.fs b/shader.fs new file mode 100644 index 0000000..34d5487 --- /dev/null +++ b/shader.fs @@ -0,0 +1,11 @@ +#version 460 core +out vec4 FragColor; + +in vec4 ourPosition; + +in vec3 ourColor; + +void main() +{ + FragColor = ourPosition;// vec4(ourColor, 1.0f); +} \ No newline at end of file diff --git a/shader.vs b/shader.vs new file mode 100644 index 0000000..5e2926d --- /dev/null +++ b/shader.vs @@ -0,0 +1,15 @@ +#version 460 core +layout (location=0) in vec3 aPos; +layout (location=1) in vec3 aColor; + +uniform float HorizontalOffset; + +out vec3 ourColor; +out vec4 ourPosition; + +void main(){ + + gl_Position = vec4(-aPos.x + HorizontalOffset, aPos.yz , 1.0); + ourColor = aColor; + ourPosition = gl_Position; +} diff --git a/src/main.cpp b/src/main.cpp index 7eda75d..218bbdb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,11 @@ #include +#include #include #include +#include "shader.h" + void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0,0, width, height); @@ -41,82 +44,20 @@ int main() { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - // TODO: Load from disk - const char* vertextShaderSource = - "#version 460 core\n" - "layout (location=0) in vec3 aPos;\n" - "void main(){\n" - "gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" - "}\n"; - - unsigned int vertexShader; - vertexShader = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertexShader, 1, &vertextShaderSource, NULL); - glCompileShader(vertexShader); - - int success; - char infolog[512]; - glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); - - if(!success){ - glGetShaderInfoLog(vertexShader, 512, NULL, infolog); - printf ( "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n %s", infolog); - - } - - - //TODO: load from disk - const char* fragmentShaderSource = - "#version 330 core\n" - "out vec4 FragColor;\n" - "void main(){\n" - "FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" - "}\n"; - - unsigned int fragmentShader; - fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); - glCompileShader(fragmentShader); - - glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); - - if(!success){ - glGetShaderInfoLog(fragmentShader, 512 ,NULL,infolog); - printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n %s", infolog); - } - - - - unsigned int shaderProgram; - shaderProgram = glCreateProgram(); - - glAttachShader(shaderProgram, vertexShader); - glAttachShader(shaderProgram, fragmentShader); - glLinkProgram(shaderProgram); - - glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); - if(!success) { - glGetProgramInfoLog(shaderProgram, 512, NULL, infolog); - printf("ERROR::SHADER_PROGRAM::LINKING_FAILED\n %s", infolog); - } - - - glDeleteShader(vertexShader); - glDeleteShader(fragmentShader); + Shader shader("shader.vs", "shader.fs"); float vertices[] = { - 0.5f, 0.5f, 0.0f, // top, right - 0.5f, -0.5f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, // bottom left - -0.5f, 0.5f, 0.0f // top left + 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left + 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top + }; unsigned int indices[] = { - 0, 1,3, // first triangle - 1,2,3 // second triangle + 0, 1,2 // first triangle }; // Vertex buffer objects!! @@ -133,9 +74,12 @@ int main() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - - glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + // Position attribute + glVertexAttribPointer(0,3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); + // Color attribute + glVertexAttribPointer(1,3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3* sizeof(float))); + glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, 0); @@ -150,11 +94,15 @@ int main() { glClear(GL_COLOR_BUFFER_BIT); - glUseProgram(shaderProgram); + + shader.use(); + shader.setFloat("HorizontalOffset", .3); + glBindVertexArray(VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glDrawElements(GL_TRIANGLES, 6,GL_UNSIGNED_INT, 0); - glBindVertexArray(0); + glfwSwapBuffers(window); glfwPollEvents(); @@ -162,7 +110,6 @@ int main() { glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); - glDeleteProgram(shaderProgram); glfwTerminate(); diff --git a/src/shader.h b/src/shader.h new file mode 100644 index 0000000..f4ccd83 --- /dev/null +++ b/src/shader.h @@ -0,0 +1,133 @@ +#pragma once +#include + +#include +#include +#include +#include + +class Shader +{ + public: + unsigned int ID;// Program ID + + // Read and build the shader upon construction + Shader(const char* vertexPath, const char* fragmentPath); + + // Activate the shader + void use(); + + void setBool(const std::string &name, bool value)const; + void setInt(const std::string &name, int value)const; + void setFloat(const std::string &name, float value)const; + + +}; + + +Shader::Shader(const char* vertextPath, const char* fragmentPath) +{ + // retrieve the vertex / fragment source code from filepath + std::string vertexCode; + std::string fragmentCode; + std::ifstream vShaderFile; + std::ifstream fShaderFile; + + vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); + fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); + + try{ + vShaderFile.open(vertextPath); + fShaderFile.open(fragmentPath); + + std::stringstream vShaderStream,fShaderStream; + + vShaderStream << vShaderFile.rdbuf(); + fShaderStream << fShaderFile.rdbuf(); + + vShaderFile.close(); + fShaderFile.close(); + + vertexCode = vShaderStream.str(); + + std::cout << "VertexShaderSource:\n\n" << vertexCode << std::endl; + + fragmentCode = fShaderStream.str(); + + std::cout << "FragmentShaderSource:\n\n" << fragmentCode << std::endl; + + } + catch(std::ifstream::failure e){ + std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl << e.what() << std::endl ; + } + + const char* vShaderCode = vertexCode.c_str(); + const char* fShaderCode = fragmentCode.c_str(); + + + // Compile Shaders + unsigned int vertex, fragment; + int success; + char infoLog[512]; + + // vertex shader + vertex = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex, 1, &vShaderCode, NULL); + glCompileShader(vertex); + + // print any compile errors if there are any + glGetShaderiv(vertex,GL_COMPILE_STATUS, &success); + if(!success) + { + glGetShaderInfoLog(vertex, 512, NULL, infoLog); + std::cout << "ERROR::SHADER:::VERTEX::COMPILATION_FAILED" << std::endl << infoLog << std::endl; + } + + // fragment shader + + fragment = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragment, 1, &fShaderCode, NULL); + glCompileShader(fragment); + + glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); + if(!success){ + glGetShaderInfoLog(fragment, 512, NULL, infoLog); + std::cout << "ERROR::SHADER:::VERTEX::COMPILATION_FAILED" << std::endl << infoLog << std::endl; + } + + + // Shader Program + ID = glCreateProgram(); + glAttachShader(ID, vertex); + glAttachShader(ID, fragment); + glLinkProgram(ID); + + glGetProgramiv(ID, GL_LINK_STATUS, &success); + if(!success){ + glGetProgramInfoLog(ID, 512, NULL, infoLog); + std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED" << std::endl << infoLog << std::endl; + } + + // delete shaders as they're now linked into our program and no longer necessary + glDeleteShader(vertex); + glDeleteShader(fragment); + + +} + + +void Shader::use(){ + glUseProgram(ID); +} + +void Shader::setBool(const std::string &name, bool value) const{ + glUniform1i(glGetUniformLocation(ID, name.c_str()), (int) value ); +} + +void Shader::setInt(const std::string &name, int value) const{ + glUniform1i(glGetUniformLocation(ID, name.c_str()), value ); +} + +void Shader::setFloat(const std::string &name, float value) const{ + glUniform1f(glGetUniformLocation(ID, name.c_str()), value ); +} \ No newline at end of file diff --git a/src/vertex.shader b/src/vertex.shader deleted file mode 100644 index 470ba71..0000000 --- a/src/vertex.shader +++ /dev/null @@ -1,7 +0,0 @@ -#version 460 core -layout (location=0) in vec3 aPos; - -void main(){ - gl_Position = vec4(aPos.x, aPos.y, aPos.z 1.0); - -} \ No newline at end of file