Improved shader definition loader

This commit is contained in:
Nigel Barink 2022-04-30 22:51:50 +02:00
parent f547ff1b8f
commit f5c680ba3e
7 changed files with 65 additions and 43 deletions

View File

@ -8,8 +8,6 @@ Shader::Shader(const std::string vertexShaderPath, const std::string fragmentSha
char* vertexCode = readFile(vertexShaderPath.c_str()); char* vertexCode = readFile(vertexShaderPath.c_str());
unsigned int vertId = glCreateShader(GL_VERTEX_SHADER); unsigned int vertId = glCreateShader(GL_VERTEX_SHADER);
spdlog::info(vertexCode);
glShaderSource(vertId, 1, &vertexCode, NULL); glShaderSource(vertId, 1, &vertexCode, NULL);
glCompileShader(vertId); glCompileShader(vertId);
@ -53,8 +51,8 @@ Shader::Shader(const std::string vertexShaderPath, const std::string fragmentSha
} }
delete[] vertexCode; delete vertexCode;
delete[] fragmentCode; delete fragmentCode;
} }
@ -80,23 +78,13 @@ char* Shader::readFile (const char* filePath){
// Create a big enough buffer for the file // Create a big enough buffer for the file
//calculate buffer size to prevent memory issues size_t bufferSize = filesize + 3;
size_t ChunksInBuffer = filesize / Shader::CHUNK_SIZE;
size_t bufferSize = (ChunksInBuffer + 1) * Shader::CHUNK_SIZE;
char* FileBuffer = new char[bufferSize]; char* FileBuffer = new char[bufferSize];
memset(FileBuffer, '\0', bufferSize); memset(FileBuffer, '\0', bufferSize);
size_t bytesRead= 0;
// read the whole file // read the whole file
while( file.eof() == false){ file.read(FileBuffer, filesize);
file.read(FileBuffer + bytesRead, CHUNK_SIZE );
bytesRead += CHUNK_SIZE;
}
FileBuffer[filesize + 1] = '\0';
return FileBuffer; return FileBuffer;
} }

View File

@ -0,0 +1,10 @@
#pragma once
#include <vector>
class Mesh {
public:
std::vector<float> vertices;
std::vector<int> elements;
std::vector<float> uv;
};

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <glad/glad.h> #include <glad/glad.h>
#define GLFW_STATIC
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>

View File

@ -0,0 +1,7 @@
#version 330 core
out vec4 FragColor;
void main{
FragColor = vec4(0.0f, 1.0f, 0.0f , 1.0f);
}

View File

@ -1,28 +1,35 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <MyGraphicsEngine/Shader.h> #include <MyGraphicsEngine/Shader.h>
#include <MyGraphicsEngine/Window.h> #include <MyGraphicsEngine/Window.h>
#include <MyGraphicsEngine/Mesh.h>
#include <string> #include <string>
extern "C" /*
* extern "C"
{ {
#include "lauxlib.h" #include "lauxlib.h"
#include "lua.h" #include "lua.h"
#include "lualib.h" #include "lualib.h"
} }
*/
float vertices[] = { int main (int argc, char *argv[] ){
Mesh mesh;
mesh.vertices = {
0.5f, 0.5f, 0.0f, // top, right 0.5f, 0.5f, 0.0f, // top, right
0.5f, -0.5f, 0.0f, // bottom right 0.5f, -0.5f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, // bottom left -0.5f, -0.5f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f // top left -0.5f, 0.5f, 0.0f // top left
}; };
unsigned int indices[] = {
mesh.elements = {
0,1,3, 0,1,3,
1,2,3 1,2,3
}; };
int main (int argc, char *argv[] ){
BarinkWindow GameWindow(800, 600); BarinkWindow GameWindow(800, 600);
@ -32,10 +39,15 @@ int main (int argc, char *argv[] ){
spdlog::info("Working directory: {}", argv[0]); spdlog::info("Working directory: {}", argv[0]);
lua_State* L = luaL_newstate(); /*
* lua_State* L = luaL_newstate();
luaL_openlibs(L); luaL_openlibs(L);
luaL_dostring(L, "print('BarinkEngine')"); luaL_dostring(L, "print('BarinkEngine')");
luaL_dofile(L,"./script.lua"); luaL_dofile(L,"./script.lua");
*/
unsigned int VBO, VAO, EBO; unsigned int VBO, VAO, EBO;
@ -43,14 +55,14 @@ int main (int argc, char *argv[] ){
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO); glGenBuffers(1, &EBO);
spdlog::info("Vertices: {}", mesh.vertices.size());
glBindVertexArray(VAO); glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); 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); 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); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);

@ -1 +1 @@
Subproject commit cc98465e3508535ba8c7f6208df934c156a018dc Subproject commit ba68939dece885cfc0b70125fc88a86d322b3ab5

View File

@ -4,7 +4,7 @@ workspace "BarinkEngine"
cppdialect "C++17" cppdialect "C++17"
targetdir "./build/%{prj.name}/%{cfg.buildcfg}" targetdir "./build/%{prj.name}/%{cfg.buildcfg}"
objdir "./intermediates/%{prj.name}/%{cfg.buildcfg}" objdir "./intermediates/%{prj.name}/%{cfg.buildcfg}"
architecture "x86_64"
project "SandboxApplication" project "SandboxApplication"
kind "ConsoleApp" kind "ConsoleApp"
@ -20,8 +20,8 @@ workspace "BarinkEngine"
} }
libdirs{ libdirs{
"./libs/spdlog/_builds/Release", "./libs/spdlog/build/Release",
"./libs/glfw/lib-vc2022", "./libs/glfw/build/src/Debug",
"./libs/lua" "./libs/lua"
} }
@ -36,6 +36,9 @@ workspace "BarinkEngine"
"SandboxApplication/*.cpp" "SandboxApplication/*.cpp"
} }
defines {"DEBUG"}
symbols "On"
-- NOTE: make these copy instructions more flexible -- NOTE: make these copy instructions more flexible
ok, err = os.copyfile("MyGraphicsEngine/shaders/fragment.shader", "build/SandboxApplication/Debug/test.fs") ok, err = os.copyfile("MyGraphicsEngine/shaders/fragment.shader", "build/SandboxApplication/Debug/test.fs")
if err then error("Copy fragment shader source failed!") end if err then error("Copy fragment shader source failed!") end
@ -53,14 +56,14 @@ workspace "BarinkEngine"
"./libs/glfw/include", "./libs/glfw/include",
"./libs/glew/include", "./libs/glew/include",
"./libs/spdlog/include", "./libs/spdlog/include",
"./libs/glm/glm", "./libs/glm",
"./MyGraphicsEngine/include" "./MyGraphicsEngine/include"
} }
libdirs{ libdirs{
"./libs/spdlog/_builds/Release", "./libs/spdlog/build/Release",
"./libs/glfw/lib-vc2022" "./libs/glfw/build/src/Debug"
} }
links { links {
@ -70,12 +73,13 @@ workspace "BarinkEngine"
files { files {
"./libs/glad/src/glad.c", "./libs/glad/src/glad.c",
"MyGraphicsEngine/*.h",
"MyGraphicsEngine/*.cpp" "MyGraphicsEngine/*.cpp"
} }
filter "configurations:Debug" filter "configurations:Debug"
defines {"NDEBUG"} defines {"DEBUG"}
symbols "On" symbols "On"
filter "configurations:Release" filter "configurations:Release"