顶点属性缓冲区绑定到错误的属性 [英] Vertex Attribute Buffers getting bound to wrong attribute

查看:136
本文介绍了顶点属性缓冲区绑定到错误的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将缓冲区绑定到着色器的属性时,它们似乎被翻转了.

When I bind my buffers to attributes for my shaders, they seem to be getting flipped.

所以,我有一个顶点着色器:

So, I've got a vertex shader:

precision highp float;

uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;

in vec3 in_position;
in vec3 in_color;

out vec3 ex_Color;

void main(void)
{ 
  gl_Position = projection_matrix * modelview_matrix * vec4(in_position, 1);
  ex_Color = in_color;
}

和片段着色器

precision highp float;

in vec3 ex_Color;

out vec4 out_frag_color;

void main(void)
{
  out_frag_color = vec4(ex_Color, 1.0);
}

没有什么太复杂的.有两个输入:一个用于顶点位置,一个用于颜色. (作为新手,我还不想处理纹理或光线.)

Nothing too complicated. There are two inputs: one for vertex locations, and one for colors. (As a newb, I didn't want to deal with textures or light yet.)

现在,在我的客户代码中,我将数据放入两个向量数组中,即positionVboData和colorVboData,并创建了VBO ...

Now, in my client code, I put data into two arrays of vectors, positionVboData and colorVboData, and I create the VBOs...

GL.GenBuffers(1, out positionVboHandle);
GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle);
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
                       new IntPtr(positionVboData.Length * Vector3.SizeInBytes),
                       positionVboData, BufferUsageHint.StaticDraw);

GL.GenBuffers(1, out colorVboHandle);
GL.BindBuffer(BufferTarget.ArrayBuffer, colorVboHandle);
GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
                new IntPtr(colorVboData.Length * Vector3.SizeInBytes),
                colorVboData, BufferUsageHint.StaticDraw);

然后,我将期望以下代码,以将vbo绑定到着色器的属性:

and then, I would expect the following code to work to bind the vbos to the attributes for the shaders:

    GL.EnableVertexAttribArray(0);
    GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle);
    GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0);
    GL.BindAttribLocation(shaderProgramHandle, 0, "in_position");

    GL.EnableVertexAttribArray(1);
    GL.BindBuffer(BufferTarget.ArrayBuffer, colorVboHandle);
    GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0);
    GL.BindAttribLocation(shaderProgramHandle, 1, "in_color");

但是,实际上,我必须在上一个代码示例中交换positionVboHandle和colorVboHandle,然后它才能完美运行.但这在我看来是倒退的.我想念什么?

But, in fact I have to swap positionVboHandle and colorVboHandle in the last code sample and then it works perfectly. But that seems backwards to me. What am I missing?

更新

有些奇怪的事情正在发生.如果我将顶点着色器更改为此:

Something weird is going on. If I change the vertex shader to this:

precision highp float;

uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;

in vec3 in_position;
in vec3 in_color;

out vec3 ex_Color;

void main(void)
{
  gl_Position = projection_matrix * modelview_matrix * vec4(in_position, 1);
  //ex_Color = in_color;
  ex_Color = vec3(1.0, 1.0, 1.0);
}"

并且不进行任何其他更改(建议的修复程序是在完成所有设置后移动程序链接,而是将正确的属性(顶点位置)加载到in_position而不是in_color中.

And make no other changes (other than the fix suggested to move the program link after all the set up, it loads the correct attribute, the vertex positions, into in_position rather than into in_color.

推荐答案

因此,在MártiņšMožeiko的帮助下,我得以弄清这一点.我正确地在LinkProgram之前调用BindAttribLocation.但是,在绑定任何属性位置之前,我没有调用GL.CreateProgram().

So, with Mārtiņš Možeiko's help, I was able to figure this out. I was calling BindAttribLocation before LinkProgram correctly. However, I wasn't calling GL.CreateProgram() BEFORE I was binding any of the attribute locations.

这篇关于顶点属性缓冲区绑定到错误的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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