Jump to content
  • Advertisement
Sign in to follow this  
slabix

OpenGL Alpha blending and z-ordering.

This topic is 2594 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.

There's a problem I've been working on for a couple of days. I'm using OpenGL with SDL in C++.

I have three objects: Player, Map (based on tileset) and Background. Player's depth is equal to 100 (which is the foreground), Map's is about 50, and Background's is equal to 25. For now, background is just filled rectangle, and tilemap is quite random. Here's what i get:
1: http://img846.imageshack.us/img846/9745/79045196.png , 2: http://img801.imageshack.us/img801/3808/38667626.png and 3: http://img823.imageshack.us/img823/1944/45278783.png

Transparent pixels of player has alpha channel set to 0, but still - he's making a black box, that's covers the Map, but doesn't cover the Background.

Here are my OpenGL settings:
[spoiler]

SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
// Projekcja - otworzenie planszy opengl.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, win_w, win_h, 0, -100, 0);
// Utworzenie, hmm... drugiej planszy
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Ustawienia:
glEnable(GL_TEXTURE_2D); // W??czenie teksturowania 2D.
glShadeModel(GL_SMOOTH); // W??czenie smootha.
glEnable(GL_BLEND); // W??czenie blendowania.
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Ustawienia blendowania.
glClearColor(0, 0, 0, 1); // Ustawienie czystego koloru.
glClearDepth(100.0); // Ustawienie g??boko?ci.
glEnable(GL_DEPTH_TEST); // W??czenie testowania g??boko?ci.
glDepthFunc(GL_LEQUAL); // Ustawienie testowania g??boko?ci.
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Ustawienie ?adniejszej perspektywy.
[/spoiler]

Here's code that sets the alpha transparency: (also - preconfigured alpha pixels (for example in Gimp) works the same):

[spoiler]// Utworzenie koloru przezroczysto?ci, ustawiaj?c kana? alpha dla ka?dego zielonego piksela na 255.
SDL_Color color;
for (int y = 0; y < optimized->h; y++)
{
for (int x = 0; x < optimized->w; x++)
{
unsigned int pix = ((unsigned int*)optimized->pixels)[y*(optimized->pitch/sizeof(unsigned int)) + x];
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
color = {(pix & 0x00ff0000)/0x10000,(pix & 0x0000ff00)/0x100,(pix & 0x000000ff),0};
#else
color = {(pix & 0x000000ff),(pix &0x0000ff00)/0x100,(pix & 0x00ff0000)/0x10000,0};
#endif
if (color.r == 0 and color.g == 255 and color.b == 0)
{
((unsigned int*)optimized->pixels)[y*(optimized->pitch/sizeof(unsigned int)) + x] = SDL_MapRGBA(optimized->format, 0, 0, 0,0);
}
}
}
[/spoiler]

Here's my code for drawing a quad (like the Background):
[spoiler]void draw_rect(GLfloat x, GLfloat y, GLfloat w, GLfloat h, GLfloat z, GLfloat r)
{
glDisable(GL_TEXTURE_2D);
glRotatef(r,0.0,0.0,1.0);
glTranslatef(x, y, z); // Ustawienie g?ównego punktu..
glBegin(GL_QUADS); // Rozpoczecie rysowanie zape?nionego kwadratu.
// Cztery rogi.
glVertex3f(0, 0, 0);
glVertex3f(w, 0, 0);
glVertex3f(w, h, 0);
glVertex3f(0, h, 0);
//Zako?czenie rysowania.
glEnd();
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
}
[/spoiler]

Texture loading function:
[spoiler]void generate_texture(Sprite * s)
{
glGenTextures(1, &(s->texture_id));
// Format tekstury.
GLenum texture_format;
// Sprawdzenie formatu tekstury.
if (s->sprite_surface->format->BytesPerPixel == 4) // Je?li jest kana? alpha.
if (s->sprite_surface->format->Rmask == 0x000000ff) texture_format = GL_RGBA;
else texture_format = GL_BGRA;
else
if (s->sprite_surface->format->Rmask == 0x000000ff) texture_format = GL_RGB;
else texture_format = GL_BGR;
// Ustawienia tekstury.
glBindTexture(GL_TEXTURE_2D, s->texture_id); // Wybranie.
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Ust. rozci?gania.
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

// Wygenerowanie tekstury.
glTexImage2D(GL_TEXTURE_2D, 0, s->sprite_surface->format->BytesPerPixel, s->sprite_surface->w, s->sprite_surface->h, 0,
texture_format, GL_UNSIGNED_BYTE, s->sprite_surface->pixels);
glLoadIdentity();
}
[/spoiler]

And, at last, my texture blitting function: (I already have all my images 256x256, so I don't have to resize the pixeldatas)
[spoiler]// Wy?wietlenie tekstury.
void draw_texture(Sprite *s)
{
// Wybór tekstury.
glBindTexture(GL_TEXTURE_2D, s->texture_id);
// Ustawienia.
glRotatef(s->angle, 0.0,0.0,1.0);
glTranslatef(s->x, s->y, s->z);
glColor4f(static_cast<GLfloat>(s->color.r)/255, static_cast<GLfloat>(s->color.g)/255,
static_cast<GLfloat>(s->color.b)/255, 1.0);//static_cast<GLfloat>(s->alpha)/255);
// Rozpocz?cie rysowania prostok?ta.
float mx = (s->clip_x/static_cast<float>(s->width));
float my = (s->clip_y/static_cast<float>(s->height));
float mw = (s->clip_width/static_cast<float>(s->width));
float mh = (s->clip_height/static_cast<float>(s->height));
glBegin(GL_QUADS);
// Górny lewy róg.
glTexCoord2f(mx, my); glVertex3d(s->zoom*(-s->center_x), s->zoom*(-s->center_y), 0);
// Górny prawy róg.
glTexCoord2f(mx+mw, my); glVertex3d(s->zoom*(s->clip_width-s->center_x), s->zoom*(-s->center_y), 0);
// Dolny prawy róg.
glTexCoord2f(mx+mw, my+mh); glVertex3d(s->zoom*(s->clip_width-s->center_x), s->zoom*(s->clip_height-s->center_y), 0);
// Dolny lewy róg.
glTexCoord2f(mx, my+mh); glVertex3d(s->zoom*(-s->center_x), s->zoom*(s->clip_height-s->center_y), 0);
glEnd();
glLoadIdentity();
}
[/spoiler]


I already tried to change depth of my objects, I tried to change the color of transparency (and did it manually throught Gimp) and tried almost all of the DepthFunc and BlendFunct settings.

Anybody have any ideas, why my texture still gives that strange, black box which covers the map?

Share this post


Link to post
Share on other sites
Advertisement
Where's the code that shows the order in which you are drawing the background, the map and the player?

EDIT: I ask, because drawing order can affect things when alpha-blending. If you draw the player before you draw the map, then the depth fragments output by the transparent portions of the player can keep the map from being drawn. If you draw the player after you draw the map, this shouldn't be a problem.

Share this post


Link to post
Share on other sites
Ouch.

I thought it doesn't matter, because I have set their z value.

Hmm... So if I want some tiles to be below the player, I have to draw them first, and if I want them to be on top of him, I have to draw them later? Too bad, I was sure that opengl has kind of automatic z-ordering, that allows me to draw'em in any order. That makes me to reorganize whole Map class (I have some tiles below and some above the player).

Ech, anyway, thanks, It's working now...

[spoiler] GL::set_color(1.0,0.0,0.0,0.5);
GL::draw_rect(0,0,400,400,25);
player.draw(); // <--
map.draw(); // <-- I switch'd them
[/spoiler]

Share this post


Link to post
Share on other sites
Z-buffering works great for solid geometry, but transparency has always played hell with it, since transparent pixels still output depth. You can use glAlphaFunc (and call glEnable(GL_ALPHA_TEST) )to actually snip out pixels whose alpha is 0, in which case they won't output a depth fragment. However, if the edges of your sprites are anti-aliased for a smoother blend, then this can result in a halo being drawn around your characters, since those partially-transparent pixels will still output depth and will still need to be properly blended with their background. Generally speaking, whenever alpha transparency or alpha blending is involved, the objects need to be sorted and drawn back to front.

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!