Thank you ZbuffeR and zeckensack! You were both right.
The problem was that I had to use the alpha test and not alpha blending. Also, I called glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); and that was why my “green pixels” were white (I’ve replaced GL_DECAL by GL_REPLACE).
For those who could be interested in texturing with a color key, the code is above (it works with SDL ; errors aren’t managed).
In fact, I have already a problem : after the drawing of my texture, I want to disable the alpha test. If I do that, the alpha test isn’t done for my texture. I call glFinish after the texture’s drawing (with no double buffering) and then glDisable( ALPHA_TEST ).
Must I keep the alpha test or is there a mean to disable it?
Thanks.
#include <GL/gl.h>
#include "SDL.h"
// Returns the next nearest power of two from n
int power_of_two( int n );
glEnable(GL_TEXTURE_2D);
glAlphaFunc(GL_GREATER,0.f);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
SDL_Surface *image = IMG_Load(imageFileName);
Uint32 colorKey = ...
SDL_SetColorKey( image, SDL_SRCCOLORKEY, colorKey );
textureImage= SDL_CreateRGBSurface(
SDL_SWSURFACE,
image->w,
image->h,
32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000
#else
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF
#endif
);
SDL_FillRect( textureImage, NULL, colorKey );
SDL_BlitSurface( image, NULL, textureImage, NULL );
SDL_FreeSurface( image );
GLuint textureID;
glGenTextures( 1, &textureID );
glBindTexture( GL_TEXTURE_2D, textureID );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
int width = power_of_two( textureImage->w );
int height = power_of_two( textureImage->h );
glTexImage2D(GL_TEXTURE_2D,
0,
4,
width, height,
0,
RGBA,
UNSIGNED_BYTE,
NULL
);
glTexSubImage2D( GL_TEXTURE_2D,
0,
0,
0,
textureImage->w,
textureImage->h,
RGBA,
UNSIGNED_BYTE,
textureImage->pixels );
GLfloat widthRate = ((GLfloat) textureImage->w) / ((GLfloat) width);
GLfloat heightRate = ((GLfloat) textureImage->h) / ((GLfloat) height);
glEnable( GL_ALPHA_TEST );
glBindTexture(GL_TEXTURE_2D, textureID);
GLfloat x = ...,
y = ...;
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(0., 0.); glVertex2i(x, y );
glTexCoord2f(widthRate, 0.); glVertex2i(x + textureImage->w, y );
glTexCoord2f(0., heightRate); glVertex2i(x, y + height);
glTexCoord2f(widthRate, heightRate); glVertex2i(x + textureImage->w, y + textureImage->h);
glEnd();
//glDisable( GL_ALPHA_TEST ); => Doesn't work!!
Hope it can be useful.