c++ - glClearColor 不起作用

标签 c++ opengl

我开始了 sdl 和 opengl 类(class)。当我尝试更改背景时遇到一个问题,此功能没有给出任何结果。我使用 MSVS 和 Win8.1。 我正在尝试使用各种带参数的选项。我总是有黑色背景。有什么想法我可能做错了吗?

#include <SDL.h>
#include <SDL_opengles2.h>
#include <GLES3/gl3.h>
#include <cstdio>
#include <cstdlib>

const unsigned int DISP_WIDTH = 800;
const unsigned int DISP_HEIGHT = 600;

int SDL_main(int argc, char *args[]) {
// ##### FIXME! #####
SDL_Window *window = NULL;
SDL_GLContext context = NULL;

if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    SDL_Log("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
    return 10;
}

atexit(SDL_Quit);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,0);

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

window = SDL_CreateWindow("Tut", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, DISP_WIDTH, DISP_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);

if (!window) {
    SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error","Couldn't create the main window",NULL);
    return EXIT_FAILURE;
}

context = SDL_GL_CreateContext(window);

if (!context) {
    SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", "Couldn't create an OpenGl context", NULL);
    return EXIT_FAILURE;
}

glClearColor(0.5F, 0.0F, 1.0F, 0.5F);   // this function changes the color
glClear(GL_COLOR_BUFFER_BIT);

SDL_GL_SwapWindow(window);
bool quit = false;

while (!quit) {
    SDL_Event event;
    if (SDL_WaitEvent(&event) != 0) {
        if (event.type == SDL_QUIT) {
            quit = true;
        }
    }

}

return EXIT_SUCCESS;

最佳答案

您不会在循环中的任何时候交换缓冲区。您必须在执行所有相关调用后交换缓冲区,否则显示不会更新。

关于c++ - glClearColor 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44796604/

相关文章:

c++ - OpenGL:多个重叠视口(viewport)闪烁

c++ - 是否有任何计划或现有项目将 OpenGL API 移植到 C++?

c++ - 与 OpenGL 的减法混合?

c++ - wifstream 中的 seekg 和 imbue 工作错误

c++ - boost::bind 如何调用私有(private)方法?

c++ - 在C中使用括号进行计算

c++ - 使用c++在excel中抑制删除工作表警告

c++ - opengl 土壤中的多线程支持

c++ - 无法在 Visual C++ 2010 中链接 glew32

c++ 字符数组是否超出范围?