1.2+OpenGL-Transparent color of OGL texture?

Hi,

I’ve gotten into using OpenGL with SDL 1.2.x branch.
How do I set the transparent color of an OpenGL texture loaded from an
SDL surface?
Googled for 2 hours and have not seen how to do this.
Thanks!

PS: I want the transparent color to be WHITE for the OGL texture…

URL LINK TO A SCREENSHOT:
http://www.silentheroproductions.com/images/SDL-I-OGL-0001.png

PASTEBIN LINK TO MY SOURCE:
http://pastebin.com/m236dd79c

JeZ-l-Lee
SLNTHERO at aol.com
www.SilentHeroProductions.com

//HERE IS MY SOURCE:
/*-------------------------------------------------------------------------------------------
S.D.L.® 1.2.x GTR Twin Turbo(OpenGL)

Source ©opyright 2010, by Silent Hero Productions®

*/

#include “SDL/SDL.h”
#include “SDL/SDL_opengl.h”

int main( int argc, char* args[] )
{
if ( SDL_Init(SDL_INIT_EVERYTHING) != 0 )
{
printf(“ERROR: Unable to initialize SDL: %s\n”, SDL_GetError());
return 1;
}
else printf(“SDL (Everything) initialized\n”);

SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

SDL_Surface* screen = SDL_SetVideoMode( 640, 480, 16, SDL_OPENGL);
if (!screen)
{
    printf("ERROR: SDL 640x480 window init failed\n");
    SDL_Quit();
    return 1;
}

glEnable( GL_TEXTURE_2D );

glClearColor( 0.5f, 0.5f, 0.5f, 0.0f ); // Gray OGL clear color

glViewport( 0, 0, 640, 480 );

glClear( GL_COLOR_BUFFER_BIT );

glMatrixMode( GL_PROJECTION );
glLoadIdentity();

glOrtho(0.0f, 640, 480, 0.0f, -1.0f, 1.0f);

glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

printf("OpenGL initialized\n");


GLuint texture;         // This is a handle to our texture object
SDL_Surface *surface;   // This surface will tell us the details of 

the image
GLenum texture_format;
GLint nOfColors;

bool BMP_Dimensions_OK = true;
bool BMP_To_Texture_OK = true;

if ( (surface = SDL_LoadBMP("data/graphics/image.bmp")) )
{
    // Check that the image's width is a power of 2
    if ( (surface->w & (surface->w - 1)) != 0 )
    {
        printf("ERROR: image.bmp's width is not a power of 2\n");
        BMP_Dimensions_OK = false;
    }

    // Also check if the height is a power of 2
    if ( (surface->h & (surface->h - 1)) != 0 )
    {
        printf("ERROR: image.bmp's height is not a power of 2\n");
        BMP_Dimensions_OK = false;
    }

    if (BMP_Dimensions_OK == true)
    {
        // get the number of channels in the SDL surface
        nOfColors = surface->format->BytesPerPixel;
        if (nOfColors == 4) // contains an alpha channel
        {
            if (surface->format->Rmask == 0x000000ff)
                texture_format = GL_RGBA;
            else
                texture_format = GL_BGRA;
        }
        else if (nOfColors == 3) // no alpha channel
        {
            if (surface->format->Rmask == 0x000000ff)
                texture_format = GL_RGB;
            else
                texture_format = GL_BGR;
        }
        else
        {
            printf("ERROR: the image is not truecolor\n");
            BMP_To_Texture_OK = false;
        }


        if (BMP_To_Texture_OK == true)
        {
            // Have OpenGL generate a texture object handle for us
            glGenTextures( 1, &texture );

            // Bind the texture object
            glBindTexture( GL_TEXTURE_2D, texture );

            // Set the texture's stretching properties
            glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 

GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_LINEAR );

            // Edit the texture object's image data using the 

information SDL_Surface gives us
glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w,
surface->h, 0,
texture_format, GL_UNSIGNED_BYTE, surface->pixels );

            // Free the SDL_Surface only if it was successfully created
            if ( surface )
            {
                SDL_FreeSurface( surface );
            }
        }
    }
}
else
{
    printf("ERROR: SDL could not load image.bmp: %s\n", SDL_GetError());
    BMP_To_Texture_OK = false;
}


if (BMP_To_Texture_OK == true)
{
    // Bind the texture to which subsequent calls refer to
    glBindTexture( GL_TEXTURE_2D, texture );

    glBegin( GL_QUADS ); // Draw texture with no rotation
        //Top-left vertex (corner)
        glTexCoord2i( 0, 0 );
        glVertex3f( 10, 10, 0.0f );

        //Bottom-left vertex (corner)
        glTexCoord2i( 1, 0 );
        glVertex3f( 130, 10, 0 );

        //Bottom-right vertex (corner)
        glTexCoord2i( 1, 1 );
        glVertex3f( 130, 130, 0 );

        //Top-right vertex (corner)
        glTexCoord2i( 0, 1 );
        glVertex3f( 10, 130, 0 );
    glEnd();


    // Bind the texture to which subsequent calls refer to
    glBindTexture( GL_TEXTURE_2D, texture );

    glBegin( GL_QUADS ); // Draw texture with some rotation
        //Top-left vertex (corner)
        glTexCoord2i( 0, 0 );
        glVertex3f( 100, 300, 0.0f );

        //Bottom-left vertex (corner)
        glTexCoord2i( 1, 0 );
        glVertex3f( 200, 100, 0 );

        //Bottom-right vertex (corner)
        glTexCoord2i( 1, 1 );
        glVertex3f( 300, 300, 0 );

        //Top-right vertex (corner)
        glTexCoord2i( 0, 1 );
        glVertex3f( 200, 500, 0 );
    glEnd();
}

printf("Main Loop executed!\n");

//-------------------------------------------------------------------------------
// MAIN LOOP

// . . .

SDL_GL_SwapBuffers();

//
MAIN LOOP
//-------------------------------------------------------------------------------

SDL_Delay(3000);


if (BMP_To_Texture_OK == true)  glDeleteTextures( 1, &texture );

SDL_Quit();
printf("SDL+OGL application successfully closed\n");
return 0;

}
// TM
// “A 100% By JeZ+Lee” of Silent Hero Productions®

You don’t! :wink:

OpenGL doesn’t support colorkeying “natively”. You could implement it as a
GLSL fragment shader, but other than that, you need to use alpha blending, by
converting the colorkeyed RGB surface into an RGBA surface.

Here’s one of the first Google hits on “opengl colorkey”:
http://www.gamedev.net/community/forums/topic.asp?topic_id=517739

glSDL does about the same thing to emulate the colorkeying of the SDL 2D API,
though it might be harder to dig the code out of there:
http://olofson.net/mixed.htmlOn Wednesday 10 February 2010, at 04.44.48, Jesse Palser wrote:

Hi,

I’ve gotten into using OpenGL with SDL 1.2.x branch.
How do I set the transparent color of an OpenGL texture loaded from an
SDL surface?


//David Olofson - Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://olofson.net http://kobodeluxe.com http://audiality.org |
| http://eel.olofson.net http://zeespace.net http://reologica.se |
’---------------------------------------------------------------------’

The “testgl” program in the SDL test suite has a function that might
help. IIRC the load function provided will correctly take into account
surfaces with per-pixel alpha, per-surface alpha and colour keys. Take
a look: http://svn.libsdl.org/branches/SDL-1.2/test/testgl.cOn 10 February 2010 10:44, David Olofson wrote:

On Wednesday 10 February 2010, at 04.44.48, Jesse Palser wrote:

Hi,

I’ve gotten into using OpenGL with SDL 1.2.x branch.
How do I set the transparent color of an OpenGL texture loaded from an
SDL surface?

You don’t! :wink:

OpenGL doesn’t support colorkeying “natively”. You could implement it as a
GLSL fragment shader, but other than that, you need to use alpha blending, by
converting the colorkeyed RGB surface into an RGBA surface.

Here’s one of the first Google hits on “opengl colorkey”:
? ? ? ?http://www.gamedev.net/community/forums/topic.asp?topic_id=517739

glSDL does about the same thing to emulate the colorkeying of the SDL 2D API,
though it might be harder to dig the code out of there:
? ? ? ?http://olofson.net/mixed.html


//David Olofson - Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| ?http://olofson.net ? http://kobodeluxe.com ? http://audiality.org ?|
| ?http://eel.olofson.net ?http://zeespace.net ? http://reologica.se ?|
’---------------------------------------------------------------------’


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

Jesse Palser wrote:

How do I set the transparent color of an OpenGL texture loaded from an
SDL surface?

Hi Again,

Thanks to the two people that responded to my email question!
I fixed the problem!

I added SDL_image and changed the BMP to a PNG(with a transparent alpha
set).
Then I added the two following lines:---------------------------------------------------------------------------------------------------------
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

right before I draw the OGL texture.

Thanks again!

JeZ-l-Lee