Sign in to follow this  
dominic1988

Framebuffer with multi texturing

Recommended Posts

Hi everyone,

Currently i am trying to render my scene to a render target. However I have encountered an odd problem with the framebuffer, everytime i tried to render the scene using multiple texture, what i get is a red screen. If it is rendering using only 1 texture, it is fine. Here is my setup for the framebuffer

void GenerateSandFrameBuffer()
{
glGenTextures(1, &sandPlaneTexture);
glBindTexture(GL_TEXTURE_2D, sandPlaneTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SFW::System::Instance()->WinSizeX(), SFW::System::Instance()->WinSizeY(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);

glGenFramebuffers(1, &sandFrameBuffer);
glBindFramebuffer(GL_FRAMEBUFFER, sandFrameBuffer);
//Attach 2D texture to this FBO
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, sandPlaneTexture, 0);
//-------------------------
glGenRenderbuffers(1, &sandDepthBuffer);
glBindRenderbuffer(GL_RENDERBUFFER, sandDepthBuffer);
//Attach depth buffer to FBO
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, sandDepthBuffer);

glBindTexture(GL_TEXTURE_2D, 0);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, SFW::System::Instance()->WinSizeX(), SFW::System::Instance()->WinSizeY());

//-------------------------


}

//This is where i draw the scene
void DrawRefractionScene()
{
// clear the backbuffer
glBindFramebuffer(GL_FRAMEBUFFER, sandFrameBuffer);
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// setup viewport and projection
f32 winSizeX = (f32)(SFW::System::Instance()->WinSizeX());
f32 winSizeY = (f32)(SFW::System::Instance()->WinSizeY());

glViewport(0, 0, winSizeX, winSizeY);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float)(winSizeX)/(float)(winSizeY), CameraNear, CameraFar);

// set the model
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
g_camera.LoadMatrix();

// simple sample scene
gSandShaders.BindShaders();

//set my first texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, sandTexture);

//set my second texture
//glActiveTexture(GL_TEXTURE0 + 1);
//glBindTexture(GL_TEXTURE_2D, sandNormalTexture);

int sandTextureLocation = glGetUniformLocation(gSandShaders.GetProgramID(), "SandTexture");
glUniform1i(sandTextureLocation, 0);

glPushMatrix();
glScalef(5.0f, 5.0f, 5.0f);
SFW::Shape::Draw(SFW::Shape::TYPE_DOME);
glPopMatrix();

gSandShaders.UnBindShaders();
}
//this is where i render the scene to a viewport to view
void DrawScreenQuad()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glViewport(0.6f * SFW::System::Instance()->WinSizeX(), 0.6f * SFW::System::Instance()->WinSizeY(), 0.6f * SFW::System::Instance()->WinSizeX(), 0.4f * SFW::System::Instance()->WinSizeY());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 0.3f * SFW::System::Instance()->WinSizeX(), 0.3f * SFW::System::Instance()->WinSizeY(), 0, -1.0f, 1.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, sandPlaneTexture);
glBegin(GL_QUADS);

glTexCoord2f(0.0f, 1.0f);
glVertex2i(0, 0);

glTexCoord2f(0.0f, 0.0f);
glVertex2i(0, 0.3f * SFW::System::Instance()->WinSizeY());

glTexCoord2f(1.0f, 0.0f);
glVertex2i(0.2f * SFW::System::Instance()->WinSizeX(), 0.3f * SFW::System::Instance()->WinSizeY());

glTexCoord2f(1.0f, 1.0f);
glVertex2i(0.2f * SFW::System::Instance()->WinSizeX(), 0);
glEnd();

glDisable(GL_TEXTURE_2D);
}

When I have multiple texture, it becomes red. MultipleTexture.jpg shows how it looks like. Does anyone knows what causes this problem? I am suspecting that it has to do with the framebuffer. If it is, is there a solution to solve it?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this