Fw: Please Help :: Simple OpenGL & SDL Texture query

I have the following code to load in a texture and display it in opengl.

I want all the pixels in the PNG with value 85,85,85 to be transparent (i want to see through these pixels). Using SDL_SetColorKey stops OpenGL colouring the pixels but i cannot get the transparent effect desired : ie glColor3f(0,255,0) does not affect the pixels with values 85,85,85.

Can anybody help me please

Thankyou in advance
----- create the texture

SDL_Surface *sfc = IMG_Load(“image.png”);
SDL_SetColorKey(sfc, SDL_SRCCOLORKEY ,SDL_MapRGB(sfc->format, 85, 85, 85));
SDL_Surface *tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, 256, 256, 24,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
0x00ff0000, 0x0000ff00, 0x000000ff, 0);
#else
0x000000ff, 0x0000ff00, 0x00ff0000, 0)
#endif
SDL_BlitSurface(sfc, NULL, tmp, NULL);
SDL_FreeSurface(sfc);

glGenTextures(1, &texture);glBindTexture(GL_TEXTURE_2D, texture);
glPixelStorei(GL_UNPACK_ROW_LENGTH, tmp->pitch /tmp->format->BytesPerPixel);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, tmp->w, tmp->h, 0, GL_RGB, GL_UNSIGNED_BYTE, tmp->pixels);

glFlush();
SDL_FreeSurface(tmp);

– and when i draw

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Title_texture);

glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glBegin(GL_QUADS);
glColor4ub(255, 255, 255, 255);
glTexCoord2f(0, 0); glVertex2f(-1, 1);
glTexCoord2f(1, 0); glVertex2f(1, 1);
glTexCoord2f(1, 1); glVertex2f(1, -1);
glTexCoord2f(0, 1); glVertex2f(-1, -1);
glEnd();

“steve” writes:

I?have the following code to load in a texture and display it in opengl.

?

I want all the pixels in the PNG with value 85,85,85 to be transparent (i want
to see through these pixels).? Using SDL_SetColorKey stops OpenGL colouring the
pixels but i cannot get the transparent effect desired : ie glColor3f(0,255,0)
does not affect the pixels with values 85,85,85.
Since you use colorkey blitting, pixels with 85,85,85 values won’t ever be
in your tmp surface - instead they will become 0,0,0.

Your initial surface might also contain alpha values - most probably
you want these to propogate to texture alphas - so you have to disable
alpha blitting:
SDL_SetAlpha(sfc, 0, 0);

Obviously, if you want alphas you need to create 32 bit tmp surface.
Then you might try to enable alpha test and set alpha func to > 0.
Your blending code should work too.

Here’s a complete snippet that should work:----

SDL_Surface *sfc = IMG_Load(“image.png”);
SDL_SetAlpha(sfc, 0, 0);
SDL_SetColorKey(sfc, SDL_SRCCOLORKEY, SDL_MapRGB(sfc->format, 85, 85, 85));

SDL_Surface *tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, 256, 256, 32,
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
??? 0x00ff0000, 0x0000ff00, 0x000000ff, 0);
#else
??? 0x000000ff, 0x0000ff00, 0x00ff0000, 0);
#endif

SDL_BlitSurface(sfc, NULL, tmp, NULL);
SDL_FreeSurface(sfc);

glGenTextures(1, &texture);glBindTexture(GL_TEXTURE_2D, texture);
glPixelStorei(GL_UNPACK_ROW_LENGTH, tmp->pitch /tmp->format->BytesPerPixel);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tmp->w, tmp->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, tmp->pixels);

– and when i draw
glEnable (GL_BLEND);

glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Or:
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATEER, 0.0);


Hope that helps,
– Ed

P.S. You might want to look at SDL_teapot
(http://icculus.org/SDL_teapot). It contains a demo “test_2d” that
does exactly what you want. Also look at “testgl.c” in SDL’s sources
"test" directory.