OpenGL深度测试有效但不如预期

时间:2016-03-25 02:06:58

标签: c++ qt opengl

使用Windows7,VS2013,Qt 5.6,OpenGL 4.4

有问题的代码如下

void TestClass::paintGL()
{
/*
* Clear the screen
*/
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

glClearColor(0, 0.25, 1, 1);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(0.0);
glDepthFunc(GL_LEQUAL);

GLenum error = glGetError();

if (error != GL_NO_ERROR)
{
    int i = 0;
    i++;
}

/*
*/

//*************************************************

//double xx = mRotation.x() * mRotation.x();
//double xy = mRotation.x() * mRotation.y();
//double xz = mRotation.x() * mRotation.z();
//double xw = mRotation.x() * mRotation.scalar();

//double yy = mRotation.y() * mRotation.y();
//double yz = mRotation.y() * mRotation.z();
//double yw = mRotation.y() * mRotation.scalar();

//double zz = mRotation.z() * mRotation.z();
//double zw = mRotation.z() * mRotation.scalar();

//double m00 = 1 - 2 * (yy + zz);
//double m01 = 2 * (xy - zw);
//double m02 = 2 * (xz + yw);
//double m03 = 0;

//double m10 = 2 * (xy + zw);
//double m11 = 1 - 2 * (xx + zz);
//double m12 = 2 * (yz - xw);
//double m13 = 0;

//double m20 = 2 * (xz - yw);
//double m21 = 2 * (yz + xw);
//double m22 = 1 - 2 * (xx + yy);
//double m23 = 0;

//double m30 = 0;
//double m31 = 0;
//double m32 = -30;
//double m33 = 1;

//double blarg[] = {m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33};
//glLoadMatrixd(&blarg[0]);

//*************************************************

QMatrix4x4 yaw;
yaw.rotate(mYPR.x(), QVector3D(0, -1, 0));
QMatrix4x4 pitch;
pitch.rotate(mYPR.y(), QVector3D(1, 0, 0));
QMatrix4x4 roll;
roll.rotate(mYPR.z(), QVector3D(0, 0, -1));
QMatrix4x4 translate;
translate.translate(0, 0, -30);
//QMatrix4x4 vnToOpenGL(0,  0, -1, 0,
//                    1,  0,  0, 0,
//                    0, -1,  0, 0,
//                    0,  0,  0, 1);

QMatrix4x4 vnToOpenGL(1,  0,  0, 0,
                      0,  1,  0, 0,
                      0,  0,  1, 0,
                      0,  0,  0, 1);

QMatrix4x4 openGLMatrix = vnToOpenGL * translate * yaw * pitch * roll * vnToOpenGL.transposed();
glLoadMatrixf(reinterpret_cast<const float*>(openGLMatrix.constData()));

int depth;
glGetIntegerv(GL_DEPTH_BITS, &depth);

QString ytext(QString("yaw   : ") + QString::number(mYPR.x()));
QString ptext(QString("pitch : ") + QString::number(mYPR.y()));
QString rtext(QString("roll  : ") + QString::number(mYPR.z()));
QString dtext(QString("depth : ") + QString::number(depth));

QPainter painter(this);
painter.drawText(10, 10, ytext);
painter.drawText(10, 25, ptext);
painter.drawText(10, 40, rtext);
painter.drawText(10, 55, dtext);
painter.end();

//*************************************************


//double scalar = std::acos(mRotation.scalar()) * 2;
//double X = std::asin(mRotation.x()) * 2;
//double Y = std::asin(mRotation.y()) * 2;
//double Z = std::asin(mRotation.z()) * 2;

//glRotated(scalar * radToDeg, X * radToDeg, Y * radToDeg, Z * radToDeg);
glScaled(mScale, mScale, mScale);

QVector3D orthogonalPoint;
QVector3D orthogonalVector;
QVector3D currentNormal;

for (size_t index = 0; index < mSphere->getTriangles().size(); index++)
{
    // BEGIN CODE IN QUESTION
    glEnable(GL_DEPTH_TEST);
    //glDepthMask(GL_TRUE);
    //glClearDepth(0.0);
    //glDepthFunc(GL_LEQUAL);
    // END CODE IN QUESTION

    currentNormal = mSphere->getTriangles()[index].getNormal();
    orthogonalVector = QVector3D::crossProduct(QVector3D(currentNormal.x(), currentNormal.y(), currentNormal.z()),
                                       QVector3D(currentNormal.x(), currentNormal.y(), 0));
    orthogonalVector.normalize();

    glLineWidth(1.0);
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_LINES);
    glVertex3f(0, 0, 0);
    glVertex3f(currentNormal.x(), currentNormal.y(), currentNormal.z());
    glEnd();

    glLineWidth(3.0);
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINE_STRIP);
    glVertex3f(0, 0, 0);
    glVertex3f(0, 0, -1);
    glEnd();

    glLineWidth(3.0);
    glColor3f(0.0, 1.0, 0.0);
    glBegin(GL_LINE_STRIP);
    glVertex3f(0, 0, 0);
    glVertex3f(1, 0, 0);
    glEnd();

    glLineWidth(3.0);
    glColor3f(0.0, 1.0, 1.0);
    glBegin(GL_LINE_STRIP);
    glVertex3f(0, 0, 0);
    glVertex3f(0, 1, 0);
    glEnd();

    glColor3f(0.0, 0.0, 0.0);
    glBegin(GL_TRIANGLE_FAN);
    glVertex3d(currentNormal.x(), currentNormal.y(), currentNormal.z());
    for (size_t fanIndex = 0; fanIndex < 9; ++fanIndex)
    {
        double degrees = static_cast<double>(fanIndex) * (360.0 / mTargetTesselations);
        QMatrix4x4 matrix;
        matrix.rotate(degrees, currentNormal.x(), currentNormal.y(), currentNormal.z());
        orthogonalPoint = (orthogonalVector * matrix) / 20.0;
        glVertex3d(currentNormal.x() + orthogonalPoint.x(),
                   currentNormal.y() + orthogonalPoint.y(),
                   currentNormal.z() + orthogonalPoint.z());
    }
    glEnd();

    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_TRIANGLE_FAN);
    glVertex3d(currentNormal.x(), currentNormal.y(), currentNormal.z());
    for (size_t fanIndex = 0; fanIndex < 9; ++fanIndex)
    {
        double degrees = 360.0 - (static_cast<double>(fanIndex) * (360.0 / mTargetTesselations));
        QMatrix4x4 matrix;
        matrix.rotate(degrees, currentNormal.x(), currentNormal.y(), currentNormal.z());
        orthogonalPoint = (orthogonalVector * matrix) / 20.0;
        glVertex3d(currentNormal.x() + orthogonalPoint.x(),
                   currentNormal.y() + orthogonalPoint.y(),
                   currentNormal.z() + orthogonalPoint.z());
    }
    glEnd();
}

/*
* Don't forget about the model-view matrix
*/
glPopMatrix();
}

我基本上整晚都在寻找深度测试不起作用的原因。远处的事物被吸引到前景中的东西上。最后,在写了一篇关于stackoverflow的问题的中途后,我放置了代码

glEnable(GL_DEPTH_TEST);
//glDepthMask(GL_TRUE);
//glClearDepth(0.0);
//glDepthFunc(GL_LEQUAL);

在paintGL函数的for循环开始时。低,看,深度是正确的。所以,既然我已经开始工作了,我真的很想知道为什么它有效。为什么这个工作正常,因为我在循环中启用了'GL_DEPTH_TEST'?如果我不手动更改它不应该状态保持不变?我想知道,因为知道是成功的一半。

0 个答案:

没有答案