OpenGLES 1.1 with FrameBuffer / ColorBuffer / DepthBuffer for Android with NDK r7b

不打扰是莪最后的温柔 提交于 2019-12-23 04:32:44

问题


After reading over the NDK docs and all my books on OpenGLES ive hit a wall. I am trying to copy my iOS OpenGLES set up to Android NDK R7 and above, mainly to get the depth buffer i overlooked earlier on when coding.

The problem is i loose the textures on some objects when i enable the color buffer as seen below and the depth buffer isn't working when i send objects into the background.

I am using OGLES 1.1 FFP and NDK R7 or above

Here is my initialization code :-

int32_t ES1Renderer::initRenderer() {

    EGLint lFormat, lNumConfigs, lErrorResult;
    EGLConfig lConfig;

    const EGLint lAttributes[] = {
        EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
        EGL_BLUE_SIZE, 5, EGL_GREEN_SIZE, 6, EGL_RED_SIZE, 5,
        EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
        EGL_NONE
    };

    mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if (mDisplay == EGL_NO_DISPLAY) goto ERROR;
    if (!eglInitialize(mDisplay, NULL, NULL)) goto ERROR;

    if(!eglChooseConfig(mDisplay, lAttributes, &lConfig, 1,
        &lNumConfigs) || (lNumConfigs <= 0)) goto ERROR;

    if (!eglGetConfigAttrib(mDisplay, lConfig,
        EGL_NATIVE_VISUAL_ID, &lFormat)) goto ERROR;
    ANativeWindow_setBuffersGeometry(mApplication->window, 0, 0,
        lFormat);


    mSurface = eglCreateWindowSurface(mDisplay, lConfig,
        mApplication->window, NULL);
    if (mSurface == EGL_NO_SURFACE) goto ERROR;
    mContext = eglCreateContext(mDisplay, lConfig, EGL_NO_CONTEXT,
        NULL);
    if (mContext == EGL_NO_CONTEXT) goto ERROR;


    if (!eglMakeCurrent(mDisplay, mSurface, mSurface, mContext)
     || !eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth)
     || !eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight)
     || (mWidth <= 0) || (mHeight <= 0)) goto ERROR;

    //Get the default FrameBuffer and bind it
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, 0);

    glGenRenderbuffersOES(1, &colorRenderbuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorRenderbuffer);

    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &mWidth);
    glGetRenderbufferParameterivOES(GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &mHeight);

    glGenRenderbuffersOES(1, &depthRenderbuffer);
    glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
    glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, mWidth, mHeight);
    glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);


    {
        const GLfloat           matAmbient[] = {1.0, 1.0, 1.0, 1.0};
        const GLfloat           matDiffuse[] = {1.0, 1.0, 1.0, 1.0};
        const GLfloat           lightShininess = 20.0;

        glEnable(GL_COLOR_MATERIAL);
        glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, matAmbient);
        glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, matDiffuse);
        glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, lightShininess);
    }

    glViewport( 0, 0, mWidth, mHeight );

    glEnable   ( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL     );
    glDepthMask( GL_TRUE );

    glEnable   ( GL_CULL_FACE );
    glShadeModel( GL_SMOOTH );

    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
    glHint( GL_GENERATE_MIPMAP_HINT       , GL_NICEST );
    glHint( GL_LINE_SMOOTH_HINT           , GL_NICEST );
    glHint( GL_POINT_SMOOTH_HINT          , GL_NICEST );
    glHint( GL_FOG_HINT                   , GL_NICEST );

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

    glMatrixMode(GL_PROJECTION);

    glOrthof(-1.0,                  //LEFT
                    1.0,                   //RIGHT
                    -1.0 * mHeight / mWidth,  //BOTTOM
                    1.0 * mHeight / mWidth,   //TOP
                    -2.0,                  //NEAR
                    100.0);                //FAR

    glMatrixMode(GL_MODELVIEW);
}

Here is my rendering code:

int32_t ES1Renderer::render() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /* 
    RENDERING GOES HERE THE REMOVED FOR EXAMPLE
    */

    glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);

    if (eglSwapBuffers(mDisplay, mSurface) != EGL_TRUE) {
        return 0;
    }

    return 1;
}

回答1:


Ok after a pointer from Sylvain Ratabouil (Check out his Book and page if you want to get up to speed on NDK7) in the setup of OGLES on Android NDK 7. I don't need to Gen and Bind the Buffers as they are handled automatically when setting up the EGL Surface. He suggested adding this:-

EGL_DEPTH_SIZE, 16, EGL_SURFACE_TYPE, EGL_WINDOW_BIT,

to

const EGLint lAttributes[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
        EGL_BLUE_SIZE, 5, EGL_GREEN_SIZE, 6, EGL_RED_SIZE, 5,
        EGL_DEPTH_SIZE, 16, EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
        EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE };

That fixed my depth buffer, note check the EGL state make sure it is supported. Here is the complete setup implementation i used:-

int32_t ES1Renderer::initialiseRenderer() {

    EGLint lFormat, lNumConfigs, lErrorResult;
    EGLConfig lConfig;

    const EGLint lAttributes[] = { EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
        EGL_BLUE_SIZE, 5, EGL_GREEN_SIZE, 6, EGL_RED_SIZE, 5,
        EGL_DEPTH_SIZE, 16, EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
        EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE };

    mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
    if (mDisplay == EGL_NO_DISPLAY)
        goto ERROR;
    if (!eglInitialize(mDisplay, NULL, NULL))
        goto ERROR;

    if (!eglChooseConfig(mDisplay, lAttributes, &lConfig, 1, &lNumConfigs)
        || (lNumConfigs <= 0))
    goto ERROR;

    if (!eglGetConfigAttrib(mDisplay, lConfig, EGL_NATIVE_VISUAL_ID, &lFormat))
    goto ERROR;
    ANativeWindow_setBuffersGeometry(mApplication->window, 0, 0, lFormat);

    mSurface = eglCreateWindowSurface(mDisplay, lConfig, mApplication->window,
        NULL);
    if (mSurface == EGL_NO_SURFACE)
    goto ERROR;
    mContext = eglCreateContext(mDisplay, lConfig, EGL_NO_CONTEXT, NULL);
    if (mContext == EGL_NO_CONTEXT)
    goto ERROR;

    if (!eglMakeCurrent(mDisplay, mSurface, mSurface, mContext)
        || !eglQuerySurface(mDisplay, mSurface, EGL_WIDTH, &mWidth)
        || !eglQuerySurface(mDisplay, mSurface, EGL_HEIGHT, &mHeight)
        || (mWidth <= 0) || (mHeight <= 0))
    goto ERROR;

{
    const GLfloat matAmbient[] = { 1.0, 1.0, 1.0, 1.0 };
    const GLfloat matDiffuse[] = { 1.0, 1.0, 1.0, 1.0 };
    const GLfloat lightShininess = 20.0;

    glEnable( GL_COLOR_MATERIAL);
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, matAmbient);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, matDiffuse);
    glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, lightShininess);
}

glLoadIdentity();

#ifdef ORTHOGANAL_PROJECTION

glViewport( 0, 0, mWidth, mHeight );
#endif

    glEnable   ( GL_DEPTH_TEST );
    glDepthFunc( GL_LEQUAL     );
    glDepthMask( GL_TRUE );

    glEnable   ( GL_CULL_FACE );
    //glShadeModel( GL_SMOOTH );

    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
    glHint( GL_GENERATE_MIPMAP_HINT       , GL_NICEST );
    glHint( GL_LINE_SMOOTH_HINT           , GL_NICEST );
    glHint( GL_POINT_SMOOTH_HINT          , GL_NICEST );
    glHint( GL_FOG_HINT                   , GL_NICEST );

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

    glMatrixMode(GL_PROJECTION);

    glEnable(GL_NORMALIZE);
    glEnable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    //glShadeModel(GL_FLAT);

    #ifdef ORTHOGANAL_PROJECTION

glOrthof(-1.0, //LEFT
        1.0, //RIGHT
        -1.0 * mHeight / mWidth, //BOTTOM
        1.0 * mHeight / mWidth, //TOP
        -2.0, //NEAR
        100.0); //FAR


    #else

{
    GLfloat mWidthf = mWidth;
    GLfloat mHeightf = mHeight;

    const GLfloat zNear = 0.1;
    const GLfloat zFar = 1000.0;
    const GLfloat fieldOfView = 60.0;

    GLfloat size = zNear * tanf(DEGREES_TO_RADIANS(fieldOfView) / 2.0);

    glFrustumf(-size, size, -size / (mWidthf / mHeightf),
            size / (mWidthf / mHeightf), zNear, zFar);

    glViewport(0, 0, mWidthf, mHeightf);
}
    #endif

    glMatrixMode(GL_MODELVIEW);
}

Render code remains the same as above.



来源:https://stackoverflow.com/questions/9750565/opengles-1-1-with-framebuffer-colorbuffer-depthbuffer-for-android-with-ndk-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!