2

我在OpenGL中模拟透明度时遇到了一个问题。这是场景:

我有一艘由球体表示的船。现在我想在船周围加一个盾牌。我也选择了一个球体,但半径更大,并将 alpha 因子设置为 0.5(不透明度)。但是,盾牌没有出现,颜色也没有混合(好像它不存在一样)。

相机位于第一个球体的中心。我认为问题在于我在球体内,所以opengl会忽略它(而不是绘制它)。

代码如下所示:

//ship colors setup with alpha 1.0f
glutSolidSphere(1, 100, 100); original sphere ( ship )
//shield colors setup with alpha 0.5f
glutSolidSphere(3, 100, 100); //the shield whose colors should blend with the rest of  the scene

我不得不在船前用平行六面体模拟盾牌。然而这不是我想要的……

编辑:我发现了错误。我将 gluPerspective() 的近参数设置得太高,所以即使我正确设置了 alpha 值,相机始终在对象前面,所以无法看到它。

4

1 回答 1

0

似乎在这里工作:

#include <GL/glut.h>

void display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0, 0, -5);

    glDisable(GL_BLEND); 
    glColor4ub(255,0,0,255);
    glutSolidCube(1.0);

    glEnable(GL_BLEND); 
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glColor4ub(0,255,0,64);
    glutSolidSphere(1.5, 100, 100);

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective( 60, (double)w / (double)h, 0.01, 100 );
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(800,600);
    glutCreateWindow("Blending");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return EXIT_SUCCESS;
}
于 2012-01-04T17:27:43.170 回答