glClearColor() doesn't work


#1

I’m trying setting the background color to blue using glClearColor(), but it seems not working …

I tried deleting GL_DEPTH_BUFFER_BIT or writing glClearColor() inside drawGame(), but nothing…

    void MainGame::initSystem() {

    SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    _window = SDL_CreateWindow("Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, _screenWidth, _screenHeight, SDL_WINDOW_OPENGL);

if (_window == nullptr) {
    fatalError("SDL Window could not be created!");
}

SDL_GLContext glContext = SDL_GL_CreateContext(_window);
if (glContext == nullptr) {
    fatalError("SDL_GL cointext could not be created!");
}

GLenum error = glewInit();
if (error != GLEW_OK) {
    fatalError("Could not initialize glew!");
}

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

}

void MainGame::drawGame() {

    glClearDepth(1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    SDL_GL_SwapWindow(_window);
}

It should color the background with blue, but the color is white.


#2

Try calling glClearColor() at the top of drawGame() before the call to glClear(). Perhaps the clear color is being changed someplace else.

Also, before creating your window, consider adding:

SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);

See these two pages in the wiki for more tips: