OpenGL“黑屏”挫败感

时间:2013-11-09 14:03:25

标签: c++ c qt opengl

我仍然想弄清楚,为什么我只看到典型的“黑屏”。我只渲染了一个矩形,但什么都没发生。

#include "expwidget.h"
#include <iostream>

ExpWidget::ExpWidget(QObject *parent) :
    QGLWidget(QGLFormat(QGL::DoubleBuffer), (QWidget *) parent)
{

    QGLFormat fmt = this->format();
    fmt.setDepth(true);

    this->setFormat(fmt);
}



void ExpWidget::initializeGL() {

    QGLWidget::initializeGL(); 

    std::cout << "inicializace...\n";

    glClearColor(0.0f,0.0f,0.0f,0.0f);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    glDisable(GL_LIGHTING);

}


void ExpWidget::paintGL() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();

    glColor3f(1, 0, .5);
    glBegin(GL_QUADS);
        glVertex3f(-1.0f,-1.0f,-5.0f);
        glVertex3f(1.0f,-1.0f,-5.0f);
        glVertex3f(1.0f,1.0f,-5.0f);
        glVertex3f(-1.0f,1.0f,-5.0f);
    glEnd();

    glFlush();


}

void ExpWidget::resizeGL(int w, int h) {

    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glFrustum(-2.0, 2.0, -2.0, 2.0, 0.0, -30.0);

    glMatrixMode(GL_MODELVIEW);


}

2 个答案:

答案 0 :(得分:2)

我过去遇到过类似的问题,但我并不认为自己是专家。

glFrustum的来电形式为glFrustum(left, right, bottom, top, near, far)

nearfar必须都是正数且非零。 (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml

因此,在您的情况下,我建议您将通话更改为:

glFrustum(-2.0, 2.0, -2.0, 2.0, 1.0, 30.0);

此外,在此视图中,您的坐标应具有负Z值。

答案 1 :(得分:1)

http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml

nearVal, farVal

Specify the distances to the near and far depth clipping planes. Both distances must be positive.