OpenGL ES2.0 offscreen context for FBO rendering

前端 未结 1 1377
醉话见心
醉话见心 2021-02-06 06:46

I would like to do offscreen rendering (in console environment without any WS) with FBOs. I know that it is necessary to create an OpenGL context, and at least a dummy window fo

相关标签:
1条回答
  • 2021-02-06 07:04

    The solution was the following (without error handling):

        #ifdef CONTEXT_ES20
        EGLint ai32ContextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, 
                                                               EGL_NONE };
        #endif
    
    // Step 1 - Get the default display.
        eglDisplay = eglGetDisplay((EGLNativeDisplayType)0);
    
    // Step 2 - Initialize EGL.
        eglInitialize(eglDisplay, 0, 0);
    
        #ifdef CONTEXT_ES20
    // Step 3 - Make OpenGL ES the current API.
        eglBindAPI(EGL_OPENGL_ES_API);
    
    // Step 4 - Specify the required configuration attributes.
        EGLint pi32ConfigAttribs[5];
        pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
        pi32ConfigAttribs[1] = EGL_WINDOW_BIT;
        pi32ConfigAttribs[2] = EGL_RENDERABLE_TYPE;
        pi32ConfigAttribs[3] = EGL_OPENGL_ES2_BIT;
        pi32ConfigAttribs[4] = EGL_NONE;
        #else
        EGLint pi32ConfigAttribs[3];
        pi32ConfigAttribs[0] = EGL_SURFACE_TYPE;
        pi32ConfigAttribs[1] = EGL_WINDOW_BIT;
        pi32ConfigAttribs[2] = EGL_NONE;
        #endif
    
    // Step 5 - Find a config that matches all requirements.
        int iConfigs;
        eglChooseConfig(eglDisplay, pi32ConfigAttribs, &eglConfig, 1, 
                                                              &iConfigs);
    
        if (iConfigs != 1) {
            printf("Error: eglChooseConfig(): config not found.\n"); 
            exit(-1);
        }
    
    // Step 6 - Create a surface to draw to.
        EGLSurface eglSurface;
        eglSurface = eglCreateWindowSurface(eglDisplay, eglConfig, 
                                            (EGLNativeWindowType)NULL, NULL);
    
    // Step 7 - Create a context.
        #ifdef CONTEXT_ES20
        eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, 
                                                         ai32ContextAttribs);
        #else
        eglContext = eglCreateContext(eglDisplay, eglConfig, NULL, NULL);
        #endif
    
    // Step 8 - Bind the context to the current thread
        eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext); 
    
    0 讨论(0)
提交回复
热议问题