SDL_Image and OpenGL

Hi
I used to use BMP loader to load from images files to use as a OpenGL Texture. However, since i started using IMG_Load*, my textures turn out weird. They have repeating bands of red, green, and blue instead of the real texture. I read previous posts on PNG files and OpenGL, but i can’t get it to work right.
Thanks
James

If You ahve any questions, please call me at 301-610-9966 @Zhuang_Zhao---------------------------------
Sponsored Link

Try Netflix today! With plans starting at only $5.99 a month what are you waiting for?

Code example please, it’s hard to pinpoint what’s wrong with the given
information.On 11/9/06, Zhuang Zhao wrote:

Hi
I used to use BMP loader to load from images files to use as a OpenGL
Texture. However, since i started using IMG_Load*, my textures turn out
weird. They have repeating bands of red, green, and blue instead of the real
texture. I read previous posts on PNG files and OpenGL, but i can’t get it
to work right.


Cheers,
Rasmus Neckelmann

Hi Zhuang,

The only way I could get it to work was by using the texture loading code
that was posted to this mailing list ages ago, but naturally I forgot to
bookmark it. sob

I’ll post the code that I use, which works without any problems for me.> On 11/9/06, Zhuang Zhao wrote:

Hi
I used to use BMP loader to load from images files to use as a OpenGL
Texture. However, since i started using IMG_Load*, my textures turn out
weird. They have repeating bands of red, green, and blue instead of the
real
texture. I read previous posts on PNG files and OpenGL, but i can’t get
it
to work right.

=========================================================
//String is defined as std::string (or std::wstring in UNICODE)
//m_tex is a GLuint

bool SceneTexture::loadImage( const String& strFilename, bool bAlpha,
bool bMipMaps, bool bRepeat )
{

	//load the image data to an SDL_Surface structure
	SDL_Surface* pTexSurface = IMG_Load( strFilename.c_str() );
	if( NULL == pTexSurface )
	{
		//error
		return false;
	}

	//calculate the total size of the image data. If you are needing
	//the alpha channel then account for that
	int dim = pTexSurface->w * pTexSurface->h * ((bAlpha) ? 4: 3);
	GLubyte *pData = new GLubyte[ dim ];

	//loop through our SDL_Surface and copy it into the array
	//if the image has an extra alpha channel of information then
	//be sure to append that
	int pos = 0;
	for( int y = (pTexSurface->h) - 1; y > -1; y-- )
	{
		for(int x = 0; x < pTexSurface->w; x++)
		{
			Uint8 r, g, b, a;

			//getPixel is defined in the SDL documentation. It just
			//grabs the pixel data from a given SDL_Surface at
			//coordinates x,y
			Uint32 color = getPixel(pTexSurface, x, y);

			if(!bAlpha)
				SDL_GetRGB( color, pTexSurface->format, &r, &g, &b);

			else
				SDL_GetRGBA( color, pTexSurface->format, &r, &g, &b, &a);

			pData[pos] = r; pos++;
			pData[pos] = g; pos++;
			pData[pos] = b; pos++;
			if( bAlpha )
				pData[pos] = a; pos++;
		}
	}


	int type = (bAlpha) ? GL_RGBA : GL_RGB;
	glGenTextures(1, &m_tex);		// Generate texture ID
	glBindTexture(GL_TEXTURE_2D, m_tex);

	int filter_min, filter_mag;


	filter_min = (bMipMaps) ? GL_NEAREST_MIPMAP_NEAREST : GL_NEAREST;
	filter_mag = GL_NEAREST;

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
		filter_min);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
		filter_mag);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
		(bRepeat) ? GL_REPEAT : GL_CLAMP);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
		(bRepeat) ? GL_REPEAT : GL_CLAMP);

	if(bMipMaps)
	{
		gluBuild2DMipmaps(GL_TEXTURE_2D, type, pTexSurface->w, pTexSurface->h,
			type, GL_UNSIGNED_BYTE, pData);
	}else
	{
		glTexImage2D(GL_TEXTURE_2D, 0, type, pTexSurface->w,
			pTexSurface->h, 0, type, GL_UNSIGNED_BYTE, pData);
	}

	//now that we are finished, do some garbage collection
	//clean up our array and destroy the surface you loaded
	delete [] pData;
	SDL_FreeSurface( pTexSurface );

	//return the texture handle
	return true;
}

/*
* Return the pixel value at (x, y)
* NOTE: The surface must be locked before calling this!
* Taken from SDL documentation.
*/
Uint32 SceneTexture::getPixel(SDL_Surface *surface, int x, int y)
{
	int bpp = surface->format->BytesPerPixel;
	/* Here p is the address to the pixel we want to retrieve */
	Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

	switch(bpp) {
	case 1:
		return *p;

	case 2:
		return *(Uint16 *)p;

	case 3:
		if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
			return p[0] << 16 | p[1] << 8 | p[2];
		else
			return p[0] | p[1] << 8 | p[2] << 16;

	case 4:
		return *(Uint32 *)p;

	default:
		return 0;       /* shouldn't happen, but avoids warnings */
	}
}

hth,
Erik Yuzwa


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

Hi,

I am planning to use SDL_Image for loading images from disk.
For other graphical operations i am using OpenGL directly.
Are there any issues / gotchas while doing so.

-Abhinav

Not completely certain, but I believe the OpenGL support is being revamped for SDL 1.3 so that it can be used as just another driver and work automagically. Does anyone have information on this, and on its progress? If that’s close to being finished, you might want to just wait for it to be ready instead of mucking around with a bunch of OpenGL API calls.>----- Original Message ----

From: Abhinav Lele <abhinav.lele at gmail.com>
Subject: [SDL] SDL_Image and OpenGL

Hi,

I am planning to use SDL_Image for loading images from disk.
For other graphical operations i am using OpenGL directly.
Are there any issues / gotchas while doing so.

-Abhinav

I wrote a Texture object for an SDL/OpenGL framework
(Google Code Archive - Long-term storage for Google Code Project Hosting.) and I didn’t find too
many problems. Thus, I just SDL for reading pngs, etc into a
SDL_Display and then cranked that to a texture.

The basic code is just:
(The underscored variables are class variables that are initialized elsewhere)

/**

  • Load using SDL RWops
    */
    void Texture::loadRwops(char * data, int size)
    {

    // use SDL IMG library to load
    SDL_Surface * texture = NULL;
    SDL_RWops * rw = SDL_RWFromMem(data, size);
    texture = IMG_Load_RW(rw, 0);

    if (texture == NULL) cout << IMG_GetError() << endl;

    else {
    buildTexture(texture);
    }
    }

/*

  • loads a texture file
    */
    void Texture::loadFromFile()
    {
    // use SDL IMG library to load
    SDL_Surface * texture = NULL;
    texture = IMG_Load(_path);

    if (texture == NULL) cout << IMG_GetError() << endl;

    else {
    buildTexture(texture);
    }
    }

/**

  • Does the GL/SDL dirtwork for building a texture
    */
    void Texture::buildTexture(SDL_Surface * texture)
    {
    //Do this once somewhere in your code
    //glEnable(GL_TEXTURE_2D);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    //glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

     glGenTextures(1, &_id);
     glBindTexture(GL_TEXTURE_2D, _id);
    
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    

    _width = texture->w;
    _height = texture->h;

    if (texture->format->BytesPerPixel == 4) {
    glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, texture->w,
    texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels );
    }
    else {
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w,
    texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, texture->pixels);
    }
    //if (SDL_MUSTLOCK(texture)) SDL_UnlockSurface(texture);
    SDL_FreeSurface(texture);
    }On Sat, Jun 28, 2008 at 10:05 AM, Mason Wheeler wrote:

Not completely certain, but I believe the OpenGL support is being revamped
for SDL 1.3 so that it can be used as just another driver and work
automagically. Does anyone have information on this, and on its progress? If
that’s close to being finished, you might want to just wait for it to be
ready instead of mucking around with a bunch of OpenGL API calls.

----- Original Message ----
From: Abhinav Lele <abhinav.lele at gmail.com>
Subject: [SDL] SDL_Image and OpenGL

Hi,

I am planning to use SDL_Image for loading images from disk.
For other graphical operations i am using OpenGL directly.
Are there any issues / gotchas while doing so.

-Abhinav


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hello!

In 1.3 there would be just three steps to using SDL_Image with OpenGL
(if you use the OpenGL render driver, as you said)

  1. load the image with IMG_Load
  2. convert the image to a texture with SDL_CreateTextureFromSurface
  3. call SDL_RenderCopy to display the texture

So yes, it’s kind of automagic

  • HolmesOn Jun 28, 2008, at 8:05 AM, Mason Wheeler wrote:

Not completely certain, but I believe the OpenGL support is being
revamped for SDL 1.3 so that it can be used as just another driver
and work automagically. Does anyone have information on this, and on
its progress? If that’s close to being finished, you might want to
just wait for it to be ready instead of mucking around with a bunch
of OpenGL API calls.

----- Original Message ----
From: Abhinav Lele <abhinav.lele at gmail.com>
Subject: [SDL] SDL_Image and OpenGL

Hi,

I am planning to use SDL_Image for loading images from disk.
For other graphical operations i am using OpenGL directly.
Are there any issues / gotchas while doing so.

-Abhinav


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org