为什么没有人用的四面体的包厢? [英] Why don't people use tetrahedrons for skyboxes?

查看:256
本文介绍了为什么没有人用的四面体的包厢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个固定的纹理在3D游戏中渲染的天空,人们通常会创建6纹理的立方体贴图,然后再使相机周围的立方体。在GLSL,您可以访问纹理像素与正常的,而不是一个纹理坐标,你可以很容易地得到这种正常通过片段位置相对于相机的规范化。然而,该方法可以与围绕照相机,因为当你正常化每一个位置它将总是导致球体的任何形状来完成。 现在我想知道:的为什么总是一个立方体,而不是一个四面体?渲染一个立方体需要12个三角形,四面体只有4正如我已经说过,周围的摄像头作品的任何形状。所以四面体需要较少的显存和更快渲染,没有任何负面影响?为什么不使用他们?

When rendering a sky with a fixed texture in 3D games, people often create 6 textures in a cube map first, and then render a cube around the camera. In GLSL, you can access the pixels in the textures with a normal instead of a texture coordinate, and you can easily get this normal by normalizing the fragment position relative to the camera. However, this process can be done with any shape that surrounds the camera, because when you normalize each position it will always result in a sphere. Now I'm wondering: Why is it always a cube and not a tetrahedron? Rendering a cube takes 12 triangles, a tetrahedron only 4. And as I already said, any shape that surrounds the camera works. So tetrahedrons take less VRAM and are faster to render, without any downsides? Why not use them?

推荐答案

您并不需要一些环境的几何形状的。所有你需要做的就是画的全屏四的,只是计算正确的纹理坐标它。现在有了现代的GL,我们甚至都不需要提供顶点数据对于这一点,我们可以使用的 attributless 的渲染:

You don't need some environment geometry at all. All you need to do is drawing a full screen quad, and just compute the correct texture coordinates for it. Now with modern GL, we don't even need to supply vertex data for this, we can use attributless rendering:

顶点着色器:

#version 330 core
out vec3 dir;
uniform mat4 invPV;
void main()
{
        vec2 pos  = vec2( (gl_VertexID & 2)>>1, 1 - (gl_VertexID & 1)) * 2.0 - 1.0;
        vec4 front= invPV * vec4(pos, -1.0, 1.0);
        vec4 back = invPV * vec4(pos,  1.0, 1.0);

        dir=back.xyz / back.w - front.xyz / front.w;
        gl_Position = vec4(pos,1.0,1.0);
}

其中, invPV 逆(投影*查看),因此将需要你的相机的方向,以及在投影考虑。这在原则上是eithen进一步simplyfied,这取决于你有多少的限制放在投影矩阵。

where invPV is inverse(Projection*View), so it will take your camera orientation as well as the projection into account. This can in principle be eithen further simplyfied, depending on how much constraints you can put on the projection matrix.

片段着色器:

#version 330 core
in vec3 dir;
out color;
uniform samplerCube uTexEnv;
void main()
{
        color=texture(uTexEnv, dir);
}

要使用此功能,你只需要绑定一个空VAO和纹理,上传你的 invPV 矩阵和呼叫(4,8,GL_TRIANGLE_STRIP,0, 4)

To use this, you simply need to bind an empty VAO and your texture, upload your invPV matrix and call glDrawArrays(GL_TRIANGLE_STRIP, 0, 4).

这个方法当然可以用于球面纹理映射,而不是立方体地图

This approach could of course be used for spherical texture mapping instead of cube maps

这篇关于为什么没有人用的四面体的包厢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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