Skybox textures do not display correctly

六月ゝ 毕业季﹏ 提交于 2020-04-14 07:36:06

问题


I have a problem with skybox textures. If you twist the camera, it creates a feeling that one texture overlays another, as in the screenshot:

Skybox show code:

private void drawSkybox(int texId){

    glColor4f(1,1,1,1);
    glDepthMask(false);
    glEnable(GL_TEXTURE_CUBE_MAP);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_CUBE_MAP, texId);
    glBindVertexArray(vao[0]);
    glBindBuffer (GL_ARRAY_BUFFER, vbo[0]);
    glDrawArrays(GL_TRIANGLES, 0, 36);
    glBindVertexArray(0);
    glBindBuffer (GL_ARRAY_BUFFER, 0);
    glDepthMask(true);
    glDisable(GL_TEXTURE_CUBE_MAP);
}

My opengl paramters:

glEnable(GL_ALPHA_TEST);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glEnable(GL_NORMALIZE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glShadeModel(GL_SMOOTH);
glColorMask (true, true, true, true);
glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);

And my call drawSkybox:

glViewport(0, 0, WIDTH, HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-max, max, -1, 1, 10, -10);
glRotated(cameraX, 1f, 0f, 0);
glRotated(cameraY, 0f, 1f, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawSkybox(texId);

How i can fix the problem?

I understand that the problem is glDepthMask(false); but how can it be replaced?

If I just remove glDepthMask(false); and replace it with an example with glDepthFunc(GL_LEQUAL); and glDepthFunc(GL_LESS); then the skybox will overlap all other objects and only it will be visible –


回答1:


Do not change the depth mask or the depth test when you draw the skybox. Keep the depth test function GL_LESS.

private void drawSkybox(int texId){

    glColor4f(1,1,1,1);
    // glDepthMask(false); <---- DELETE

    // [...]
}

But clear the depth buffer again, after drawing the skybox. Draw the skybox first, then the skybox covers the entire screen. Then clear the depth buffer, thus all objects which are draw after, will cover the skybox:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// [...]

drawSkybox(texId);
glClear(GL_DEPTH_BUFFER_BIT);

// render other objects
// [...]


来源:https://stackoverflow.com/questions/61181470/skybox-textures-do-not-display-correctly

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