【问题标题】:Getting started with OpenGL in C++C++ 中的 OpenGL 入门
【发布时间】:2011-12-11 11:12:49
【问题描述】:

我已经尝试使用this tutorial 开始使用 OpenGL 进行编程,并且目前拥有以下代码:

#include <gl/glut.h> // Include the GLUT header file

void display (void)
{
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv); // Initialize GLUT
    // Set up a basic display buffer (only single buffered for now)
    glutInitDisplayMode (GLUT_SINGLE);
    glutInitWindowSize (500, 500); // Set the width and height of the window
    glutInitWindowPosition (100, 100); // Set the position of the window
    // Set the title for the window
    glutCreateWindow("Your first OpenGL Window!");

    glutDisplayFunc(display);
    glutMainLoop();
    glClearColor(1.f, 0.f, 0.f, 1.f); // Clear the background of our window to red

    return 0;
}

我在 Eclipse 中构建并运行该项目,它编译一切正常,但没有任何反应(没有弹出窗口或任何东西)。谁能告诉我我做错了什么?

【问题讨论】:

  • 添加打印语句以确保您的代码实际运行。

标签: c++ eclipse opengl


【解决方案1】:
glutInitDisplayMode (GLUT_SINGLE);

您还需要定义所需的帧缓冲区格式,即添加一个GLUT_RGBA,至少(并且您可能还需要一个深度缓冲区)。而且只有少数情况下不需要双缓冲。所以:glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

那么你的显示函数就不会被调用,除非你在glutCreateWindow之后添加glutDisplayFunc(display);

glutMainLoop();
glClearColor(1.f, 0.f, 0.f, 1.f);

glutMainLoop 不返回。即使有,glClearColor 在程序中的那个地方也没有效果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-01
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多