Sign in to follow this  
artefon

OpenGL SDL + OpenGL + Alpha

Recommended Posts

Hi folks, im having some problems using OpenGL with SDL. The way i am doing i can blit images on screen but i get a wierd result in the alpha channel. I am using the following code: Initing SDL:
 39     glEnable( GL_TEXTURE_2D );
 40     glEnable( GL_ALPHA_TEST );
 41     glAlphaFunc( GL_GREATER,0.01f);
 42
 43     glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
 44     glViewport( 0,
 45                 0,
 46                 Config::SCREEN_WIDTH,
 47                 Config::SCREEN_HEIGHT
 48                 );
 49
 50     glClear( GL_COLOR_BUFFER_BIT );
 51     glMatrixMode( GL_PROJECTION );
 52     glLoadIdentity();
 53
 54     glOrtho(0.0f,
 55             Config::SCREEN_WIDTH,
 56             Config::SCREEN_HEIGHT,
 57             0.0f,
 58             -1.0f,
 59             1.0f);
 60
 61     glMatrixMode( GL_MODELVIEW );
 62     glLoadIdentity();
 63
 64     if( glGetError() != GL_NO_ERROR ) { throw SDLError(); }
Screen draw:
 81             glBindTexture(GL_TEXTURE_2D, img->texture);
 82             glBegin( GL_QUADS );
 83             //Top-left vertex (corner)
 84             glTexCoord2i( 0, 0 );
 85             glVertex3f( x, y, 0 );
 86             //Top-right vertex (corner)
 87             glTexCoord2i( 1, 0 );
 88             glVertex3f( x+img->width, y, 0 );
 89             //Bottom-right vertex (corner)
 90             glTexCoord2i( 1, 1 );
 91             glVertex3f( x+img->width, y+img->height, 0 );
 92             //Bottom-left vertex (corner)
 93             glTexCoord2i( 0, 1 );
 94             glVertex3f( x, y+img->height, 0 );
 95             glEnd();
Texture creating:
 68     glGenTextures( 1, &texture );
 69     glBindTexture( GL_TEXTURE_2D, texture );
 70
 71     glTexImage2D( GL_TEXTURE_2D,
 72                   0,
 73                   GL_RGBA,
 74                   surface->w,
 75                   surface->h,
 76                   0,
 77                   GL_RGBA,
 78                   GL_UNSIGNED_BYTE,
 79                   surface->pixels);
 80
 81     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
 82     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 83     SDL_FreeSurface(surface);
And the image used has a power of 2 size and was created by the following code:
 26     image = SDL_CreateRGBSurface(
 27             SDL_SWSURFACE,
 28             w,
 29             h,
 30             Config::SCREEN_BPP,
 31             #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
 32             0x000000FF,
 33             0x0000FF00,
 34             0x00FF0000,
 35             0xFF000000
 36             #else
 37             0xFF000000,
 38             0x00FF0000,
 39             0x0000FF00,
 40             0x000000FF
 41             #endif
 42             );
if i try using one of the following modes all the screen gets black:
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable( GL_ALPHA_TEST );
glAlphaFunc( GL_GREATER,0.1f);
Using only SDL the image is displayed correctly but if i use opengl the alpha channel gets black like this: http://img48.imageshack.us/img48/3606/opengltd4.png can someone help me?? thanks in advance!

Share this post


Link to post
Share on other sites
Quote:
Original post by Split Thumbs
Accessing pixels straight from an SDL surface does not work.

It does, but you must lock the surface first.

Share this post


Link to post
Share on other sites
i corrected it! missed this code:


/* Save the alpha blending attributes */
Uint32 savedFlags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
Uint8 savedAlpha = surface->format->alpha;
if ( (savedFlags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
SDL_SetAlpha(surface, 0, 0);
}

area.x = 0;
area.y = 0;
area.w = w;
area.h = h;
SDL_BlitSurface(surface, &area, image, &area);

/* Restore the alpha blending attributes */
if ((savedFlags & SDL_SRCALPHA) == SDL_SRCALPHA) {
SDL_SetAlpha(surface, savedFlags, savedAlpha);
}


Hope it helps!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this