openGl / glut:透过块看,

时间:2014-10-23 11:05:06

标签: opengl glut

我正在编写一个程序,试图显示某种由独立块组成的球体。不幸的是,从某些角度来看,我可以看到这些块,看到它们背后的块。 好角度:http://i.imgur.com/6o120KF.png 坏角度:http://i.imgur.com/DbB9iVO.png

我正在检查一些帖子,但没有任何帮助。我关心深度缓冲,zNear但现在我没有想法。可能有人停下来?

我的代码

int main(int argc, char** argv) {

//init world
world.init();
//configure GLUT
glutInit(&argc, argv);              // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE);   // Enable double buffered mode
glutInitWindowSize(WIDTH, HEIGHT);   // Set the window's initial width & height
glutInitWindowPosition(50, 50);         // Position the window's initial top-left corner
glutCreateWindow(title);            // Create window with the given title
glutDisplayFunc(display);               // Register callback handler for window re-paint event
glutKeyboardFunc(keyboard);         // call keyboard() when key is hit
glutIdleFunc(display);              // if no rendering to be, call display
glutReshapeFunc(reshape);               // Register callback handler for window re-size event
initGL();                               // Our own OpenGL initialization
glutMainLoop();                         // Enter the infinite event-processing loop
return 0;
}

void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
glClearDepth(1.0f);                   // Set background depth to farthest
glEnable(GL_DEPTH_TEST);   // Enable depth testing for z-culling
glDisable(GL_BLEND);
glDepthFunc(GL_LEQUAL);    // Set the type of depth-test
glShadeModel(GL_SMOOTH);   // Enable smooth shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Nice perspective corrections
glClear(GL_DEPTH_BUFFER_BIT);
}

void display() {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW);                         // To operate on model-view matrix
glLoadIdentity();                 // Reset the model-view matrix

//formulas for camera changes
glTranslatef(translatex, 2.0f, -7.0f);  // Move right and into the screen
glRotated(rotAnglex, 1, 0, 0);  // rotate by rotAngle about y-axis
glRotated(rotAngley, 0, 1, 0);  // rotate by rotAngle about y-axis
glRotated(rotAnglez, 0, 0, 1);  // rotate by rotAngle about y-axis
glScalef(zoom ,zoom ,zoom);


glBegin(GL_LINES);
// draw line for x axis
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0 - xmove, 0.0 - ymove, 0.0 - zmove);
glVertex3f(100.0 - xmove, 0.0 - ymove, 0.0 -zmove);
// draw line for y axis
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0 - xmove, 0.0 - ymove, 0.0 - zmove);
glVertex3f(0.0 - xmove, 100.0 - ymove, 0.0 -zmove);
// draw line for Z axis
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0 - xmove, 0.0 - ymove, 0.0 - zmove);
glVertex3f(0.0 - xmove, 0.0 - ymove, 100.0 -zmove);
glEnd();
glBegin(GL_QUADS);
//draw axes


if(it_counter++ < iterations){
//print world
 BOOST_FOREACH(std::Cell* now_cell, world.cells){
    BOOST_FOREACH(std::Voxel* now_voxel, now_cell->content){
        addVoxel(*now_voxel);
    }
 }
}

glEnd();  // End of drawing color-cube

glLoadIdentity();                  // Reset the model-view matrix
glTranslatef(-1.5f, 0.0f, -6.0f);  // Move left and into the screen
glScalef(3.0f ,3.0f ,3.0f);


glutSwapBuffers();  // Swap the front and back frame buffers (double buffering)
}

添加块(代码中的体素)的顺序是否对渲染很重要?

1 个答案:

答案 0 :(得分:2)

它接缝就好像你创建了一个没有深度缓冲区的窗口。只有在窗口有这样的缓冲区时才能启用深度测试。

要解决您的问题,您必须更改

glutInitDisplayMode(GLUT_DOUBLE);

glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE);