Completed a simple gBuffer

This commit is contained in:
Grayson Riffe (Laptop) 2021-09-24 13:16:20 -05:00
parent 4d4b6d51ae
commit be4df5c853
16 changed files with 168 additions and 83 deletions

View File

@ -18,6 +18,16 @@ void MainState::onEnter() {
cm.create(nf::BaseAssets::cubemap);
sound.create(ap["sound.wav"]);
for (int x = 0; x < 10; x++) {
for (int y = 0; y < 10; y++) {
for (int z = 0; z < 10; z++) {
entities.push_back(new nf::Entity);
entities.back()->create(ap["spec.obj"]);
entities.back()->setPosition(5.0 + x * 2.1, 0.05 + z * 2.1, -5.0 + y * 2.1);
}
}
}
}
void MainState::update(double deltaTime) {
@ -51,7 +61,7 @@ void MainState::update(double deltaTime) {
xrot += offset;
if (app->isKeyHeld(NFI_RIGHT))
xrot -= offset;
light.setPosition(nf::Vec3(-5.0, yrot / 10.0 + 10.0, 5.0));
plane.setRotation(nf::Vec3(yrot * 10.0, 0.0, yrot * 10.0));
text.setText("FPS: " + std::to_string(app->getFPS()));
@ -73,6 +83,9 @@ void MainState::render(nf::Renderer& renderer) {
renderer.render(button);
renderer.render(button2);
renderer.render(cm);
for (nf::Entity* curr : entities)
renderer.render(*curr);
}
void MainState::onExit() {

View File

@ -23,4 +23,6 @@ private:
nf::Sound sound;
float xrot, yrot;
std::vector<nf::Entity*> entities;
};

View File

@ -5,6 +5,5 @@ uniform float opacity;
out vec4 color;
void main() {
color = vec4(0.0, 0.0, 0.0, opacity);
color = vec4(vec3(0.0), opacity);
}

View File

@ -3,6 +3,5 @@
layout(location = 0) in vec2 pos;
void main() {
gl_Position = vec4(pos.xy, 0.0, 1.0);
}

View File

@ -1,2 +1,43 @@
#version 330 core
in vec3 fragPos;
in vec2 texCoord;
in vec3 normal;
struct Material {
bool hasDiffuseTex;
sampler2D diffuseTexture;
vec3 diffuseColor;
bool hasSpecTex;
sampler2D specularTexture;
float specPower;
bool hasNormTex;
sampler2D normalTexture;
};
uniform Material material;
layout(location = 0) out vec3 pos;
layout(location = 1) out vec3 normals;
layout(location = 2) out vec3 diffuse;
layout(location = 3) out vec3 specular;
void main() {
pos = fragPos;
if (material.hasNormTex)
normals = texture(material.normalTexture, texCoord).xyz;
else
normals = normalize(normal);
if (material.hasDiffuseTex)
diffuse = texture(material.diffuseTexture, texCoord).rgb;
else
diffuse = material.diffuseColor;
specular.r = material.specPower;
if (material.hasSpecTex)
specular.g = texture(material.specularTexture, texCoord).r;
else
specular.g = 1.0;
}

View File

@ -17,7 +17,7 @@ void main() {
fragPos = world.xyz;
texCoord = texCoords;
mat3 normalMat = transpose(inverse(mat3(model)));
normal - normalMat * normals;
normal = normalMat * normals;
gl_Position = proj * view * world;
}

View File

@ -9,17 +9,6 @@ struct Camera {
vec3 pos;
};
struct Material {
bool hasDiffuseTex;
sampler2D diffuseTexture;
vec3 diffuseColor;
bool hasSpecTex;
sampler2D specularTexture;
float specPower;
bool hasNormTex;
sampler2D normalTexture;
};
struct Light {
//Directional = 1, Point = 2
int type;
@ -31,7 +20,6 @@ struct Light {
};
uniform Camera camera;
uniform Material material;
uniform Light light[100];
uniform int numberOfLights;
uniform bool isContinued;

View File

@ -71,7 +71,7 @@ namespace nf {
return m_front;
}
void Camera::bind(Shader* entityShader, Shader* cubemapShader) {
void Camera::bind(Shader* gBufferShader, Shader* lightingShader, Shader* cubemapShader) {
glm::mat4 view;
switch (m_type) {
@ -83,7 +83,7 @@ namespace nf {
int mouseDiffx = 0;
int mouseDiffy = 0;
m_app->getMouseDiff(mouseDiffx, mouseDiffy);
float mouseX = (float)mouseDiffx * 0.1f;
float mouseX = (float)mouseDiffx * 0.1f; //TODO: Mouse sensitivity
float mouseY = (float)mouseDiffy * 0.1f;
m_yaw += mouseX;
m_pitch += mouseY;
@ -102,9 +102,6 @@ namespace nf {
break;
}
}
glm::vec3 pos(m_position.x, m_position.y, m_position.z);
entityShader->setUniform("camera.pos", pos);
glm::vec3 rotation;
rotation.x = std::cos(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
rotation.y = std::sin(glm::radians(m_pitch));
@ -114,7 +111,10 @@ namespace nf {
glm::vec3 position(m_position.x, m_position.y, m_position.z);
glm::vec3 up(0.0, 1.0, 0.0);
view = glm::lookAt(position, position + rotation, up);
entityShader->setUniform("view", view);
gBufferShader->setUniform("view", view);
glm::vec3 pos(m_position.x, m_position.y, m_position.z);
//lightingShader->setUniform("camera.pos", pos);
glm::mat4 cubemapView = glm::mat4(glm::mat3(view));
cubemapShader->setUniform("view", cubemapView);

View File

@ -4,6 +4,7 @@
#include "Application.h"
#include "Entity.h"
#include "Shader.h"
namespace nf {
GBuffer::GBuffer() :
@ -17,22 +18,58 @@ namespace nf {
m_width = Application::getApp()->getConfig().width;
m_height = Application::getApp()->getConfig().height;
glGenTextures(m_textures.size(), &m_textures[0]);
glGenTextures(1, &m_textures[0]);
glGenTextures(1, &m_textures[1]);
glGenTextures(1, &m_textures[2]);
glGenTextures(1, &m_textures[3]);
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);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, m_width, m_height, 0, GL_RGB, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
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 };
unsigned int draw[] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, GL_COLOR_ATTACHMENT3 };
glDrawBuffers(m_textures.size(), draw);
glGenRenderbuffers(1, &m_depth);
glBindRenderbuffer(GL_RENDERBUFFER, m_depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, m_width, m_height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depth);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void GBuffer::render(std::vector<Entity*>& entites, Shader* shader) {
int prevFBO;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prevFBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_FBO);
resize();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_BLEND);
for (Entity* curr : entites)
curr->render(shader);
glEnable(GL_BLEND);
//TODO: Blit depth buffer for transparent objects later
glBindFramebuffer(GL_FRAMEBUFFER, prevFBO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_textures[1]);
}
void GBuffer::bindTextures(Shader* shader) {
for (unsigned int i = 0; i < m_textures.size(); i++) {
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, m_textures[i]);
}
shader->setUniform("gBPos", 0);
shader->setUniform("gBNorm", 1);
shader->setUniform("gBDiff", 2);
shader->setUniform("gBSpec", 3);
}
void GBuffer::resize() {
unsigned int width, height;
width = Application::getApp()->getConfig().width;
@ -44,17 +81,11 @@ namespace nf {
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]);
glDeleteRenderbuffers(1, &m_depth);
glGenRenderbuffers(1, &m_depth);
glBindRenderbuffer(GL_RENDERBUFFER, m_depth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, m_width, m_height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depth);
}
}

View File

@ -82,6 +82,7 @@ namespace nf {
Material::~Material() {
delete m_diffuseTexture;
delete m_specularTexture;
delete m_normalTexture;
}
Model::Model(AModel* model) :

View File

@ -15,6 +15,7 @@
#include "Button.h"
#include "Camera.h"
#include "Utility.h"
#include "Texture.h"
namespace nf {
Renderer::Renderer(Application* app) :
@ -79,7 +80,7 @@ namespace nf {
m_directionalDepthTexSize = 4096;
m_pointDepthTexSize = 1024;
createShadowMap();
createShadowMaps();
if (!m_app->isCustomWindowIcon()) {
ATexture& windowTex = *(ATexture*)m_baseAP["defaultwindowicon.png"];
@ -152,13 +153,23 @@ namespace nf {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera->bind(m_entityShader, m_cubemapShader);
//Draw Entities (3D models)
glm::mat4 proj = glm::perspective(glm::radians(45.0f), (float)m_app->getConfig().width / (float)m_app->getConfig().height, 0.1f, 10000.0f);
m_entityShader->bind();
m_entityShader->setUniform("proj", proj);
for (Entity* draw : m_lGame) {
camera->bind(m_gBufferShader, m_lightingShader, m_cubemapShader);
//First, draw the cubemap if one is currently set
if (m_cubemap != nullptr) {
m_cubemapShader->setUniform("proj", proj);
m_cubemap->render(m_cubemapShader);
}
m_cubemap = nullptr;
//Fill gBuffer with entities
m_gBufferShader->setUniform("proj", proj);
m_gBuffer->render(m_lGame, m_gBufferShader);
m_lGame.clear();
//Light entities using the gBuffer
/*for (Entity* draw : m_lGame) {
Entity& curr = *draw;
unsigned int drawCount = (unsigned int)std::ceil(m_lights.size() / (double)m_texSlots);
if (drawCount == 0)
@ -171,11 +182,11 @@ namespace nf {
currLight = -(int)lightsRemaining;
for (unsigned int i = 0; i < drawCount; i++) {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
m_entityShader->setUniform("isContinued", false);
m_lightingShader->setUniform("isContinued", false);
if (i != 0) {
glBlendFunc(GL_ONE, GL_ONE);
glDepthFunc(GL_LEQUAL);
m_entityShader->setUniform("isContinued", true);
m_lightingShader->setUniform("isContinued", true);
}
unsigned int currLightsDrawn;
if (lightsRemaining >= m_texSlots)
@ -184,28 +195,21 @@ namespace nf {
currLightsDrawn = lightsRemaining;
lightsRemaining -= currLightsDrawn;
currLight += (int)currLightsDrawn;
m_entityShader->setUniform("numberOfLights", (int)currLightsDrawn);
m_lightingShader->setUniform("numberOfLights", (int)currLightsDrawn);
for (unsigned int j = 0; j < currLightsDrawn; j++) {
m_lights[j + (unsigned int)currLight]->bind(m_entityShader, j);
m_lights[j + (unsigned int)currLight]->bind(m_lightingShader, j);
}
renderShadowMaps(currLight, currLightsDrawn);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
curr.render(m_entityShader);
curr.render(m_lightingShader);
}
glDepthFunc(GL_LESS);
}
m_lGame.clear();
m_lGame.clear();*/
m_lights.clear();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//Draw cubemap where there isn't anything else
if (m_cubemap != nullptr) {
m_cubemapShader->setUniform("proj", proj);
m_cubemap->render(m_cubemapShader);
}
m_cubemap = nullptr;
//Draw UI elements
glDisable(GL_DEPTH_TEST);
proj = glm::ortho(0.0f, (float)m_app->getConfig().width, 0.0f, (float)m_app->getConfig().height);
@ -245,8 +249,8 @@ namespace nf {
opacity -= 2.5 * dT;
if (opacity <= 0.0) {
m_fadeIn = false;
m_fadeOutComplete = false;
opacity = 1.0;
m_fadeOutComplete = false;
}
}
else if (m_fadeOut) {
@ -263,17 +267,18 @@ namespace nf {
opacity += 3.0 * dT;
if (opacity >= 1.0) {
m_fadeIn = false;
m_fadeOutComplete = true;
opacity = 0.0;
m_fadeOutComplete = true;
}
}
glEnable(GL_DEPTH_TEST);
SwapBuffers(m_hdc);
//Check for OpenGL errors
GLenum err = glGetError();
if (err != GL_NO_ERROR)
Error("OpenGL error " + std::to_string(err));
SwapBuffers(m_hdc);
}
void Renderer::renderShadowMaps(unsigned int startingLight, unsigned int count) {
@ -282,6 +287,8 @@ namespace nf {
glm::mat4 pointLightProj = glm::perspective(glm::radians(90.0f), 1.0f, nearP, farP);
glm::mat4 lightView;
glm::mat4 lightSpaceMat;
int prevFBO;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prevFBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_shadowMapFBO);
for (unsigned int i = 0; i < count; i++) {
Light::Type type = m_lights[i]->getType();
@ -301,7 +308,7 @@ namespace nf {
std::string stringPos = "lightSpaceMat[";
stringPos += std::to_string(i);
stringPos += "]";
m_entityShader->setUniform(stringPos, lightSpaceMat);
m_lightingShader->setUniform(stringPos, lightSpaceMat);
for (Entity* curr : m_lGame) {
curr->render(m_directionalShadowShader, true);
}
@ -310,7 +317,7 @@ namespace nf {
stringPos += "].directionalDepthTex";
glActiveTexture(GL_TEXTURE3 + i);
glBindTexture(GL_TEXTURE_2D, tex);
m_entityShader->setUniform(stringPos, 3 + (int)i);
m_lightingShader->setUniform(stringPos, 3 + (int)i);
break;
}
case Light::Type::POINT: {
@ -344,23 +351,25 @@ namespace nf {
stringPos += "].pointDepthTex";
glActiveTexture(GL_TEXTURE3 + i);
glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
m_entityShader->setUniform(stringPos, 3 + (int)i);
m_entityShader->setUniform("farPlane", farP);
m_lightingShader->setUniform(stringPos, 3 + (int)i);
m_lightingShader->setUniform("farPlane", farP);
break;
}
}
}
m_entityShader->setUniform("numMats", (int)count);
m_lightingShader->setUniform("numMats", (int)count);
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
glBindFramebuffer(GL_FRAMEBUFFER, prevFBO);
}
void Renderer::loadBaseAssets() {
m_baseAP.load("base.nfpack");
const char* entityVertex = m_baseAP["entityVertex.shader"]->data;
const char* entityFragment = m_baseAP["entityFragment.shader"]->data;
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* lightingVertex = m_baseAP["lightingVertex.shader"]->data;
const char* lightingFragment = m_baseAP["lightingFragment.shader"]->data;
//m_lightingShader = new Shader(lightingVertex, lightingFragment);
const char* textVertex = m_baseAP["textVertex.shader"]->data;
const char* textFragment = m_baseAP["textFragment.shader"]->data;
m_textShader = new Shader(textVertex, textFragment);
@ -393,7 +402,7 @@ namespace nf {
BaseAssets::button = (AButton*)m_baseAP["default.button"];
}
void Renderer::createShadowMap() {
void Renderer::createShadowMaps() {
m_texSlots = 13;
glGenFramebuffers(1, &m_shadowMapFBO);
glBindFramebuffer(GL_FRAMEBUFFER, m_shadowMapFBO);
@ -425,7 +434,8 @@ namespace nf {
}
Renderer::~Renderer() {
delete m_entityShader;
delete m_gBufferShader;
delete m_lightingShader;
delete m_textShader;
delete m_uiTextureShader;
delete m_cubemapShader;

View File

@ -27,7 +27,7 @@ namespace nf {
const Vec3& getPosition();
const Vec3& getRotation();
void bind(Shader* entityShader, Shader* cubemapShader);
void bind(Shader* gBufferShader, Shader* lightingShader, Shader* cubemapShader);
~Camera();
private:

View File

@ -4,19 +4,20 @@
namespace nf {
class Entity;
class Shader;
class GBuffer {
public:
GBuffer();
void resize();
void render(std::vector<Entity*>& entities);
void bindTextures();
void render(std::vector<Entity*>& entities, Shader* shader);
void bindTextures(Shader* shader);
~GBuffer();
private:
void resize();
unsigned int m_FBO;
std::array<unsigned int, 5> m_textures; //TODO: Check this number
std::array<unsigned int, 4> m_textures; //TODO: Check this number
unsigned int m_depth;
unsigned int m_width, m_height;

View File

@ -44,7 +44,7 @@ namespace nf {
void renderShadowMaps(unsigned int startingLight, unsigned int count);
void loadBaseAssets();
void createShadowMap();
void createShadowMaps();
Application* m_app;

View File

@ -36,7 +36,7 @@ namespace nf {
void renderShadowMaps(unsigned int startingLight, unsigned int count);
void loadBaseAssets();
void createShadowMap();
void createShadowMaps();
Application* m_app;
@ -59,7 +59,7 @@ namespace nf {
Cubemap* m_cubemap;
std::vector<UIElement*> m_lUI;
Shader* m_gBufferShader;
Shader* m_entityShader;
Shader* m_lightingShader;
Shader* m_textShader;
Shader* m_uiTextureShader;
Shader* m_cubemapShader;