OpenGL Sprite Drawing 2D
Hi!
I would like to create a 2d game using opengl''s 3d acceleration capabilities. My problem is this, in direct draw I could always blit my sprite using the preset colorkey which becomes transparent and also use translucency at the same time to make a sprite translucent (eg. for explosions and effects), how do I replicate that effect in OpenGL. By the way I am using glOrtho Mode (2D).
Thanks in advamce!
use GL_ALPHA_TEST and GL_BLEND:
I don''t exactly remember the usage of GL_ALPHA_TEST, it was some
thing like:
Browse MSDN for further information!
glEnable (GL_BLEND);glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);glColor4f (1,1,1,0.5); // alpha=0.5=half visibleglBegin (GL_TRIANGLES); // or GL_POLYGON or whatever// ... your drawing code goes here...// glTexCoord2f (u,v); for texture coord on polygon// glVertex2f (x, y); for vertex coord on screenglEnd ();glDisable (GL_BLEND);
I don''t exactly remember the usage of GL_ALPHA_TEST, it was some
thing like:
glEnable (GL_ALPHA_TEST);glAlphaFunc (GL_GREATER, 0.666f);// drawing code...glDisable (GL_ALPHA_TEST);
Browse MSDN for further information!
Easy, create a mask of the sprite where everything you DON''T want to show up is white, and everything you want to show up is BLACK (make sure it''s true white and true black x0000 and xFFFF).
Then make sure your sprite has TRUE BLACK for the areas you don''t want to show (basically where the sprite is black, the mask should be white)
Then:
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
then bind the MASK texture
and use:
glBlendFunc(GL_DST_COLOR,GL_ZERO);
then draw your textured quad (which will be the mask)
now it''s time for the sprite!
bind the SPRITE texture
and use:
glBlendFunc(GL_ONE,GL_ONE);
draw the textured quad (of the sprite)
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
and voila you''re lookin'' at your sprite the way it''s supposed to be... only problem with this method is that it takes 2 passes... so if you have a slow comp or a SHITLOAD of stuff on the screen you''ll take a performance hit... but it should be fine for most things you''ll do.
Then make sure your sprite has TRUE BLACK for the areas you don''t want to show (basically where the sprite is black, the mask should be white)
Then:
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
then bind the MASK texture
and use:
glBlendFunc(GL_DST_COLOR,GL_ZERO);
then draw your textured quad (which will be the mask)
now it''s time for the sprite!
bind the SPRITE texture
and use:
glBlendFunc(GL_ONE,GL_ONE);
draw the textured quad (of the sprite)
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
and voila you''re lookin'' at your sprite the way it''s supposed to be... only problem with this method is that it takes 2 passes... so if you have a slow comp or a SHITLOAD of stuff on the screen you''ll take a performance hit... but it should be fine for most things you''ll do.
Are these for transparency only or both?
I need a transparent background at the same time the sprite is translucent.
I want to imitate the dark templar effect (when he is on your side), in 2D.
Thanks for the replies
I need a transparent background at the same time the sprite is translucent.
I want to imitate the dark templar effect (when he is on your side), in 2D.
Thanks for the replies
Hello,
another thing you want to watch is filtering as
this can mess up the edges of the sprites. For best
results turn off filtering and have a 1:1 ratio of
quad size and texture.
Mark
another thing you want to watch is filtering as
this can mess up the edges of the sprites. For best
results turn off filtering and have a 1:1 ratio of
quad size and texture.
Mark
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement