[1.2+OpenGL]-PNG W/ Alpha Tranparency To OpenGL Texture?

[1.2+OpenGL]-PNG W/ Alpha Tranparency To OpenGL Texture?

Hi,

My previous post was closed because it was too large
so I made this new post.

I am having great troubles with using SDL_image
to load a PNG image with alpha transparency
then converting it to an OpenGL texture.

I want one set of source code for Windows®, Linux, & Mac OS X®.
I have got the PNG image loading with correct colors on all three OS platforms.
My problem now is that the PNG’s alpha transparent pixels are not being recognized
and are drawn to the screen.

Here is a screenshot:
[Image: http://16bitsoft.com/images/LettersFall2/LF2-3OS-Trans01.png ]

Please review this source snippet and provide a solution:

Code:
if (filePath[0] != ‘~’)
{
tempSurface = NULL;

	    if ( (tempSurface = IMG_Load(filePath)) )
        {
            if ( (tempSurface->w & (tempSurface->w - 1)) != 0 )
            {
                printf("ERROR: Image width is not a power of 2\n");
                loadedImageIsOK = false;
            }

            if ( (tempSurface->h & (tempSurface->h - 1)) != 0 )
            {
                printf("ERROR: Image height is not a power of 2\n");
                loadedImageIsOK = false;
            }

            if (loadedImageIsOK == true)
            {
				tempSurfaceAlpha = SDL_DisplayFormat(tempSurface);
				SDL_SetAlpha(tempSurfaceAlpha, 0, SDL_ALPHA_TRANSPARENT);

				tempSurfaceFixed = SDL_CreateRGBSurface(SDL_SWSURFACE, tempSurface->w, tempSurface->h, 32,
														R_Mask, G_Mask, B_Mask, A_Mask);

				SDL_BlitSurface(tempSurfaceAlpha, NULL, tempSurfaceFixed, NULL);

				numberOfColors = tempSurfaceFixed->format->BytesPerPixel;
                if (numberOfColors == 4)
                {
                    textureFormat = GL_RGBA;
                }
                else if (numberOfColors == 3)
                {
                    textureFormat = GL_RGB;
                }
                else
                {
                    printf("ERROR: The image is not truecolor\n");
                    imageToTextureOK = false;
                }

                Sprites[index].OGL_Texture = (GLuint)NULL;
                if (imageToTextureOK == true)
                {
					glGenTextures( 1, &Sprites[index].OGL_Texture );

                    glBindTexture( GL_TEXTURE_2D, Sprites[index].OGL_Texture );

                    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR);
                    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR);

                    glTexImage2D( GL_TEXTURE_2D, 0, numberOfColors, tempSurface->w, tempSurface->h, 0,
                                  textureFormat, GL_UNSIGNED_BYTE, tempSurfaceFixed->pixels );

                    if ( tempSurface )
                    {
                        SDL_FreeSurface( tempSurface );
						SDL_FreeSurface(tempSurfaceFixed);
						SDL_FreeSurface(tempSurfaceAlpha);
                    }
                }
            }
        }
        else
        {
            printf("ERROR: SDL could not load image %s\n", SDL_GetError());
            loadedImageIsOK = false;
            imageToTextureOK = false;
        }

        if (loadedImageIsOK == false || imageToTextureOK == false)  return false;//index = NumberOfSprites+1;

        Sprites[index].ScreenX = 320;
        Sprites[index].ScreenY = 240;
        Sprites[index].ScaleX = 1.0f;
        Sprites[index].ScaleY = 1.0f;
        Sprites[index].RotationDegree = 0.0f;
        Sprites[index].Transparency = 1.0f;
        Sprites[index].redHue = 1.0f;
        Sprites[index].greenHue = 1.0f;
        Sprites[index].blueHue = 1.0f;
        Sprites[index].Smooth = true;
        Sprites[index].FlipX = false;
        Sprites[index].Width = tempSurface->w;
        Sprites[index].Height = tempSurface->h;
        Sprites[index].CenterX = Sprites[index].Width / 2;
        Sprites[index].CenterY = Sprites[index].Height / 2;
        Sprites[index].AnimationTimer = -1;
    }------------------------

JeZ+Lee
JessePalser <AT> Gmail <DOT> com
16BitSoft®
Video Game Design Studio
www.16BitSoft.com

Again, how about SDL_DisplayFormatAlpha?

Jonny DOn Wed, Sep 15, 2010 at 8:34 AM, JeZ-l-Lee wrote:

[1.2+OpenGL]-PNG W/ Alpha Tranparency To OpenGL Texture?

Hi,

My previous post was closed because it was too large
so I made this new post.

I am having great troubles with using SDL_image
to load a PNG image with alpha transparency
then converting it to an OpenGL texture.

I want one set of source code for Windows®, Linux, & Mac OS X®.
I have got the PNG image loading with correct colors on all three OS
platforms.
My problem now is that the PNG’s alpha transparent pixels are not being
recognized
and are drawn to the screen.

Here is a screenshot:

Please review this source snippet and provide a solution:

Code:

     if (filePath[0] != '~')
    {
        tempSurface = NULL;

      if ( (tempSurface = IMG_Load(filePath)) )
        {
            if ( (tempSurface->w & (tempSurface->w - 1)) != 0 )
            {
                printf("ERROR: Image width is not a power of 2\n");
                loadedImageIsOK = false;
            }

            if ( (tempSurface->h & (tempSurface->h - 1)) != 0 )
            {
                printf("ERROR: Image height is not a power of 2\n");
                loadedImageIsOK = false;
            }

            if (loadedImageIsOK == true)
            {
           tempSurfaceAlpha = SDL_DisplayFormat(tempSurface);
           SDL_SetAlpha(tempSurfaceAlpha, 0, SDL_ALPHA_TRANSPARENT);

           tempSurfaceFixed = SDL_CreateRGBSurface(SDL_SWSURFACE,

tempSurface->w, tempSurface->h, 32,
R_Mask, G_Mask, B_Mask,
A_Mask);

           SDL_BlitSurface(tempSurfaceAlpha, NULL, tempSurfaceFixed,

NULL);

           numberOfColors = tempSurfaceFixed->format->BytesPerPixel;
                if (numberOfColors == 4)
                {
                    textureFormat = GL_RGBA;
                }
                else if (numberOfColors == 3)
                {
                    textureFormat = GL_RGB;
                }
                else
                {
                    printf("ERROR: The image is not truecolor\n");
                    imageToTextureOK = false;
                }

                Sprites[index].OGL_Texture = (GLuint)NULL;
                if (imageToTextureOK == true)
                {
              glGenTextures( 1, &Sprites[index].OGL_Texture );

                    glBindTexture( GL_TEXTURE_2D,

Sprites[index].OGL_Texture );

                    glTexParameteri(GL_TEXTURE_2D,

GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR);

                    glTexImage2D( GL_TEXTURE_2D, 0, numberOfColors,

tempSurface->w, tempSurface->h, 0,
textureFormat, GL_UNSIGNED_BYTE,
tempSurfaceFixed->pixels );

                    if ( tempSurface )
                    {
                        SDL_FreeSurface( tempSurface );
                 SDL_FreeSurface(tempSurfaceFixed);
                 SDL_FreeSurface(tempSurfaceAlpha);
                    }
                }
            }
        }
        else
        {
            printf("ERROR: SDL could not load image %s\n",

SDL_GetError());
loadedImageIsOK = false;
imageToTextureOK = false;
}

        if (loadedImageIsOK == false || imageToTextureOK == false)

return false;//index = NumberOfSprites+1;

        Sprites[index].ScreenX = 320;
        Sprites[index].ScreenY = 240;
        Sprites[index].ScaleX = 1.0f;
        Sprites[index].ScaleY = 1.0f;
        Sprites[index].RotationDegree = 0.0f;
        Sprites[index].Transparency = 1.0f;
        Sprites[index].redHue = 1.0f;
        Sprites[index].greenHue = 1.0f;
        Sprites[index].blueHue = 1.0f;
        Sprites[index].Smooth = true;
        Sprites[index].FlipX = false;
        Sprites[index].Width = tempSurface->w;
        Sprites[index].Height = tempSurface->h;
        Sprites[index].CenterX = Sprites[index].Width / 2;
        Sprites[index].CenterY = Sprites[index].Height / 2;
        Sprites[index].AnimationTimer = -1;
    }

JeZ+Lee
JessePalser Gmail com
16BitSoft®
Video Game Design Studio
www.16BitSoft.com


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

Hi,

Using SDL_DisplayFormatAlpha results in no image show.
Any other ideas?

JesseOn 09/15/2010 08:40 AM, Jonathan Dearborn wrote:

Again, how about SDL_DisplayFormatAlpha?

Jonny D

On Wed, Sep 15, 2010 at 8:34 AM, JeZ-l-Lee <SLNTHERO at aol.com <mailto:SLNTHERO at aol.com>> wrote:

[1.2+OpenGL]-PNG W/ Alpha Tranparency To OpenGL Texture?

Hi,

My previous post was closed because it was too large
so I made this new post.

I am having great troubles with using SDL_image
to load a PNG image with alpha transparency
then converting it to an OpenGL texture.

I want one set of source code for Windows(R), Linux, & Mac OS X(R).
I have got the PNG image loading with correct colors on all three
OS platforms.
My problem now is that the PNG's alpha transparent pixels are not
being recognized
and are drawn to the screen.

Here is a screenshot:


Please review this source snippet and provide a solution:



Code: 	

        if (filePath[0] != '~')
        {
            tempSurface = NULL;

          if ( (tempSurface = IMG_Load(filePath)) )
            {
                if ( (tempSurface->w & (tempSurface->w - 1)) != 0 )
                {
                    printf("ERROR: Image width is not a power of
2\n");
                    loadedImageIsOK = false;
                }

                if ( (tempSurface->h & (tempSurface->h - 1)) != 0 )
                {
                    printf("ERROR: Image height is not a power of
2\n");
                    loadedImageIsOK = false;
                }

                if (loadedImageIsOK == true)
                {
               tempSurfaceAlpha = SDL_DisplayFormat(tempSurface);
               SDL_SetAlpha(tempSurfaceAlpha, 0,
SDL_ALPHA_TRANSPARENT);

               tempSurfaceFixed =
SDL_CreateRGBSurface(SDL_SWSURFACE, tempSurface->w,
tempSurface->h, 32,
                                             R_Mask, G_Mask,
B_Mask, A_Mask);

               SDL_BlitSurface(tempSurfaceAlpha, NULL,
tempSurfaceFixed, NULL);

               numberOfColors =
tempSurfaceFixed->format->BytesPerPixel;
                    if (numberOfColors == 4)
                    {
                        textureFormat = GL_RGBA;
                    }
                    else if (numberOfColors == 3)
                    {
                        textureFormat = GL_RGB;
                    }
                    else
                    {
                        printf("ERROR: The image is not truecolor\n");
                        imageToTextureOK = false;
                    }

                    Sprites[index].OGL_Texture = (GLuint)NULL;
                    if (imageToTextureOK == true)
                    {
                  glGenTextures( 1, &Sprites[index].OGL_Texture );

                        glBindTexture( GL_TEXTURE_2D,
Sprites[index].OGL_Texture );

                        glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR);
                        glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR);

                        glTexImage2D( GL_TEXTURE_2D, 0,
numberOfColors, tempSurface->w, tempSurface->h, 0,
                                      textureFormat,
GL_UNSIGNED_BYTE, tempSurfaceFixed->pixels );

                        if ( tempSurface )
                        {
                            SDL_FreeSurface( tempSurface );
                     SDL_FreeSurface(tempSurfaceFixed);
                     SDL_FreeSurface(tempSurfaceAlpha);
                        }
                    }
                }
            }
            else
            {
                printf("ERROR: SDL could not load image %s\n",
SDL_GetError());
                loadedImageIsOK = false;
                imageToTextureOK = false;
            }

            if (loadedImageIsOK == false || imageToTextureOK ==
false)  return false;//index = NumberOfSprites+1;

            Sprites[index].ScreenX = 320;
            Sprites[index].ScreenY = 240;
            Sprites[index].ScaleX = 1.0f;
            Sprites[index].ScaleY = 1.0f;
            Sprites[index].RotationDegree = 0.0f;
            Sprites[index].Transparency = 1.0f;
            Sprites[index].redHue = 1.0f;
            Sprites[index].greenHue = 1.0f;
            Sprites[index].blueHue = 1.0f;
            Sprites[index].Smooth = true;
            Sprites[index].FlipX = false;
            Sprites[index].Width = tempSurface->w;
            Sprites[index].Height = tempSurface->h;
            Sprites[index].CenterX = Sprites[index].Width / 2;
            Sprites[index].CenterY = Sprites[index].Height / 2;
            Sprites[index].AnimationTimer = -1;
        } 	



------------------------------------------------------------------------

JeZ+Lee
JessePalser <AT> Gmail <DOT> com
16BitSoft(R)
Video Game Design Studio
www.16BitSoft.com <http://www.16BitSoft.com>

_______________________________________________
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

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

Take a look at testgl.c, one of the SDL 1.2 sample programs. It loads images
to an SDL surface and then converts properly to OpenGL textures. The
function to look at is SDL_GL_LoadTexture. Once you have the texture loaded
properly, you need to make sure you have OpenGL set up to use alpha properly
as well. Here’s the init code I’m using (which uses alpha-transparency PNG
files made in Gimp):

// this code all goes after SetVideoMode

/* Set the gamma for the window */
if ( gamma != 0.0 ) {
SDL_SetGamma(gamma, gamma, gamma);
}

glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );

glOrtho( -2.0, 2.0, -2.0, 2.0, -20.0, 20.0 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

glEnable(GL_CULL_FACE);
// FIXME - change vertex order so we can cull back faces
glCullFace(GL_FRONT);

glShadeModel(GL_SMOOTH);

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);    // allow alpha blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Only tested on Windows so far, but I don’t think anything is win-specific.

Hope this helps.On Wed, Sep 15, 2010 at 8:34 AM, JeZ-l-Lee wrote:

[1.2+OpenGL]-PNG W/ Alpha Tranparency To OpenGL Texture?

Hi,

My previous post was closed because it was too large
so I made this new post.

I am having great troubles with using SDL_image
to load a PNG image with alpha transparency
then converting it to an OpenGL texture.

I want one set of source code for Windows®, Linux, & Mac OS X®.
I have got the PNG image loading with correct colors on all three OS
platforms.
My problem now is that the PNG’s alpha transparent pixels are not being
recognized
and are drawn to the screen.

Here is a screenshot:

Please review this source snippet and provide a solution:

Code:

    if (filePath[0] != '~')
    {
        tempSurface = NULL;

      if ( (tempSurface = IMG_Load(filePath)) )
        {
            if ( (tempSurface->w & (tempSurface->w - 1)) != 0 )
            {
                printf("ERROR: Image width is not a power of 2\n");
                loadedImageIsOK = false;
            }

            if ( (tempSurface->h & (tempSurface->h - 1)) != 0 )
            {
                printf("ERROR: Image height is not a power of 2\n");
                loadedImageIsOK = false;
            }

            if (loadedImageIsOK == true)
            {
           tempSurfaceAlpha = SDL_DisplayFormat(tempSurface);
           SDL_SetAlpha(tempSurfaceAlpha, 0, SDL_ALPHA_TRANSPARENT);

           tempSurfaceFixed = SDL_CreateRGBSurface(SDL_SWSURFACE,

tempSurface->w, tempSurface->h, 32,
R_Mask, G_Mask, B_Mask,
A_Mask);

           SDL_BlitSurface(tempSurfaceAlpha, NULL, tempSurfaceFixed,

NULL);

           numberOfColors = tempSurfaceFixed->format->BytesPerPixel;
                if (numberOfColors == 4)
                {
                    textureFormat = GL_RGBA;
                }
                else if (numberOfColors == 3)
                {
                    textureFormat = GL_RGB;
                }
                else
                {
                    printf("ERROR: The image is not truecolor\n");
                    imageToTextureOK = false;
                }

                Sprites[index].OGL_Texture = (GLuint)NULL;
                if (imageToTextureOK == true)
                {
              glGenTextures( 1, &Sprites[index].OGL_Texture );

                    glBindTexture( GL_TEXTURE_2D,

Sprites[index].OGL_Texture );

                    glTexParameteri(GL_TEXTURE_2D,

GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR);

                    glTexImage2D( GL_TEXTURE_2D, 0, numberOfColors,

tempSurface->w, tempSurface->h, 0,
textureFormat, GL_UNSIGNED_BYTE,
tempSurfaceFixed->pixels );

                    if ( tempSurface )
                    {
                        SDL_FreeSurface( tempSurface );
                 SDL_FreeSurface(tempSurfaceFixed);
                 SDL_FreeSurface(tempSurfaceAlpha);
                    }
                }
            }
        }
        else
        {
            printf("ERROR: SDL could not load image %s\n",

SDL_GetError());
loadedImageIsOK = false;
imageToTextureOK = false;
}

        if (loadedImageIsOK == false || imageToTextureOK == false)

return false;//index = NumberOfSprites+1;

        Sprites[index].ScreenX = 320;
        Sprites[index].ScreenY = 240;
        Sprites[index].ScaleX = 1.0f;
        Sprites[index].ScaleY = 1.0f;
        Sprites[index].RotationDegree = 0.0f;
        Sprites[index].Transparency = 1.0f;
        Sprites[index].redHue = 1.0f;
        Sprites[index].greenHue = 1.0f;
        Sprites[index].blueHue = 1.0f;
        Sprites[index].Smooth = true;
        Sprites[index].FlipX = false;
        Sprites[index].Width = tempSurface->w;
        Sprites[index].Height = tempSurface->h;
        Sprites[index].CenterX = Sprites[index].Width / 2;
        Sprites[index].CenterY = Sprites[index].Height / 2;
        Sprites[index].AnimationTimer = -1;
    }

JeZ+Lee
JessePalser Gmail com
16BitSoft®
Video Game Design Studio
www.16BitSoft.com


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

Hi,

Sorry, but where do I got “testGL.c” ?
I Googled for it but can not find it.
Thanks!

JesseOn 09/15/2010 08:50 AM, Justin Coleman wrote:

Take a look at testgl.c, one of the SDL 1.2 sample programs. It loads
images to an SDL surface and then converts properly to OpenGL
textures. The function to look at is SDL_GL_LoadTexture. Once you have
the texture loaded properly, you need to make sure you have OpenGL set
up to use alpha properly as well. Here’s the init code I’m using
(which uses alpha-transparency PNG files made in Gimp):

// this code all goes after SetVideoMode

/* Set the gamma for the window */
if ( gamma != 0.0 ) {
SDL_SetGamma(gamma, gamma, gamma);
}

glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );

glOrtho( -2.0, 2.0, -2.0, 2.0, -20.0, 20.0 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

glEnable(GL_CULL_FACE);
// FIXME - change vertex order so we can cull back faces
glCullFace(GL_FRONT);

glShadeModel(GL_SMOOTH);

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);    // allow alpha blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Only tested on Windows so far, but I don’t think anything is win-specific.

Hope this helps.

On Wed, Sep 15, 2010 at 8:34 AM, JeZ-l-Lee <SLNTHERO at aol.com <mailto:SLNTHERO at aol.com>> wrote:

[1.2+OpenGL]-PNG W/ Alpha Tranparency To OpenGL Texture?

Hi,

My previous post was closed because it was too large
so I made this new post.

I am having great troubles with using SDL_image
to load a PNG image with alpha transparency
then converting it to an OpenGL texture.

I want one set of source code for Windows(R), Linux, & Mac OS X(R).
I have got the PNG image loading with correct colors on all three
OS platforms.
My problem now is that the PNG's alpha transparent pixels are not
being recognized
and are drawn to the screen.

Here is a screenshot:


Please review this source snippet and provide a solution:


	Code: 	


	        if (filePath[0] != '~')
        {
            tempSurface = NULL;

          if ( (tempSurface = IMG_Load(filePath)) )
            {
                if ( (tempSurface->w & (tempSurface->w - 1)) != 0 )
                {
                    printf("ERROR: Image width is not a power of
2\n");
                    loadedImageIsOK = false;
                }

                if ( (tempSurface->h & (tempSurface->h - 1)) != 0 )
                {
                    printf("ERROR: Image height is not a power of
2\n");
                    loadedImageIsOK = false;
                }

                if (loadedImageIsOK == true)
                {
               tempSurfaceAlpha = SDL_DisplayFormat(tempSurface);
               SDL_SetAlpha(tempSurfaceAlpha, 0,
SDL_ALPHA_TRANSPARENT);

               tempSurfaceFixed =
SDL_CreateRGBSurface(SDL_SWSURFACE, tempSurface->w,
tempSurface->h, 32,
                                             R_Mask, G_Mask,
B_Mask, A_Mask);

               SDL_BlitSurface(tempSurfaceAlpha, NULL,
tempSurfaceFixed, NULL);

               numberOfColors =
tempSurfaceFixed->format->BytesPerPixel;
                    if (numberOfColors == 4)
                    {
                        textureFormat = GL_RGBA;
                    }
                    else if (numberOfColors == 3)
                    {
                        textureFormat = GL_RGB;
                    }
                    else
                    {
                        printf("ERROR: The image is not truecolor\n");
                        imageToTextureOK = false;
                    }

                    Sprites[index].OGL_Texture = (GLuint)NULL;
                    if (imageToTextureOK == true)
                    {
                  glGenTextures( 1, &Sprites[index].OGL_Texture );

                        glBindTexture( GL_TEXTURE_2D,
Sprites[index].OGL_Texture );

                        glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR);
                        glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR);

                        glTexImage2D( GL_TEXTURE_2D, 0,
numberOfColors, tempSurface->w, tempSurface->h, 0,
                                      textureFormat,
GL_UNSIGNED_BYTE, tempSurfaceFixed->pixels );

                        if ( tempSurface )
                        {
                            SDL_FreeSurface( tempSurface );
                     SDL_FreeSurface(tempSurfaceFixed);
                     SDL_FreeSurface(tempSurfaceAlpha);
                        }
                    }
                }
            }
            else
            {
                printf("ERROR: SDL could not load image %s\n",
SDL_GetError());
                loadedImageIsOK = false;
                imageToTextureOK = false;
            }

            if (loadedImageIsOK == false || imageToTextureOK ==
false)  return false;//index = NumberOfSprites+1;

            Sprites[index].ScreenX = 320;
            Sprites[index].ScreenY = 240;
            Sprites[index].ScaleX = 1.0f;
            Sprites[index].ScaleY = 1.0f;
            Sprites[index].RotationDegree = 0.0f;
            Sprites[index].Transparency = 1.0f;
            Sprites[index].redHue = 1.0f;
            Sprites[index].greenHue = 1.0f;
            Sprites[index].blueHue = 1.0f;
            Sprites[index].Smooth = true;
            Sprites[index].FlipX = false;
            Sprites[index].Width = tempSurface->w;
            Sprites[index].Height = tempSurface->h;
            Sprites[index].CenterX = Sprites[index].Width / 2;
            Sprites[index].CenterY = Sprites[index].Height / 2;
            Sprites[index].AnimationTimer = -1;
        } 	



------------------------------------------------------------------------

JeZ+Lee
JessePalser <AT> Gmail <DOT> com
16BitSoft(R)
Video Game Design Studio
www.16BitSoft.com <http://www.16BitSoft.com>

_______________________________________________
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

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

Wherever you have your copy of SDL unzipped. Mine is
c:\wrk\sdl-1.2.14\test\testgl.c, for example. If you didn’t build SDL from
source, you should probably get a source copy just for the test programs.
Very useful to have, IMO.On Wed, Sep 15, 2010 at 9:03 AM, Jesse Palser wrote:

Hi,

Sorry, but where do I got “testGL.c” ?
I Googled for it but can not find it.
Thanks!

Jesse

On 09/15/2010 08:50 AM, Justin Coleman wrote:

Take a look at testgl.c, one of the SDL 1.2 sample programs. It loads
images to an SDL surface and then converts properly to OpenGL textures. The
function to look at is SDL_GL_LoadTexture. Once you have the texture loaded
properly, you need to make sure you have OpenGL set up to use alpha properly
as well. Here’s the init code I’m using (which uses alpha-transparency PNG
files made in Gimp):

// this code all goes after SetVideoMode

/* Set the gamma for the window */
if ( gamma != 0.0 ) {
SDL_SetGamma(gamma, gamma, gamma);
}

glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );

glOrtho( -2.0, 2.0, -2.0, 2.0, -20.0, 20.0 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

glEnable(GL_CULL_FACE);
// FIXME - change vertex order so we can cull back faces
glCullFace(GL_FRONT);

glShadeModel(GL_SMOOTH);

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);    // allow alpha blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Only tested on Windows so far, but I don’t think anything is win-specific.

Hope this helps.

On Wed, Sep 15, 2010 at 8:34 AM, JeZ-l-Lee wrote:

[1.2+OpenGL]-PNG W/ Alpha Tranparency To OpenGL Texture?

Hi,

My previous post was closed because it was too large
so I made this new post.

I am having great troubles with using SDL_image
to load a PNG image with alpha transparency
then converting it to an OpenGL texture.

I want one set of source code for Windows®, Linux, & Mac OS X®.
I have got the PNG image loading with correct colors on all three OS
platforms.
My problem now is that the PNG’s alpha transparent pixels are not being
recognized
and are drawn to the screen.

Here is a screenshot:

Please review this source snippet and provide a solution:

Code:

     if (filePath[0] != '~')
    {
        tempSurface = NULL;

      if ( (tempSurface = IMG_Load(filePath)) )
        {
            if ( (tempSurface->w & (tempSurface->w - 1)) != 0 )
            {
                printf("ERROR: Image width is not a power of 2\n");
                loadedImageIsOK = false;
            }

            if ( (tempSurface->h & (tempSurface->h - 1)) != 0 )
            {
                printf("ERROR: Image height is not a power of 2\n");
                loadedImageIsOK = false;
            }

            if (loadedImageIsOK == true)
            {
           tempSurfaceAlpha = SDL_DisplayFormat(tempSurface);
           SDL_SetAlpha(tempSurfaceAlpha, 0, SDL_ALPHA_TRANSPARENT);

           tempSurfaceFixed = SDL_CreateRGBSurface(SDL_SWSURFACE,

tempSurface->w, tempSurface->h, 32,
R_Mask, G_Mask, B_Mask,
A_Mask);

           SDL_BlitSurface(tempSurfaceAlpha, NULL, tempSurfaceFixed,

NULL);

           numberOfColors = tempSurfaceFixed->format->BytesPerPixel;
                if (numberOfColors == 4)
                {
                    textureFormat = GL_RGBA;
                }
                else if (numberOfColors == 3)
                {
                    textureFormat = GL_RGB;
                }
                else
                {
                    printf("ERROR: The image is not truecolor\n");
                    imageToTextureOK = false;
                }

                Sprites[index].OGL_Texture = (GLuint)NULL;
                if (imageToTextureOK == true)
                {
              glGenTextures( 1, &Sprites[index].OGL_Texture );

                    glBindTexture( GL_TEXTURE_2D,

Sprites[index].OGL_Texture );

                    glTexParameteri(GL_TEXTURE_2D,

GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR);

                    glTexImage2D( GL_TEXTURE_2D, 0, numberOfColors,

tempSurface->w, tempSurface->h, 0,
textureFormat, GL_UNSIGNED_BYTE,
tempSurfaceFixed->pixels );

                    if ( tempSurface )
                    {
                        SDL_FreeSurface( tempSurface );
                 SDL_FreeSurface(tempSurfaceFixed);
                 SDL_FreeSurface(tempSurfaceAlpha);
                    }
                }
            }
        }
        else
        {
            printf("ERROR: SDL could not load image %s\n",

SDL_GetError());
loadedImageIsOK = false;
imageToTextureOK = false;
}

        if (loadedImageIsOK == false || imageToTextureOK == false)

return false;//index = NumberOfSprites+1;

        Sprites[index].ScreenX = 320;
        Sprites[index].ScreenY = 240;
        Sprites[index].ScaleX = 1.0f;
        Sprites[index].ScaleY = 1.0f;
        Sprites[index].RotationDegree = 0.0f;
        Sprites[index].Transparency = 1.0f;
        Sprites[index].redHue = 1.0f;
        Sprites[index].greenHue = 1.0f;
        Sprites[index].blueHue = 1.0f;
        Sprites[index].Smooth = true;
        Sprites[index].FlipX = false;
        Sprites[index].Width = tempSurface->w;
        Sprites[index].Height = tempSurface->h;
        Sprites[index].CenterX = Sprites[index].Width / 2;
        Sprites[index].CenterY = Sprites[index].Height / 2;
        Sprites[index].AnimationTimer = -1;
    }

JeZ+Lee
JessePalser Gmail com
16BitSoft®
Video Game Design Studio
www.16BitSoft.com


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


SDL mailing listSDL at lists.libsdl.orghttp://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


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

Hi,

I think I finally got it working now.
Thank you all for your help with this project!

PS - I’ll release the new source soon…

JesseOn 09/15/2010 08:50 AM, Justin Coleman wrote:

Take a look at testgl.c, one of the SDL 1.2 sample programs. It loads
images to an SDL surface and then converts properly to OpenGL
textures. The function to look at is SDL_GL_LoadTexture. Once you have
the texture loaded properly, you need to make sure you have OpenGL set
up to use alpha properly as well. Here’s the init code I’m using
(which uses alpha-transparency PNG files made in Gimp):

// this code all goes after SetVideoMode

/* Set the gamma for the window */
if ( gamma != 0.0 ) {
SDL_SetGamma(gamma, gamma, gamma);
}

glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity( );

glOrtho( -2.0, 2.0, -2.0, 2.0, -20.0, 20.0 );

glMatrixMode( GL_MODELVIEW );
glLoadIdentity( );

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

glEnable(GL_CULL_FACE);
// FIXME - change vertex order so we can cull back faces
glCullFace(GL_FRONT);

glShadeModel(GL_SMOOTH);

glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);    // allow alpha blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

Only tested on Windows so far, but I don’t think anything is win-specific.

Hope this helps.

On Wed, Sep 15, 2010 at 8:34 AM, JeZ-l-Lee <SLNTHERO at aol.com <mailto:SLNTHERO at aol.com>> wrote:

[1.2+OpenGL]-PNG W/ Alpha Tranparency To OpenGL Texture?

Hi,

My previous post was closed because it was too large
so I made this new post.

I am having great troubles with using SDL_image
to load a PNG image with alpha transparency
then converting it to an OpenGL texture.

I want one set of source code for Windows(R), Linux, & Mac OS X(R).
I have got the PNG image loading with correct colors on all three
OS platforms.
My problem now is that the PNG's alpha transparent pixels are not
being recognized
and are drawn to the screen.

Here is a screenshot:


Please review this source snippet and provide a solution:


	Code: 	


	        if (filePath[0] != '~')
        {
            tempSurface = NULL;

          if ( (tempSurface = IMG_Load(filePath)) )
            {
                if ( (tempSurface->w & (tempSurface->w - 1)) != 0 )
                {
                    printf("ERROR: Image width is not a power of
2\n");
                    loadedImageIsOK = false;
                }

                if ( (tempSurface->h & (tempSurface->h - 1)) != 0 )
                {
                    printf("ERROR: Image height is not a power of
2\n");
                    loadedImageIsOK = false;
                }

                if (loadedImageIsOK == true)
                {
               tempSurfaceAlpha = SDL_DisplayFormat(tempSurface);
               SDL_SetAlpha(tempSurfaceAlpha, 0,
SDL_ALPHA_TRANSPARENT);

               tempSurfaceFixed =
SDL_CreateRGBSurface(SDL_SWSURFACE, tempSurface->w,
tempSurface->h, 32,
                                             R_Mask, G_Mask,
B_Mask, A_Mask);

               SDL_BlitSurface(tempSurfaceAlpha, NULL,
tempSurfaceFixed, NULL);

               numberOfColors =
tempSurfaceFixed->format->BytesPerPixel;
                    if (numberOfColors == 4)
                    {
                        textureFormat = GL_RGBA;
                    }
                    else if (numberOfColors == 3)
                    {
                        textureFormat = GL_RGB;
                    }
                    else
                    {
                        printf("ERROR: The image is not truecolor\n");
                        imageToTextureOK = false;
                    }

                    Sprites[index].OGL_Texture = (GLuint)NULL;
                    if (imageToTextureOK == true)
                    {
                  glGenTextures( 1, &Sprites[index].OGL_Texture );

                        glBindTexture( GL_TEXTURE_2D,
Sprites[index].OGL_Texture );

                        glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER, GL_NEAREST);//GL_LINEAR);
                        glTexParameteri(GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER, GL_NEAREST);//GL_LINEAR);

                        glTexImage2D( GL_TEXTURE_2D, 0,
numberOfColors, tempSurface->w, tempSurface->h, 0,
                                      textureFormat,
GL_UNSIGNED_BYTE, tempSurfaceFixed->pixels );

                        if ( tempSurface )
                        {
                            SDL_FreeSurface( tempSurface );
                     SDL_FreeSurface(tempSurfaceFixed);
                     SDL_FreeSurface(tempSurfaceAlpha);
                        }
                    }
                }
            }
            else
            {
                printf("ERROR: SDL could not load image %s\n",
SDL_GetError());
                loadedImageIsOK = false;
                imageToTextureOK = false;
            }

            if (loadedImageIsOK == false || imageToTextureOK ==
false)  return false;//index = NumberOfSprites+1;

            Sprites[index].ScreenX = 320;
            Sprites[index].ScreenY = 240;
            Sprites[index].ScaleX = 1.0f;
            Sprites[index].ScaleY = 1.0f;
            Sprites[index].RotationDegree = 0.0f;
            Sprites[index].Transparency = 1.0f;
            Sprites[index].redHue = 1.0f;
            Sprites[index].greenHue = 1.0f;
            Sprites[index].blueHue = 1.0f;
            Sprites[index].Smooth = true;
            Sprites[index].FlipX = false;
            Sprites[index].Width = tempSurface->w;
            Sprites[index].Height = tempSurface->h;
            Sprites[index].CenterX = Sprites[index].Width / 2;
            Sprites[index].CenterY = Sprites[index].Height / 2;
            Sprites[index].AnimationTimer = -1;
        } 	



------------------------------------------------------------------------

JeZ+Lee
JessePalser <AT> Gmail <DOT> com
16BitSoft(R)
Video Game Design Studio
www.16BitSoft.com <http://www.16BitSoft.com>

_______________________________________________
SDL mailing list
SDL at lists.libsdl.org <mailto:SDL at lists.libsdl.org>
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

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