Working on basic rendering #4

* Added a basic material abstraction
* Started implementation of RenderTarget (such as render textures)
This commit is contained in:
Nigel Barink 2022-06-04 18:26:58 +02:00
parent d9f0f40ad9
commit d019155d10
20 changed files with 633 additions and 452 deletions

6
.gitmodules vendored
View File

@ -19,3 +19,9 @@
[submodule "assimp"] [submodule "assimp"]
path = libs/assimp path = libs/assimp
url = https://github.com/assimp/assimp.git url = https://github.com/assimp/assimp.git
[submodule "libs/steam-audio"]
path = libs/steam-audio
url = https://github.com/ValveSoftware/steam-audio.git
[submodule "libs/physx"]
path = libs/physx
url = https://git.barink.dev/Nigel/PhysX.git

View File

@ -1,8 +1,8 @@
#include "BarinkEngine.h" #include "BarinkEngine.h"
#include <phonon.h>
EngineStatistics* ES; EngineStatistics* ES;
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
// Setup performance sampler // Setup performance sampler
PerfomanceSamplerInit(); PerfomanceSamplerInit();
@ -18,6 +18,8 @@ int main(int argc, char* argv[]) {
GUIManager GUISystem = GUIManager(&MainWindow); GUIManager GUISystem = GUIManager(&MainWindow);
// First call to setup game // First call to setup game
Start(); Start();

View File

@ -4,6 +4,7 @@
#include "graphics/Window.h" #include "graphics/Window.h"
#include "graphics/Camera.h" #include "graphics/Camera.h"
#include "graphics/Renderable.h" #include "graphics/Renderable.h"
#include "Graphics/Material.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#include "Input/InputManager.h" #include "Input/InputManager.h"
@ -12,6 +13,7 @@
#include "Scene.h" #include "Scene.h"
#include "PerfCounter.h" #include "PerfCounter.h"
extern void Start(); extern void Start();
extern void Update(); extern void Update();
extern void ImmediateGraphicsDraw(); extern void ImmediateGraphicsDraw();

View File

@ -0,0 +1,19 @@
#pragma once
#include <glm/glm.hpp>
#include <string>
#include "Shader.h"
class Material {
public:
Material(const Shader& shader);
void Apply();
glm::vec3 Color;
private:
const Shader& shader;
};

View File

@ -0,0 +1,30 @@
#pragma once
#include "BarinkEngine.h"
#include <vector>;
class RenderSurface
{
public:
RenderSurface();
~RenderSurface();
void Draw();
private:
// would normally be a material
// however rendersurface is special and
// thus does not contain a material
Shader* shader;
// Basically a mesh
std::vector<glm::vec3> verts;
std::vector<unsigned int > indices;
Buffer vertexBuffer;
Buffer elementBuffer;
VertexArray VAO;
};

View File

@ -2,14 +2,26 @@
#include <vector> #include <vector>
#include "Mesh.h" #include "Mesh.h"
#include "Buffer.h" #include "Buffer.h"
#include "Material.h"
#include "VertexArray.h" #include "VertexArray.h"
#include "Scene.h" #include "Scene.h"
class Renderable : public SceneNode { class Renderable : public SceneNode {
public: public:
/*
* NOTE: Should combine into a Mesh!!
*/
Buffer vertexBuffer; Buffer vertexBuffer;
Buffer elementBuffer; Buffer elementBuffer;
VertexArray VAO; VertexArray VAO;
Material* material;
Shader* shader;
~Renderable(); ~Renderable();
static Renderable* Load(); static Renderable* Load();

View File

@ -15,12 +15,12 @@ class Shader {
public: public:
Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath); Shader(const std::string vertexShaderPath, const std::string fragmentShaderPath);
void Use(); void Use();
void setUniformMat4(std::string uniformName, glm::mat4 matrix4); void setUniformMat4(std::string uniformName, glm::mat4 matrix4)const;
void setUniformVec4(std::string uniformName, glm::vec4 vector4); void setUniformVec4(std::string uniformName, glm::vec4 vector4)const;
void setUniformVec3(std::string uniformName, glm::vec3 vector3); void setUniformVec3(std::string uniformName, glm::vec3 vector3)const;
void setUniformVec2(std::string uniformName, glm::vec2 vector2); void setUniformVec2(std::string uniformName, glm::vec2 vector2)const;
void setUniformFloat(std::string uniformName, float value); void setUniformFloat(std::string uniformName, float value)const;
void setUniformInt(std::string uniformName, int value); void setUniformInt(std::string uniformName, int value) const ;
}; };

View File

@ -2,7 +2,6 @@
void BarinkEngine::InputManager::PollEvents() void BarinkEngine::InputManager::PollEvents()
{ {
for (std::vector<BarinkWindow*>::iterator it = windows.begin(); it != windows.end(); ++it) { for (std::vector<BarinkWindow*>::iterator it = windows.begin(); it != windows.end(); ++it) {
(*it)->Poll(); (*it)->Poll();
} }

View File

@ -0,0 +1,10 @@
#include "../Include/Graphics/Material.h"
Material::Material(const Shader& shader) :
shader(shader) {
}
void Material::Apply() {
shader.setUniformVec3("Color", Color);
}

View File

@ -0,0 +1,46 @@
#include "Graphics/RenderSurface.h";
RenderSurface::RenderSurface(){
shader = new Shader("build/SandboxAppliction/Debug/renderSuface.vs", "build/SandboxApplication/Debug/renderSurface.fs");
verts = std::vector<glm::vec3>{
{-0.5f, 0.5f, 0.0f}, // 0
{-0.5f, -0.5f, 0.0f}, // 1
{0.5f, -0.5f, 0.0f}, // 2
{0.5f, 0.5f, 0.0f}, // 3
};
indices = std::vector<unsigned int>{
0,2,1,
0,3,2
};
VAO.Create();
VAO.Bind();
vertexBuffer.createBuffer();
vertexBuffer.Bind(false);
vertexBuffer.setBufferData(&verts[0], verts.size() * sizeof(glm::vec3), false);
elementBuffer.createBuffer();
elementBuffer.Bind(true);
elementBuffer.setBufferData(&indices[0], indices.size() * sizeof(unsigned int), true);
VAO.AttachAttribute(0, 3, 0);
vertexBuffer.Unbind(false);
VAO.Unbind();
}
RenderSurface::~RenderSurface() {
delete shader;
}
void RenderSurface::Draw() {
}

View File

@ -12,6 +12,7 @@ BarinkEngine::Renderer::~Renderer()
void BarinkEngine::Renderer::Render() void BarinkEngine::Renderer::Render()
{ {
for (auto model : models) { for (auto model : models) {
model->Draw(); model->Draw();
} }

View File

@ -96,29 +96,29 @@ void Shader::Use()
} }
void Shader::setUniformMat4(std::string uniformName, glm::mat4 matrix4) void Shader::setUniformMat4(std::string uniformName, glm::mat4 matrix4) const
{ {
glUniformMatrix4fv(glGetUniformLocation(id, uniformName.c_str()), 1, GL_FALSE, glm::value_ptr(matrix4)); glUniformMatrix4fv(glGetUniformLocation(id, uniformName.c_str()), 1, GL_FALSE, glm::value_ptr(matrix4));
} }
void Shader::setUniformVec4(std::string uniformName, glm::vec4 vector4) void Shader::setUniformVec4(std::string uniformName, glm::vec4 vector4) const
{ {
glUniform4fv(glGetUniformLocation(id, uniformName.c_str()), 1, glm::value_ptr(vector4)); glUniform4fv(glGetUniformLocation(id, uniformName.c_str()), 1, glm::value_ptr(vector4));
} }
void Shader::setUniformVec3(std::string uniformName, glm::vec3 vector3) void Shader::setUniformVec3(std::string uniformName, glm::vec3 vector3) const
{ {
glUniform3fv(glGetUniformLocation(id, uniformName.c_str()), 1, glm::value_ptr(vector3)); glUniform3fv(glGetUniformLocation(id, uniformName.c_str()), 1, glm::value_ptr(vector3));
} }
void Shader::setUniformVec2(std::string uniformName, glm::vec2 vector2) void Shader::setUniformVec2(std::string uniformName, glm::vec2 vector2) const
{ {
glUniform2fv(glGetUniformLocation(id, uniformName.c_str()),1, glm::value_ptr(vector2)); glUniform2fv(glGetUniformLocation(id, uniformName.c_str()),1, glm::value_ptr(vector2));
} }
void Shader::setUniformFloat(std::string uniformName, float value) void Shader::setUniformFloat(std::string uniformName, float value) const
{ {
glUniform1f(glGetUniformLocation(id, uniformName.c_str()), value); glUniform1f(glGetUniformLocation(id, uniformName.c_str()), value);
} }
void Shader::setUniformInt(std::string uniformName, int value) void Shader::setUniformInt(std::string uniformName, int value) const
{ {
glUniform1i(glGetUniformLocation(id, uniformName.c_str()), value); glUniform1i(glGetUniformLocation(id, uniformName.c_str()), value);
} }

View File

@ -0,0 +1,11 @@
#version 440 core
layout (location = 0) in vec2 aPos;
layout (location = 1) in vec2 aTexCoords;
out vec2 aTexCoords;
void main(){
gl_Position = vec4(aPos.xy , 0.0 ,1.0);
aTexCoords = aTexCoords;
}

View File

@ -0,0 +1,11 @@
#version 440 core
out vec4 FragColor;
in vec2 TexCoords;
uniform sampler2D screenTexture;
void main(){
FragColor = texture(screenTexture, aTexCoords);
}

View File

@ -36,6 +36,9 @@ Width(width), Height(height), FullScreen(false){
exit(-1); exit(-1);
} }
// Set vsync off !!
glfwSwapInterval(0);
VulkanSupported = glfwVulkanSupported(); VulkanSupported = glfwVulkanSupported();
glfwGetFramebufferSize(window, &Width, &Height); glfwGetFramebufferSize(window, &Width, &Height);

View File

@ -11,6 +11,8 @@ project "BarinkEngine"
"../libs/glm", "../libs/glm",
"../libs/GorillaAudio/include", "../libs/GorillaAudio/include",
"../libs/physx/physx/include",
"../libs/steam-audio/include",
"../libs/assimp/include", "../libs/assimp/include",
"../libs/glad/include", "../libs/glad/include",
"../libs/glfw/include", "../libs/glfw/include",
@ -22,6 +24,7 @@ project "BarinkEngine"
} }
libdirs { libdirs {
"../libs/steam-audio/lib/windows-x64",
"../libs/lua", "../libs/lua",
"../libs/spdlog/build/Release", "../libs/spdlog/build/Release",
"../libs/assimp/lib/Debug", "../libs/assimp/lib/Debug",
@ -30,6 +33,7 @@ project "BarinkEngine"
} }
links { links {
"phonon",
"lua54", "lua54",
"spdlog", "spdlog",
"assimp-vc143-mtd", "assimp-vc143-mtd",
@ -56,3 +60,10 @@ project "BarinkEngine"
ok, err = os.copyfile("graphics/shaders/vertex.shader", "../build/SandboxApplication/Debug/test.vs") ok, err = os.copyfile("graphics/shaders/vertex.shader", "../build/SandboxApplication/Debug/test.vs")
if err then error("Copy vertex shader source failed!") end if err then error("Copy vertex shader source failed!") end
ok, err = os.copyfile("graphics/shaders/RenderSurfaceFrag.shader", "../build/SandboxApplication/Debug/RenderSurface.fs")
if err then error("Copy fragment shader source failed!") end
ok, err = os.copyfile("graphics/shaders/RenderSurfaceVert.shader", "../build/SandboxApplication/Debug/RenderSurface.vs")
if err then error("Copy vertex shader source failed!") end

View File

@ -6,6 +6,10 @@ void CameraTool(Camera* cam) {
ImGui::SliderFloat("Zoom:", &cam->Zoom, 10, 190); ImGui::SliderFloat("Zoom:", &cam->Zoom, 10, 190);
ImGui::InputFloat3("Position:", &cam->Position[0]);
ImGui::InputFloat3("Rotation:", &cam->Rotation[0]);
ImGui::End(); ImGui::End();
} }

View File

@ -11,12 +11,17 @@ Renderable* Cube;
Renderable* Cube2; Renderable* Cube2;
Shader* shader; Shader* shader;
Material* matCube;
Material* matCube2;
char* code = new char[254]; char* code = new char[254];
const std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs"; const std::string vertexShaderSource = "build/SandboxApplication/Debug/test.vs";
const std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs"; const std::string fragmentShaderSource = "build/SandboxApplication/Debug/test.fs";
/* /*
* Runs once at startup * Runs once at startup
* - USe to initialize the game/sandbox/demo * - USe to initialize the game/sandbox/demo
@ -47,9 +52,13 @@ void Start() {
// Walk scene graph // Walk scene graph
PrintSceneTree(scene.GetRoot(),0); PrintSceneTree(scene.GetRoot(),0);
shader = new Shader(vertexShaderSource, fragmentShaderSource); shader = new Shader(vertexShaderSource, fragmentShaderSource);
matCube = new Material(*shader);
matCube->Color = glm::vec3(1.0, 0.0, 0.0);
matCube2 = new Material(*shader);
matCube2->Color = glm::vec3(0.0, 1.0f, 0.0);
/* /*
* load meshes * load meshes
@ -113,12 +122,12 @@ void Update()
shader->setUniformMat4("P", projection); shader->setUniformMat4("P", projection);
shader->setUniformMat4("M", CalculateModelMat(Cube->transform)); shader->setUniformMat4("M", CalculateModelMat(Cube->transform));
shader->setUniformMat4("V", cam->GetViewMatrix()); shader->setUniformMat4("V", cam->GetViewMatrix());
shader->setUniformVec3("MatColour", glm::vec3(1.0f, 0.0f, 0.0f)); matCube->Apply();
Cube->Draw(); Cube->Draw();
shader->setUniformMat4("M", CalculateModelMat(Cube2->transform)); shader->setUniformMat4("M", CalculateModelMat(Cube2->transform));
shader->setUniformVec3("MatColour", glm::vec3(0.0f, 1.0f, 0.0f)); matCube2->Apply();
Cube2->Draw(); Cube2->Draw();
@ -139,5 +148,8 @@ void Stop() {
delete Cube2; delete Cube2;
delete Cube; delete Cube;
delete matCube;
delete matCube2;
delete shader; delete shader;
} }

1
libs/physx Submodule

@ -0,0 +1 @@
Subproject commit c3d5537bdebd6f5cd82fcaf87474b838fe6fd5fa

1
libs/steam-audio Submodule

@ -0,0 +1 @@
Subproject commit de6414694deccde57396c95dc44f8cc5f7d790f5