From f547ff1b8f4fc1a253a3b3bd25d3a3bb41611162 Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Sat, 30 Apr 2022 20:20:07 +0200 Subject: [PATCH] Basic rectangle drawing --- .gitmodules | 2 +- MyGraphicsEngine/Shader.cpp | 15 ++++- .../include/MyGraphicsEngine/Window.h | 3 +- MyGraphicsEngine/window.cpp | 12 +++- SandboxApplication/Sandbox.cpp | 60 +++++++++++++++---- SandboxApplication/script.lua | 1 + libs/glm | 2 +- premake5.lua | 38 +++++------- 8 files changed, 93 insertions(+), 40 deletions(-) create mode 100644 SandboxApplication/script.lua diff --git a/.gitmodules b/.gitmodules index 4b478e5..04856a3 100644 --- a/.gitmodules +++ b/.gitmodules @@ -5,5 +5,5 @@ path = libs/glm url = https://github.com/nigelbarink/glm.git [submodule "spdlog"] - path = libs\\spdlog + path = libs/spdlog url = https://github.com/nigelbarink/spdlog.git diff --git a/MyGraphicsEngine/Shader.cpp b/MyGraphicsEngine/Shader.cpp index 1d25f55..91516b5 100644 --- a/MyGraphicsEngine/Shader.cpp +++ b/MyGraphicsEngine/Shader.cpp @@ -38,6 +38,19 @@ Shader::Shader(const std::string vertexShaderPath, const std::string fragmentSha return; } + id = glCreateProgram(); + + glAttachShader(id, vertId); + glAttachShader(id, fragId); + glLinkProgram(id); + + + int success; + glGetProgramiv(id, GL_LINK_STATUS, &success); + if(!success) { + glGetProgramInfoLog(id, 512, NULL, infoLog); + printf("ERROR::SHADER_PROGRAM::LINKING_FAILED\n %s", infoLog); + } delete[] vertexCode; @@ -90,5 +103,5 @@ char* Shader::readFile (const char* filePath){ void Shader::Use() { - // glUseProgam(id); + glUseProgram(id); } \ No newline at end of file diff --git a/MyGraphicsEngine/include/MyGraphicsEngine/Window.h b/MyGraphicsEngine/include/MyGraphicsEngine/Window.h index b84a197..ce1822f 100644 --- a/MyGraphicsEngine/include/MyGraphicsEngine/Window.h +++ b/MyGraphicsEngine/include/MyGraphicsEngine/Window.h @@ -1,4 +1,5 @@ #pragma once +#include #include @@ -9,7 +10,7 @@ class BarinkWindow{ bool VulkanSupported; int Width, Height; - bool InitGLFW(); + static bool InitGLFW(); public: diff --git a/MyGraphicsEngine/window.cpp b/MyGraphicsEngine/window.cpp index 3174696..ca3825b 100644 --- a/MyGraphicsEngine/window.cpp +++ b/MyGraphicsEngine/window.cpp @@ -8,12 +8,15 @@ bool BarinkWindow::InitGLFW(){ spdlog::error("Failed to initialise GLFW!"); return false; } + return true; } BarinkWindow::BarinkWindow(const int width, const int height) : Width(width), Height(height), FullScreen(false){ - InitGLFW(); + if (InitGLFW()==false) { + exit(-1); + } window = glfwCreateWindow(Width, Height, "BarinkEngine", NULL, NULL); @@ -25,6 +28,10 @@ Width(width), Height(height), FullScreen(false){ } glfwMakeContextCurrent(window); + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { + printf("Failed to initialize GLAD!\n"); + exit(-1); + } VulkanSupported = glfwVulkanSupported(); @@ -33,6 +40,9 @@ Width(width), Height(height), FullScreen(false){ + glClearColor(0.2f, 0.2f, 0.2f, 1.0f); + + } diff --git a/SandboxApplication/Sandbox.cpp b/SandboxApplication/Sandbox.cpp index 8d3c313..d5d8868 100644 --- a/SandboxApplication/Sandbox.cpp +++ b/SandboxApplication/Sandbox.cpp @@ -10,36 +10,74 @@ extern "C" #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[] ){ - 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 shader (vertexShaderSource, fragmentShaderSource); + Shader shader (vertexShaderSource, fragmentShaderSource); + + spdlog::info("Working directory: {}", argv[0]); lua_State* L = luaL_newstate(); luaL_openlibs(L); luaL_dostring(L, "print('BarinkEngine')"); - luaL_dofile(L,"script.lua"); + luaL_dofile(L,"./script.lua"); + + unsigned int VBO, VAO, EBO; + + glGenVertexArrays(1, &VAO); + glGenBuffers(1, &VBO); + glGenBuffers(1, &EBO); + + + glBindVertexArray(VAO); + + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + + 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); + glEnableVertexAttribArray(0); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + + glBindVertexArray(0); + - glClearColor(0.2f, 0.2f, 0.2f, 1.0f); - while (!GameWindow.WindowShouldClose()) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT) ; + shader.Use(); + glBindVertexArray(VAO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + glBindVertexArray(0); GameWindow.Poll(); } + + + glDeleteVertexArrays(1, &VAO); + glDeleteBuffers(1, &EBO); + + } diff --git a/SandboxApplication/script.lua b/SandboxApplication/script.lua new file mode 100644 index 0000000..442659b --- /dev/null +++ b/SandboxApplication/script.lua @@ -0,0 +1 @@ +print("Hello world!") \ No newline at end of file diff --git a/libs/glm b/libs/glm index 72a01d3..cc98465 160000 --- a/libs/glm +++ b/libs/glm @@ -1 +1 @@ -Subproject commit 72a01d3432204128442b2198d3f595bb84f011bb +Subproject commit cc98465e3508535ba8c7f6208df934c156a018dc diff --git a/premake5.lua b/premake5.lua index 957a0b3..64ba518 100644 --- a/premake5.lua +++ b/premake5.lua @@ -14,26 +14,21 @@ workspace "BarinkEngine" includedirs { "./libs/glad/include", "./MyGraphicsEngine/include", - "./libs/lua-5.4.4/lua" + "./libs/spdlog/include", + "./libs/lua/include", + "./libs/glfw/include", } - - libdirs{ - "./libs/spdlog-1.9.1/build", - "./libs/lua-5.4.4/lua" + "./libs/spdlog/_builds/Release", + "./libs/glfw/lib-vc2022", + "./libs/lua" } links{ "liblua", "spdlog", "glfw3", - "X11", - "GL", - "GLU", - "pthread", - "dl", - "m", "MyGraphicsEngine" } @@ -55,27 +50,22 @@ workspace "BarinkEngine" includedirs { "./libs/glad/include", - "./libs/glfw-3.3.4/include", - "./libs/glew-2.2.0/include", - "./libs/spdlog-1.9.1/include", + "./libs/glfw/include", + "./libs/glew/include", + "./libs/spdlog/include", "./libs/glm/glm", "./MyGraphicsEngine/include" } libdirs{ - "./libs/spdlog-1.9.1/build" + "./libs/spdlog/_builds/Release", + "./libs/glfw/lib-vc2022" } links { - "libspdlog", - "glfw3", - "X11", - "GL", - "GLU", - "pthread", - "dl", - "m" + "spdlog", + "glfw3" } files { @@ -85,7 +75,7 @@ workspace "BarinkEngine" } filter "configurations:Debug" - defines {"DEBUG"} + defines {"NDEBUG"} symbols "On" filter "configurations:Release"