0

我正在使用 SFML、OpenGL 和 VC++ 编译器。当我渲染某些东西时,它只会在第二次运行时正确弹出(?)。这是我的代码:

void Renderer::initializeOpenGL() {
    glEnable(GL_DEPTH_TEST);
    glEnableClientState(GL_VERTEX_ARRAY);
    glOrtho(0, 1024, 0, 576, 0, 1024);
}

void Renderer::drawWorld(World* world) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3f(1.F, 1.F, 1.F);
    std::vector<int> vertices;
    for(unsigned int x = 0; x < world->width; x++) {
        bool left = false, right = false;
        if(x == 0)
            left = true;
        else if(x == world->width -1)
            right = true;
        for(unsigned int z = 0; z < world->depth; z++) {
            bool front = false, back = false;
            if(z == 0)
                front = true;
            else if(z == world->depth -1)
                back = true;
            for(unsigned int y = 0; y < world->height; y++) {
                if(!world->blocks[x][z][y])
                    continue;
                if(left == true || !world->blocks[x-1][z][y]) {
                    vertices.push_back(x);
                    vertices.push_back(y);
                    vertices.push_back(z);

                    vertices.push_back(x);
                    vertices.push_back(y);
                    vertices.push_back(z+1);

                    vertices.push_back(x);
                    vertices.push_back(y+1);
                    vertices.push_back(z+1);

                    vertices.push_back(x);
                    vertices.push_back(y+1);
                    vertices.push_back(z);
                }
                if(right == true || !world->blocks[x+1][z][y]) {
                    vertices.push_back(x+1);
                    vertices.push_back(y);
                    vertices.push_back(z);

                    vertices.push_back(x+1);
                    vertices.push_back(y);
                    vertices.push_back(z+1);

                    vertices.push_back(x+1);
                    vertices.push_back(y+1);
                    vertices.push_back(z+1);

                    vertices.push_back(x+1);
                    vertices.push_back(y+1);
                    vertices.push_back(z);
                }

                if(front == true || !world->blocks[x][z-1][y]) {
                    vertices.push_back(x);
                    vertices.push_back(y);
                    vertices.push_back(z+1);

                    vertices.push_back(x+1);
                    vertices.push_back(y);
                    vertices.push_back(z+1);

                    vertices.push_back(x+1);
                    vertices.push_back(y+1);
                    vertices.push_back(z+1);

                    vertices.push_back(x);
                    vertices.push_back(y+1);
                    vertices.push_back(z+1);
                }
                if(back == true || !world->blocks[x][z+1][y]) {
                    vertices.push_back(x);
                    vertices.push_back(y);
                    vertices.push_back(z);

                    vertices.push_back(x+1);
                    vertices.push_back(y);
                    vertices.push_back(z);

                    vertices.push_back(x+1);
                    vertices.push_back(y+1);
                    vertices.push_back(z);

                    vertices.push_back(x);
                    vertices.push_back(y+1);
                    vertices.push_back(z);
                }

                if(y == 0 || !world->blocks[x][z][y-1]) {
                    vertices.push_back(x);
                    vertices.push_back(y);
                    vertices.push_back(z);

                    vertices.push_back(x+1);
                    vertices.push_back(y);
                    vertices.push_back(z);

                    vertices.push_back(x+1);
                    vertices.push_back(y);
                    vertices.push_back(z+1);

                    vertices.push_back(x);
                    vertices.push_back(y);
                    vertices.push_back(z+1);
                }
                if(y == world->height -1 || !world->blocks[x][z][y+1]) {
                    vertices.push_back(x);
                    vertices.push_back(y+1);
                    vertices.push_back(z);

                    vertices.push_back(x+1);
                    vertices.push_back(y+1);
                    vertices.push_back(z);

                    vertices.push_back(x+1);
                    vertices.push_back(y+1);
                    vertices.push_back(z+1);

                    vertices.push_back(x);
                    vertices.push_back(y+1);
                    vertices.push_back(z+1);
                }
            }
        }
    }

    glVertexPointer(3, GL_INT, 0, &vertices[0]);
    glDrawArrays(GL_QUADS, 0, vertices.size() /3);
    std::cout << "Vertices: " << vertices.size() << std::endl;
}

这是我的结果:
drawWorld 的第一次调用:http://i.imgur.com/yMchEG9.png drawWorld
第二次调用:http: //i.imgur.com/d9swgao.png

显然,在第一次调用之前,initializeOpenGL 只被调用了 1 次。在提供的代码之外没有 OpenGL 语句。

4

1 回答 1

0

更新:

显然,正如通过我的回答的评论所讨论的那样,我的方法是错误的。正确的是用户derhass在评论中指出的。引自derhass:

不,GL 是一个状态机。不使用状态时如何设置它们并不重要。对于顶点数组状态,重要的是绘制调用。您必须确保在绘制调用时启用了所有需要的数组,并且禁用了所有不需要的数组。实际上,避免不必要的状态更改是一种很好的做法。

我导致此更新的错误假设:

void Renderer::initializeOpenGL() {
    glEnable(GL_DEPTH_TEST);
    glEnableClientState(GL_VERTEX_ARRAY);
    glOrtho(0, 1024, 0, 576, 0, 1024);
}

glEnableClientState 并不是这样工作的。您需要在绘制调用之前启用客户端状态并在之后禁用它。

所以我看到你glDisableClientState(GL_VERTEX_ARRAY);在你的抽奖电话后失踪了。还要glEnableClientState(GL_VERTEX_ARRAY);从您的 initializeOpenGL() 函数中删除并将其放在您的 drawWorld 函数中(编辑:在您的顶点指针调用之前)。

这可能会导致您的问题。

于 2013-07-26T22:17:56.113 回答