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

喜夏-厌秋 提交于 2019-12-06 03:59:46

问题


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?


回答1:


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();



回答2:


glColor4f(float r,float g, float b, flaot alpha);
(in your case maybe clColor4b)
also make sure, that blending is enabled.
(you have to reset the color to non-alpha afterwads, which might involve a glGet* to save the old vertexcolor)




回答3:


Use glColor4 instead of glColor3. For example:

glBlendFunc(GL_SRC_ALPHA,GL_ONE);
glColor4f(1.0f,1.0f,1.0f,0.5f);



回答4:


You can set colors per vertex

glBegin(GL_QUADS);
glColor4f(1.0, 0.0, 0.0, 0.5); // red, 50% alpha
glVertex3f(-1.0, +1.0, 0.0); // top left
// Make sure to set the color back since the color state persists
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();


来源:https://stackoverflow.com/questions/721705/how-do-i-set-the-opacity-of-a-vertex-in-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!