下午好,
问题是在 Win7(C#,VS2010)上编写的显示 OpenGL 平铺网格的代码在 Mac OS X(C#,MonoDevelop)中显示不同。当前每个图块都是单独渲染的,x/y 偏移量实际上存储在顶点信息中。瓷砖是这样建造的:
private void BuildTile()
{
Vector3[] vertices = new Vector3[4];
Vector2[] uvs = new Vector2[] { new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 1), new Vector2(1, 0) };
int[] indices = new int[] { 1, 0, 2, 1, 2, 3 };
// build vertex list based on position
vertices[0] = new Vector3(Location.X, Location.Y + 1, 0);
vertices[1] = new Vector3(Location.X, Location.Y, 0);
vertices[2] = new Vector3(Location.X + 1, Location.Y + 1, 0);
vertices[3] = new Vector3(Location.X + 1, Location.Y, 0);
VBO<Vector3> vertex = new VBO<Vector3>(vertices, BufferTarget.ArrayBuffer, BufferUsageHint.StaticRead);
VBO<Vector2> uv = new VBO<Vector2>(uvs, BufferTarget.ArrayBuffer, BufferUsageHint.StaticRead);
VBO<int> element = new VBO<int>(indices, BufferTarget.ElementArrayBuffer, BufferUsageHint.StaticRead);
VAO = new VAO(ShaderProgram, vertex, uv, element);
}
由于 Mac OS X 不支持 OpenGL 3,因此每次绘制调用发生时,VAO 对象都会绑定属性。
public void BindAttributes()
{
if (vertex == null) throw new Exception("Error binding attributes. No vertices were supplied.");
if (element == null) throw new Exception("Error binding attributes. No element array was supplied.");
uint array = 0;
Gl.EnableVertexAttribArray(array);
Gl.BindAttribLocation(Program.ProgramID, array, "in_position");
Gl.BindBuffer(vertex.BufferTarget, vertex.vboID);
Gl.VertexAttribPointer(array++, vertex.Size, vertex.PointerType, true, 12, new IntPtr(0));
Gl.EnableVertexAttribArray(array);
Gl.BindAttribLocation(Program.ProgramID, array, "in_uv");
Gl.BindBuffer(uv.BufferTarget, uv.vboID);
Gl.VertexAttribPointer(array++, uv.Size, uv.PointerType, true, 8, new IntPtr(0));
Gl.BindBuffer(BufferTarget.ElementArrayBuffer, element.vboID);
}
着色器非常简单。顶点着色器:
uniform mat4 projection_matrix;
uniform mat4 modelview_matrix;
attribute vec3 in_position;
attribute vec2 in_uv;
varying vec2 uv;
void main(void)
{
uv = in_uv;
gl_Position = projection_matrix * modelview_matrix * vec4(in_position, 1);
}
片段着色器:
uniform sampler2D active_texture;
varying vec2 uv;
void main(void)
{
gl_FragColor = texture2D(active_texture, uv);
}
问题是 Mac OS X 版本覆盖了所有图块。所有 100 个图块将在第一个图块的位置相互堆叠。但是,在 Win7 上,图块将按预期分布在 10x10 网格中。有谁知道为什么会发生这种情况?我查看了顶点数据,它存储了 Mac OS X 和 Win7 上的偏移量,并且 VBO ID 是唯一的,并且正在绑定正确的 VBO。我猜我绑定属性的方法一定有问题,但我看不到问题。OpenGL 2 和 3 如何处理顶点属性之间有什么有趣的区别吗?
谢谢,如果您需要我的代码来帮助我找到问题,请告诉我。
注意:我可以将顶点偏移存储在着色器中(作为统一)并在每个图块中更新它。这行得通!所以,我相信只有第一个 VBO 被绑定,并且只是被渲染了 100 次。