简体   繁体   English

可靠地创建 OpenGL 上下文?

[英]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.在开发计算机上成功创建了 OpenGL 上下文,但在尝试分发应用程序时,屏幕仅显示黑色。 What kind of issues need to be considered when distributing an OpenGL application?分发OpenGL应用程序需要考虑哪些问题?

Details:细节:

I am using SDL2 to create a OpenGL 3.1 context.我正在使用 SDL2 创建 OpenGL 3.1 上下文。 The context has to be at least 3.1 to work.上下文必须至少为 3.1 才能工作。

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.但是,我对摘要中提出的关于分发 OpenGL 应用程序时需要考虑的一般问题更感兴趣。

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.是的,标准说 GL_DEPTH 最初是禁用的。 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.永远不要接受无声的 OpenGL 错误。 "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.浮点精度在 OpenGL 标准中并不是那么严格(在 OpenGL 中更严格),并且对于具有未定义行为的操作(例如除以零或任何基于 NaN 的操作),您永远都不想依赖它。
  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.每个人都先编码,然后永远调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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