From f5c680ba3e260a2bd9ae6100b727c23f69136568 Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Sat, 30 Apr 2022 22:51:50 +0200 Subject: [PATCH] Improved shader definition loader --- MyGraphicsEngine/Shader.cpp | 24 +++------- .../include/MyGraphicsEngine/Mesh.h | 10 ++++ .../include/MyGraphicsEngine/Window.h | 1 + MyGraphicsEngine/shaders/fragment.shader | 7 +++ SandboxApplication/Sandbox.cpp | 46 ++++++++++++------- libs/glm | 2 +- premake5.lua | 18 +++++--- 7 files changed, 65 insertions(+), 43 deletions(-) create mode 100644 MyGraphicsEngine/include/MyGraphicsEngine/Mesh.h diff --git a/MyGraphicsEngine/Shader.cpp b/MyGraphicsEngine/Shader.cpp index 91516b5..6fda38a 100644 --- a/MyGraphicsEngine/Shader.cpp +++ b/MyGraphicsEngine/Shader.cpp @@ -8,8 +8,6 @@ Shader::Shader(const std::string vertexShaderPath, const std::string fragmentSha char* vertexCode = readFile(vertexShaderPath.c_str()); unsigned int vertId = glCreateShader(GL_VERTEX_SHADER); - spdlog::info(vertexCode); - glShaderSource(vertId, 1, &vertexCode, NULL); glCompileShader(vertId); @@ -53,8 +51,8 @@ Shader::Shader(const std::string vertexShaderPath, const std::string fragmentSha } - delete[] vertexCode; - delete[] fragmentCode; + delete vertexCode; + delete fragmentCode; } @@ -80,24 +78,14 @@ char* Shader::readFile (const char* filePath){ // 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; - - + size_t bufferSize = filesize + 3; 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'; - + file.read(FileBuffer, filesize); + return FileBuffer; } diff --git a/MyGraphicsEngine/include/MyGraphicsEngine/Mesh.h b/MyGraphicsEngine/include/MyGraphicsEngine/Mesh.h new file mode 100644 index 0000000..e2c1ee7 --- /dev/null +++ b/MyGraphicsEngine/include/MyGraphicsEngine/Mesh.h @@ -0,0 +1,10 @@ +#pragma once +#include + +class Mesh { +public: + std::vector vertices; + std::vector elements; + std::vector uv; + +}; \ No newline at end of file diff --git a/MyGraphicsEngine/include/MyGraphicsEngine/Window.h b/MyGraphicsEngine/include/MyGraphicsEngine/Window.h index ce1822f..3cee4f1 100644 --- a/MyGraphicsEngine/include/MyGraphicsEngine/Window.h +++ b/MyGraphicsEngine/include/MyGraphicsEngine/Window.h @@ -1,5 +1,6 @@ #pragma once #include +#define GLFW_STATIC #include diff --git a/MyGraphicsEngine/shaders/fragment.shader b/MyGraphicsEngine/shaders/fragment.shader index e69de29..7f9f820 100644 --- a/MyGraphicsEngine/shaders/fragment.shader +++ b/MyGraphicsEngine/shaders/fragment.shader @@ -0,0 +1,7 @@ +#version 330 core + +out vec4 FragColor; + +void main{ + FragColor = vec4(0.0f, 1.0f, 0.0f , 1.0f); +} \ No newline at end of file diff --git a/SandboxApplication/Sandbox.cpp b/SandboxApplication/Sandbox.cpp index d5d8868..d57578a 100644 --- a/SandboxApplication/Sandbox.cpp +++ b/SandboxApplication/Sandbox.cpp @@ -1,29 +1,36 @@ #include #include #include +#include + #include -extern "C" +/* +* extern "C" { #include "lauxlib.h" #include "lua.h" #include "lualib.h" } - -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 -}; - -unsigned int indices[] = { - 0,1,3, - 1,2,3 -}; +*/ int main (int argc, char *argv[] ){ + Mesh mesh; + + mesh.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 + }; + + + mesh.elements = { + 0,1,3, + 1,2,3 + }; + BarinkWindow GameWindow(800, 600); std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs"; @@ -32,10 +39,15 @@ int main (int argc, char *argv[] ){ spdlog::info("Working directory: {}", argv[0]); - lua_State* L = luaL_newstate(); + /* + * lua_State* L = luaL_newstate(); luaL_openlibs(L); luaL_dostring(L, "print('BarinkEngine')"); luaL_dofile(L,"./script.lua"); + */ + + + unsigned int VBO, VAO, EBO; @@ -43,14 +55,14 @@ int main (int argc, char *argv[] ){ glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); - + spdlog::info("Vertices: {}", mesh.vertices.size()); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(float) * mesh.vertices.size() , &mesh.vertices[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * mesh.elements.size(), &mesh.elements[0], GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); diff --git a/libs/glm b/libs/glm index cc98465..ba68939 160000 --- a/libs/glm +++ b/libs/glm @@ -1 +1 @@ -Subproject commit cc98465e3508535ba8c7f6208df934c156a018dc +Subproject commit ba68939dece885cfc0b70125fc88a86d322b3ab5 diff --git a/premake5.lua b/premake5.lua index 64ba518..4ef673c 100644 --- a/premake5.lua +++ b/premake5.lua @@ -4,7 +4,7 @@ workspace "BarinkEngine" cppdialect "C++17" targetdir "./build/%{prj.name}/%{cfg.buildcfg}" objdir "./intermediates/%{prj.name}/%{cfg.buildcfg}" - + architecture "x86_64" project "SandboxApplication" kind "ConsoleApp" @@ -20,8 +20,8 @@ workspace "BarinkEngine" } libdirs{ - "./libs/spdlog/_builds/Release", - "./libs/glfw/lib-vc2022", + "./libs/spdlog/build/Release", + "./libs/glfw/build/src/Debug", "./libs/lua" } @@ -36,6 +36,9 @@ workspace "BarinkEngine" "SandboxApplication/*.cpp" } + defines {"DEBUG"} + symbols "On" + -- 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 @@ -53,14 +56,14 @@ workspace "BarinkEngine" "./libs/glfw/include", "./libs/glew/include", "./libs/spdlog/include", - "./libs/glm/glm", + "./libs/glm", "./MyGraphicsEngine/include" } libdirs{ - "./libs/spdlog/_builds/Release", - "./libs/glfw/lib-vc2022" + "./libs/spdlog/build/Release", + "./libs/glfw/build/src/Debug" } links { @@ -70,12 +73,13 @@ workspace "BarinkEngine" files { "./libs/glad/src/glad.c", + "MyGraphicsEngine/*.h", "MyGraphicsEngine/*.cpp" } filter "configurations:Debug" - defines {"NDEBUG"} + defines {"DEBUG"} symbols "On" filter "configurations:Release"