From 5d536e7b717a5fe7c46fc35e817e69c46dd49f53 Mon Sep 17 00:00:00 2001 From: Huseyn Ismayilov Date: Wed, 31 Jul 2024 17:40:16 +0400 Subject: [PATCH] feat: Create shader class --- editor/include/Editor.h | 3 + editor/src/Editor.cpp | 3 +- engine/include/Engine.h | 2 + engine/include/Graphics.h | 5 ++ engine/include/Shader.h | 28 ++++++++ engine/resources/shaders/shader.fs | 9 +++ engine/resources/shaders/shader.vs | 11 +++ engine/src/Engine.cpp | 5 ++ engine/src/Graphics.cpp | 24 +++++-- engine/src/Shader.cpp | 110 +++++++++++++++++++++++++++++ 10 files changed, 195 insertions(+), 5 deletions(-) create mode 100644 engine/include/Shader.h create mode 100644 engine/resources/shaders/shader.fs create mode 100644 engine/resources/shaders/shader.vs create mode 100644 engine/src/Shader.cpp diff --git a/editor/include/Editor.h b/editor/include/Editor.h index 616b100..97ed526 100644 --- a/editor/include/Editor.h +++ b/editor/include/Editor.h @@ -21,6 +21,9 @@ private: Engine engine; UI ui; + int screenWidth = 800; + int screenHeight = 600; + void InitializeWindow(); void InitializeImGui(); }; diff --git a/editor/src/Editor.cpp b/editor/src/Editor.cpp index 0359b8d..4c34563 100644 --- a/editor/src/Editor.cpp +++ b/editor/src/Editor.cpp @@ -48,7 +48,7 @@ void Editor::InitializeWindow() { glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - window = glfwCreateWindow(1280, 720, "Editor", nullptr, nullptr); + window = glfwCreateWindow(screenWidth, screenHeight, "Editor", nullptr, nullptr); if (!window) { std::cerr << "Failed to create GLFW window" << std::endl; glfwTerminate(); @@ -56,4 +56,5 @@ void Editor::InitializeWindow() { } glfwMakeContextCurrent(window); + glfwSetFramebufferSizeCallback(window, Engine::FramebufferSizeCallback); } \ No newline at end of file diff --git a/engine/include/Engine.h b/engine/include/Engine.h index d659740..87befb1 100644 --- a/engine/include/Engine.h +++ b/engine/include/Engine.h @@ -10,6 +10,8 @@ public: Engine(); ~Engine(); + static void FramebufferSizeCallback(GLFWwindow* window, int width, int height); + void Initialize(GLFWwindow* window); void Render(); void Shutdown(); diff --git a/engine/include/Graphics.h b/engine/include/Graphics.h index 278db7e..8a8e2f8 100644 --- a/engine/include/Graphics.h +++ b/engine/include/Graphics.h @@ -2,6 +2,7 @@ #define GRAPHICS_H #include +#include "Shader.h" class Graphics { @@ -9,14 +10,18 @@ public: Graphics(); ~Graphics(); + static void FramebufferSizeCallback(GLFWwindow* window, int width, int height); + void Initialize(GLFWwindow* window); void Render(); void Shutdown(); private: GLFWwindow* window; + Shader shader; unsigned int VAO, VBO; + void LoadShaders(); void SetupBuffers(); }; diff --git a/engine/include/Shader.h b/engine/include/Shader.h new file mode 100644 index 0000000..d7a5cd8 --- /dev/null +++ b/engine/include/Shader.h @@ -0,0 +1,28 @@ +#ifndef SHADER_H +#define SHADER_H + +#include +#include +#include + +class Shader +{ +public: + Shader(); + Shader(const char* vPath, const char* fPath); + ~Shader(); + + void Use(); + + unsigned int GetID(); + + void SetBool(const char* name, bool value) const; + void SetInt(const char* name, int value) const; + void SetFloat(const char* name, float value) const; + void SetMat4(const char* name, glm::mat4 value) const; + +private: + unsigned int ID; +}; + +#endif diff --git a/engine/resources/shaders/shader.fs b/engine/resources/shaders/shader.fs new file mode 100644 index 0000000..9f10653 --- /dev/null +++ b/engine/resources/shaders/shader.fs @@ -0,0 +1,9 @@ +#version 330 core +out vec4 fragColor; + +in vec3 ourColor; + +void main() +{ + fragColor = vec4(ourColor, 1.0f); +} \ No newline at end of file diff --git a/engine/resources/shaders/shader.vs b/engine/resources/shaders/shader.vs new file mode 100644 index 0000000..a69d96b --- /dev/null +++ b/engine/resources/shaders/shader.vs @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aColor; + +out vec3 ourColor; + +void main() +{ + gl_Position = vec4(aPos, 1.0f); + ourColor = aColor; +} \ No newline at end of file diff --git a/engine/src/Engine.cpp b/engine/src/Engine.cpp index 00dd847..3f6534b 100644 --- a/engine/src/Engine.cpp +++ b/engine/src/Engine.cpp @@ -7,6 +7,11 @@ Engine::~Engine() Shutdown(); } +void Engine::FramebufferSizeCallback(GLFWwindow* window, int width, int height) +{ + Graphics::FramebufferSizeCallback(window, width, height); +} + void Engine::Initialize(GLFWwindow* window) { graphics.Initialize(window); diff --git a/engine/src/Graphics.cpp b/engine/src/Graphics.cpp index b80e171..3f62301 100644 --- a/engine/src/Graphics.cpp +++ b/engine/src/Graphics.cpp @@ -1,6 +1,7 @@ #include #include #include "Graphics.h" +#include "Shader.h" Graphics::Graphics() : window(nullptr), VAO(0), VBO(0) {} @@ -18,15 +19,21 @@ void Graphics::Initialize(GLFWwindow* window) return; } + LoadShaders(); SetupBuffers(); } +void Graphics::LoadShaders() +{ + shader = Shader(RESOURCES_PATH"shaders/shader.vs", RESOURCES_PATH"shaders/shader.fs"); +} + void Graphics::SetupBuffers() { float vertices[] = { - 0.0f, 0.5f, 0.0f, - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f + 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 }; glGenVertexArrays(1, &VAO); @@ -37,16 +44,25 @@ void Graphics::SetupBuffers() glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); + glEnableVertexAttribArray(1); + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); } +void Graphics::FramebufferSizeCallback(GLFWwindow* window, int width, int height) +{ + glViewport(0, 0, width, height); +} + void Graphics::Render() { glClear(GL_COLOR_BUFFER_BIT); + shader.Use(); glBindVertexArray(VAO); glDrawArrays(GL_TRIANGLES, 0, 3); } diff --git a/engine/src/Shader.cpp b/engine/src/Shader.cpp new file mode 100644 index 0000000..82a870e --- /dev/null +++ b/engine/src/Shader.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include "Shader.h" + +Shader::Shader(): ID(0){} + +Shader::Shader(const char* vPath, const char* fPath) +{ + std::string vCode; + std::string fCode; + 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(vPath); + fShaderFile.open(fPath); + std::stringstream vShaderStream, fShaderStream; + + vShaderStream << vShaderFile.rdbuf(); + fShaderStream << fShaderFile.rdbuf(); + + vShaderFile.close(); + fShaderFile.close(); + + vCode = vShaderStream.str(); + fCode = fShaderStream.str(); + } + catch(std::ifstream::failure& e) + { + std::cerr << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ::" << e.what() << std::endl; + std::cerr << "VPATH::" << vPath << std::endl << "FPATH::" << fPath << std::endl; + } + const char* vShaderCode = vCode.c_str(); + const char* fShaderCode = fCode.c_str(); + + unsigned int vertex, fragment; + int success; + char infoLog[512]; + + vertex = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex, 1, &vShaderCode, nullptr); + glCompileShader(vertex); + + glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); + if(!success) + { + glGetShaderInfoLog(vertex, 512, nullptr, infoLog); + std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl; + }; + + fragment = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragment, 1, &fShaderCode, nullptr); + glCompileShader(fragment); + + glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); + if(!success) + { + glGetShaderInfoLog(fragment, 512, nullptr, infoLog); + std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; + }; + + ID = glCreateProgram(); + glAttachShader(ID, vertex); + glAttachShader(ID, fragment); + glLinkProgram(ID); + + glGetProgramiv(ID, GL_LINK_STATUS, &success); + if(!success) + { + glGetProgramInfoLog(ID, 512, nullptr, infoLog); + std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; + } + + glDeleteShader(vertex); + glDeleteShader(fragment); +} + +Shader::~Shader(){}; + +void Shader::Use() +{ + glUseProgram(ID); +} + +unsigned int Shader::GetID() +{ + return ID; +} + +void Shader::SetBool(const char* name, bool value) const +{ + glUniform1i(glGetUniformLocation(ID, name), (int)value); +} +void Shader::SetInt(const char* name, int value) const +{ + glUniform1i(glGetUniformLocation(ID, name), value); +} +void Shader::SetFloat(const char* name, float value) const +{ + glUniform1f(glGetUniformLocation(ID, name), value); +} +void Shader::SetMat4(const char* name, glm::mat4 value) const +{ + glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, glm::value_ptr(value)); +}