The question of FBO

Started by
11 comments, last by htz92127 12 years, 8 months ago

Are you setting up your projection matrix when rendering to that FBO?


I have set it at the beginning like this: glOrtho(0, mWidth, mHeight, 0, 1000, -1000);

I have regulated my code as a sample demo, this is the entire code:



typedef unsigned int u32;

u32 mWidth;
u32 mHeight;

u32 texID;
u32 fboID;

u32 mFboWidth = 256;
u32 mFboHeight = 256;

// create fbo
bool CreateFbo(int nWidth, int nHeight)
{
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, nWidth, nHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

glGenFramebuffersEXT(1,&fboID);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,fboID);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texID, 0);
u32 buffers[] = {GL_COLOR_ATTACHMENT0_EXT};
glDrawBuffers(1, buffers);

GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);

if (GL_FRAMEBUFFER_COMPLETE_EXT != status)
{
return false;
}

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);
return true;
}

void DrawLine(int x1, int y1, int x2, int y2)
{
glDisable(GL_TEXTURE_2D);
glColor4ub(255, 0, 0, 255);
glBegin(GL_LINES);
glVertex2i(x1, y1);
glVertex2i(x2, y2);
glEnd();
}

void DrawFbo()
{
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fboID);
glClearColor(1, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, mFboWidth, mFboHeight);
DrawLine(0, 0, mFboWidth, mFboHeight);
DrawLine(0, 40, mFboWidth, 40);
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
glViewport(0, 0, mWidth, mHeight);
}

void DrawFboTexture(int x1, int y1, int x2, int y2)
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texID);

glColor4ub(255, 255, 255, 255);
glBegin(GL_QUADS);

glTexCoord2f(0.0f, 1.0f);
glVertex2i(x1, y1);

glTexCoord2f(1.0f, 1.0f);
glVertex2i(x2, y1);

glTexCoord2f(1.0f, 0.0f);
glVertex2i(x2, y2);

glTexCoord2f(0.0f, 0.0f);
glVertex2i(x1, y2);

glEnd();
}


void Display()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);

// draw a line for comparison
DrawLine(mFboWidth, 0, mFboWidth, 600);


// prepare FBO
DrawFbo();

// draw FBO texture buffer
DrawFboTexture(0, 100, mFboWidth, mFboHeight);

glutSwapBuffers();
glFlush();
}

void Init(int _Argc, char** argv)
{
glutInit(&_Argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_STENCIL);
glutInitWindowPosition(100, 100);
glutInitWindowSize(mWidth, mHeight);
glutCreateWindow("FBO test");
glewInit();

glOrtho(0, mWidth, mHeight, 0, 1000, -1000);
glViewport(0, 0, mWidth, mHeight);
glutDisplayFunc(&Display);
glutIdleFunc(&Display);

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

glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);

glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);

glDisable(GL_DEPTH_TEST);

CreateFbo(mFboWidth, mFboHeight);
}

int main(int _Argc, char** argv)
{
mWidth = 800;
mHeight = 600;
Init(_Argc, argv);
glutMainLoop();
return 0;
}
Advertisement
I'm using straight shaders for everything, so my matrix pipeline is all done in software with my math library. Can you post a screen shot of the results you are getting? I was able to get post-processing working today. The code in my thread may be able to help you. It's posted above. Are the dimensions are your rendered texture the same as the viewport dimensions? When rendering to a texture, the dimensions do not need to be a power of 2 from my studies. If you'd like to use power of 2 textures, I have a simple work-around for that too.

I'm using straight shaders for everything, so my matrix pipeline is all done in software with my math library. Can you post a screen shot of the results you are getting? I was able to get post-processing working today. The code in my thread may be able to help you. It's posted above. Are the dimensions are your rendered texture the same as the viewport dimensions? When rendering to a texture, the dimensions do not need to be a power of 2 from my studies. If you'd like to use power of 2 textures, I have a simple work-around for that too.

Thanks for you attention. The problem has been solved. The reason is that: After glBindFramebufferEXT, must use follow code to change viewport and project.



glBindFramebufferEXT(...)

glViewport(0, 0, xxWidth, xxHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, xxWidth, xxHeight, 0, 1000, -1000);



Also want to thanks all the people who help me about this problem.

This topic is closed to new replies.

Advertisement