Jump to content
  • Advertisement
Sign in to follow this  
artefon

OpenGL SDL + OpenGL + Alpha

This topic is 3715 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!