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());
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,23 +78,13 @@ 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;
}

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
#include <glad/glad.h>
#define GLFW_STATIC
#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,29 +1,36 @@
#include <glad/glad.h>
#include <MyGraphicsEngine/Shader.h>
#include <MyGraphicsEngine/Window.h>
#include <MyGraphicsEngine/Mesh.h>
#include <string>
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);

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

View File

@ -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"