OpenGL渲染会导致图形故障

时间:2015-01-29 16:03:06

标签: opengl graphics 3d sfml immediate-mode

代码如下。当程序运行时,它会生成一个或多或少正确的图像,但有一些图形故障。以下是一些图片:1 2 3。关于可能导致这种情况的任何想法?最奇怪的部分是问题似乎在三角形上形成,即使图像是用多边形(四边形)绘制的。

void buildList() {
    cubelist = glGenLists(1);
    glNewList(cubelist,GL_COMPILE);

    // Multi-colored side - FRONT
    glBegin(GL_POLYGON);
    glColor3b(0,0,0);
    glVertex3f( -0.5, -0.5, -0.5);
    glVertex3f( -0.5,  0.5, -0.5);
    glVertex3f(  0.5,  0.5, -0.5);
    glVertex3f(  0.5, -0.5, -0.5);
    // Black side - BACK
    glBegin(GL_POLYGON);
    glColor3b(0,0,0);
    glVertex3f(  0.5, -0.5, 0.5 );
    glVertex3f(  0.5,  0.5, 0.5 );
    glVertex3f( -0.5,  0.5, 0.5 );
    glVertex3f( -0.5, -0.5, 0.5 );
    glEnd();

    // Purple side - RIGHT
    glBegin(GL_POLYGON);
    glColor3f(  1.0,  0.0,  1.0 );
    glVertex3f( 0.5, -0.5, -0.5 );
    glVertex3f( 0.5,  0.5, -0.5 );
    glVertex3f( 0.5,  0.5,  0.5 );
    glVertex3f( 0.5, -0.5,  0.5 );
    glEnd();

    // Green side - LEFT
    glBegin(GL_POLYGON);
    glColor3f(   0.0,  1.0,  0.0 );
    glVertex3f( -0.5, -0.5,  0.5 );
    glVertex3f( -0.5,  0.5,  0.5 );
    glVertex3f( -0.5,  0.5, -0.5 );
    glVertex3f( -0.5, -0.5, -0.5 );
    glEnd();

    // Blue side - TOP
    glBegin(GL_POLYGON);
    glColor3f(   0.0,  0.0,  1.0 );
    glVertex3f(  0.5,  0.5,  0.5 );
    glVertex3f(  0.5,  0.5, -0.5 );
    glVertex3f( -0.5,  0.5, -0.5 );
    glVertex3f( -0.5,  0.5,  0.5 );
    glEnd();

    // Red side - BOTTOM
    glBegin(GL_POLYGON);
    glColor3f(   1.0,  0.0,  0.0 );
    glVertex3f(  0.5, -0.5, -0.5 );
    glVertex3f(  0.5, -0.5,  0.5 );
    glVertex3f( -0.5, -0.5,  0.5 );
    glVertex3f( -0.5, -0.5, -0.5 );
    glEnd();
    glEndList();
}

void setupGL() {
    glClearColor(1, 1, 1, 0);
    glEnable(GL_DEPTH_CLAMP);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glCullFace(GL_FRONT_AND_BACK);
}

int main(int, char const**)
{
    sf::Window window(sf::VideoMode(1600, 1200), "OpenGL", sf::Style::Default, sf::ContextSettings(32));
    window.setVerticalSyncEnabled(true);
    //initWorld();
    setupGL();
    buildList();

    while (running)
    {

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        // Process events
        sf::Event event;
        handleInput(window);
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == sf::Event::Closed) {
                running = false;
            }

            // Escape pressed: exit
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
                running = false;
            }
        }

        glCallList(cubelist);

        // Update the window (c is a "Camera" object that just sets the perspective.   I can provide code if necessary)
        c.draw();
        window.display();
    }

    return EXIT_SUCCESS;
}

编辑:所以将GL_POLYGONS语句更改为GL_QUADS似乎可以解决问题,但我仍然对此感到好奇。

1 个答案:

答案 0 :(得分:1)

您的第一个glBegin没有匹配的glEnd,这会导致所有其他glBegin被忽略,直到下一个glEnd。这意味着正面泄漏到背面。

表示立即模式和相关的displayList已弃用,而是查看VBO以存储顶点数据。

相关问题