openGl对象在某个zCoord

时间:2016-09-23 19:04:01

标签: c++ opengl

我有立方体,我正试图使用​​glTranslatef()函数移动。一切都很好,除了cubeb开始在z> 1时消失。

这是我的代码

void owidget::initializeGL()
{   
    glDepthRange(0,100);
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glOrtho(-5.0,5.0,-5.0,5.0,-500.0,500.0);
}

void owidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glPushMatrix();
        glTranslatef(xPos/100,yPos/100,zPos/100);
        glRotatef(xRot,1,0,0);
        glRotatef(yRot,0,1,0);
        glRotatef(zRot,0,0,1);
        cube(0.3);
    glPopMatrix(); //Fuction for dislpaying cube
}

void owidget::resizeGL(int width, int height)
{
    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
}

Here is a picture

1 个答案:

答案 0 :(得分:2)

你的设置有点奇怪。您在初始化GL中正确地在zFar中设置了gluPerspective,但它在paintGL中被glLoadIdentity覆盖,后者将zFar重置为默认值,{{3}为1 }}

如果您将glMatrixMode(GL_PROJECTION);更改为glMatrixMode(GL_MODELVIEW);,这应该可以。

我还建议将glMatrixMode默认为GL_MODELVIEW。我的意思是,无论何时编辑投影矩阵,都要调用glMatrixMode(GL_MODELVIEW);

void owidget::initializeGL()
{   
    glDepthRange(0,100);
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glMatrixMode(GL_PROJECTION);
    gluPerspective(0,650/600,20.0,100.0);
    glMatrixMode(GL_MODELVIEW);
}

void owidget::paintGL()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glPushMatrix();
        glTranslatef(xPos/100,yPos/100,zPos/100);
        glRotatef(xRot,1,0,0);
        glRotatef(yRot,0,1,0);
        glRotatef(zRot,0,0,1);
        cube(0.3);
    glPopMatrix(); //Fuction for dislpaying cube
}

void owidget::resizeGL(int width, int height)
{
    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
}