0

它应该是矩形的,它将占据整个窗口的宽度。我期待这一点,因为截锥体宽度为 -5 到 5,矩形大小为 10x10,矩形位于同一 z 轴位置,如截锥体平面附近。结果非常小,看起来不像一个矩形,我不知道为什么?

void GLWidget::initializeGL() {

    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
}

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

    glViewport(0,0, w, h);
    glMatrixMode(GL_PROJECTION);
    double ratio = (double)w/(double)h;
    double size = ((10.0/ratio)/2.0);
    glFrustum(-5.0, 5.0, -size, size, 10.0, 50.0);

}

void GLWidget::paintGL() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -10.0f);

    glColor3f(0.0f, 0.0f, 0.0f);
    glBegin(GL_QUADS);
    glVertex3f(5.0f, -5.0f, 0.0f);
    glVertex3f(5.0f, 5.0f, 0.0f);
    glVertex3f(-5.0f, 5.0f, 0.0f);
    glVertex3f(-5.0f, -5.0f, 0.0f);
    glEnd();

    glFlush();


}
4

1 回答 1

1

它应该是矩形的,它将占据整个窗口的宽度。

好吧,然后将您的投影切换到更适合的投影,即正射投影,以完成绘制矩形时的任务。此外,您必须在应用平截头体或正交之前将投影矩阵重置为恒等。

提示:你的所有代码都resizeGL应该进入paintGL方法。我看到您正在使用 Qt,因此您可以使用widthheightgetter 函数方便地获取小部件的宽度和高度。也glClearColor进入painGL

void GLWidget::initializeGL() {
}

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


}

void GLWidget::paintGL()
{
    double const ratio = (double)width()/(double)height();
    double const size = ((10.0/ratio)/2.0);

    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glViewport(0,0, width(), height());
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-5.0, 5.0, -size, size, 10.0, 50.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -10.0f);

    draw_perspective_stuff();

    glViewport(0,0, width(), height());
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // using identity projection, this fills the viewport
    glColor3f(0.0f, 0.0f, 0.0f);
    glBegin(GL_QUADS);
    glVertex3f(-1.f, -1.f, 0.0f);
    glVertex3f( 1.f, -1.f, 0.0f);
    glVertex3f( 1.f,  1.f, 0.0f);
    glVertex3f(-1.f,  1.f, 0.0f);
    glEnd();

    glFlush();
}
于 2013-04-18T09:51:04.880 回答