Using png as gl textures

I have a weird problem when I load a png file with sdl-image and try to
use it as an open gl texture. The texture looks bad and it is not
stretched to the area as it should be. When I convert the image to jpg,
the textures is applied correctly. But in both cases I have a similar
problem: the colors are too dark, I got blue instead of cyan. In fact,
I tried with a white texture and it was almost blue.
Also, in nehe tutorials and some other tutorials I have read that you
must change SDL surfaces byte order before using as textures. When I do
this the image is flipped. There has been some change in sdl surface
structure lately?–
Roger D. Vargas
Formatur Santiago

Le Vendredi 9 D?cembre 2005 17:26, Roger D. Vargas a ?crit?:

I have a weird problem when I load a png file with sdl-image and try to
use it as an open gl texture. The texture looks bad and it is not
stretched to the area as it should be. When I convert the image to jpg,
the textures is applied correctly. But in both cases I have a similar
problem: the colors are too dark, I got blue instead of cyan. In fact,
I tried with a white texture and it was almost blue.
Also, in nehe tutorials and some other tutorials I have read that you
must change SDL surfaces byte order before using as textures. When I do
this the image is flipped. There has been some change in sdl surface
structure lately?

And changing the flag passed to glTexImage2D ?
Try with GL_BGR / GL_BGRA instead of GL_RGB / GL_RGBA …

Regards,–
jabber : lordheavy at jabber.fr
mail : lordheavym at REMOVEMEgmail.com

As for the stretching issue, I’m drawing a blank for the PNG/JPEG
difference – in my experience, once it’s loaded as an SDL_Surface, it
should be nearly the same (pixel format may differ [JPEG has no Alpha
component], and the difference in compression schemes may make slightly
different colors)
As for the blue-colored images, you might be unintentionally blending
with the current GL Color (as set b y glColor* functions). You might
try doing a glColor3u(255,255,255) before you render your primitive. If
you’re not drawing any untextured primitives, or any textured primitives
that don’t need this blending effect, you will get better performance
out of a single glDisable(GL_BLEND) during initialization.

Roger D. Vargas wrote:> I have a weird problem when I load a png file with sdl-image and try to

use it as an open gl texture. The texture looks bad and it is not
stretched to the area as it should be. When I convert the image to jpg,
the textures is applied correctly. But in both cases I have a similar
problem: the colors are too dark, I got blue instead of cyan. In fact,
I tried with a white texture and it was almost blue.
Also, in nehe tutorials and some other tutorials I have read that you
must change SDL surfaces byte order before using as textures. When I do
this the image is flipped. There has been some change in sdl surface
structure lately?

Also, in nehe tutorials and some other tutorials I have read that you
must change SDL surfaces byte order before using as textures. When I do
this the image is flipped. There has been some change in sdl surface
structure lately?

Nehes tutorials (or maybe the SDL port of them) are plain wrong in this
part. In the coordinate space for texture coordinates, (0,0) is the upper
left corner of the texture, as in ordinary graphics.

IIRC, BMPs are used somewhere in those tutorials, and these BMPs are read
more or less unaltered, and BMPs are (for some strange reason) stored
upside-down with the lowest rows first. Therefore, the coordinate system
for textures by accident seem to coincidence with the one with (0,0) in
the lower left corner. BMPs loaded with some proper loader, or anything
loaded with SDL_image, has the coordinate system starting from top left.

This has nothing to do with SDL, it’s only due to using routines which
doesn’t process the BMP correctly, and incorrect explanations in Nehes
tutorials (or was it the SDL port of them?).

// MartinOn Fri, 9 Dec 2005, Roger D. Vargas wrote:

If when you change the byte order, the image is the right color but just
"upside down", you have 1 of 2 scenarios…

  1. You are applying the texture coordinates wrong (upside down), flip the Y
    coords and it should be fixed.

  2. You are applying the texture coordinates correctly, but still the image
    is upside down due to some problem. How do you fix that? Flip the Y coords.> ----- Original Message -----

From: jsilicon@earthlink.net (John Silicon)
To: "A list for developers using the SDL library. (includes SDL-announce)"

Sent: Friday, December 09, 2005 9:00 AM
Subject: Re: [SDL] using png as gl textures

As for the stretching issue, I’m drawing a blank for the PNG/JPEG
difference – in my experience, once it’s loaded as an SDL_Surface, it
should be nearly the same (pixel format may differ [JPEG has no Alpha
component], and the difference in compression schemes may make slightly
different colors)
As for the blue-colored images, you might be unintentionally blending
with the current GL Color (as set b y glColor* functions). You might
try doing a glColor3u(255,255,255) before you render your primitive. If
you’re not drawing any untextured primitives, or any textured primitives
that don’t need this blending effect, you will get better performance
out of a single glDisable(GL_BLEND) during initialization.

Roger D. Vargas wrote:

I have a weird problem when I load a png file with sdl-image and try to
use it as an open gl texture. The texture looks bad and it is not
stretched to the area as it should be. When I convert the image to jpg,
the textures is applied correctly. But in both cases I have a similar
problem: the colors are too dark, I got blue instead of cyan. In fact,
I tried with a white texture and it was almost blue.
Also, in nehe tutorials and some other tutorials I have read that you
must change SDL surfaces byte order before using as textures. When I do
this the image is flipped. There has been some change in sdl surface
structure lately?


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

El vie, 09-12-2005 a las 10:00 -0700, John Silicon escribi?:

As for the stretching issue, I’m drawing a blank for the PNG/JPEG
difference – in my experience, once it’s loaded as an SDL_Surface, it
should be nearly the same (pixel format may differ [JPEG has no Alpha
component], and the difference in compression schemes may make slightly
different colors)
As for the blue-colored images, you might be unintentionally blending
with the current GL Color (as set b y glColor* functions). You might
try doing a glColor3u(255,255,255) before you render your primitive. If
you’re not drawing any untextured primitives, or any textured primitives
that don’t need this blending effect, you will get better performance
out of a single glDisable(GL_BLEND) during initialization.
You are right. Blue is the color applied to las t vertex. But disabling
blending causes a weird effect: the polygon rendered outside ortho mode
now is not visible. When I used glColor3i(255,255,255) before drawing
widget then it wasnt visible neither.–
Roger D. Vargas
Formatur Santiago

El vie, 09-12-2005 a las 11:26 -0500, Roger D. Vargas escribi?:

I have a weird problem when I load a png file with sdl-image and try to
use it as an open gl texture. The texture looks bad and it is not
stretched to the area as it should be.

I do this…

void OGLVideoDriver::initPixelFormats (void)
{
#ifdef APPLE
m_pPixelFormat.nBPP = 24;
m_pPixelFormat.nAMask = 0x00000000;
m_pPixelFormat.nRMask = 0x00FF0000;
m_pPixelFormat.nGMask = 0x0000FF00;
m_pPixelFormat.nBMask = 0x000000FF;

	m_pPixelFormatAlpha.nBPP = 32;
	m_pPixelFormatAlpha.nAMask = 0x000000FF;
	m_pPixelFormatAlpha.nRMask = 0xFF000000;
	m_pPixelFormatAlpha.nGMask = 0x00FF0000;
	m_pPixelFormatAlpha.nBMask = 0x0000FF00;
#else
	m_pPixelFormat.nBPP = 24;
	m_pPixelFormat.nAMask = 0x00000000;
	m_pPixelFormat.nRMask = 0x000000FF;
	m_pPixelFormat.nGMask = 0x0000FF00;
	m_pPixelFormat.nBMask = 0x00FF0000;

	m_pPixelFormatAlpha.nBPP = 32;
	m_pPixelFormatAlpha.nAMask = 0xFF000000;
	m_pPixelFormatAlpha.nRMask = 0x000000FF;
	m_pPixelFormatAlpha.nGMask = 0x0000FF00;
	m_pPixelFormatAlpha.nBMask = 0x00FF0000;
#endif

}

I convert all my surfaces to that format, and later…

byte* pData;
int nPitch;

pSurf->lock(pRect, &pData, &nPitch);

int nPitchBytes = nPitch;
int nPitchMod = nPitch % nChannels;
nPitch /= nChannels;

static const int lModToAlign[4] = { 1, 2, 4, 8 };
glPixelStorei(GL_UNPACK_ALIGNMENT, lModToAlign[nPitchMod]);
glPixelStorei(GL_UNPACK_ROW_LENGTH, nPitch);

glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, nW, nH, nFormat,

GL_UNSIGNED_BYTE, pData);

pSurf->unlock();-- 

Gabriel Gambetta
Mystery Studio - http://www.mysterystudio.com
Gabriel on Graphics - http://gabrielongraphics.blogspot.com