PDA

View Full Version : GL texture rect copy?



Almindor
12-01-2007, 10:02 PM
Hi, I'm trying to sort of simulate SDL "style" of 2D operations via openGL and don't understand one thing.

I need to draw a rectangular area from one texture to the screen or to another texture.

I've found the glCopyTexImage2D() and glCopyTexSubImage2D() functions but I don't understand one thing. What is the source? I mean, do I have to first draw the source texture somewhere before I can "copy" it ?

Nitrogen
12-01-2007, 10:21 PM
The glCopyTexSubImage2D() function should copy what's on the screen/framebuffer straight into the currently bound (blank) texture.

This is an excerpt from a dynamic texture class at www.Delphi3d.net:


procedure TFrameBufferTexture.BeginTextureUpdate;
begin
//Save the drawing style and all settings
glPushAttrib(GL_ALL_ATTRIB_BITS);
glCallList(FSetup); //Setup all the drawing styles needed for the texture update
glViewport(0, 0, FWidth, FHeight); //restrict the drawing area to the size of the texture.

end;

//After calling BeginTextureUpdate, you would draw onto the screen like you would normally, except it will be drawn to the texture, not the screen.

procedure TFrameBufferTexture.EndTextureUpdate;
begin

// The texture is rendered, now copy the image.
glBindTexture(GL_TEXTURE_2D, FTexture);

glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, FWidth, FHeight);

glPopAttrib; //Restore the saved states.

end;

Nitrogen
12-01-2007, 10:27 PM
If you want to load a bitmap into a texture and display that on the screen, well, that's substantially easier. There are plenty of good opengl tuts out there which show "drawing your first texture"

To get a subsection of a texture to draw, you need to alter the texture coordinates of each vertex of the rectangle drawn to the screen.

To draw from one texture to another, you will first have to draw the first texture to the screen, then use the method described above to copy from the screen (or should I say framebuffer) to the second texture.

Almindor
13-01-2007, 09:54 AM
Yes thanks, I fiddled with it yesterday and got some first results, albeit odd regarding the width and height (but I didn't change viewport so I guess that's it). I'll report the results when it's done :)

Almindor
13-01-2007, 10:18 PM
Ok I got something to work but I got 2 problems:

glCopyTexSubImage2d() doesn't work for me (result = empty texture), whereas with glCopyTexImage2d() I get the expected result BUT:

only if I "copy" the whole thing. If I change the width or height param it get's distorted. Moving x, y works ok as long as w and h are sized as the original.

Am I missing something here?