opengl中的背景颜色

发布于 2024-09-02 22:31:30 字数 1468 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

dawn曙光 2024-09-09 22:31:30

glClearColor 本身不进行任何清除——它只是设置实际清除时的颜色。要自行进行清除,您需要使用(至少)COLOR_BUFFER_BIT 调用 glClear

编辑:自从我使用 glut 以来已经有一段时间了,所以这个细节可能是错误的,但如果没记错的话,要改变屏幕颜色以响应按下键盘上的按键,你会做这样的事情:

void keyboard (unsigned char key, int x, int y)
{
    // we'll switch between red and blue when the user presses a key:
    GLfloat colors[][3] = { { 0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f } };
    static int back;

    switch (key) {
    case 27: 
        exit(0);
    default:
        back ^= 1;
        glClearColor(colors[back][0], colors[back][1], colors[back][2], 1.0f);
        glutPostRedisplay();
    }
}

void draw() { 
    glClear(GL_COLOR_BUFFER_BIT);
    // other drawing here...
}

int main() { 

    // glutInit, glutInitDisplayMode, etc.

     glutDisplayFunc(draw);
     glutKeyboardFunc(keyboard);
     glutMainLoop();
}

基本上,您可以在传递给 glutDisplayFunc 的任何函数中完成所有绘图。几乎任何其他事情都只是更改状态,然后调用 PostRedisplayFunc(); 来告诉 glut 窗口需要重新绘制。警告:正如我所说,自从我使用 glut 以来已经有一段时间了,而且我还没有测试过这段代码。据我所知,它展示了过剩程序的一般结构,但不要指望它能完全按原样工作。

glClearColor does not do any clearing itself -- it just sets what the color will be when you do actually clear. To do the clearing itself, you need to call glClear with (at least) COLOR_BUFFER_BIT.

Edit: it's been quite a while since I used glut, so the details of this could be wrong, but if memory serves, to change the screen color in response to pressing a key on the keyboard, you'd do something like this:

void keyboard (unsigned char key, int x, int y)
{
    // we'll switch between red and blue when the user presses a key:
    GLfloat colors[][3] = { { 0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f } };
    static int back;

    switch (key) {
    case 27: 
        exit(0);
    default:
        back ^= 1;
        glClearColor(colors[back][0], colors[back][1], colors[back][2], 1.0f);
        glutPostRedisplay();
    }
}

void draw() { 
    glClear(GL_COLOR_BUFFER_BIT);
    // other drawing here...
}

int main() { 

    // glutInit, glutInitDisplayMode, etc.

     glutDisplayFunc(draw);
     glutKeyboardFunc(keyboard);
     glutMainLoop();
}

Basically, you do all your drawing in whatever function you pass to glutDisplayFunc. Almost anything else just changes the state, then calls PostRedisplayFunc(); to tell glut that the window needs to be redrawn. Warning: as I said, it's been a while since I used glut and I haven't tested this code. It shows the general structure of a glut program to the best of my recollection, but don't expect it to work exactly as-is.

红ご颜醉 2024-09-09 22:31:30

我想您在调用 glClearColor 时没有 OpenGL 上下文。

但是……

我以前从未使用过 glut,所以快速浏览一下 docs 表明您实际上在 glutCreateWindow 之后会有一个上下文,所以也许不是这样。

正如我对您的问题的评论所说,我很好奇第二次调用 glClearColor 以及您认为如何实现它。这更有可能是问题的原因。要在按键上执行任何操作,我相信您必须使用 glutKeyboardFunc 注册回调。

I would imagine you don't have an OpenGL context at that point that you call glClearColor.

But...

...I've never used glut before so a quick look at the docs suggests that you will actually have a context after glutCreateWindow so perhaps that's not it.

As my comment on your question says I'm curious about the second call to glClearColor and how you think you will reach it. That's more likely the cause of the problem. To do anything on a key press I believe you would have to register a callback using glutKeyboardFunc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文