opengl中的背景颜色 [英] background colour in opengl

查看:711
本文介绍了opengl中的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在按下按钮后改变窗口的背景颜色,但是我的程序不工作,有人可以告诉我为什么,提前感谢

  int main(int argc,char * argv [])
{
glutInit(& argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(800,600);
glutInitWindowPosition(300,50);
glutCreateWindow(GLRect);

glClearColor(1.0f,0.0f,0.0f,1.0f); < ---

glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutMainLoop();

系统(暂停);
glClearColor(0.0f,1.0f,0.0f,1.0f); < ---

return 0;


解决方案

glClearColor 不会自行清除 - 它只是设置实际清除时的颜色。要自行清算,你需要用(至少) COLOR_BUFFER_BIT 来调用 glClear



编辑:自从我使用glut之后已经有一段时间了,所以这些细节可能是错误的,但是如果内存提供服务,为了响应键盘上的按键来改变屏幕颜色,你可以做这样的事情:

$ p $ void keyboard(unsigned char key,int x,int y)
{
//当用户按下某个键时,我们将在红色和蓝色之间切换:
GLfloat颜色[] [3] = {{0.0f,0.0f,1.0f},{1.0f,0.0 f,0.0f}};
static int返回;

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

void draw(){
glClear(GL_COLOR_BUFFER_BIT);
//其他绘图在这里...
}

int main(){

// glutInit,glutInitDisplayMode等

glutDisplayFunc(draw);
glutKeyboardFunc(键盘);
glutMainLoop();
}

基本上,您将所有绘图函数传递给 glutDisplayFunc 。几乎其他任何东西都只是改变状态,然后调用 PostRedisplayFunc(); 来告诉过剩的窗口需要重绘。警告:正如我所说,因为我使用了过量的产品已经有一段时间了,我还没有测试过这个代码。根据我的记忆,它显示了一个过剩的程序的一般结构,但不要指望它完全按照原样工作。


I want to change background color of the window after pressing the button, but my program doesn't work, can somebody tell me why, thanks in advance

int main(int argc, char* argv[])
    {
        glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
        glutInitWindowSize(800, 600);
        glutInitWindowPosition(300,50);
    glutCreateWindow("GLRect");

glClearColor(1.0f, 0.0f, 0.0f, 1.0f);   <---

    glutDisplayFunc(RenderScene);
        glutReshapeFunc(ChangeSize);
    glutMainLoop();

    system("pause");
glClearColor(0.0f, 1.0f, 0.0f, 1.0f);   <---

        return 0;
    }

解决方案

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.

这篇关于opengl中的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆