2D Graphics and arbitrary-size textures

  • Follow


Hi,

 I did a couple of NeHe tutorials, but since I'm a zero at using 3D 
programs and much better at the drawing board, I'd like to whip together 
a little 2D Sprite Engine instead of doing real 3D. I'd love to use 
OpenGL for that, but I have a few things I'm not clear about:

1) I thought I'd draw my images on rectangular polygons by making them 
textures for the polygon. However, polygons are limited to be powers of 
two in size and square ... what's the best way to work around that?

-> make image and polygon square and just fill the unneeded part of the 
image with transparent pixels

-> scale image to be square (i.e. stretch it) and map it onto a 
non-square polygon, which should give me my original size again

-> make image square, polygon non-square and just map the image onto the 
polygon thus that the unneeded part "falls off" the polygon using 
glTexCoord()

-> ???

And which of these is faster?


2) What's a good way to do pixel-precise collision testing on the 
polygon, *and* on its texture? I'll need to determine both overlapping 
of polygons as well as detect clicks in polygons.


3) Is there any good sample code for 2D sprites with OpenGL somewhere?


BTW -- I'm using MacOS X, so samples for MacOS or FreeBSD would be very 
cool, but I'm grateful for any generous donations or even just pointers 
to a good FM to R.

Thanks & Cheers,
-- M. Uli Kusterer
http://www.zathras.de
0
Reply Uli 8/15/2003 12:32:39 AM

In article <0001HW.BB64890C00B036E5F0284600@netnews.comcast.net>,
 Derek Ledbetter <derekledbetter@mindspring.com> wrote:

> Actually, they don't have to be square.  The width and height can be
> different powers of two.

Ah,

 that'll make things considerably easier.

> Also, some cards support the
> GL_EXT_texture_rectangle extension.  This lets your texture dimensions
> be non-powers of two.

 Thanks, but that would make my code less portable. And I don't think my 
measly Rage 128 Mobility has this feature.

> The second method doesn't look to be faster that the others, and the
> image quality will suffer a bit from the resampling, so I wouldn't do
> that.

 That's what I thought, but a friend who has some OpenGL experience 
suggested it, so I thought I'd verify whether OpenGL has any weird 
behaviour that makes this work better.

> The third method fills fewer pixels than the first, so it should
> be faster.

 OK, I'll try that. Thanks for the infos.

Cheers,
-- M. Uli Kusterer
http://www.zathras.de
0
Reply Uli 8/20/2003 7:52:36 PM


>  Derek Ledbetter <derekledbetter@mindspring.com> wrote:
>>Also, some cards support the
>>GL_EXT_texture_rectangle extension.  This lets your texture dimensions
>>be non-powers of two.


Uli Kusterer replied:

>  Thanks, but that would make my code less portable. And I don't think my 
> measly Rage 128 Mobility has this feature.


You can allocate a texture with the next powers-of-two larger than your 
desired texture width and height and fill and use just the right 
portion. Use glTexImage2D with a null pointer-to-texels, and then 
glCopyTexImage2D to fill in the right portion.

You can't use REPEAT mode in this case, and you are using up more 
texture memory, but for most things it works just fine.

--
Andy V

0
Reply Andy 8/21/2003 4:08:00 AM

In article <AAX0b.216$m53.32708@petpeeve.ziplink.net>,
 Andy V <nobody@nowhere.net> wrote:

> You can allocate a texture with the next powers-of-two larger than your 
> desired texture width and height and fill and use just the right 
> portion. Use glTexImage2D with a null pointer-to-texels, and then 
> glCopyTexImage2D to fill in the right portion.

Andy,

 thanks, I'll look into the gl(...)TexImage(...) stuff.

 I feel a little bad asking about all this stuff ... does anyone know a 
URL where I can find documentation on the various (groups of) OpenGL 
APIs? There's got to be some sort of FAQ about this stuff somewhere?

 I madly googled for all that stuff, but the best I got were extremely 
dry pages that told me about the matrix maths behind the various APIs... 
if I wanted to get involved in the mathematics of 3D graphics, I'd be 
writing my own 3D rendering code... ;-)

 Also, it seems to be pretty hard to find much on 2D. Googles search 
patterns always give me mostly 3D stuff. Anybody know any sites that 
tell about that? Or, failing that, a book with a decent chapter on 2D 
OpenGL?

Thanks in advance,
-- M. Uli Kusterer
http://www.zathras.de
0
Reply Uli 8/21/2003 12:38:32 PM

In article <HM82b.220$m53.33347@petpeeve.ziplink.net>,
 Andy V <nobody@nowhere.net> wrote:

> www.opengl.org is a good starting place -- you can get the 
> specifications of OpenGL, plus there are at least 3 FAQ resources there, 
> including the technical FAQ managed by fungus.

Hmmm... strange, I didn't find anything there the first time round. It 
was pretty much only SGI's advertisements ... I'll give it another try. 
Maybe a Google limited to opengl.org will bring something up.

> I'm afraid you will have to bite the bullet and become comfortable with 
> matrix math. You will need to know this stuff even using OpenGL.

 I think I'll be able to avoid it for my 2D stuff right now. I have 
simple requirements :-)

> 2D is 3D with a constant Z value -- often 0.0. For that matter, "3D" is 
> really "homogeneous" space with a W value of 1.0. You can get away 
> without learning about homogeneous coordinates for a while. :-)

 Yeah, it's just that a lot of the sample code does very complicated 
things which are needed for 3D, but shouldn't be necessary when doing 2D 
(not counting that I'm carrying around a useless 3rd coordinate, I'm 
fine with that).

Well, I'll manage somehow, thanks.
Cheers,
-- M. Uli Kusterer
http://www.zathras.de
0
Reply Uli 8/28/2003 3:49:57 PM

4 Replies
135 Views

(page loaded in 0.046 seconds)

Similiar Articles:













7/10/2012 7:25:04 PM


Reply: