Sign in to follow this  
htz92127

The question of FBO

Recommended Posts

htz92127    107
I want to render some thing to the RenderTarget, then render the RenderTarget on the screen. But the question is that, the result of RenderTarget is not accurate right. It have been stretched.
I use this code to change the RenderTarget:

[code]
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,mCurrentTarget.nFboID);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, mCurrentTarget.nWidth, mCurrentTarget.nHeight);[/code]


Is there something wrong?

Share this post


Link to post
Share on other sites
htz92127    107
[quote name='capricorn' timestamp='1311513905' post='4839585']
Would you mind showing us your texture dimensions and exactly how you output it to the screen?
[/quote]
[size="2"]Sure, but my code is in[/size][size="2"][size="2"] my[/size][size="2"] office[/size][/size][size="2"][size="2"] computer, I am at home now. I will paste it out tomorrow.[/size][/size]
[size="3"]And there is another question:[/size]
[size="2"]I call glClearColor() function after [/size][size="2"]glBindFramebufferEXT to clear the BackColor of the fbo[/size][size="2"], But the BackColor of normal buffer is also be changed. Why such a situation?[/size]

Share this post


Link to post
Share on other sites
capricorn    139
[quote name='htz92127' timestamp='1311517678' post='4839594']
[size="2"]I call glClearColor() function after [/size][size="2"]glBindFramebufferEXT to clear the BackColor of the fbo[/size][size="2"], But the BackColor of normal buffer is also be changed. Why such a situation?[/size]
[/quote]

[font=monospace][size=2]GL_COLOR_CLEAR_VALUE is a part of rendering context state. It is not tied to any specific buffer.[/size][/font]

Share this post


Link to post
Share on other sites
htz92127    107
[quote name='capricorn' timestamp='1311518231' post='4839597']
[quote name='htz92127' timestamp='1311517678' post='4839594']
[size="2"]I call glClearColor() function after [/size][size="2"]glBindFramebufferEXT to clear the BackColor of the fbo[/size][size="2"], But the BackColor of normal buffer is also be changed. Why such a situation?[/size]
[/quote]

[font="monospace"][size="2"]GL_COLOR_CLEAR_VALUE is a part of rendering context state. It is not tied to any specific buffer.[/size][/font]
[/quote]
Maybe there is some Ambiguity of my expression. I mean that:
[color="#1C2837"][font="CourierNew, monospace"][size="2"] [/size][/font][/color]
[color="#1C2837"][font="CourierNew, monospace"][size="2"] [/size][/font][/color][code]
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,mCurrentTarget.nFboID);
glClearColor(1.0, 0, 0, 0);
glViewport(0,0, mCurrentTarget.nWidth, mCurrentTarget.nHeight);
[/code]

[size="2"]This code [/size][size="2"]should just change the backcolor of the FBO which id is nFboID, But the window's color([/size]glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)[size=2]) is also be changed to red. why?[/size]

Share this post


Link to post
Share on other sites
capricorn    139
[font="monospace"][size="2"][color="#1c2837"]No. This code, among other things, should change context-global background color value to red.[/color][/size][/font]

Share this post


Link to post
Share on other sites
Vincent_M    969
You are getting results with FBOs? I just posted a question about this. My texture doesn't display when drawing the texture to a sprite in my sprite batch: [url="http://www.gamedev.net/topic/607000-framebuffer-objects-opengl/"]http://www.gamedev.net/topic/607000-framebuffer-objects-opengl/[/url]. Everything checks out fine, but I get nothing.

Share this post


Link to post
Share on other sites
htz92127    107
[quote name='Vincent_M' timestamp='1311540863' post='4839733']
You are getting results with FBOs? I just posted a question about this. My texture doesn't display when drawing the texture to a sprite in my sprite batch: [url="http://www.gamedev.net/topic/607000-framebuffer-objects-opengl/"]http://www.gamedev.n...objects-opengl/[/url]. Everything checks out fine, but I get nothing.
[/quote]
Yes, I got a results, But the viewport is not right.

The code of create fbo:

[code]
u32 nTexID;
glGenTextures(1, &nTexID);
glBindTexture(GL_TEXTURE_2D, nTexID);
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);

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

GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);

if (GL_FRAMEBUFFER_COMPLETE_EXT != status)
{
return ImageRef::Null();
}

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT,0);[/code]

The code of draw fbo textrue:



[code]

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, mCurrentTarget.nID);
glClearColor(1, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, mCurrentTarget.nWidth, mCurrentTarget.nHeight);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex->GetID());

glColor4ub(color.R, color.G, color.B, color.A);
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();[/code]


Is there some thing wrong?

Share this post


Link to post
Share on other sites
htz92127    107
[quote name='V-man' timestamp='1311564837' post='4839829']
Are you setting up your projection matrix when rendering to that FBO?
[/quote]

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:


[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;
}[/code]

Share this post


Link to post
Share on other sites
Vincent_M    969
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.

Share this post


Link to post
Share on other sites
htz92127    107
[quote name='Vincent_M' timestamp='1311578120' post='4839883']
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.
[/quote]
Thanks for you attention. The problem has been solved. The reason is that: After glBindFramebufferEXT, must use follow code to change viewport and project.


[code]
glBindFramebufferEXT(...)

glViewport(0, 0, xxWidth, xxHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, xxWidth, xxHeight, 0, 1000, -1000);[/code]


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

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