c# - OpenGL - 如何创建和绑定(bind)立方体贴图数组?

标签 c# opengl opentk

在超过纹理单元限制后,我决定使用立方体贴图数组纹理。

出于测试目的,我将立方体贴图的内容渲染为天空盒。问题是,当我绑定(bind)一个立方体贴图数组时,OpenGL 会忽略它,而是使用之前绑定(bind)的立方体贴图来渲染天空盒。

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

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

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

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

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

顶点

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

最佳答案

您需要在着色器代码中使用相应的采样器类型。立方体贴图数组在 OpenGL 4.0 及更高版本中可用,因此您还需要将 GLSL 版本增加到 400 或更高版本。

然后着色器代码看起来像这样:

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

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

关于c# - OpenGL - 如何创建和绑定(bind)立方体贴图数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40469508/

相关文章:

opengl - OpenGL 中的轨迹球旋转

c++ - OpenGL 着色器 : uniform variables count incorrect

c# - GLSL 110 不允许子矩阵或超矩阵构造函数

c# - OpenTK 多线程?

c# - 纹理缺乏颜色

c# - 将用户输入的数据转换为大写

c# - 没有现有记录时填充数据时出错。 ASP.NET C#

c++ - glScalef 命令偏离中心

c# - 如何修复 xamarin.forms 移动应用程序页面中的空 ListView

c# - 通过 Web 服务自动公开数据库表