OpenGLES 1.1帧缓冲/ ColorBuffer /深度缓存为Android NDK与R7B [英] OpenGLES 1.1 with FrameBuffer / ColorBuffer / DepthBuffer for Android with NDK r7b

查看:554
本文介绍了OpenGLES 1.1帧缓冲/ ColorBuffer /深度缓存为Android NDK与R7B的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读过NDK文档和我所有的书上OpenGLES后IVE碰了壁。我想复制我的iOS设置到Android NDK R7 OpenGLES以上,主要是为了获取深度缓冲区我前面忽略编码时。

问题是我失去了一些物体的纹理时启用颜色缓冲区,如下图所示,当我发送对象为背景的深度缓冲不工作。

我使用OGLES 1.1 FFP和NDK R7或以上。

下面是我的初始code: -

  int32_t ES1Renderer :: initRenderer(){    EGLint lFormat,lNumConfigs,lErrorResult;
    EGLConfig lConfig;    常量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);
    如果(填充mDisplay == EGL_NO_DISPLAY)转到错误;
    如果转到错误(eglInitialize(填充mDisplay,NULL,NULL)!);    如果(eglChooseConfig(填充mDisplay,lAttributes,&安培;!lConfig,1,
        &安培; lNumConfigs)|| (lNumConfigs< = 0))转到错误;    如果(!eglGetConfigAttrib(填充mDisplay,lConfig,
        EGL_NATIVE_VISUAL_ID,&安培; lFormat))转到错误;
    ANativeWindow_setBuffersGeometry(mApplication->窗口,0,0,
        lFormat);
    mSurface = eglCreateWindowSurface(填充mDisplay,lConfig,
        mApplication->窗口,NULL);
    如果(mSurface == EGL_NO_SURFACE)转到错误;
    mContext = eglCreateContext(填充mDisplay,lConfig,EGL_NO_CONTEXT,
        空值);
    如果(mContext == EGL_NO_CONTEXT)转到错误;
    如果(!eglMakeCurrent(填充mDisplay,mSurface,mSurface,mContext)
     || !eglQuerySurface(填充mDisplay,mSurface,EGL_WIDTH,&安培; mWidth)
     || !eglQuerySurface(填充mDisplay,mSurface,EGL_HEIGHT,&安培; mHeight)
     || (mWidth&下; = 0)|| (mHeight< = 0))转到错误;    //获取默认的帧缓冲,并将其绑定
    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);
    {
        常量GLfloat matAmbient [] = {1.0,1.0,1.0,1.0};
        常量GLfloat matDiffuse [] = {1.0,1.0,1.0,1.0};
        常量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.0,0.0,0.0,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); //远    glMatrixMode(GL_MODELVIEW);
}

下面是我的渲染code:

  int32_t ES1Renderer ::渲染(){    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    / *
    渲染GOES HERE的移除。例如
    * /    glBindRenderbufferOES(GL_RENDERBUFFER_OES,colorRenderbuffer);    如果(eglSwapBuffers(填充mDisplay,mSurface)!= EGL_TRUE){
        返回0;
    }    返回1;
}


解决方案

从西尔Ratabouil指针(看看他的书和页面,如果你想加快速度上NDK7)在OGLES Android上NDK设置后单击确定7.我不需要根和绑定缓冲与设置EGL表面时,它们会自动处理。他建议增加这样的: -

  EGL_DEPTH_SIZE,16,EGL_SURFACE_TYPE,EGL_WINDOW_BIT,

 常量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};

这是我的固定深度缓存,注意检查EGL状态,确保它是支持的。下面是我用完整的设置实现: -

  int32_t ES1Renderer :: initialiseRenderer(){    EGLint lFormat,lNumConfigs,lErrorResult;
    EGLConfig lConfig;    常量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);
    如果(填充mDisplay == EGL_NO_DISPLAY)
        转到错误;
    如果(!eglInitialize(填充mDisplay,NULL,NULL))
        转到错误;    如果(eglChooseConfig(填充mDisplay,lAttributes,&安培;!lConfig,1,&安培; lNumConfigs)
        || (lNumConfigs&下; = 0))
    转到错误;    如果(eglGetConfigAttrib(填充mDisplay,lConfig,EGL_NATIVE_VISUAL_ID,&安培;!lFormat))
    转到错误;
    ANativeWindow_setBuffersGeometry(mApplication->窗口,0,0,lFormat);    mSurface = eglCreateWindowSurface(填充mDisplay,lConfig,mApplication->窗口,
        空值);
    如果(mSurface == EGL_NO_SURFACE)
    转到错误;
    mContext = eglCreateContext(填充mDisplay,lConfig,EGL_NO_CONTEXT,NULL);
    如果(mContext == EGL_NO_CONTEXT)
    转到错误;    如果(!eglMakeCurrent(填充mDisplay,mSurface,mSurface,mContext)
        || !eglQuerySurface(填充mDisplay,mSurface,EGL_WIDTH,&安培; mWidth)
        || !eglQuerySurface(填充mDisplay,mSurface,EGL_HEIGHT,&安培; mHeight)
        || (mWidth&下; = 0)|| (mHeight&下; = 0))
    转到错误;{
    常量GLfloat matAmbient [] = {1.0,1.0,1.0,1.0};
    常量GLfloat matDiffuse [] = {1.0,1.0,1.0,1.0};
    常量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_PROJECTIONglViewport(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.0,0.0,0.0,1.0F);    glMatrixMode(GL_PROJECTION);    过glEnable(GL_NORMALIZE);
    过glEnable(GL_DEPTH_TEST);
    glDisable(GL_CULL_FACE);
    // glShadeModel(GL_FLAT);    #IFDEF ORTHOGANAL_PROJECTIONglOrthof(-1.0,// LEFT
        1.0,// RIGHT
        -1.0 * mHeight / mWidth,// BOTTOM
        1.0 * mHeight / mWidth,// TOP
        -2.0,// NEAR
        100.0); //远
    #其他{
    GLfloat mWidthf = mWidth;
    GLfloat mHeightf = mHeight;    常量GLfloat zNear = 0.1;
    常量GLfloat zFar = 1000.0;
    常量GLfloat fieldOfView(视野)= 60.0;    GLfloat大小= zNear * TANF(DEGREES_TO_RADIANS(fieldOfView(视野))/ 2.0);    glFrustumf(-size,大小,-size /(mWidthf / mHeightf),
            大小/(mWidthf / mHeightf),zNear,zFar);    glViewport(0,0,mWidthf,mHeightf);
}
    #万一    glMatrixMode(GL_MODELVIEW);
}

渲染code保持与上述相同。

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;
}

解决方案

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.

这篇关于OpenGLES 1.1帧缓冲/ ColorBuffer /深度缓存为Android NDK与R7B的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆