I have tested rendering the teapot straight to the screen, that was ok. I has tested my quad shading code by making the screen yellow in a fragment shader, that was ok too. However when trying to render to the texture I just don't get the results I want! I get NOTHING!
I have a framebuffer struct, and a helper function to set up a FBO as required:
typedef struct
{
GLubyte * imageData;
GLuint bpp;
GLuint width;
GLuint height;
GLuint texID;
GLuint type;
} Texture;
typedef struct
{
GLuint handle;
GLuint depthBuffer;
GLint depthHeight;
GLint depthWidth;
Texture textureData;
Texture nonTexture;
}FBO;
void FrameBufferObject(FBO& fboData,GLenum minFilter,GLenum magFilter)
{
glGenFramebuffers(1,&fboData.handle);
glBindFramebuffer(GL_FRAMEBUFFER,fboData.handle);
glGenTextures(1,&fboData.textureData.texID);
glBindTexture(GL_TEXTURE_2D,fboData.textureData.texID);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,fboData.depthWidth,fboData.depthHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,NULL);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glGenRenderbuffers(1,&fboData.depthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER,fboData.depthBuffer);
glRenderbufferStorage(GL_RENDERBUFFER,GL_DEPTH_COMPONENT,fboData.depthWidth,fboData.depthHeight);
glFramebufferRenderbuffer(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_RENDERBUFFER,fboData.depthBuffer);
glFramebufferTexture(GL_FRAMEBUFFER,GL_COLOR_ATTACHMENT0,fboData.textureData.texID,0);
GLenum drawBufs[] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1,drawBufs);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
std::cout<<"problem with framebuffer!";
else
std::cout<<"framebuffer ok! ";
glBindFramebuffer(GL_FRAMEBUFFER,0);
}
my shader code is as follows:
//texture.vp
#version 330
in vec3 position;
out vec2 UV;
void main()
{
gl_Position = vec4(position,1);
UV = (position.xy+vec2(1,1))/2.0;
}
//texture.fp
#version 330
in vec2 UV;
out vec3 color;
uniform sampler2D renderedTexture;
void main()
{
color = texture(renderedTexture,UV).rgb;
}
I call my rendering code as follows...
void RenderToTexture()
{
glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT );
glUseProgram(renderProgram);
glUniformMatrix4fv(MVPMatrixUniform,1,GL_FALSE,&MVPMatrix[0][0]);
glEnableVertexAttribArray(0);
//set variables for the fbo
glBindFramebuffer(GL_FRAMEBUFFER,frameBuf.handle);
glViewport(0,0,512,512);
GLenum drawBufs[] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1,drawBufs);
teapot->render();
glUseProgram(0);
glDisableVertexAttribArray(0);
glBindFramebuffer(GL_FRAMEBUFFER,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.textureData.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();
}
If anyone can help, I'd be really grateful!