OpenGL: Drawing to a texture

Well im just a bit stuck wondering how to draw an item to a texture.

Edit: Well unfortunately my graphics card doesn't support FrameBuffer Objects :/. So i've been trying to get the copy contents from backbuffer method working.

Edit: So i've been trying now for the best part of a couple of hours to get this thing working. Unfortunately all i still get is a white square.

Here's my little testing code that i have set-up. What it should do is draw a red square at 100, 100. But even this isn't working for me :/.

glViewport(0 , 0, 512, 512);

glPushAttrib(GL_CURRENT_BIT);
glColor4ub(255, 0, 0, 255);
glBindTexture(GL_TEXTURE_2D, Texture);

// Top-left vertex (corner)
glTexCoord2i(0, 0);
glVertex2i(100, 100);

// Bottom-left vertex (corner)
glTexCoord2i(1, 0);
glVertex2i( 100 + 200, 100);

// Bottom-right vertex (corner)
glTexCoord2i(1, 1);
glVertex2i( 100 + 200, 100 + 200);

// Top-right vertex (corner)
glTexCoord2i(0, 1);
glVertex2i(100, 100 + 200);

glEnd();
glPopAttrib();

//Copy Our ViewPort To The Texture
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 0, 0, 512, 512, 0);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
//Reset Viewport
glViewport(0, 0, 768, 480);

glBindTexture (GL_TEXTURE_2D, Texture);
glEnable(GL_TEXTURE_2D);

// Top-left vertex (corner)
glTexCoord2i(0, 0);
glVertex2i(0, 0);

// Bottom-left vertex (corner)
glTexCoord2i(1, 0);
glVertex2i( 0 + 512, 0);

// Bottom-right vertex (corner)
glTexCoord2i(1, 1);
glVertex2i( 0 + 512, 0 + 512);

// Top-right vertex (corner)
glTexCoord2i(0, 1);
glVertex2i(0, 0 + 512);

glEnd();
glDisable(GL_TEXTURE_2D);


Screenshot of the current output:

Any help is greatly appreciated!

• Have a look at this. If you don't have FBO support, you can render to back buffer and then do do glCopyTexSubImage2D – Dan Jun 22 '12 at 17:11
• I thought that's what i was doing already? in = pastebin.com/dJpPt6Pd. – dan369 Jun 22 '12 at 17:41

This is most likely not how you want to call this function! The first index is an offset relative to vertex data sources. Most likely, you want:

glDrawArrays(GL_LINE_STRIP, 0, indices.size());


As for your actual question, use a framebuffer object: http://www.opengl.org/wiki/Framebuffer_Object - alternatively, you can also simply draw to the back-buffer and copy that to a texture via glCopyTextureX

• Unfortunately my graphics card doesn't support frameBuffers :/. So since you've answered ive been trying to get your alternative answer working. Unfortunately it just isn't working for me :/. I'll update my question – dan369 Jun 22 '12 at 16:16

I understand that you want to actually "render to a texture", other words, render something and rather than showing it on screen store it as a texture.

If this is the case, you should read about Frame Buffer Objects (FBOs) and the "renter to texture" techniques, which basically consists in attaching a texture to an FBO.

I would recommend that you read this, which explains pretty much everything you need and contains a fantastic demo, in which they render a teapot and use it to texture a cube. Here you have another tutorial.