Skip to main content
gamedev.net
🔒 Locked

Render-To-Texture - pBuffer

Started by absriram 4 replies 2.9k views
Original Post
absriram
absriram
Hi All, I want to do Render-to-Texture using pbuffers. I tried the following without using pbuffers and if works fine. I draw a red quad bigger than the size of my screen and copy the framebuffer values to a texture. I then clear the screen to Grey. Now I render a green triangle and use the previously created texture on the triangle. I get a red triangle, which is correct. Now I tried the same thing using pbuffer. I create a pbuffer and make it the current rendering context. I render the red quad to the pbuffer. Now, I make the window the current rendering context and bind the pbuffer to a texture. I then draw the green triangle and use the previously created texture on it. I should be getting a red triangle here too. But I don't. Sometimes,I get a green triangle & half green and half black triangle some other time. I don't know whats happening. I am including my code here. It would be great someone can help me out in this. Program without using pBuffer:
void Draw (void)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glClearColor (0.5f, 0.5f, 0.5f, 1.0f);
glClearDepth (1.0f);
glLoadIdentity();

gluLookAt(0.0f,0.0f,50.0f,0.0f,0.0f,0.0f,0,1,0);

glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex3f(-100.0,-100.0,-10.0);
glVertex3f( 100.0,-100.0,-10.0);
glVertex3f( 100.0, 100.0,-10.0);
glVertex3f(-100.0, 100.0,-10.0);
glEnd();

texture1 = LoadGLTextures();

glBindTexture(GL_TEXTURE_2D, texture1);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, 512, 512, 0);

glClear(GL_COLOR_BUFFER_BIT); //Clear screen to grey color

glColor3f(0.0,0.0,1.0);
glBegin(GL_TRIANGLES);
glTexCoord2f(0.0f,0.0f); glVertex3f(-10.0, 3.0, 0.0);
glTexCoord2f(1.0f,0.0f); glVertex3f( 0.0, 3.0, 0.0);
glTexCoord2f(0.5f,0.5f); glVertex3f( -5.0,10.0, 0.0);
glEnd();

glFlush();
}
Here's the LoadGLTextures() method
GLuint LoadGLTextures(void)
{

glGenTextures(1, &texture);        // Create The Texture
glBindTexture(GL_TEXTURE_2D, texture);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

return texture;

}
Here is the code using pbuffers:
void Draw (void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glClearColor (0.5f, 0.5f, 0.5f, 1.0f);
glClearDepth (1.0f);
glLoadIdentity();

gluLookAt(0.0f,0.0f,50.0f,0.0f,0.0f,0.0f,0,1,0);

pbufferEnable(512, 512);

glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex3f(-100.0,-100.0,-10.0);
glVertex3f( 100.0,-100.0,-10.0);
glVertex3f( 100.0, 100.0,-10.0);
glVertex3f(-100.0, 100.0,-10.0);
glEnd();

pbufferDisable(512, 512);
pbufferBind();

glColor3f(0.0,0.0,1.0);
glBegin(GL_TRIANGLES);
glTexCoord2f(0.0f,0.0f); glVertex3f(-10.0, 3.0, 0.0);
glTexCoord2f(1.0f,0.0f); glVertex3f( 0.0, 3.0, 0.0);
glTexCoord2f(0.5f,0.5f); glVertex3f( -5.0,10.0, 0.0);
glEnd();

glFlush();
}

The pbufferEnable(), pbufferDisable(), and pbufferBind are from ATI's pbuffer demo. My window size is also 512,512. I am initializing the pbuffer in my InitGL() method. Thank you, Sriram
silvermace
silvermace
a pbuffer has its own openGl context, and as a result has its own GL_MODELVIEW_MATRIX and GL_PROJECTION_MATRIX etc.

when you enable the pbuffer to render to it, you need to setup its projection matrix (gluPerspective etc.) and also need to change the view frustrum (gluLookAt)

so you're new code would be something like this:

// GameDev.net CodeBox Formatter By Danushka "Silvermace"
// http://www.unsoundminds.com/codeformat/
void InitGL() {
// create your pbuffer here and do the appropriate gluPerspective calls
}

// helper function
void preDraw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor (0.5f, 0.5f, 0.5f, 1.0f);
glClearDepth (1.0f);

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

gluLookAt(0.0f,0.0f,50.0f,0.0f,0.0f,0.0f,0,1,0);
}

void Draw (void)
{
preDraw();

// enter PBuffer OpenGL context
pbufferEnable(512, 512);
preDraw();

glColor3f(1.0,0.0,0.0);
glBegin(GL_QUADS);
glVertex3f(-100.0,-100.0,-10.0);
glVertex3f( 100.0,-100.0,-10.0);
glVertex3f( 100.0, 100.0,-10.0);
glVertex3f(-100.0, 100.0,-10.0);
glEnd();

pbufferDisable(512, 512);
pbufferBind();
// now back too application OpenGL context

glColor3f(0.0,0.0,1.0);
glBegin(GL_TRIANGLES);
glTexCoord2f(0.0f,0.0f); glVertex3f(-10.0, 3.0, 0.0);
glTexCoord2f(1.0f,0.0f); glVertex3f( 0.0, 3.0, 0.0);
glTexCoord2f(0.5f,0.5f); glVertex3f( -5.0,10.0, 0.0);
glEnd();

glFlush();
}
absriram
absriram
Hi I tried it all, but it still doesn't seem to work. Is there a way to check if my rendering to pbuffer is done correctly?

Sriram.
absriram
absriram
Hi,

Can I do glCopyTexImage2d() when I am rendering to pbuffer, just as I did when not using pbuffer? Just in case the pbuffer binding is not working.

Sriram
s_p_oneil
s_p_oneil
Did you realize that you also have to bind the textures to the pbuffer's context? And I believe there are cases where textures created in one context can't be seen in another, so you may even need to create and load the texture with the pbuffer context enabled. All OpenGL state calls (i.e. lighting settings) also need to be set up for your pbuffer context. You may also have forgotten to set your pbuffer's projection matrix and viewport.

You should start with the simplest test you can think of. Enable the pbuffer, disable lighting and textures, set up a very simple projection and modelview matrix, and draw a white triangle. If it works, then build on it from there.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.