cha*_*rlt 7 opengl glsl vertex-buffer vertex-attributes
我可以将颜色作为 4 个浮点发送到着色器 - 没问题。但是我想将其作为整数(或无符号整数,并不重要,重要的是 32 位)发送,并在着色器上的 vec4 中分解。
我使用 OpenTK 作为 OpenGL 的 C# 包装器(尽管它应该只是一个直接包装器)。
让我们考虑最简单的着色器之一,其顶点包含位置(xyz)和颜色(rgba)。
顶点着色器:
#version 150 core
in vec3 in_position;
in vec4 in_color;
out vec4 pass_color;
uniform mat4 u_WorldViewProj;
void main()
{
gl_Position = vec4(in_position, 1.0f) * u_WorldViewProj;
pass_color = in_color;
}
Run Code Online (Sandbox Code Playgroud)
片段着色器:
#version 150 core
in vec4 pass_color;
out vec4 out_color;
void main()
{
out_color = pass_color;
}
Run Code Online (Sandbox Code Playgroud)
让我们创建顶点缓冲区:
public static int CreateVertexBufferColor(int attributeIndex, int[] rawData)
{
var bufferIndex = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, bufferIndex);
GL.BufferData(BufferTarget.ArrayBuffer, sizeof(int) * rawData.Length, rawData, BufferUsageHint.StaticDraw);
GL.VertexAttribIPointer(attributeIndex, 4, VertexAttribIntegerType.UnsignedByte, 0, rawData);
GL.EnableVertexAttribArray(attributeIndex);
GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
return bufferIndex;
}
Run Code Online (Sandbox Code Playgroud)
我在顶点着色器中得到 vec4 'in_color' 的全零。不知道出了什么问题。
我发现的最接近的东西:https://www.opengl.org/discussion_boards/showthread.php/198690-I-cannot-send-RGBA-color-as-unsigned-int。
同样在 VertexAttribIPointer 中,我将 0 作为步幅传递,因为我确实有 VertexBufferArray 并保持数据分离。因此,每个顶点的颜色紧密排列为 32 位(每种颜色)。
好吧,这对我来说是有效的:
将数据管理为int[]紧密包装的仅颜色数组(其中int格式为:RGBA含义0xAABBGGRR),然后将顶点属性定义为:GL.VertexAttribPointer(index, 4, VertexAttribPointerType.UnsignedByte, true, sizeof(int), IntPtr.Zero)并在着色器中将其使用为:in vec4 in_color;。