From 168b93694508600f0f6af87f1a441ed630fec319 Mon Sep 17 00:00:00 2001 From: Nigel Date: Fri, 22 Apr 2022 22:37:38 +0200 Subject: [PATCH] Created a basic shader object and window object Using GLAD to load OpenGL/Vulkan extensions --- MyGraphicsEngine/Shader.cpp | 94 +++++++++++++++++++ .../include/MyGraphicsEngine/Camera.h | 13 +++ .../include/MyGraphicsEngine/Graphics.h | 9 -- .../include/MyGraphicsEngine/Shader.h | 18 ++++ .../MyGraphicsEngine/{window.h => Window.h} | 0 MyGraphicsEngine/shaders/vertex.shader | 5 +- MyGraphicsEngine/window.cpp | 12 ++- SandboxApplication/Sandbox.cpp | 16 +++- TODO.md | 10 +- premake5.lua | 17 +++- 10 files changed, 171 insertions(+), 23 deletions(-) create mode 100644 MyGraphicsEngine/Shader.cpp create mode 100644 MyGraphicsEngine/include/MyGraphicsEngine/Camera.h delete mode 100644 MyGraphicsEngine/include/MyGraphicsEngine/Graphics.h create mode 100644 MyGraphicsEngine/include/MyGraphicsEngine/Shader.h rename MyGraphicsEngine/include/MyGraphicsEngine/{window.h => Window.h} (100%) diff --git a/MyGraphicsEngine/Shader.cpp b/MyGraphicsEngine/Shader.cpp new file mode 100644 index 0000000..1d25f55 --- /dev/null +++ b/MyGraphicsEngine/Shader.cpp @@ -0,0 +1,94 @@ +#include "MyGraphicsEngine/Shader.h" + +Shader::Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath) +{ + char infoLog[512]; + int succes; + + char* vertexCode = readFile(vertexShaderPath.c_str()); + unsigned int vertId = glCreateShader(GL_VERTEX_SHADER); + + spdlog::info(vertexCode); + + glShaderSource(vertId, 1, &vertexCode, NULL); + glCompileShader(vertId); + + glGetShaderiv(vertId, GL_COMPILE_STATUS, &succes); + if(!succes){ + glGetShaderInfoLog(vertId, 512, NULL, infoLog); + spdlog::error( "Vertex shader has compile error {}", infoLog); + return; + } + + + char* fragmentCode = readFile(fragmentShaderPath.c_str()); + + unsigned int fragId = glCreateShader(GL_FRAGMENT_SHADER); + + + + glShaderSource(fragId, 1, &fragmentCode, NULL); + glCompileShader(fragId); + + + glGetShaderiv(fragId, GL_COMPILE_STATUS, &succes); + if(!succes){ + glGetShaderInfoLog(fragId, 512, NULL, infoLog); + spdlog::error("Fragment shader has compile error {}", infoLog); + return; + } + + + + delete[] vertexCode; + delete[] fragmentCode; + + +} +char* Shader::readFile (const char* filePath){ + spdlog::info("try Open file {}", filePath); + std::ifstream file ; + file.open(filePath); + + if(file.is_open() == false){ + spdlog::info("File not found."); + return nullptr; + } + + + // Determine the file size! + file.seekg(0, std::ios::end); + size_t filesize = file.tellg(); + + // Undo previous seek. + file.seekg(0, std::ios::beg); + spdlog::info("filesize: {}", filesize); + + + // Create a big enough buffer for the file + + //calculate buffer size to prevent memory issues + size_t ChunksInBuffer = filesize / Shader::CHUNK_SIZE; + size_t bufferSize = (ChunksInBuffer + 1) * Shader::CHUNK_SIZE; + + + char* FileBuffer = new char[bufferSize]; + + memset(FileBuffer, '\0', bufferSize); + size_t bytesRead= 0; + + // read the whole file + while( file.eof() == false){ + file.read(FileBuffer + bytesRead, CHUNK_SIZE ); + bytesRead += CHUNK_SIZE; + } + + FileBuffer[filesize + 1] = '\0'; + + return FileBuffer; +} + +void Shader::Use() +{ + // glUseProgam(id); +} \ No newline at end of file diff --git a/MyGraphicsEngine/include/MyGraphicsEngine/Camera.h b/MyGraphicsEngine/include/MyGraphicsEngine/Camera.h new file mode 100644 index 0000000..0829bfc --- /dev/null +++ b/MyGraphicsEngine/include/MyGraphicsEngine/Camera.h @@ -0,0 +1,13 @@ +#pragma once +#include + +struct Camera { + glm::vec3 Position; + glm::mat4 ViewMat; + float Zoom; +private: + glm::vec3 Front; + glm::vec3 Right; + glm::vec3 Up; + +}; \ No newline at end of file diff --git a/MyGraphicsEngine/include/MyGraphicsEngine/Graphics.h b/MyGraphicsEngine/include/MyGraphicsEngine/Graphics.h deleted file mode 100644 index 075860b..0000000 --- a/MyGraphicsEngine/include/MyGraphicsEngine/Graphics.h +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once -#include -#include "MyGraphicsEngine/window.h" - - -inline void test (){ - spdlog::info("Linked correctly!"); - -} \ No newline at end of file diff --git a/MyGraphicsEngine/include/MyGraphicsEngine/Shader.h b/MyGraphicsEngine/include/MyGraphicsEngine/Shader.h new file mode 100644 index 0000000..b8c7e23 --- /dev/null +++ b/MyGraphicsEngine/include/MyGraphicsEngine/Shader.h @@ -0,0 +1,18 @@ +#pragma once +#include +#include +#include +#include +#include + + +class Shader { + private: + int id; + const size_t CHUNK_SIZE = 10; + char* readFile (const char* filePath); + public: + Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath); + void Use(); + +}; \ No newline at end of file diff --git a/MyGraphicsEngine/include/MyGraphicsEngine/window.h b/MyGraphicsEngine/include/MyGraphicsEngine/Window.h similarity index 100% rename from MyGraphicsEngine/include/MyGraphicsEngine/window.h rename to MyGraphicsEngine/include/MyGraphicsEngine/Window.h diff --git a/MyGraphicsEngine/shaders/vertex.shader b/MyGraphicsEngine/shaders/vertex.shader index cdd6dd3..4a38668 100644 --- a/MyGraphicsEngine/shaders/vertex.shader +++ b/MyGraphicsEngine/shaders/vertex.shader @@ -1,7 +1,6 @@ #version 330 core -layout (location = 0) in vec3 aPos; +layout in vec3 aPos; -void main() -{ +void main() { gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); } \ No newline at end of file diff --git a/MyGraphicsEngine/window.cpp b/MyGraphicsEngine/window.cpp index 7e462ac..4f695f9 100644 --- a/MyGraphicsEngine/window.cpp +++ b/MyGraphicsEngine/window.cpp @@ -1,8 +1,10 @@ -#include "MyGraphicsEngine/window.h" - +#include "MyGraphicsEngine/Window.h" +#include bool BarinkWindow::InitGLFW(){ - if(!glfwInit()){ + if(!glfwInit()) + { + spdlog::error("Failed to initialise GLFW!"); return false; } return true; @@ -14,7 +16,9 @@ Width(width), Height(height), FullScreen(false){ window = glfwCreateWindow(Width, Height, "BarinkEngine", NULL, NULL); - if( !window){ + if( !window) + { + spdlog::error("GLFW failed to create window!"); glfwTerminate(); return; } diff --git a/SandboxApplication/Sandbox.cpp b/SandboxApplication/Sandbox.cpp index 2f293dd..860d894 100644 --- a/SandboxApplication/Sandbox.cpp +++ b/SandboxApplication/Sandbox.cpp @@ -1,11 +1,23 @@ -#include +#include +#include +#include +#include int main (int argc, char *argv[] ){ - test(); BarinkWindow GameWindow(800, 600); + + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){ + printf("Failed to initialize GLAD!\n"); + return -1; + } + std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs"; + std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs"; + Shader(vertexShaderSource, fragmentShaderSource); + + GameWindow.EnterLoop(); } diff --git a/TODO.md b/TODO.md index 7a28d34..74bf9ae 100644 --- a/TODO.md +++ b/TODO.md @@ -3,9 +3,9 @@ **NOTE:** __Fairly detailed planning__ - Setup build system \ - Link with GLFW \ - Basic Window \ + Setup build system \ + Link with GLFW \ + Basic Window \ Basic Triangle rendering \ Basic Textures \ Link GLEW \ @@ -15,3 +15,7 @@ Load FBX model files \ Basic Physics \ To far in the future + + +## Resources +https://renderdoc.org/ \ No newline at end of file diff --git a/premake5.lua b/premake5.lua index 2b8f040..1ce0a01 100644 --- a/premake5.lua +++ b/premake5.lua @@ -12,6 +12,7 @@ workspace "BarinkEngine" buildmessage "Building BarinkEngineSandbox ..." includedirs { + "./libs/glad/include", "./MyGraphicsEngine/include" } @@ -19,6 +20,7 @@ workspace "BarinkEngine" libdirs{ "./libs/spdlog-1.9.1/build" + } links{ @@ -37,8 +39,12 @@ workspace "BarinkEngine" "SandboxApplication/*.cpp" } - + -- NOTE: make these copy instructions more flexible + ok, err = os.copyfile("MyGraphicsEngine/shaders/fragment.shader", "build/SandboxApplication/Debug/test.fs") + if err then error("Copy fragment shader source failed!") end + ok, err = os.copyfile("MyGraphicsEngine/shaders/vertex.shader", "build/SandboxApplication/Debug/test.vs") + if err then error("Copy vertex shader source failed!") end project "MyGraphicsEngine" kind "StaticLib" @@ -46,8 +52,11 @@ workspace "BarinkEngine" buildmessage "Building MyGraphicsEngine ..." includedirs { + "./libs/glad/include", "./libs/glfw-3.3.4/include", + "./libs/glew-2.2.0/include", "./libs/spdlog-1.9.1/include", + "./libs/glm/glm", "./MyGraphicsEngine/include" } @@ -67,7 +76,11 @@ workspace "BarinkEngine" "m" } - files {"MyGraphicsEngine/*.cpp"} + files { + "./libs/glad/src/glad.c", + "MyGraphicsEngine/*.cpp" + + } filter "configurations:Debug" defines {"DEBUG"}