PDA

View Full Version : Bizarre Framebuffer render to texture issue



MrLloyd1981
07-12-2012, 10:08 PM
I've been having various issues trying to render a spinning teapot to a texture, then render said texture to a full screen quad as baby steps into framebuffer goodness. I was getting no texture to the screen and checking in gDebugger there was no data being rendered to my texture.

HOWEVER, if I comment out the following line in the framebuffer init code:





//glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COM PONENT,fboData.depthWidth,fboData.depthHeight);



I get a STATIC teapot rendered to my texture. Upon further experimentation I tried commenting out two very important lines:



//GLenum drawBufs[] = {GL_COLOR_ATTACHMENT0};
//glDrawBuffers(1,drawBufs);


and in the corresponding fragment shader I removed layout(location=0) out vec3 color and changed it to out vec3 color. This means there is no color attachment and as such nothing should render right? However, the static teapot STILL renders to texture. So I have a few questions:

1) Why does my framebuffer fail to render to texture at all when i allocate renderbufferStorage

2) why does the texture still render when i remove calls to glDrawBuffers?

3) why is the teapot STATIC? if i remove the call to glBindBuffer before the teapot render and remove the quad texturing code I can see that the teapot renders and rotates just fine!

This has been troubling me a lot! PLease,please help.

My version of opengl is 3.3 and my graphics card is a Radeon HD 4200 mobility card.

You can look at my code here:

http://www.mediafire.com/?cezo2t1knpfype1

or browse below



#include "..\shared\Includes.h"
#include "..\shared\ShaderUtilities.h"
#include "Texture1.h"
#include "Cube.h"
#include "Teapot.h"


void Render();


GLuint renderProgram,textureProgram,textureUniform;
GLuint MVPMatrixUniform;
Texture testTexture;
GLuint vertexbuffer;
GLuint uvbuffer;
GLuint normalbuffer;
GLuint elementbuffer;


glm::mat4 perspMatrix,MVPMatrix,camMatrix,scaleMatrix,transf ormMatrix;

VBOTeapot* teapot;
FBO frameBuf;



const std::string strVertexShaderFile1 = "render.vp";

const std::string strFragmentShaderFile1 = "render.fp";

const std::string strVertexShaderFile2 = "texture.vp";

const std::string strFragmentShaderFile2 = "texture.fp";


///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Function prototypes!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////


void InitVertexBuffer();

GLvoid Reshape(GLsizei width,GLsizei height);

void InitGL();

void DrawGLScene();

void Update();

static const GLfloat g_quad_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
};

GLuint quad_vertexbuffer;




int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPT H);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutCreateWindow("ArcSynth shizznit");

GLenum err = glewInit();
if (GLEW_OK != err)
{
return 1;
}

//int things for this app
InitGL();

//callback funcs

glutDisplayFunc(DrawGLScene);
glutReshapeFunc(Reshape);
glutMainLoop();

glDeleteTextures(1,&frameBuf.texID);
glDeleteBuffers(1,&frameBuf.handle);
delete teapot;
return 0;
}



void InitVertexBuffer()
{

glGenBuffers(1, &quad_vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_quad_vertex_buffer_data), g_quad_vertex_buffer_data, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,0);
}


GLvoid Reshape(GLsizei width,GLsizei height)
{
if(height == 0)
height = 1;


glViewport(0,0,width,height);

//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();


//glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
}

void InitProgramSpecificData()
{
MVPMatrixUniform = glGetUniformLocation(renderProgram,"MVPMatrix");

ZeroMemory(&frameBuf,sizeof(frameBuf));

frameBuf.depthWidth = 640;
frameBuf.depthHeight = 480;


FrameBufferObject(frameBuf,GL_NEAREST,GL_NEAREST);

perspMatrix = glm::perspective(45.0f,1.0f,0.2f,600.0f);


teapot = new VBOTeapot(8.0f,glm::translate(0.0f,0.0f,0.0f));

camMatrix = glm::lookAt(glm::vec3(0.0f,2.0f,-7.0f),
glm::vec3(0.0f,2.0f,0.0f),
glm::vec3(0.0f,1.0f,0.0f));

InitVertexBuffer();



}

void InitGL(GLvoid)
{
glShadeModel(GL_SMOOTH);
glClearColor(0.5f,0.0f,0.0f,0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glEnable(GL_MULTISAMPLE);
InitVertexBuffer();

std::vector<GLchar*> attribs;
attribs.push_back("position");
InitShaderProgram(ReadFile(strVertexShaderFile1),R eadFile(strFragmentShaderFile1),renderProgram,attr ibs);
InitShaderProgram(ReadFile(strVertexShaderFile2),R eadFile(strFragmentShaderFile2),textureProgram,att ribs);

InitProgramSpecificData();

}


void Update()
{
static float angle = 0.0f;

angle += 0.05f;

if(angle > 360.0f)
angle = 0.0f;

glm::mat4 rotateMatrix = glm::rotate(angle,0.0f,1.0f,0.0f);

transformMatrix = rotateMatrix * glm::rotate(-90.0f,1.0f,0.0f,0.0f);

}

void RenderToTexture()
{
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT );
glUseProgram(renderProgram);

glUniformMatrix4fv(MVPMatrixUniform,1,GL_FALSE,&MVPMatrix[0][0]);


//set variables for the fbo
glBindFramebuffer(GL_FRAMEBUFFER,frameBuf.handle);
glViewport(0,0,640,480);
glEnableVertexAttribArray(0);
teapot->render();
glUseProgram(0);
glDisableVertexAttribArray(0);
glBindFramebuffer(GL_FRAMEBUFFER,0);
//glBindTexture(GL_TEXTURE_2D,0);
}

void DrawTextureToQuad()
{
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT );
glViewport(0,0,640,480);
glUseProgram(textureProgram);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,frameBuf.texID);
GLuint texID = glGetUniformLocation(textureProgram, "renderedTexture");
glUniform1i(texID,0);

glEnableVertexAttribArray(0);



//Render to the screen
glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);

// Draw the triangle !

glDrawArrays(GL_TRIANGLES, 0, 6); // From index 0 to 3 -> 1 triangle

glDisableVertexAttribArray(0);
glUseProgram(0);
}

void DrawGLScene(GLvoid)
{

MVPMatrix = perspMatrix * camMatrix * transformMatrix;

Update();
RenderToTexture();
DrawTextureToQuad();



glutSwapBuffers();
glutPostRedisplay();
}



framebuffer setup code as follows



void FrameBufferObject(FBO& fboData,GLenum minFilter,GLenum magFilter)
{


glGenFramebuffers(1,&fboData.handle);
glBindFramebuffer(GL_FRAMEBUFFER,fboData.handle);

//GLenum drawBufs[] = {GL_COLOR_ATTACHMENT0};
//glDrawBuffers(1,drawBufs);

glGenTextures(1,&fboData.texID);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,fboData.texID);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,minFilter);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,magFilter);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,fboData.dept hWidth,fboData.depthHeight,0,GL_RGBA,GL_UNSIGNED_B YTE,NULL);

glFramebufferTexture2D(GL_FRAMEBUFFER,GL_COLOR_ATT ACHMENT0,GL_TEXTURE_2D,fboData.texID,0);

glGenRenderbuffers(1,&fboData.depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER,fboData.depthBu ffer);

//glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COM PONENT,fboData.depthWidth,fboData.depthHeight);

glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ ATTACHMENT,GL_RENDERBUFFER,fboData.depthBuffer);


if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout<<"problem with framebuffer!";
else
std::cout<<"framebuffer ok! :)";


glBindRenderbuffer(GL_RENDERBUFFER,0);
glBindFramebuffer(GL_FRAMEBUFFER,0);

}


Please help! Regards, Lloyd