Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use alpha transparency in OpenGL?

Here's my code:

void display(void);  int main(int argc, char** argv) {     glutInit(&argc, argv);     glutInitDisplayMode(GLUT_SINGLE|GLUT_RGBA);     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);     glEnable( GL_BLEND );     glutInitWindowSize(600,600);     glutInitWindowPosition(200,50);     glutCreateWindow("glut test");     glutDisplayFunc(display);     glutMainLoop();     return 0; }  void display() {     glClear(GL_COLOR_BUFFER_BIT);     glPointSize(8);     glBegin(GL_POINTS);     glColor4f(.23,.78,.32,1.0);     glVertex2f(0,0);     glColor4f(.23,.78,.32,0.1);     glVertex2f(0.1,0);     glEnd();     glFlush(); } 

The problem is that these two points appear identical (even when I set the alpha to 0). Is there something I missed to enable alpha transparency?

like image 223
H-H Avatar asked Oct 24 '09 08:10

H-H


People also ask

How do I enable transparency in OpenGL?

OpenGL “transparency” would instead blend the RGB of the red object with the RGB of the green glass, giving a shade of yellow. Instead of using glColor3f( ) to specify the red, green, and blue of an object, use glColor4f( ) to specify red, green, blue, and alpha. Alpha is the transparency factor.

What is alpha testing OpenGL?

Description. The alpha test discards fragments depending on the outcome of a comparison between an incoming fragment's alpha value and a constant reference value. glAlphaFunc specifies the reference value and the comparison function. The comparison is performed only if alpha testing is enabled.

What is transparency Alpha?

Alpha transparency is a way to support gradations of opacity on a continuum. Values between zero (0) and one (1) allow some of the background to “show through” and “mix” with that color. Values closer to zero (0) show more background, and values closer to one (1) show less background.

What is blending in OpenGL?

Blending is the stage of OpenGL rendering pipeline that takes the fragment color outputs from the Fragment Shader and combines them with the colors in the color buffers that these outputs map to. Blending parameters can allow the source and destination colors for each output to be combined in various ways.


2 Answers

have you glEnable'd alpha blending? And have you set up your blend parameters? You can't just set the alpha you need to setup various other parameters in OpenGL.

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable( GL_BLEND ); 
like image 118
Goz Avatar answered Oct 06 '22 04:10

Goz


Just a guess, but could it be that you dont have a background color ? So, when your rendering the second vertex which has alpha 0.1, there is no background to compute the proper color ? Just a guess, been years since i used opengl.

like image 36
Andrew Keith Avatar answered Oct 06 '22 04:10

Andrew Keith