View Full Version : Render to Texture - works intermittently.
06-09-2010, 05:28 AM
Hi. I am trying to set up rendering to a texture using orthogonal projection. I've done it before with perspective projection, but I am making a 2d application and need the same functionality.
Thing is it works sometimes and sometimes it doesn't its just random. So I've forgotten to initialise something. I am always doing the rendering at the same time, at the start of the frame. Here is the code:
// Copy Our ViewPort To The Texture
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 0, 0, TEXSIZE, TEXSIZE, 0);
I call this right after clearing the screen buffer at the start of rendering. Sometimes the image is there (on its black background) and sometimes the texture is just black. Can anything think of what I'm doing wrong?
06-09-2010, 05:35 AM
My texture creation code just to ensure I'm doing that right.
glTexImage2D(GL_TEXTURE_2D, 0, 4, TEXSIZE, TEXSIZE, 0,
GL_RGBA, GL_UNSIGNED_BYTE, 0);
06-09-2010, 10:29 PM
maybe you didn't enable?
i dunno, i run into that one a lot. i'm a newbie :|
06-10-2010, 05:27 AM
I don't see it. Perhaps best to encode you problem into a short GLUT test program that you can post in its entirety. Here's a template to start from: link (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&Number=278315#Post2783 15)
06-10-2010, 08:07 AM
unsigned int finish();
unsigned int texture;
unsigned int frameBuffer;
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 90, 90, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texture, 0);
if (glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT) != GL_FRAMEBUFFER_COMPLETE_EXT)
unsigned int RenderToTexture::finish()
void checkGLError( const char hdr )
int err = glGetError();
if( err )
fprintf(stderr, "ERROR %s: %s\n", hdr, gluErrorString(err));
void reshape(GLsizei w, GLsizei h)
glViewport(0, 0, w, h);
void keyboard( unsigned char key, int x, int y )
// Key Bindings
switch( key )
case 27 : exit(0); break;
glClearColor( 0,0,1,1 );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
renderQuad(); // Creates a texture with a square cut out of the corner
renderQuad();// Tries to render a square in the corner with the texture on it
checkGLError( "display() end" );
main( int argc, char *argv )
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB );
glutCreateWindow( "window title" );
checkGLError( "Create window" );
glutReshapeFunc ( reshape );
glutDisplayFunc ( display );
glutKeyboardFunc( keyboard );
rtt = new RenderToTexture;
06-10-2010, 08:12 AM
That code in the post uses a different method, the original posting uses one method involving copying the current framebuffer into a texture - the new method sets the texture as the current rendering context. Neither of them work :(
06-10-2010, 10:52 AM
Well, I don't see a glDrawBuffer() call there.
06-10-2010, 05:42 PM
Testing. Will update with problems as I notice them:
1) You're reallocating the texture's base map every frame. That's not good. Call glTexImage2D only once when you allocate the texture up near glGenTexture. Set the filtering there too.
3) You are not clearing the texture you are rendering to
4) Your code looks like you are trying to read from and write to the same texture at the same time. Generally speaking this is not allowed (see NV_texture_barrier for an exception). The only thing saving you from doing this is that you have glDisable( GL_TEXTURE_2D ) before you render to your FBO texture.
5) After your rtt->finish() call line, do this:
glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE );
so you can see your texture.
6) Also, as mentioned, use glDrawBuffer( GL_COLOR_ATTACHMENT0 ); and glDrawBuffer( GL_BACK );
Powered by vBulletin® Version 4.2.3 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.