我正在使用 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 语句。