OpenGL - 如何创建和绑定立方体贴图数组?

时间:2016-11-07 16:05:54

标签: c# opengl opentk

超过纹理单位限制后,我决定使用立方体贴图阵列纹理。

为了测试,我将立方体贴图的内容渲染为天空盒。事实上,OpenGL在绑定立方体贴图数组时忽略,而是使用先前绑定的立方体贴图渲染天空盒。

GL.BindTexture(TextureTarget.TextureCubeMap, shadowMaps[5].cubeMapTexture);
GL.BindTexture(TextureTarget.TextureCubeMapArray, shadowMapArray.cubeMapHandle); // Ignored

我假设这是错误创建的立方体贴图数组的默认行为。据我所知,我已经完成了所有事情:Framebuffer complete

那么,创建和绑定立方体贴图数组的正确方法是什么?

public class CubeMapArray
{
    public static int size = 512;
    public static int layers = 8; // number of cube maps in array

    public int FBO_handle;
    public int cubeMapHandle;
    public int cubeMapDepthHandle;

    // Constructor //
    public CubeMapArray()
    {
        // Create the FBO
        GL.GenFramebuffers(1, out FBO_handle);

        // Create and bind the CubeMap array
        GL.GenTextures(1, out cubeMapHandle);
        GL.BindTexture(TextureTarget.TextureCubeMapArray, cubeMapHandle);

        // Allocate storage space
        GL.TexImage3D(TextureTarget.TextureCubeMapArray, 0, PixelInternalFormat.Rg16, size, size, layers * 6, 0, PixelFormat.Red, PixelType.Float, IntPtr.Zero);

        // Set the suitable texture parameters
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge);
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureBaseLevel, 0);
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureMaxLevel, 0);

        // Create and bind the CubeMap depth array
        GL.GenTextures(1, out cubeMapDepthHandle);
        GL.BindTexture(TextureTarget.TextureCubeMapArray, cubeMapDepthHandle);

        // Allocate storage space
        GL.TexImage3D(TextureTarget.TextureCubeMapArray, 0, PixelInternalFormat.DepthComponent, size, size, layers * 6, 0, PixelFormat.DepthComponent, PixelType.UnsignedByte, IntPtr.Zero);

        // Set the suitable texture parameters
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge);
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureBaseLevel, 0);
        GL.TexParameter(TextureTarget.TextureCubeMapArray, TextureParameterName.TextureMaxLevel, 0);

        // Attach cubemap texture as the FBO's color buffer
        GL.BindFramebuffer(FramebufferTarget.Framebuffer, FBO_handle);
        GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, cubeMapHandle, 0);
        GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, cubeMapDepthHandle, 0);

        // Error check
        var errorcheck = GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
        Console.WriteLine("CUBEMAP ARRAY: " + errorcheck);

        // Bind default framebuffer
        GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
    }
}

}

着色器非常简单,令人惊讶的是,它可以绑定常规立方体贴图。

顶点

#version 330

in vec3 texCoord;
out vec4 fragColor;
uniform samplerCube cubeMapArray[16];

void main (void) 
{
    fragColor = texture(cubeMapArray[0], texCoord);
}

片段

#version 330

uniform mat4 modelMatrix; 
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;

in vec3 in_position;
out vec3 texCoord;

void main() 
{
    gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(in_position, 1.0);
    texCoord = in_position;
}

2 个答案:

答案 0 :(得分:3)

您需要在着色器代码中使用相应的采样器类型。 OpenGL 4.0及更高版本中提供了多维数据集地图数组,因此您还需要将GLSL版本增加到400或更高版本。

然后着色器代码将沿着以下几行显示:

#version 400
...
uniform samplerCubeArray cubeMapArray;
...
fragColor = texture(cubeMapArray, vec4(texCoord, 0));

请注意,采样的数组层被指定为纹理坐标的第4个分量。

答案 1 :(得分:3)

uniform samplerCube cubeMapArray[16];

好的,array of samplersarray texture之间存在差异。你在这里有一系列的采样器。你在OpenGL中创建的是一个数组纹理。

数组纹理来自非数组对象的separate texture types。立方体贴图阵列纹理不是立方体贴图纹理;它是立方体贴图数组纹理。就像2D数组纹理不是2D纹理一样。

因此,如果您需要数组纹理,则必须使用适当的采样器类型。由于它是立方体贴图数组纹理,因此使用samplerCubeArray。你必须摆脱数组下标。

从立方体贴图阵列纹理采样时,第四个成分是要从中采样的数组图层:

fragColor = texture(cubeMapArray, vec4(texCoord, 0));

此外,如果您打算使用立方体贴图阵列纹理,请不要在所有处绑定非数组立方体贴图纹理。这不违法;它只是令人困惑,最好避免。