PDA

View Full Version : Framebuffer giving a black screen



Rockinredross867
01-01-2016, 11:34 PM
I have been following this guy's nice tutorials http://learnopengl.com/ specifically this one http://learnopengl.com/#!Advanced-OpenGL/Framebuffers I ran into a problem with my window just rendering a black screen. I think it is because of my model causing the problem, but I could be wrong. Here is my code.

core.cpp


#include <iostream>

#include "data\graphics\window.h"
#include "data\shaders\shaderProgram.h"
#include "data\graphics\buffers\buffer.h"
#include "data\graphics\buffers\indexBuffer.h"
#include "data\graphics\buffers\vertexarray.h"
#include "data\graphics\renderer.h"
#include "data\graphics\texture\texture.h"
#include "data\graphics\buffers\bufferdata.h"
#include "data\gameobjects\light.h"
#include "data\gameobjects\model\model.h"
#include "data\graphics\gui\rawframe.h"

int main() {
std::vector<glm::vec3> lightPositions;
lightPositions.push_back(glm::vec3(0.0f, 4.0f, 0.0f));
lightPositions.push_back(glm::vec3(0.0f, 2.0f, 0.0f));

JokesterEngine::graphics::Window m_Window(1080, 720, "SPACE BATTLES");
JokesterEngine::shaders::ShaderProgram m_ModelShader("Resources/shaders/modelVertexShader.txt", "Resources/shaders/modelFragmentShader.txt");
JokesterEngine::shaders::ShaderProgram m_FBOShader("Resources/shaders/framebufferVertexShader.txt", "Resources/shaders/framebufferFragmentShader.txt");
JokesterEngine::gameobject::Model m_Model("Resources/models/nanosuit/nanosuit.obj");
JokesterEngine::graphics::Renderer m_Renderer;
JokesterEngine::gameobject::Light light(lightPositions);
JokesterEngine::graphics::TextureLoader m_TextureLoader;

GLuint texture = m_TextureLoader.generateAttachment(GL_FALSE, GL_FALSE);

glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

GLfloat quadVertices[] = {
-1.0f, 1.0f, 0.0f, 1.0f,
-1.0f, -1.0f, 0.0f, 0.0f,
1.0f, -1.0f, 1.0f, 0.0f,

-1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f
};

GLuint VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);

glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);

glBufferData(GL_ARRAY_BUFFER, sizeof(quadVertices), quadVertices, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)0);

glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (GLvoid*)(2 * sizeof(GLfloat)));

glBindVertexArray(0);

GLuint FBO, RBO;
glGenFramebuffers(1, &FBO);
glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);

glGenRenderbuffers(1, &RBO);

glBindRenderbuffer(GL_RENDERBUFFER, RBO);

glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, 1080, 720);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, RBO);

if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout << "FRAMEBUFFER::ERROR: Framebuffer is incomplete!" << std::endl;

glBindFramebuffer(GL_FRAMEBUFFER, 0);

while (!m_Window.closed())
{
m_Window.varUpdate();
m_Window.Do_Movement();

glBindFramebuffer(GL_FRAMEBUFFER, FBO);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);

m_ModelShader.enable();

GLuint projectionLoc = glGetUniformLocation(m_ModelShader.m_ProgramID, "projection");
GLuint viewLoc = glGetUniformLocation(m_ModelShader.m_ProgramID, "view");
GLuint modelLoc = glGetUniformLocation(m_ModelShader.m_ProgramID, "model");
GLuint viewPosLoc = glGetUniformLocation(m_ModelShader.m_ProgramID, "viewPos");

glm::mat4 view;
view = m_Window.m_Camera.GetViewMatrix();

glm::mat4 projection;
projection = glm::perspective(45.0f, (float) m_Window.getWindowX() / (float) m_Window.getWindowY(), 0.1f, 100.0f);

glm::mat4 model;
model = glm::translate(model, glm::vec3(0.0f));
model = glm::scale(model, glm::vec3(0.2f));

glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniform3f(viewPosLoc, m_Window.m_Camera.Position.x, m_Window.m_Camera.Position.y, m_Window.m_Camera.Position.z);

m_ModelShader.loadPointLight(light, 2);
m_Model.Draw(m_ModelShader);

glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);

m_FBOShader.enable();
glBindVertexArray(VAO);
glBindTexture(GL_TEXTURE_2D, texture);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);

m_Window.update();
}

glDeleteFramebuffers(1, &FBO);
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
m_ModelShader.disable();

return 0;
}


texture.h


#pragma once

#include <SOIL\SOIL.h>
#include <GL\glew.h>
#include <iostream>

namespace JokesterEngine {
namespace graphics {
class TextureLoader {
public:
GLuint loadTexture(const char* path, GLboolean boolean);
GLuint generateAttachment(GLboolean depth, GLboolean stencil);
};
}
}

texture.cpp


#include "texture.h"

namespace JokesterEngine {
namespace graphics {

GLuint TextureLoader::loadTexture(const char* path, GLboolean boolean)
{
GLuint textureID;

glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, boolean == GL_TRUE ? GL_REPEAT : GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, boolean == GL_TRUE ? GL_REPEAT : GL_CLAMP_TO_EDGE);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

int width, height;
unsigned char* image = SOIL_load_image(path, &width, &height, 0, SOIL_LOAD_RGBA);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D, 0);

return textureID;
}

GLuint TextureLoader::generateAttachment(GLboolean depth, GLboolean stencil)
{
GLenum attachment_type;
if (!depth && !stencil)
attachment_type = GL_RGB;
else if (depth && !stencil)
attachment_type = GL_DEPTH_COMPONENT;
else if (!depth && stencil)
attachment_type = GL_STENCIL_INDEX;

GLuint textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
if (!depth && !stencil)
glTexImage2D(GL_TEXTURE_2D, 0, attachment_type, 1080, 720, 0, attachment_type, GL_UNSIGNED_BYTE, NULL);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, 1080, 720, 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);

return textureID;
}

}
}

framebufferVertexShader.txt


#version 330 core

layout (location = 0) in vec2 position;
layout (location = 1) in vec2 texCoords;

out vec2 TexCoords;

void main()
{
gl_Position = vec4(position, 0.0f, 1.0f);
TexCoords = texCoords;
}

framebufferFragmentShader.txt


#version 330 core

in vec2 TexCoords;

out vec4 color;

uniform sampler2D screenTexture;

void main()
{
color = texture(screenTexture, TexCoords);
}