简体   繁体   中英

Reliably creating an OpenGL context?

Summary:

An OpenGL context is created successfully on the development computer, but when trying to distribute the application, the screen only shows black. What kind of issues need to be considered when distributing an OpenGL application?

Details:

I am using SDL2 to create a OpenGL 3.1 context. The context has to be at least 3.1 to work.

I have not thoroughly tested the issue, so I do not have information such as the graphics cards in use. However, I am more interested in the general question asked in the summary about what needs to be considered when distributing an OpenGL application.

Here is the context creation code.

    // CREATE SDL
    U32 flags;

    flags |= SDL_INIT_VIDEO;
    flags |= SDL_INIT_EVENTS;

    if(!SDL_WasInit(0)) // Make sure SDL is initialized.
        SDL_Init(0);

    CHECK(!SDL_InitSubSystem(flags));

    // SET OPENGL ATTRIBUTES
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, config.glVersionMajor);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, config.glVersionMinor);
    if(config.glCoreProfile)
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
    else
        SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
    //SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, config.glDepthBuffer);
    SDL_GL_SetSwapInterval(0);

    // CREATE WINDOW
    flags = SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN;
    if(config.fullscreen)
        flags = flags | SDL_WINDOW_FULLSCREEN_DESKTOP;
    else if(config.maximized)
        flags = flags | SDL_WINDOW_MAXIMIZED;
    if(config.resizable)
        flags = flags | SDL_WINDOW_RESIZABLE;
    mainWindow = SDL_CreateWindow(config.programName, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                                    config.windowWidth, config.windowHeight, flags);
    SDL_GetWindowSize(mainWindow, (int*)&windowWidth, (int*)&windowHeight);
    CHECK(mainWindow != NULL);

    // CREATE OPENGL CONTEXT
    mainContext = SDL_GL_CreateContext(mainWindow);
    CHECK(mainContext != NULL);

    // INIT GLEW
#ifdef _WIN32
    CHECK(GLEW_OK == glewInit());
#endif

    glEnable(GL_DEPTH_TEST);
    glViewport(0,0,windowWidth,windowHeight);

    glClearColor(0,0,0,1);
    //glEnable(GL_PRIMITIVE_RESTART);
    glEnable(GL_CULL_FACE);
    //glPrimitiveRestartIndex(0xFFFFFFFF);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    TTF_Init();
  1. Make sure you know what your application is dependent on, and demand it from the platform. Saying "Core profile" means little in my experience. Better query each and every extension your application need and shut the application down (gracefully and kindly in the eyes of the user) if something is missing. And extensions are not everything. Check maximum sizes of all buffers, too. Of that I have real life experience.
  2. Never rely on standard compliance. Yes, the standard says that GL_DEPTH is intially disabled. And no, thou shalt never rely on that the driver is compliant to that rule. And yes, that was a real life scenario.
  3. Run proper tests on a variaty of hardwares. Vendors implement drivers differently. Some may think negative vertex index is perfectly fine. Some may not. Real life experience there as well...
  4. Never accept a silent OpenGL error. "Something went wrong and it is probably nothing to worry about. For this hardware. For this driver. For this version. For this OS. Maybe."
  5. Do the math. Floating point precision is not that strict by the OpenGL standard (more strict in OpenGL) and behavior for operations with undefined behavior, such as division by zero or any operations based on NaN, is never something you want to rely on.
  6. Read the standard. Yes it is a pain, but trust me it will pay out. You won´t feel it of course since you will never experience problems you will never have.

As a side note, nobody really follow this practice. Everybody code first and then debug forever.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM