【问题标题】:My display() function only displays when it enters it the first time. Then it shows a blank window我的 display() 函数仅在第一次进入时显示。然后它显示一个空白窗口
【发布时间】:2012-10-17 08:11:45
【问题描述】:


void init(void)
{ 
    glEnable(GL_DEPTH_TEST);
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}

void display(void) { glClearColor(1.0, 1.0, 1.0, 1.0);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glScalef(1.0,1.0,1.0);
glColor3f(0.0,0.0,0.0);
    glBegin(GL_POLYGON);
    glVertex2f(-0.5, -0.5);
    glVertex2f(-0.5, 0.5);
    glVertex2f(0.5, 0.5);
    glVertex2f(0.5, -0.5);
glEnd();
    glutSwapBuffers();

}

void reshape(int w, int h) { int height = h; int width = w; glViewport(0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); }

int main(int argc, char* argv[]) { Complex c(0,0); glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize(512, 512); glutInitWindowPosition(100, 100); winID = glutCreateWindow("Fractal"); init(); glutDisplayFunc(display); glutIdleFunc(display); glutReshapeFunc(reshape); // Compute the update rate here... glutMainLoop(); return 0; }

如果我将代码放在 display() 中,我会得到一个正方形,除了 if 条件中的 glutSwapBuffers() 检查代码是否第一次进入显示。如果我删除 if,我会得到一个白色窗口

【问题讨论】:

  • 问题措辞不当。你有什么问题?
  • // Compute the update rate here… 不,它没有。在运行程序的过程中,该特定代码行恰好到达一次。如果那里有一个非终止循环,您将无法到达 GLUT 主循环,从而阻止事件被处理。此外,如果您想要持续更新,您应该将 glutPostRedisplay 注册为空闲函数,而不是 display

标签: opengl


【解决方案1】:

在您的display 函数中添加一个额外的glTranslatef(0.0f, 0.0f, -5.0f),将所有5 个单位的东西推回场景中,否则您将看不到您在原点绘制的内容。

顺便说一句,glScalef(1.0,1.0,1.0) 行不做任何事情,删除它以避免不必要的计算。

编辑: OpenGL 中没有“眼睛”。更具体地说,它始终固定在 (0,0,0) 向下看负 z 轴。您必须使用与眼睛变换相反的方式移动整个场景。您可以手动移动场景以便您的眼睛可以看到它,或者使用 gluLookAt,您可以在其中指定“眼睛”,但在背景中它只是变换场景,因此固定的“眼睛”可以看看吧。

延伸阅读:http://www.opengl.org/archives/resources/faq/technical/viewing.htm

【讨论】:

  • 非常感谢!但是为什么它第一次工作正常。默认的眼睛位置是否不会引起任何问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 2015-01-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多