How to copy a subsection of a texture to the same texture?

How to copy a subsection of a texture to the same texture?

Post by Stephen An » Wed, 05 Nov 2003 09:16:07


I plan to have a 320x240 pixel texture for an emulator I'm working on.
Because of the power of 2 limitation, the texture will actually be
512x256. So I plan to store font data in the 64x256 unused strip on
the right side of the texture.

I'm wondering how to 'cut' pieces of this texture and display it
within *the same texture*.

So say that letter 'A' was located at (x,y)=(350, 16) and was 8 pixels
x 8 pixels. Further, I want to draw the 'A' at location (x,y)=(50,
75). Basically, I'd want to copy an 8x8 pixel image from location
(350,16) to location (50,75).

Is this even possible? I assumed that dealing with only one texture
would make things a lot faster. And it wouldn't use any more memory
than normal since it was allocated anyway. And I plan to
pre-calculate the locations for each character.

I think (hope) this is a very fast way to do it. All I need to know
is:

1) can it even be done?
2) if so, what commands are required to do it?

To anyone familiar with 2D programming, I'm trying to implement a
'blit' from one part of a surface to another part. I assume that
since the texture is in video card memory, it would be faster to copy
from within that texture (in the OpenGL server), vs having to send
the data from the client.

Any help is appreciated,
Steve
 
 
 

How to copy a subsection of a texture to the same texture?

Post by fungu » Wed, 05 Nov 2003 10:06:55


I'm not sure I understand. Is this texture for storing
the screen? How do you intend to draw to it? You mention
"performance" a lot but glTexSubImage() isn't fast at all.


That's nice but I think you need to get other stuff clear
before worrying about the text.

If glTexSubImage() is fast enough for the rest of it then
it should be fast enough for the text as well...


--
<\___/> For email, remove my socks.
/ O O \
\_____/ FTB. Why isn't there mouse-flavored cat food?

 
 
 

How to copy a subsection of a texture to the same texture?

Post by Stephen An » Wed, 05 Nov 2003 20:55:04


Yes, the emulator generates a palette based framebuffer, which I convert to
a texture and send to OpenGL. What I propose is that the fonts be part of
that texture, but I only draw the screen itself every frame (using the
appropriate GL_QUAD. It seems fast enough for what I do. What other
method do you suggest?


I do have everything else done. I only post code examples here because the
actual program code is too large. So basically, could someone answer my
original question. Specifically, if a 512x256 texture is present in the
OpenGL server (ie, has already been sent), how do I copy a subsection OF
THAT TEXTURE to the screen?


Again, when I speak about speed, I meant that I thought it would be faster
to have everything in one texture, versus having one texture for the screen
(with wasted space because of power of 2), and then 256 more textures (one
for each character). So it would be faster in terms of only having to bind
to one texture, ever.

Steve
 
 
 

How to copy a subsection of a texture to the same texture?

Post by Roger Rowl » Wed, 05 Nov 2003 21:39:38

If you only want part of the texture, just draw a quad and work out the
correct texture coordinates to map to the section you want.

Or did *I* misunderstand too? (wouldn't be the first time ;-)))


----------------------------------------------------------------------------
-----------
EMail: roger dot rowland at rmrsystems dot co dot uk
 
 
 

How to copy a subsection of a texture to the same texture?

Post by Stephen An » Wed, 05 Nov 2003 22:58:28


Since there have been a number of misunderstandings, I would say that I
explained it incorrectly :( So I'll try to give more detail. You know the
saying, "A picture is worth a 1000 words". So have a look at the following
picture: http://www.yqcomputer.com/ ~stephena/texture.png

I have a texture that is 512x256, which is represented in the picture by the
outermost rectangle. This is created with glTexImage2D() at the beginning
of the program.

The main area (as designated in the picture) is approx 320 x 256. I update
that using glTexSubImage2D() with the appropriate sized QUAD.

Now, have a look at the picture. Say I want to copy the letter 'A' from the
area indicated (the offscreen font area of the texture) to the designated
area in the onscreen part of the texture. Can this be done, and if so,
how?

Or more generally, say that the font data was in an entirely different
texture. How would I copy PIECES of it to another texture. All the
examples I've seen explain how to copying a FULL texture onto the current
one (which would work if I had a separate 8x8 texture for each character).
What I want to know is, how do you copy PART OF a texture onto another one.

And if possible, I'd like to do this in texture memory, and not have to send
the font data to the OpenGL server. In other words, the full texture has
already been sent to the OpenGL server, and I want to move parts of it onto
itself (I assume this is fast, since it doesn't involve a round-trip from
client to server).

Thanks,
Steve
 
 
 

How to copy a subsection of a texture to the same texture?

Post by Roger Rowl » Wed, 05 Nov 2003 23:38:21

Ok, I did misunderstand :-(

I though you said you wanted a section of texture copied to the screen, not
to another texture.

Having said that, is there really any difference? If you want to just
replace part of a textured quad, is it just a matter of drawing another
(smaller) quad on top and using whatever texture coordinates you need to map
to your replacement section. I would guess that's easier unless you somehow
need to *combine* the two sections (like overlaying transparent text)?

If the latter, then you can't just do a copy anyway, you'll need to use
belnding or somesuch.

Am I getting warmer now?

--
----------------------------------------------------------------------------
-----------
EMail: roger dot rowland at rmrsystems dot co dot uk



the
following
the
update
the
one.
send
onto
 
 
 

How to copy a subsection of a texture to the same texture?

Post by Stephen An » Thu, 06 Nov 2003 04:24:13


Well, yes, I do want it copied to the screen. It just so happens that the
screen is a texture :) Now that I think of it, since the text will overlay
whatever is on the screen, I don't actually want to copy it to a texture,
but to the screen itself. I'm sorry, but I confused the terminology here.


Actually, it is the latter :) The text itself will have alpha of one, but
the background of it will have alpha of 0, so that I create a billboarding
(letting those pixels in the 8x8 character which are not actually character
data be transparent.

Come to think of it, maybe it isn't as hard as I thought. I will try some
other stuff and get back to you.

Thanks,
Steve
 
 
 

How to copy a subsection of a texture to the same texture?

Post by fungu » Thu, 06 Nov 2003 06:38:49


I understand that. The bit which is escaping me at the
moment is that you're worrying about the cost of binding
a texture in a program which uses glTexSubImage2D().

To use an analogy: That's like falling in the ocean and
worring if a raindrop might fall from the sky and hit
you.

Put all your letters in a separate texture (just one) and
bind it when you need them.


Use a quad with the appropriate texture coordinates.

> onto the current one

Use the render_texture extension, draw a quad, don't use
(0,0), (0,1), (1,1), (1,0) as the texture coordinates.


Onto itself or onto another texture? Make your mind up!

If you mean onto itself then you're stuck, you can't do it
directly.

--
<\___/> For email, remove my socks.
/ O O \
\_____/ FTB. Why isn't there mouse-flavored cat food?
 
 
 

How to copy a subsection of a texture to the same texture?

Post by Stephen An » Thu, 06 Nov 2003 07:41:13


OK, this was a mistake on my part. Some extra OpenGL commands which
were not required were causing CPU usage to go through the roof. I
assumed it was because of binding to another texture, when it was
because of repeated calls to glEnable(GL_TEXTURE_2D). I now
understand what you're saying, and won't mention performance again.


This is what I will experiment with next.


I think the problem here is that I used the wrong terminology. I will
experiment with copying from a separate font texture and see what
happens.

Thanks for the help so far,
Steve