[英]Setting transparent background color in OpenGL doesn't work
I tried setting the background color to a transparent one using the functions - glClearColor() and glClear().我尝试使用函数 glClearColor() 和 glClear() 将背景颜色设置为透明颜色。 But, the alpha values passed to glClearColor() simply doesn't change anything.
但是,传递给 glClearColor() 的 alpha 值根本不会改变任何东西。
Here is the code I tried running:这是我尝试运行的代码:
#include<GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(float)glutGet(GLUT_WINDOW_WIDTH),0.0,(float)glutGet(GLUT_WINDOW_HEIGHT));
glBegin(GL_LINES);
glVertex2i(200,200);
glVertex2i(300,305);
glEnd();
glFlush();
}
int main(int argc, char *argv[const])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA);
glutInitWindowSize(1100,620);
glutInitWindowPosition((glutGet(GLUT_SCREEN_WIDTH)-1100)/2,(glutGet(GLUT_SCREEN_HEIGHT)-620)/2);
glutCreateWindow("GLUT Programming");
glClearColor(0.0f,0.0f,0.0f,0.5f); // I have tried experimenting with this part, but, nothing happens
glutDisplayFunc(display);
glutMainLoop();
}
I am using freeglut and freeglut-devel on my machine running Fedora 26, if it helps.如果有帮助,我在运行 Fedora 26 的机器上使用 freeglut 和 freeglut-devel。
EDIT :编辑 :
Result I am getting :结果我得到:
Result I am trying to obtain :我试图获得的结果:
If you want to enable Blending , then you have to enable blending ( glEnable( GL_BLEND )
) and you have to set the blend function ( glBlendFunc
).如果要启用Blending ,则必须启用混合 (
glEnable( GL_BLEND )
) 并且必须设置混合功能 ( glBlendFunc
)。
Further you have to set the alpha channel of the color, which you use to draw the geometry ( glColor4f
)此外,您必须设置颜色的 alpha 通道,用于绘制几何图形(
glColor4f
)
Change your code somehow like this:像这样以某种方式更改您的代码:
glClearColor( 0.5f, 0.5f, 0.5f, 1.0f ); // background color
glClear(GL_COLOR_BUFFER_BIT); // clear background with background color
glEnable( GL_BLEND );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); // = color * alpha + background * (1-alpha)
glColor4f( 1.0f, 0.0f, 0.0f, 0.1f ); // color of the line, alpha channel 0.1 (very "transparent")
glLineWidth( 5.0 );
glBegin(GL_LINES);
.....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.