开发者

Background colour in opengl [closed]

开发者 https://www.devze.com 2022-12-31 05:24 出处:网络
Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code nece
Closed. This question needs debugging details. It is not currently accepting answers.

Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.

Closed last year.

Improve this question

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

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,开发者_如何学Python 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消