天空盒纹理无法正确显示 [英] Skybox textures do not display correctly

查看:236
本文介绍了天空盒纹理无法正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Skybox纹理有问题.如果您扭曲相机,则会产生一种纹理覆盖另一种纹理的感觉,如屏幕截图所示:

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显示代码:

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

我的opengl参数:

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

然后我打电话给drawSkybox:

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

我该如何解决该问题?

我知道问题是glDepthMask(false);,但是如何将其替换?

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

如果我只是删除glDepthMask(false);,并用glDepthFunc(GL_LEQUAL);glDepthFunc(GL_LESS);的示例替换它,那么天空盒将与所有其他对象重叠,并且只有它可见–

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 –

推荐答案

绘制天空盒时,请勿更改深度蒙版或深度测试.保留深度测试功能GL_LESS.

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
// [...]

这篇关于天空盒纹理无法正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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