PDA

View Full Version : Backbuffer as Texture



jos_t_tarigan
12-27-2010, 11:28 AM
hi,

im trying to draw something on my backbuffer, save it as a texture, and then put the texture in a simple plane. here is the snipplet of my code:



void display() {

glClearColor(1.0, 1.0, 1.0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushAttrib(GL_COLOR_BUFFER_BIT | GL_PIXEL_MODE_BIT);
glDrawBuffer(GL_BACK);
glReadBuffer(GL_BACK);

drawOrbit();

glBindTexture(GL_TEXTURE_2D, img);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, width, height);
glBindTexture(GL_TEXTURE_2D, 0);

glPopAttrib();


glClearColor(1.0, 1.0, 1.0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

glLoadIdentity();

//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

glTranslatef(0, 0, -1.5f);
glTranslatef(0, 0, -zoom);

glBindTexture(GL_TEXTURE_2D, img);
glBegin(GL_QUADS);
{
glNormal3f(0.0, 0.0, 1.0);
glTexCoord2f(1, 1); glVertex3f(1,1,0);
glTexCoord2f(0, 1); glVertex3f(-1,1,0);
glTexCoord2f(0, 0); glVertex3f(-1,-1,0);
glTexCoord2f(1, 0); glVertex3f(1,-1,0);
}
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
//glFlush();
glutSwapBuffers();
}


the drawOrbit() method is simply drawing to the screen and works properly. however i can see the result on my screen. it simply draw the plane with default color. i use variable img as my texture ide, here is when i set my texture:



glGenTextures(1, &img);
glBindTexture(GL_TEXTURE_2D, img);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, 0);



did i miss anything? how can i make it works?

thanks in advance

carsten neumann
12-27-2010, 12:59 PM
Before drawing your textured plane you need to enable texturing:



glEnable(GL_TEXTURE_2D);