0
在閱讀了NDK文檔以及我所有關於OpenGLES的書之後,我都碰到了一堵牆。我試圖將我的iOS OpenGLES設置爲Android NDK R7及更高版本,主要是爲了獲得編碼時忽略的深度緩衝區。OpenGLES 1.1 with FrameBuffer/ColorBuffer/DepthBuffer for Android with NDK r7b
問題是我鬆開了一些物體上的紋理,當我啓用顏色緩衝區,如下所示,深度緩衝區不工作時,我發送對象到背景。
我使用OGLES 1.1 FFP和NDK R7或以上
這裏是我的初始化代碼: -
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);
}
這裏是我的渲染代碼:
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;
}