Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the opacity of a vertex in OpenGL?

The following snippet draws a gray square.

glColor3b(50, 50, 50);

glBegin(GL_QUADS);
glVertex3f(-1.0, +1.0, 0.0); // top left
glVertex3f(-1.0, -1.0, 0.0); // bottom left
glVertex3f(+1.0, -1.0, 0.0); // bottom right
glVertex3f(+1.0, +1.0, 0.0); // top right
glEnd();

In my application, behind this single square exists a colored cube.

What function should I use to make square (and only this square) opaque?

like image 804
Nick Bolton Avatar asked Apr 06 '09 14:04

Nick Bolton


People also ask

How do I change opacity 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 in OpenGL?

The alpha color value is the 4th component of a color vector that you've probably seen quite often now. Up until this chapter, we've always kept this 4th component at a value of 1.0 giving the object 0.0 transparency. An alpha value of 0.0 would result in the object having complete transparency.


1 Answers

In the init function, use these two lines:

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

And in your render function, ensure that glColor4f is used instead of glColor3f, and set the 4th argument to the level of opacity required.

glColor4f(1.0, 1.0, 1.0, 0.5);

glBegin(GL_QUADS);
glVertex3f(-1.0, +1.0, 0.0); // top left
glVertex3f(-1.0, -1.0, 0.0); // bottom left
glVertex3f(+1.0, -1.0, 0.0); // bottom right
glVertex3f(+1.0, +1.0, 0.0); // top right
glEnd();
like image 123
Nick Bolton Avatar answered Sep 16 '22 16:09

Nick Bolton