即使启用了Alpha混合,OpenGL透明度也无法在C#中工作

时间:2013-06-14 00:19:21

标签: c# windows opengl transparency alphablending

我正在尝试将我们的一个应用程序从Managed DirectX转换为C#中的OpenGL,因此它将与Windows 8兼容。我们需要的一个关键功能是透明度,而且对于练习,我试图创建一些NeHe lesson 2 C#项目的透明形状。看来,即使我添加glEnable(GL_BLEND)glColor4ub(255, 255, 255, 128),我也无法获得任何透明度。

对于这个测试项目,我只是复制了上面链接的C#项目,并更改了2个函数。对于InitGL(),我添加了必要的行(或者我已经知道)以使alpha混合工作。

private bool InitGL()
{
    glShadeModel(GL_SMOOTH);
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    // These are the lines I've added
    glEnable(GL_BLEND);                                         // Add alpha blending
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);          // Use this alpha function
    glEnable(GL_CULL_FACE);                                     // Don't render the backface
    return true;                                                // Initialization Went OK
}

对于DrawGLScene(),我为每个顶点绘制三角形和四边形。

public bool DrawGLScene()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(-1.5f,0.0f,-6.0f);
    glBegin(GL_TRIANGLES);
        glColor4ub(255, 0, 0, 128);                     // Transparent red
        glVertex3f( 0.0f, 1.0f, 0.0f);                  // Top
        glColor4ub(0, 255, 0, 128);                     // Transparent green
        glVertex3f(-1.0f,-1.0f, 0.0f);                  // Bottom Left
        glColor4ub(0, 0, 255, 128);                     // Transparent blue
        glVertex3f( 1.0f,-1.0f, 0.0f);                  // Bottom Right

        glColor4ub(0, 0, 255, 128);                     // Transparent red
        glVertex3f(1.0f, 1.0f, 1.0f);                   // Bottom Right
        glColor4ub(0, 255, 0, 128);                     // Transparent green
        glVertex3f(-1.0f, 1.0f, 1.0f);                  // Bottom Left
        glColor4ub(255, 0, 0, 128);                     // Transparent blue
        glVertex3f(0.0f, -1.0f, 1.0f);                  // Top
    glEnd();
    glTranslatef(3.0f,0.0f,0.0f);
    glBegin(GL_QUADS);
        glColor4ub(0, 255, 255, 128);                   // Transparent Cyan
        glVertex3f(1.0f, 1.0f, 0.0f);                   // Top Right
        glColor4ub(255, 255, 0, 128);                   // Transparent Yellow
        glVertex3f(-1.0f, 1.0f, 0.0f);                  // Top Left
        glColor4ub(255, 255, 255, 128);                 // Transparent White
        glVertex3f(-1.0f, -1.0f, 0.0f);                 // Bottom Left
        glColor4ub(255, 0, 255, 128);                   // Transparent Magenta
        glVertex3f( 1.0f,-1.0f, 0.0f);                  // Bottom Right
    glEnd();
    return true;
}

我已经使用Windows 8和7对此进行了测试,但两者似乎都没有描述任何透明度。那是为什么?

0 个答案:

没有答案