How to use alpha transparency in OpenGL?

懵懂的女人 提交于 2019-11-27 11:36:16

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/1617370/how-to-use-alpha-transparency-in-opengl

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