Started work on GBuffer and the gBuffer shader

This commit is contained in:
Grayson Riffe (Laptop) 2021-09-23 08:02:36 -05:00
parent 2fc9fa0c2d
commit 4d4b6d51ae
9 changed files with 138 additions and 0 deletions

View File

@ -0,0 +1,2 @@
#version 330 core

View File

@ -0,0 +1,23 @@
#version 330 core
layout(location = 0) in vec3 pos;
layout(location = 1) in vec2 texCoords;
layout(location = 2) in vec3 normals;
uniform mat4 model;
uniform mat4 view;
uniform mat4 proj;
out vec3 fragPos;
out vec2 texCoord;
out vec3 normal;
void main() {
vec4 world = model * vec4(pos, 1.0);
fragPos = world.xyz;
texCoord = texCoords;
mat3 normalMat = transpose(inverse(mat3(model)));
normal - normalMat * normals;
gl_Position = proj * view * world;
}

View File

@ -206,6 +206,7 @@
<ClCompile Include="src\Renderer\Drawable.cpp" /> <ClCompile Include="src\Renderer\Drawable.cpp" />
<ClCompile Include="src\NFObject\Entity.cpp" /> <ClCompile Include="src\NFObject\Entity.cpp" />
<ClCompile Include="src\NFObject\Light.cpp" /> <ClCompile Include="src\NFObject\Light.cpp" />
<ClCompile Include="src\Renderer\GBuffer.cpp" />
<ClCompile Include="src\Renderer\Model.cpp" /> <ClCompile Include="src\Renderer\Model.cpp" />
<ClCompile Include="src\NFObject\Text.cpp" /> <ClCompile Include="src\NFObject\Text.cpp" />
<ClCompile Include="src\Renderer\UIElement.cpp" /> <ClCompile Include="src\Renderer\UIElement.cpp" />
@ -230,6 +231,7 @@
<ClInclude Include="src\include\Config.h" /> <ClInclude Include="src\include\Config.h" />
<ClInclude Include="src\include\Drawable.h" /> <ClInclude Include="src\include\Drawable.h" />
<ClInclude Include="src\include\Gamestate.h" /> <ClInclude Include="src\include\Gamestate.h" />
<ClInclude Include="src\include\GBuffer.h" />
<ClInclude Include="src\include\IndexBuffer.h" /> <ClInclude Include="src\include\IndexBuffer.h" />
<ClInclude Include="src\include\IntroGamestate.h" /> <ClInclude Include="src\include\IntroGamestate.h" />
<ClInclude Include="src\include\Input.h" /> <ClInclude Include="src\include\Input.h" />

View File

@ -84,6 +84,9 @@
<ClCompile Include="src\NFObject\Sound.cpp"> <ClCompile Include="src\NFObject\Sound.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\Renderer\GBuffer.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="src\include\Config.h"> <ClInclude Include="src\include\Config.h">
@ -167,6 +170,9 @@
<ClInclude Include="src\include\Sound.h"> <ClInclude Include="src\include\Sound.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="src\include\GBuffer.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Natvis Include="NatvisFile.natvis" /> <Natvis Include="NatvisFile.natvis" />

View File

@ -0,0 +1,68 @@
#include "GBuffer.h"
#include "GL/glew.h"
#include "Application.h"
#include "Entity.h"
namespace nf {
GBuffer::GBuffer() :
m_FBO(0),
m_depth(0),
m_width(0),
m_height(0)
{
glGenFramebuffers(1, &m_FBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
m_width = Application::getApp()->getConfig().width;
m_height = Application::getApp()->getConfig().height;
glGenTextures(m_textures.size(), &m_textures[0]);
for (unsigned int i = 0; i < m_textures.size(); i++) {
glBindTexture(GL_TEXTURE_2D, m_textures[i]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, m_width, m_height, 0, GL_RGB, GL_FLOAT, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, GL_TEXTURE_2D, m_textures[i], 0);
}
glGenTextures(1, &m_depth);
glBindTexture(GL_TEXTURE_2D, m_depth);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, m_width, m_height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, m_depth, 0);
GLenum draw[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT4, GL_COLOR_ATTACHMENT5, GL_COLOR_ATTACHMENT6, GL_COLOR_ATTACHMENT7 };
glDrawBuffers(m_textures.size(), draw);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void GBuffer::resize() {
unsigned int width, height;
width = Application::getApp()->getConfig().width;
height = Application::getApp()->getConfig().height;
if (m_width != width || m_height != height) {
m_width = width;
m_height = height;
for (unsigned int curr : m_textures) {
glBindTexture(GL_TEXTURE_2D, curr);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, m_width, m_height, 0, GL_RGB, GL_FLOAT, nullptr);
}
}
}
void GBuffer::render(std::vector<Entity*>& entites) {
}
void GBuffer::bindTextures() {
for (unsigned int i = 0; i < m_textures.size(); i++) {
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, m_textures[i]);
}
}
GBuffer::~GBuffer() {
for (unsigned int curr : m_textures)
glDeleteTextures(1, &curr);
glDeleteTextures(1, &m_depth);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glDeleteFramebuffers(1, &m_FBO);
}
}

View File

@ -6,6 +6,7 @@
#include "stb_image.h" #include "stb_image.h"
#include "Application.h" #include "Application.h"
#include "GBuffer.h"
#include "Shader.h" #include "Shader.h"
#include "Light.h" #include "Light.h"
#include "Entity.h" #include "Entity.h"
@ -17,6 +18,7 @@
namespace nf { namespace nf {
Renderer::Renderer(Application* app) : Renderer::Renderer(Application* app) :
m_gBuffer(nullptr),
m_shadowMapFBO(0), m_shadowMapFBO(0),
m_directionalDepthTexSize(0), m_directionalDepthTexSize(0),
m_pointDepthTexSize(0), m_pointDepthTexSize(0),
@ -73,6 +75,8 @@ namespace nf {
loadBaseAssets(); loadBaseAssets();
m_gBuffer = new GBuffer;
m_directionalDepthTexSize = 4096; m_directionalDepthTexSize = 4096;
m_pointDepthTexSize = 1024; m_pointDepthTexSize = 1024;
createShadowMap(); createShadowMap();
@ -354,6 +358,9 @@ namespace nf {
const char* entityVertex = m_baseAP["entityVertex.shader"]->data; const char* entityVertex = m_baseAP["entityVertex.shader"]->data;
const char* entityFragment = m_baseAP["entityFragment.shader"]->data; const char* entityFragment = m_baseAP["entityFragment.shader"]->data;
m_entityShader = new Shader(entityVertex, entityFragment); m_entityShader = new Shader(entityVertex, entityFragment);
const char* gBufferVertex = m_baseAP["gBufferVertex.shader"]->data;
const char* gBufferFragment = m_baseAP["gBufferFragment.shader"]->data;
m_gBufferShader = new Shader(gBufferVertex, gBufferFragment);
const char* textVertex = m_baseAP["textVertex.shader"]->data; const char* textVertex = m_baseAP["textVertex.shader"]->data;
const char* textFragment = m_baseAP["textFragment.shader"]->data; const char* textFragment = m_baseAP["textFragment.shader"]->data;
m_textShader = new Shader(textVertex, textFragment); m_textShader = new Shader(textVertex, textFragment);
@ -424,6 +431,7 @@ namespace nf {
delete m_cubemapShader; delete m_cubemapShader;
delete m_fadeShader; delete m_fadeShader;
delete m_directionalShadowShader; delete m_directionalShadowShader;
delete m_gBuffer;
delete m_fadeVAO; delete m_fadeVAO;
delete m_fadeIB; delete m_fadeIB;
ReleaseDC(m_app->getWindow(), m_hdc); ReleaseDC(m_app->getWindow(), m_hdc);

View File

@ -0,0 +1,24 @@
#pragma once
#include <vector>
#include <array>
namespace nf {
class Entity;
class GBuffer {
public:
GBuffer();
void resize();
void render(std::vector<Entity*>& entities);
void bindTextures();
~GBuffer();
private:
unsigned int m_FBO;
std::array<unsigned int, 5> m_textures; //TODO: Check this number
unsigned int m_depth;
unsigned int m_width, m_height;
};
}

View File

@ -64,6 +64,7 @@ namespace nf {
std::vector<Entity*> m_lGame; std::vector<Entity*> m_lGame;
Cubemap* m_cubemap; Cubemap* m_cubemap;
std::vector<UIElement*> m_lUI; std::vector<UIElement*> m_lUI;
Shader* m_gBufferShader;
Shader* m_entityShader; Shader* m_entityShader;
Shader* m_textShader; Shader* m_textShader;
Shader* m_uiTextureShader; Shader* m_uiTextureShader;

View File

@ -13,6 +13,7 @@ namespace nf {
class Light; class Light;
class Cubemap; class Cubemap;
class Camera; class Camera;
class GBuffer;
class VertexArray; class VertexArray;
class IndexBuffer; class IndexBuffer;
@ -44,6 +45,8 @@ namespace nf {
AssetPack m_baseAP; AssetPack m_baseAP;
GBuffer* m_gBuffer;
unsigned int m_shadowMapFBO; unsigned int m_shadowMapFBO;
int m_directionalDepthTexSize; int m_directionalDepthTexSize;
int m_pointDepthTexSize; int m_pointDepthTexSize;
@ -55,6 +58,7 @@ namespace nf {
std::vector<Entity*> m_lGame; std::vector<Entity*> m_lGame;
Cubemap* m_cubemap; Cubemap* m_cubemap;
std::vector<UIElement*> m_lUI; std::vector<UIElement*> m_lUI;
Shader* m_gBufferShader;
Shader* m_entityShader; Shader* m_entityShader;
Shader* m_textShader; Shader* m_textShader;
Shader* m_uiTextureShader; Shader* m_uiTextureShader;