2D with openGL/SDL

Doing 2D wiht openGL isn’t bad at all. I’m currently working a library for
this exact purpose, but it’s not ready yet (and may never be), so here are
some quick tips –

Resize all your images so that the dimensions are a power of two.
Otherwise you must use mipmaps, which use more memory.

Loading the images… This part’s tricky. I use a second program I wrote which
uses image magick to convert to a specialized format for use with openGL
textures. You can do something similar with SDL_Image, but it won’t
be easy. Don’t forget you want FOUR channels (RGBA), and watch out for
byte ordering. Good luck.

Create all your textures during init. This is when images are transferred
over the AGP bus to the graphics card. Then free the images from main
memory.

Bind to the appropraite texture and draw a polygon to draw the “sprite”.
Make sure the color is
maxed (i.e. do a glColor4f(1.0, 1.0, 1.0, 1.0) first). If you’re doing
alpha stuff the usual openGL transparency rules apply – you’ll need to
use blending and draw back to front.

http://nehe.gamedev.net/ . Great source of openGL tutorials. Check out his
tutorial on blending (though he purposely does it wrong since he doesn’t
use sorting – be careful) as well as his later tutorial on sorting (also
includes picking and some other stuff). Very useful information. If you’ve
never worked with openGL before, read tutorials 1-8 before you attempt to
do ANYTHING else.

Don’t forget all the benefits of this approach! Your polygon can be
rotated, resized, or moved closer or farther away and it’s all done on
card! Not only that, if you use linear filtering it looks WAY better than
anything SDL will do for you.

I’ll post to the list once the library I mentioned is ready…if it ever
is. It’s a pet project, so I wouldn’t sit around waiting for it. If
anyone has any more questions, I’ll answer them as best I can.

–oberon

oberon wrote:

Loading the images… This part’s tricky.
You can do something similar with SDL_Image, but it won’t
be easy. Don’t forget you want FOUR channels (RGBA), and watch out for
byte ordering. Good luck.

With SDL_Image it isn’t very hard, IMHO. I’ve done a generic function
which uses SDL_Image to load the file, then loops through each pixel and
puts them to an byte array, which is then transferred to OpenGL with
correct parameters. It supports alpha channel, mipmapping and texture
repeating or clamping. I can post the function if you want.

Don’t forget all the benefits of this approach!

Not only that, if you use linear filtering it looks WAY better than
anything SDL will do for you.

You meant bilinear filtering, right?–
Mika Halttunen
@Mika_Halttunen

  • CLEANUP

With SDL_Image it isn’t very hard, IMHO. I’ve done a generic function
which uses SDL_Image to load the file, then loops through each pixel and
puts them to an byte array, which is then transferred to OpenGL with
correct parameters. It supports alpha channel, mipmapping and texture
repeating or clamping. I can post the function if you want.

You might want limit your image file type support and make a loader that
loads it directly in a storage space that can be sent to OpenGL.
If you want to do dynamic texture loading (versus loading everything at
startup) then you need to keep texture creation/image load time to a
minimum.

Don’t forget all the benefits of this approach!

Not only that, if you use linear filtering it looks WAY better than
anything SDL will do for you.

You meant bilinear filtering, right?

I think he means the magnification and minification filters.
GL_TEXTURE_MAG_FILTER and GL_TEXTURE_MIN_FILTER respectivly.
these combined with GL_LINEAR give’s you pretty textures at the expense of
some performance. with GL_LINEAR the pixel color is averiged in
a linear fasion from the texture data.

I myself use my own CImage class for all image loading combined with my
texture manager which will be available in the GUCEF framework.
I hope some of you might be interseted in testing it when it’s ready for
testing.

GreeTz.


| Dinand Vanvelzen, |
| Programmer, |

Software Engineering student

----- Original Message -----
From: lsoft@mbnet.fi (Mika Halttunen)
To:
Sent: Sunday, June 29, 2003 11:12 AM
Subject: Re: [SDL] 2D with openGL/SDL

Dinand Vanvelzen wrote:

I think he means the magnification and minification filters.
GL_TEXTURE_MAG_FILTER and GL_TEXTURE_MIN_FILTER respectivly.
these combined with GL_LINEAR give’s you pretty textures at the expense of
some performance. with GL_LINEAR the pixel color is averiged in
a linear fasion from the texture data.

True enough, but the the filtering technique enabled by GL_LINEAR is
called bilinear filtering.–
Mika Halttunen
@Mika_Halttunen

that would be great!
texture loading is my main problem.> ----- Original Message -----

From: lsoft@mbnet.fi (Mika Halttunen)
To:
Sent: Sunday, June 29, 2003 11:12 AM
Subject: Re: [SDL] 2D with openGL/SDL

oberon wrote:

Loading the images… This part’s tricky.
You can do something similar with SDL_Image, but it won’t
be easy. Don’t forget you want FOUR channels (RGBA), and watch out for
byte ordering. Good luck.

With SDL_Image it isn’t very hard, IMHO. I’ve done a generic function
which uses SDL_Image to load the file, then loops through each pixel and
puts them to an byte array, which is then transferred to OpenGL with
correct parameters. It supports alpha channel, mipmapping and texture
repeating or clamping. I can post the function if you want.

Don’t forget all the benefits of this approach!

Not only that, if you use linear filtering it looks WAY better than
anything SDL will do for you.

You meant bilinear filtering, right?


Mika Halttunen
lsoft at mbnet.fi


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

thanks, i didn’t knew that i have to max the colors.
i know nehe, but i thought i can get along without reading the tutorials,

now i know better.> ----- Original Message -----

From: oberon@brandeis.edu (oberon)
To:
Sent: Thursday, June 26, 2003 1:54 PM
Subject: [SDL] 2D with openGL/SDL

Doing 2D wiht openGL isn’t bad at all. I’m currently working a library for
this exact purpose, but it’s not ready yet (and may never be), so here are
some quick tips –

Resize all your images so that the dimensions are a power of two.
Otherwise you must use mipmaps, which use more memory.

Loading the images… This part’s tricky. I use a second program I wrote
which
uses image magick to convert to a specialized format for use with openGL
textures. You can do something similar with SDL_Image, but it won’t
be easy. Don’t forget you want FOUR channels (RGBA), and watch out for
byte ordering. Good luck.

Create all your textures during init. This is when images are transferred
over the AGP bus to the graphics card. Then free the images from main
memory.

Bind to the appropraite texture and draw a polygon to draw the “sprite”.
Make sure the color is
maxed (i.e. do a glColor4f(1.0, 1.0, 1.0, 1.0) first). If you’re doing
alpha stuff the usual openGL transparency rules apply – you’ll need to
use blending and draw back to front.

http://nehe.gamedev.net/ . Great source of openGL tutorials. Check out his
tutorial on blending (though he purposely does it wrong since he doesn’t
use sorting – be careful) as well as his later tutorial on sorting (also
includes picking and some other stuff). Very useful information. If you’ve
never worked with openGL before, read tutorials 1-8 before you attempt to
do ANYTHING else.

Don’t forget all the benefits of this approach! Your polygon can be
rotated, resized, or moved closer or farther away and it’s all done on
card! Not only that, if you use linear filtering it looks WAY better than
anything SDL will do for you.

I’ll post to the list once the library I mentioned is ready…if it ever
is. It’s a pet project, so I wouldn’t sit around waiting for it. If
anyone has any more questions, I’ll answer them as best I can.

–oberon


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Florian Hufsky wrote:

that would be great!
texture loading is my main problem.

I’ve attached the function. There are a few comments and it
shouldn’t be too difficult to understand. Of course it requires some
modifications eg. error-handling to suit your program, but it should
work ok.

Ask if there’s any problems.–
Mika Halttunen
@Mika_Halttunen
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed…
Name: load_texture.cpp
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20030629/bfa25a1e/attachment.asc

thanks, i didn’t knew that i have to max the colors.
i know nehe, but i thought i can get along without reading the tutorials,

now i know better.> ----- Original Message -----

From: oberon@brandeis.edu (oberon)
To:
Sent: Thursday, June 26, 2003 1:54 PM
Subject: [SDL] 2D with openGL/SDL

Doing 2D wiht openGL isn’t bad at all. I’m currently working a library for
this exact purpose, but it’s not ready yet (and may never be), so here are
some quick tips –

Resize all your images so that the dimensions are a power of two.
Otherwise you must use mipmaps, which use more memory.

Loading the images… This part’s tricky. I use a second program I wrote
which
uses image magick to convert to a specialized format for use with openGL
textures. You can do something similar with SDL_Image, but it won’t
be easy. Don’t forget you want FOUR channels (RGBA), and watch out for
byte ordering. Good luck.

Create all your textures during init. This is when images are transferred
over the AGP bus to the graphics card. Then free the images from main
memory.

Bind to the appropraite texture and draw a polygon to draw the “sprite”.
Make sure the color is
maxed (i.e. do a glColor4f(1.0, 1.0, 1.0, 1.0) first). If you’re doing
alpha stuff the usual openGL transparency rules apply – you’ll need to
use blending and draw back to front.

http://nehe.gamedev.net/ . Great source of openGL tutorials. Check out his
tutorial on blending (though he purposely does it wrong since he doesn’t
use sorting – be careful) as well as his later tutorial on sorting (also
includes picking and some other stuff). Very useful information. If you’ve
never worked with openGL before, read tutorials 1-8 before you attempt to
do ANYTHING else.

Don’t forget all the benefits of this approach! Your polygon can be
rotated, resized, or moved closer or farther away and it’s all done on
card! Not only that, if you use linear filtering it looks WAY better than
anything SDL will do for you.

I’ll post to the list once the library I mentioned is ready…if it ever
is. It’s a pet project, so I wouldn’t sit around waiting for it. If
anyone has any more questions, I’ll answer them as best I can.

–oberon


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Create all your textures during init. This is when images are transferred
over the AGP bus to the graphics card. Then free the images from main
memory.

Offcourse it’s not garanteed that A particular texture is in texture memory
and thus has been transfferd over the AGP bus. You can make it more likely
by
setting texture priority to high for a texture,… but there are no
garantees.
when you call glBindTexture() it transferres the textures from system memory
if not present in video memory. Needles to say,… loading everything at init
is nice if possible but not always possible.
Don’t make textures to big either or you might have relativly large chunks
of
video memory that you cannot use because the remaining chunck is to small
to house your texture.
One advantages of bigger textures is texture coordinate
mapping versus repeated calls to glBindTexture(). if you are rendering text
for
example then be sure to make a single font texture and use texture
coordinates
to acces the different glyphs.

On a side note:
When redering with OpenGL i recommend maintaining a standard cartesian
coordinate system with (0,0) in the bottom left corner. SDL mouse
coordinates
use top left as (0,0) wich is odd for a 3D API but not for a 2D API.

GreeTz--------------------------------
| Dinand Vanvelzen, |
| Programmer, |

Software Engineering student