Created a basic shader object and window object

Using GLAD to load OpenGL/Vulkan extensions
This commit is contained in:
Nigel Barink 2022-04-22 22:37:38 +02:00
parent dab01f1541
commit 168b936945
10 changed files with 171 additions and 23 deletions

View File

@ -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);
}

View File

@ -0,0 +1,13 @@
#pragma once
#include <glm/glm.hpp>
struct Camera {
glm::vec3 Position;
glm::mat4 ViewMat;
float Zoom;
private:
glm::vec3 Front;
glm::vec3 Right;
glm::vec3 Up;
};

View File

@ -1,9 +0,0 @@
#pragma once
#include <spdlog/spdlog.h>
#include "MyGraphicsEngine/window.h"
inline void test (){
spdlog::info("Linked correctly!");
}

View File

@ -0,0 +1,18 @@
#pragma once
#include <glad/glad.h>
#include <string>
#include <iostream>
#include <fstream>
#include <spdlog/spdlog.h>
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();
};

View File

@ -1,7 +1,6 @@
#version 330 core #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); gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
} }

View File

@ -1,8 +1,10 @@
#include "MyGraphicsEngine/window.h" #include "MyGraphicsEngine/Window.h"
#include <spdlog/spdlog.h>
bool BarinkWindow::InitGLFW(){ bool BarinkWindow::InitGLFW(){
if(!glfwInit()){ if(!glfwInit())
{
spdlog::error("Failed to initialise GLFW!");
return false; return false;
} }
return true; return true;
@ -14,7 +16,9 @@ Width(width), Height(height), FullScreen(false){
window = glfwCreateWindow(Width, Height, "BarinkEngine", NULL, NULL); window = glfwCreateWindow(Width, Height, "BarinkEngine", NULL, NULL);
if( !window){ if( !window)
{
spdlog::error("GLFW failed to create window!");
glfwTerminate(); glfwTerminate();
return; return;
} }

View File

@ -1,11 +1,23 @@
#include <MyGraphicsEngine/Graphics.h> #include <glad/glad.h>
#include <MyGraphicsEngine/Shader.h>
#include <MyGraphicsEngine/Window.h>
#include <string>
int main (int argc, char *argv[] ){ int main (int argc, char *argv[] ){
test();
BarinkWindow GameWindow(800, 600); 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(); GameWindow.EnterLoop();
} }

10
TODO.md
View File

@ -3,9 +3,9 @@
**NOTE:** __Fairly detailed planning__ **NOTE:** __Fairly detailed planning__
<input type="checkbox"></input> Setup build system \ <input type="checkbox" checked></input> Setup build system \
<input type="checkbox"></input> Link with GLFW \ <input type="checkbox" checked></input> Link with GLFW \
<input type="checkbox"></input> Basic Window \ <input type="checkbox" checked></input> Basic Window \
<input type="checkbox"></input> Basic Triangle rendering \ <input type="checkbox"></input> Basic Triangle rendering \
<input type="checkbox"></input> Basic Textures \ <input type="checkbox"></input> Basic Textures \
<input type="checkbox"></input> Link GLEW \ <input type="checkbox"></input> Link GLEW \
@ -15,3 +15,7 @@
<input type="checkbox"></input> Load FBX model files \ <input type="checkbox"></input> Load FBX model files \
<input type="checkbox"></input> Basic Physics \ <input type="checkbox"></input> Basic Physics \
<input type="checkbox"></input> To far in the future <input type="checkbox"></input> To far in the future
## Resources
https://renderdoc.org/

View File

@ -12,6 +12,7 @@ workspace "BarinkEngine"
buildmessage "Building BarinkEngineSandbox ..." buildmessage "Building BarinkEngineSandbox ..."
includedirs { includedirs {
"./libs/glad/include",
"./MyGraphicsEngine/include" "./MyGraphicsEngine/include"
} }
@ -19,6 +20,7 @@ workspace "BarinkEngine"
libdirs{ libdirs{
"./libs/spdlog-1.9.1/build" "./libs/spdlog-1.9.1/build"
} }
links{ links{
@ -37,8 +39,12 @@ workspace "BarinkEngine"
"SandboxApplication/*.cpp" "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" project "MyGraphicsEngine"
kind "StaticLib" kind "StaticLib"
@ -46,8 +52,11 @@ workspace "BarinkEngine"
buildmessage "Building MyGraphicsEngine ..." buildmessage "Building MyGraphicsEngine ..."
includedirs { includedirs {
"./libs/glad/include",
"./libs/glfw-3.3.4/include", "./libs/glfw-3.3.4/include",
"./libs/glew-2.2.0/include",
"./libs/spdlog-1.9.1/include", "./libs/spdlog-1.9.1/include",
"./libs/glm/glm",
"./MyGraphicsEngine/include" "./MyGraphicsEngine/include"
} }
@ -67,7 +76,11 @@ workspace "BarinkEngine"
"m" "m"
} }
files {"MyGraphicsEngine/*.cpp"} files {
"./libs/glad/src/glad.c",
"MyGraphicsEngine/*.cpp"
}
filter "configurations:Debug" filter "configurations:Debug"
defines {"DEBUG"} defines {"DEBUG"}