SDL and OpenGL textures

Hello,

is somebody able to give me a pointer to a code-sniplet where a texture is mapped to an
OpenGL 3D surface (GL_QUADS)? The texture should be an SDL_Surface.

I’ve already messed around with the examples from the testgl.c file and the NeHe-Tutorial (lesson 6) with no success. With the testgl.c code I’m able to load arbitrary images and use the SDL_Surface texture in the 2D-mode only, but was not able to map the same texture to on of the cube surfaces.

Since no OpenGL and no SDL error is reported, I’m a little bit lost in confusion! Any help will be appreciated.

Regards
Ralf_____________________________________________________________________
Der WEB.DE Virenschutz schuetzt Ihr Postfach vor dem Wurm Netsky.A-P!
Kostenfrei fuer alle FreeMail Nutzer. http://f.web.de/?mc=021157

go to nehe.gamedev.net and try the tutorials. there are SDL/Linux ports at the
bottom of the tutorial.

also when doing textures in GL you must make sure that the texture is a factor
of 2

512x512, 256x256, 64x64, …

here check out this snipit

int LoadTextures ()
{
int status = FALSE;

SDL_Surface *textureImage;

/* load the bitmap, check for errors,

  • quit if bmp not found */
    if ((textureImage = SDL_LoadBMP (“texture.bmp”)))
    {
    status = TRUE;

    /* setup the texture image */
    glGenTextures (1, &texture);
    glBindTexture (GL_TEXTURE_2D, texture);

    /* map out the texture image */
    glTexImage2D (GL_TEXTURE_2D,
    0,
    3,
    textureImage->w,
    textureImage->h,
    0, GL_BGR, GL_UNSIGNED_BYTE, textureImage->pixels);

    /* filter the texture image with linear filtering */
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    /* free texture memory */
    SDL_FreeSurface (textureImage);
    }
    return status;
    }

On Tuesday 20 April 2004 08:37 am, Ralf Berger wrote:

Hello,

is somebody able to give me a pointer to a code-sniplet where a texture is
mapped to an OpenGL 3D surface (GL_QUADS)? The texture should be an
SDL_Surface.

I’ve already messed around with the examples from the testgl.c file and the
NeHe-Tutorial (lesson 6) with no success. With the testgl.c code I’m able
to load arbitrary images and use the SDL_Surface texture in the 2D-mode
only, but was not able to map the same texture to on of the cube surfaces.

Since no OpenGL and no SDL error is reported, I’m a little bit lost in
confusion! Any help will be appreciated.

Regards
Ralf


Der WEB.DE Virenschutz schuetzt Ihr Postfach vor dem Wurm Netsky.A-P!
Kostenfrei fuer alle FreeMail Nutzer. http://f.web.de/?mc=021157


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


"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety. "
–Benjamin Franklin

also when doing textures in GL you must make sure that the texture is a factor
of 2

You mean power of two, not factor.

–>Neil-------------------------------------------------------------------------------
Neil Bradley "Your mistletoe is no match for my T.O.W. missile!"
Synthcom Systems, Inc. - Santabot - Futurama
ICQ #29402898

errr…right sorry, early morning, just woke up, pre-coffee.On Tuesday 20 April 2004 09:48 am, Neil Bradley wrote:

also when doing textures in GL you must make sure that the texture is a
factor of 2

You mean power of two, not factor.

–>Neil


---- Neil Bradley “Your mistletoe is no match for my T.O.W.
missile!” Synthcom Systems, Inc. - Santabot - Futurama
ICQ #29402898


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


"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety. "
–Benjamin Franklin

Many thanks for your competent answers!

sdl at libsdl.org schrieb am 20.04.04 18:44:18:

go to nehe.gamedev.net and try the tutorials. there are SDL/Linux ports at the
bottom of the tutorial.

This was the important hint. (I’ve overlooked it in my past attempts.)
In the sources, I’ve found the hint, that the mapping of the texture coordinates has to be clockwise while the coordinates of the surface are given counter clockwise! This is in contrast to the original NeHe lesson6 tutorial.

also when doing textures in GL you must make sure that the texture is a factor
of 2

512x512, 256x256, 64x64, …

If you use the trick from testgl.c the bitmap could be of arbitrary size.

Anyway, thank you for your advise.

Regards

Ralf____________________________________________________________________
Der WEB.DE Virenschutz schuetzt Ihr Postfach vor dem Wurm Sober.A-F!
Kostenfrei fuer FreeMail Nutzer. http://f.web.de/?mc=021158

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1On Wednesday 21 April 2004 10:07, Ralf Berger wrote:

This was the important hint. (I’ve overlooked it in my past attempts.)
In the sources, I’ve found the hint, that the mapping of the texture
coordinates has to be clockwise while the coordinates of the surface are
given counter clockwise! This is in contrast to the original NeHe lesson6
tutorial.

Different sense of rotation for vertices vs. texcoords makes no sense.

Maybe what you’re really seeing is that the texture coordinate system is
vertically flipped as opposed to the coordinate system usually used in 2D
APIs. In other words, positive v values point “up” in the texture image,
while negative v points “down”. In 2D APIs, positive y usually points down.

cu,
Nicolai
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAhp/gsxPozBga0lwRAjkIAKC5oOOKLD38bs9KvvzR31fyNlQ0uwCfQhRE
goSIXXLkNUYTDhEmj/Pz4aM=
=rVSr
-----END PGP SIGNATURE-----

also…

the coordinates of polygons only have to be given in clockwise or counter
clockwise order if you have back face culling on, and it depends which face
is considered the back that determines which direction you have to give the
coordinates in.

Maybe you know that already but just making sure (:> ----- Original Message -----

From: prefect_@gmx.net (Nicolai Haehnle)
To:
Sent: Wednesday, April 21, 2004 9:22 AM
Subject: Re: [SDL] SDL and OpenGL textures

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wednesday 21 April 2004 10:07, Ralf Berger wrote:

This was the important hint. (I’ve overlooked it in my past attempts.)
In the sources, I’ve found the hint, that the mapping of the texture
coordinates has to be clockwise while the coordinates of the surface are
given counter clockwise! This is in contrast to the original NeHe lesson6
tutorial.

Different sense of rotation for vertices vs. texcoords makes no sense.

Maybe what you’re really seeing is that the texture coordinate system is
vertically flipped as opposed to the coordinate system usually used in 2D
APIs. In other words, positive v values point “up” in the texture image,
while negative v points “down”. In 2D APIs, positive y usually points down.

cu,
Nicolai
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAhp/gsxPozBga0lwRAjkIAKC5oOOKLD38bs9KvvzR31fyNlQ0uwCfQhRE
goSIXXLkNUYTDhEmj/Pz4aM=
=rVSr
-----END PGP SIGNATURE-----


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

go to nehe.gamedev.net and try the tutorials. there are SDL/Linux ports at the
bottom of the tutorial.

also when doing textures in GL you must make sure that the texture is a factor
of 2

512x512, 256x256, 64x64, …

here check out this snipit

int LoadTextures ()
{
int status = FALSE;

SDL_Surface *textureImage;

/* load the bitmap, check for errors,

  • quit if bmp not found */
    if ((textureImage = SDL_LoadBMP (“texture.bmp”)))
    {
    status = TRUE;

    /* setup the texture image */
    glGenTextures (1, &texture);
    glBindTexture (GL_TEXTURE_2D, texture);

    /* map out the texture image */
    glTexImage2D (GL_TEXTURE_2D,
    0,
    3,
    textureImage->w,
    textureImage->h,
    0, GL_BGR, GL_UNSIGNED_BYTE, textureImage->pixels);

    /* filter the texture image with linear filtering */
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    /* free texture memory */
    SDL_FreeSurface (textureImage);
    }
    return status;
    }

On Tuesday 20 April 2004 08:37 am, Ralf Berger wrote:

Hello,

is somebody able to give me a pointer to a code-sniplet where a texture is
mapped to an OpenGL 3D surface (GL_QUADS)? The texture should be an
SDL_Surface.

I’ve already messed around with the examples from the testgl.c file and the
NeHe-Tutorial (lesson 6) with no success. With the testgl.c code I’m able
to load arbitrary images and use the SDL_Surface texture in the 2D-mode
only, but was not able to map the same texture to on of the cube surfaces.

Since no OpenGL and no SDL error is reported, I’m a little bit lost in
confusion! Any help will be appreciated.

Regards
Ralf


Der WEB.DE Virenschutz schuetzt Ihr Postfach vor dem Wurm Netsky.A-P!
Kostenfrei fuer alle FreeMail Nutzer. http://f.web.de/?mc=021157


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


"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety. "
–Benjamin Franklin

Just so you know, don’t forget to to a “glEnable(GL_TEXTURE_2D);” before
you actually RENDER the object you’re texturing… I uh… Just forgot
to do that, and spent about an HOUR looking for the reason it wasn’t
working right… -.-;;;

–Scott

Samuel wrote:>go to nehe.gamedev.net and try the tutorials. there are SDL/Linux ports at the

bottom of the tutorial.

also when doing textures in GL you must make sure that the texture is a factor
of 2

512x512, 256x256, 64x64, …

here check out this snipit

int LoadTextures ()
{
int status = FALSE;

SDL_Surface *textureImage;

/* load the bitmap, check for errors,

  • quit if bmp not found */
    if ((textureImage = SDL_LoadBMP (“texture.bmp”)))
    {
    status = TRUE;

    /* setup the texture image */
    glGenTextures (1, &texture);
    glBindTexture (GL_TEXTURE_2D, texture);

    /* map out the texture image */
    glTexImage2D (GL_TEXTURE_2D,
    0,
    3,
    textureImage->w,
    textureImage->h,
    0, GL_BGR, GL_UNSIGNED_BYTE, textureImage->pixels);

    /* filter the texture image with linear filtering */
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    /* free texture memory */
    SDL_FreeSurface (textureImage);
    }
    return status;
    }

On Tuesday 20 April 2004 08:37 am, Ralf Berger wrote:

Hello,

is somebody able to give me a pointer to a code-sniplet where a texture is
mapped to an OpenGL 3D surface (GL_QUADS)? The texture should be an
SDL_Surface.

I’ve already messed around with the examples from the testgl.c file and the
NeHe-Tutorial (lesson 6) with no success. With the testgl.c code I’m able
to load arbitrary images and use the SDL_Surface texture in the 2D-mode
only, but was not able to map the same texture to on of the cube surfaces.

Since no OpenGL and no SDL error is reported, I’m a little bit lost in
confusion! Any help will be appreciated.

Regards
Ralf


Der WEB.DE Virenschutz schuetzt Ihr Postfach vor dem Wurm Netsky.A-P!
Kostenfrei fuer alle FreeMail Nutzer. http://f.web.de/?mc=021157


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

I tried the “GL_BGR” here, um… by the compiler doesn’t recognize that
one. (And yes, it seems the blue and red are swapped without it…).
Any suggestions?> /* map out the texture image */

 glTexImage2D (GL_TEXTURE_2D,
               0,
               3,
               textureImage->w,
               textureImage->h,
               0, GL_BGR, GL_UNSIGNED_BYTE, textureImage->pixels);

I had to find some example code online for this, wherein you take the
"textureImage->pixels" data, and reorder it in a new variable.

unsigned char *data = textureImage->pixels;
unsigned char *newData;

Then they created some variable for getting the pixel data, set it to
zero, byte-shifted it left, and used SDL_GetRGB(); on it given temp
values of r, g, and b (unsigned bytes), then placed them into the
newData structure, and fed those in order to:

glTexImage2D (GL_TEXTURE_2D, 0, 3, textureImage->w, textureImage->h, 0,
GL_RGB, GL_UNSIGNED_BYTE, newData);

[changed is the “GL_RGB” and the “newData” – original below]

I’m not sure the reason for the byteshifting… um. Could someone
explain that, maybe?
Here’s the code of byte shifting and the SDL_GetRGB call:

int BytesPerPixel = textureImage->format->BytesPerPixel;

for(int j = BytesPerPixel - 1 ; j >=0; --j) // for
each byte in the pixel (from the right)
{
pixel_value = pixel_value << 8; // left
shift pixel value by 8 bits
pixel_value = pixel_value | data[ (i * BytesPerPixel) + j ]; //
then make the last 8 bits of pixel value =
}
// the byte that we extract from pBitmap’s data

SDL_GetRGB(pixel_value, texmap->format, (Uint8 *)&r, (Uint8 *)&g, (Uint8
*)&b);

If someone could explain why this is necessary, that’d be just
wonderful. :slight_smile: Thanks a lot! I’m still new-ish to the more moderate
level programming techniques, and don’t fully understand byte data
storage, and byte shifting, and whatnot. Thanks a lot!

–Scott

Samuel wrote:>go to nehe.gamedev.net and try the tutorials. there are SDL/Linux ports at the

bottom of the tutorial.

also when doing textures in GL you must make sure that the texture is a factor
of 2

512x512, 256x256, 64x64, …

here check out this snipit

int LoadTextures ()
{
int status = FALSE;

SDL_Surface *textureImage;

/* load the bitmap, check for errors,

  • quit if bmp not found */
    if ((textureImage = SDL_LoadBMP (“texture.bmp”)))
    {
    status = TRUE;

    /* setup the texture image */
    glGenTextures (1, &texture);
    glBindTexture (GL_TEXTURE_2D, texture);

    /* map out the texture image */
    glTexImage2D (GL_TEXTURE_2D,
    0,
    3,
    textureImage->w,
    textureImage->h,
    0, GL_BGR, GL_UNSIGNED_BYTE, textureImage->pixels);

    /* filter the texture image with linear filtering */
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    /* free texture memory */
    SDL_FreeSurface (textureImage);
    }
    return status;
    }

On Tuesday 20 April 2004 08:37 am, Ralf Berger wrote:

Hello,

is somebody able to give me a pointer to a code-sniplet where a texture is
mapped to an OpenGL 3D surface (GL_QUADS)? The texture should be an
SDL_Surface.

I’ve already messed around with the examples from the testgl.c file and the
NeHe-Tutorial (lesson 6) with no success. With the testgl.c code I’m able
to load arbitrary images and use the SDL_Surface texture in the 2D-mode
only, but was not able to map the same texture to on of the cube surfaces.

Since no OpenGL and no SDL error is reported, I’m a little bit lost in
confusion! Any help will be appreciated.

Regards
Ralf


Der WEB.DE Virenschutz schuetzt Ihr Postfach vor dem Wurm Netsky.A-P!
Kostenfrei fuer alle FreeMail Nutzer. http://f.web.de/?mc=021157


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