OpenGL Textures

What is the best way to load image files into OpenGL textures?
Should I use SDL_image to load it and then convert to an OpenGL texture?
(Anybody have code for that?)
Or use some other library?

Adam.

allow: post

SL_image is the right way…
you sould look at nehe tutoril lesson06:
(here is the relevant code from lesson06)---------------------------------------------------------
SDL_Surface *LoadBMP(char *filename)
{
Uint8 *rowhi, *rowlo;
Uint8 *tmpbuf, tmpch;
SDL_Surface *image;
int i, j;

image = SDL_LoadBMP(filename);
if ( image == NULL ) {
    fprintf(stderr, "Unable to load %s: %s\n", filename,  

SDL_GetError());
return(NULL);
}

/* GL surfaces are upsidedown and RGB, not BGR :-) */
tmpbuf = (Uint8 *)malloc(image->pitch);
if ( tmpbuf == NULL ) {
    fprintf(stderr, "Out of memory\n");
    return(NULL);
}
rowhi = (Uint8 *)image->pixels;
rowlo = rowhi + (image->h * image->pitch) - image->pitch;
for ( i=0; i<image->h/2; ++i ) {
    for ( j=0; j<image->w; ++j ) {
        tmpch = rowhi[j*3];
        rowhi[j*3] = rowhi[j*3+2];
        rowhi[j*3+2] = tmpch;
        tmpch = rowlo[j*3];
        rowlo[j*3] = rowlo[j*3+2];
        rowlo[j*3+2] = tmpch;
    }
    memcpy(tmpbuf, rowhi, image->pitch);
    memcpy(rowhi, rowlo, image->pitch);
    memcpy(rowlo, tmpbuf, image->pitch);
    rowhi += image->pitch;
    rowlo -= image->pitch;
}
free(tmpbuf);
return(image);

}

// Load Bitmaps And Convert To Textures
void LoadGLTextures(void)
{
// Load Texture
SDL_Surface *image1;

image1 = LoadBMP("Data06/NeHe.bmp");
if (!image1) {
    SDL_Quit();
    exit(1);
}

// Create Texture	
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);   // 2d texture (x and  

y size)

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);  

// scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
// scale linearly when image smalled than texture

// 2d texture, level of detail 0 (normal), 3 components (red,  

green, blue), x size from image, y size from image,
// border 0 (normal), rgb color data, unsigned byte data, and
finally the data itself.
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1->w, image1->h, 0,
GL_RGB, GL_UNSIGNED_BYTE, image1->pixels);
};

You could use SDL_LoadBMP to load the texture. And for many that is a
sufficient way, but it has one great dissadvantadge. You have to store the
bitmap in a standard format (like BMP) and so everybody can view the images
in your directory prior to playing your game or application. In many cases,
you don’t want that.

One easy way to make your own image file format is to make a small seperate
application that loads the image using a image library like SDL_LoadBMP or
OpenIL(can load almost any image format) and then storing the pixel
information using the following C++ functions:

FILE fout;
fout = fopen(“my_format.bmf”, “wb”);
fwrite(pixel_data, 1, 256
256, fout);
fclose(fout);

You don’t need a file header or other shit since no one else is supposed to
read the file. After this you write the code in your main application that
loads the file my_format.bmf. You can use this simple code:

FILE fin;
fin = fopen(“my_format.bmf”, “rb”);
GLubyte pixel_data[256
256];
fread( pixel_data, 1, 256*256, fin );
fclose(fin);

You can also store numerous textures into one big file. After this you use
the OpenGL functions to convert the array of pixel-data to an OpenGL
texture.

Hope this helps,

Peter> ----- Original Message -----

From: adam@preston.net (Adam Gates)
To: sdl_ml
Sent: Monday, August 13, 2001 6:10 AM
Subject: [SDL] OpenGL Textures

What is the best way to load image files into OpenGL textures?
Should I use SDL_image to load it and then convert to an OpenGL texture?
(Anybody have code for that?)
Or use some other library?

Adam.


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

Lloyd Dupont wrote:

/* GL surfaces are upsidedown and RGB, not BGR :slight_smile: */
[ broken code ]

maybe we should add texture loading functions to SDL_image, since nobody
seems to get it right anyway

“Peter Venis” wrote:

You could use SDL_LoadBMP to load the texture. And for many that is a
sufficient way, but it has one great dissadvantadge. You have to store the
bitmap in a standard format (like BMP) and so everybody can view the images
in your directory prior to playing your game or application. In many cases,
you don’t want that.

there are sometimes reasons to use a non-standard image format, but this
cannot be one of them (especially given the highly encrypted format you
propose)

Er, why the hell not?

I realize that SDL is LGPL and therefore you are not required to keep
things open sourced and all of that, but every popular game made in the
last five years has seen people making custom modifications to the look
and feel of the game. For multiplayer games it’s not only common but
essential. You are deliberately obfuscating data in a very lame way. A
very obvious lame way that anybody with a two-week visual basic calss can
undo for no other reason than making users’ lives a little harder.

The only other possible reason is that a million tiny files are a real
pain for the casual user who has no interest in modifying things, and your
encoding technique doesn’t do a thing to help that at all. That can be
handled quite simply by packing your data into some sort of archive. Zip
seems to be the thing to do now since it doesn’t require special tools to
fiddle with, but things such as Quake’s .pak format are also rather common
since they’re dead easy to code for and tools already exist in droves for
them.

If there is one disadvantage to using BMP files for graphics, it’s that
there are enough different types of BMP that loading them is annoying.
SDL does that for you though, so no biggie. They also lack an alpha
channel, so they’re not as useful as other formats such as PNG and the old
standby Targas when working in OpenGL.On Mon, Aug 13, 2001 at 10:49:38AM +0200, Peter Venis wrote:

You could use SDL_LoadBMP to load the texture. And for many that is a
sufficient way, but it has one great dissadvantadge. You have to store the
bitmap in a standard format (like BMP) and so everybody can view the images
in your directory prior to playing your game or application. In many cases,
you don’t want that.


Joseph Carter Free software developer

Mere nonexistence is a feeble excuse for declaring a thing unseeable. You
can see dragons. You just have to look in the right direction.
– John Hasler

-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 273 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20010813/df3377b4/attachment.pgp

maybe we should add texture loading functions to SDL_image, since nobody
seems to get it right anyway
sounds like a good idea, as SDL_Surface still have to be converted
it is a performance penality.
Obviously you should do this at the beginning but so you will even
get a faster startup and an easier SDL-GL texture load code for every
one discovering it.

(i want to say, though a good idea, i don’t believe it is a high
priority one)

Actually, we should have an SDL-GL utility library. surface to texture,
surface to sub-texture, readpixels, and writepixels.

-RayOn Monday 13 August 2001 05:24, you wrote:

Lloyd Dupont wrote:

/* GL surfaces are upsidedown and RGB, not BGR :slight_smile: */

[ broken code ]

maybe we should add texture loading functions to SDL_image, since nobody
seems to get it right anyway

non standart image format change the rgb bitmap to bgr open gl format, the
txture looks bad in paint but is easy to load into opengl!> ----- Original Message -----

From: f91-men@nada.kth.se (Mattias Engdegard)
To:
Cc: “Peter Venis”
Sent: Monday, August 13, 2001 11:28 AM
Subject: Re: [SDL] OpenGL Textures

“Peter Venis” wrote:

You could use SDL_LoadBMP to load the texture. And for many that is a
sufficient way, but it has one great dissadvantadge. You have to store
the

bitmap in a standard format (like BMP) and so everybody can view the
images

in your directory prior to playing your game or application. In many
cases,

you don’t want that.

there are sometimes reasons to use a non-standard image format, but this
cannot be one of them (especially given the highly encrypted format you
propose)


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

Then something is wrong with the OpenGL code. File formats have a
specified (if only implicitly by the platform the format originates
from) byte order, and you should stick to that. Messing with files that
appear to be of some well known format is generally a very bad idea.

Or; “easy to load” in this context is analogous to “sell your soul now -
worry about the consequences later”. :wink:

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Monday 13 August 2001 19:30, Dan wrote:

non standart image format change the rgb bitmap to bgr open gl format,
the txture looks bad in paint but is easy to load into opengl!

Then something is wrong with the OpenGL code. File formats have a
specified (if only implicitly by the platform the format originates
from) byte order, and you should stick to that. Messing with files that
appear to be of some well known format is generally a very bad idea.

Not necessarily. You can feed BGRA textures to OpenGL as opposed to RGBA
or whatever; in fact, this makes GL code significantly faster on the
Voodoo 1/2/3 chipsets: BGRA is the format that Glide wants, so Mesa is
just going to have to convert every texture in software to get it to the
card if you don’t pass it in that format.

OpenGL doesn’t understand file formats: just chunks of memory with certain
characteristics. You can feed it RGBA data and tell it it’s BGRA if you
want, but doing so explicitly doesn’t make the OpenGL code or library
incorrect.

–ryan.

I wasn’t very clear; “Messing with files…” etc was meant as in
"Creating files that don’t contain data in the format their headers
suggest, just to make the loader simpler." - or simply “Creating broken
files.” It just can’t be a good idea IMO, and besides, it’s not
portable so it hardly even solves the problem! :slight_smile:

As to the original problem, the solution lies in knowing what the image
loader generates (shouldn’t be too hard with SDL_image), and how to
describe it to OpenGL (almost easier).

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------------> http://www.linuxdj.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Monday 13 August 2001 21:14, you wrote:

Then something is wrong with the OpenGL code. File formats have a
specified (if only implicitly by the platform the format originates
from) byte order, and you should stick to that. Messing with files
that appear to be of some well known format is generally a very bad
idea.

Not necessarily. You can feed BGRA textures to OpenGL as opposed to
RGBA or whatever; in fact, this makes GL code significantly faster on
the Voodoo 1/2/3 chipsets: BGRA is the format that Glide wants, so Mesa
is just going to have to convert every texture in software to get it to
the card if you don’t pass it in that format.

OpenGL doesn’t understand file formats: just chunks of memory with
certain characteristics. You can feed it RGBA data and tell it it’s
BGRA if you want, but doing so explicitly doesn’t make the OpenGL code
or library incorrect.

texture=IMG_Load("…/deracer-data/road.png");
assert(texture!=NULL);

glBindTexture(GL_TEXTURE_2D,textures[TextureRoad]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,0,3,texture->w,texture->h,0,GL_RGB,GL_UNSIGNED_BYTE,texture->pixels);

SDL_FreeSurface(texture);On Mon, Aug 13, 2001 at 02:10:21PM +1000, Adam Gates wrote:

What is the best way to load image files into OpenGL textures?
Should I use SDL_image to load it and then convert to an OpenGL texture?
(Anybody have code for that?)


Turn round slowly
Tell us what you see
Oh no - The fly got the spider
And now he’s chasing me “Otherworld” - Ronnie James Dio

Jacek Poplawski wrote:

texture=IMG_Load("…/deracer-data/road.png");
[…]
glTexImage2D(GL_TEXTURE_2D,0,3,texture->w,texture->h,0,GL_RGB,GL_UNSIGNED_BYTE,texture->pixels);

Oh dear. This crops up every week, and since people don’t seem
to bother reading the archives before posting a question (or indeed
an incorrect answer), maybe we have to live with it. At least, until
we introduce a mandatory archive reading test before subscriptions
are granted

In a valiant attempt to fight this I wrote a small sample program
that does the necessary conversion, at

ftp://ptah.lnf.kth.se/pub/misc/convgltex.c

but that obvious was a futile attempt. Also as Ryan points out
it can be improved by using other pixel formats where that is
faster, but I don’t know the right incantations for determining
what formats indeed are faster (preferably at compile time).
If anyone does, please tell.

Has anyone tried DevIL (http://www.imagelib.org/ or
http://openil.sourceforge.net/)?
It seems they have already support loading to a OpenGL texture (and a
couple of other targets ie DirectX).
I guess the question is now: Has anybody tried converting the DevIL
image to an SDL Surface?

Adam Gates wrote:>

What is the best way to load image files into OpenGL textures?
Should I use SDL_image to load it and then convert to an OpenGL texture?
(Anybody have code for that?)
Or use some other library?

Adam.


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

Hi,

I want to embark on a lengthy game project revolving
around OpenGL and I wanted to ask if using SDL with GL
has any performance downfalls compared to GL and
SVGALIB or allegro or other X libs.

A second question was to verify if the being LGPL of
SDL allows the end product to be sold etc without the
source code.
Thanks.__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/

I want to embark on a lengthy game project revolving
around OpenGL and I wanted to ask if using SDL with GL
has any performance downfalls compared to GL and
SVGALIB or allegro or other X libs.

SDL just sets up the GL context, after that, the rendering speed will be
up to the OpenGL implementation in use.

SVGALIB and Allegro don’t have OpenGL support. On Linux, your options are
to use glx (non portable), or SDL, which is a wrapper over glx on Unix,
and a wrapper over wgl on Windows, etc.

I’d go with SDL, honestly.

A second question was to verify if the being LGPL of
SDL allows the end product to be sold etc without the
source code.
Thanks.

Yes. You can distribute binaries without source, so long as you supply a
dynamically linked binary (that is, you include a version of the program
that uses SDL.dll or libSDL.so or whatever instead of linking it
statically into the main binary). This allows people to use a different
version of SDL at their discretion. You need to supply this binary, but
you aren’t required to support it. If you planned on doing it this way
already, you’re fine. :slight_smile:

–ryan.

An embedded and charset-unspecified text was scrubbed…
Name: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20010815/32a13657/attachment.txt

So what is your solution if it is slow … ??

“npguy Last Name” a ?crit dans le message news:
mailman.997883044.11391.sdl at libsdl.org

…slow!

Jacek iso-8859-2?Q?Pop=B3awski sdl at libsdl.org Re:
[SDL] OpenGL TexturesReply-To: sdl at libsdl.org

What is the best way to load image files into OpenGL textures?
Should I use SDL_image to load it and then convert to an OpenGL
texture?

(Anybody have code for that?)

texture=IMG_Load("…/deracer-data/road.png");
assert(texture!=NULL);

glBindTexture(GL_TEXTURE_2D,textures[TextureRoad]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D,0,3,texture->w,texture->h,0,GL_RGB,GL_UNSIGNED_BY
TE,texture->pixels);

SDL_FreeSurface(texture);


Turn round slowly
Tell us what you see
Oh no - The fly got the spider
And now he’s chasing me “Otherworld” - Ronnie
James Dio> >Date: Tue, 14 Aug 2001 05:22:11 +0200

On Mon, Aug 13, 2001 at 02:10:21PM +1000, Adam Gates wrote:


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


–== Sent via Deja.com ==–
http://www.deja.com/