簡體   English   中英

android native活動中的opengl es,gl_depth_test和gl_depth_buffer_bit不起作用

[英]opengl es in android native activity, gl_depth_test and gl_ depth_buffer_bit are not working

void setupViewPort(GLint width, GLint height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

GLfloat aspect = (GLfloat)width / (GLfloat)height;

glFrustumf(-1.0f, 1.0f, -1.0 / aspect, 1.0 / aspect, 1.5f, 40.0f);


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(0, 0, 5,                          //eyeXYZ
    0.0f, 0.0f, -1.0f,                  //centerXYZ
    0.0f, 1.0f, 0.0f);
}

和------------------------------------------------- ----------------------------

void eglInitSetting(GLManager* _glManager) {
glManager = _glManager;

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(true);
}

和 - - - - - - - - - - - - - - - - - - - - - - - - - ----------------------------

void eglInitDrawingObject() {
rect = new GLRectangle();
tri = new GLTriangle();
circle = new GLCircle();
sphere = new GLSphere();
cube = new GLCube();
point = new GLPoint();
ecube = new GLECube();


//initailize each oject...

glManager->object()->push_back(rect);
glManager->object()->push_back(tri);
glManager->object()->push_back(circle);
glManager->object()->push_back(sphere);
glManager->object()->push_back(cube);
glManager->object()->push_back(point);
glManager->object()->push_back(ecube);
}

和.... - - - - - - - - - - - - - - - - - - - - - - - --------------------------------

void eglDraw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

i += 3;
GLfloat angle = (float)(M_PI / 180.0f) * i;

GLfloat x = (float)(cos(angle));
GLfloat y = (float)(sin(angle));
//GLfloat z = (float)0.0f;

point->move(x, y, 0.0f);

sphere->rotate(0.0f, ry += 0.5f, 0.0f);

rect->rotate(rx += 0.5f, 0.0f, 0.0f);

cube->rotate(rx, ry, 0.0f);
ecube->rotate(rx, ry, 0.0f);

tri->rotate(0.0f, 0.0f, rz += 100.0f);
circle->rotate(0.0f, 0.0f, rz += 100.0f);

glManager->object()->drawAll();
glFlush();
}

最后.. - - - - - - - - - - - - - - - - - - - - - - - - ------------------------------

static int engine_init_display(struct engine* engine) {

setupViewPort(engine->width, engine->height);

GLManager* glManager;
glManager = GLManager::getInstance();
glManager->object()->clearAll();
glManager->setAssetManager(engine->app->activity->assetManager);

eglInitSetting(glManager);
eglInitDrawingObject();

return 0;
}

static void engine_draw_frame(struct engine* engine) {
if (engine->display == NULL) {
    // No display.
    return;
}
eglDraw();

eglSwapBuffers(engine->display, engine->surface);
}

當我畫一個立方體...

多維數據集顯示不正確。

當我在android java版本上使用相同的代碼時,win32 opengl

它畫得完美..

請幫我..

我該怎么辦?

這可能是因為您沒有深度緩沖區。 在為渲染上下文選擇EGL配置時,請確保您要求使用深度輸入的配置。

PS不相關,但不要手動調用glFlush() 如果需要,調用eglSwapBuffers()會自動刷新,多余的刷新可能會導致冗余的GPU處理。 相信司機做正確的事...

const EGLint attribs[] = {
    EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
    EGL_BLUE_SIZE, 8,
    EGL_GREEN_SIZE, 8,
    EGL_RED_SIZE, 8,
    EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
    EGL_NONE
};

添加GL_DEPTH_SIZE

const EGLint attribs[] = {
    EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
    EGL_BLUE_SIZE, 8,
    EGL_GREEN_SIZE, 8,
    EGL_RED_SIZE, 8,
    EGL_RENDERABLE_TYPE, EGL_OPENGL_ES_BIT,
    EGL_DEPTH_SIZE, 24,
    EGL_NONE
};

我解決了這個問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM