C# 奇怪的OpenGL立方体
我只是尝试使用VBOs。所以我渲染了一个立方体,下面是出现的情况 如果我不旋转它,一切正常: 但当我旋转它时,会出现这样的情况: 看起来立方体是半透明的,而且。。。我真的不知道,它扰乱了我的思想 这是我的密码:C# 奇怪的OpenGL立方体,c#,opengl,cube,opentk,C#,Opengl,Cube,Opentk,我只是尝试使用VBOs。所以我渲染了一个立方体,下面是出现的情况 如果我不旋转它,一切正常: 但当我旋转它时,会出现这样的情况: 看起来立方体是半透明的,而且。。。我真的不知道,它扰乱了我的思想 这是我的密码: internal class CubeRenderer { private VertexBuffer vertexBuffer; private IndexBuffer indexBuffer; public CubeRenderer() {
internal class CubeRenderer
{
private VertexBuffer vertexBuffer;
private IndexBuffer indexBuffer;
public CubeRenderer()
{
vertexBuffer = new VertexBuffer(new[]
{
// front
new Vertex(-1.0f, -1.0f, 1.0f, Color.Red),
new Vertex(1.0f, -1.0f, 1.0f, Color.Beige),
new Vertex(1.0f, 1.0f, 1.0f, Color.SaddleBrown),
new Vertex(-1.0f, 1.0f, 1.0f, Color.AliceBlue),
//back
new Vertex(-1.0f, -1.0f, -1.0f, Color.DarkBlue),
new Vertex(1.0f, -1.0f, -1.0f, Color.Firebrick),
new Vertex(1.0f, 1.0f, -1.0f, Color.IndianRed),
new Vertex(-1.0f, 1.0f, -1.0f, Color.Yellow)
});
indexBuffer = new IndexBuffer(new uint[]
{
// front
0, 1, 2,
2, 3, 0,
// top
3, 2, 6,
6, 7, 3,
// back
7, 6, 5,
5, 4, 7,
// bottom
4, 5, 1,
1, 0, 4,
// left
4, 0, 3,
3, 7, 4,
// right
1, 5, 6,
6, 2, 1
});
}
public void Draw()
{
// 1) Ensure that the VertexArray client state is enabled.
GL.EnableClientState(ArrayCap.VertexArray);
GL.EnableClientState(ArrayCap.NormalArray);
GL.EnableClientState(ArrayCap.TextureCoordArray);
GL.EnableClientState(ArrayCap.ColorArray);
// 2) Bind the vertex and element (=indices) buffer handles.
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer.Id);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBuffer.Id);
GL.VertexPointer(3, VertexPointerType.Float, vertexBuffer.Stride, IntPtr.Zero);
GL.NormalPointer(NormalPointerType.Float, vertexBuffer.Stride, new IntPtr(Vector3.SizeInBytes));
GL.TexCoordPointer(2, TexCoordPointerType.Float, vertexBuffer.Stride, new IntPtr(Vector3.SizeInBytes*2));
GL.ColorPointer(4, ColorPointerType.UnsignedByte, vertexBuffer.Stride, new IntPtr(Vector3.SizeInBytes*2 + Vector2.SizeInBytes));
// 4) Call DrawElements. (Note: the last parameter is an offset into the element buffer and will usually be IntPtr.Zero).
GL.DrawElements(PrimitiveType.Triangles, indexBuffer.Count, DrawElementsType.UnsignedInt, IntPtr.Zero);
//Disable client state
GL.DisableClientState(ArrayCap.VertexArray);
GL.DisableClientState(ArrayCap.NormalArray);
GL.DisableClientState(ArrayCap.TextureCoordArray);
GL.DisableClientState(ArrayCap.ColorArray);
}
}
编辑1:
看起来这是深度缓冲区的问题。我试图启用DepthTest,但它仍然执行相同的操作
编辑2:
它可能来自我旋转矩阵的方式
GL.Ortho(-Zoom * ratio, Zoom * ratio, -Zoom, Zoom, 0, 100);
好吧,我自己找到了答案。问题来自于我使用glOrtho进行缩放的事实,并且不知何故使用了错误的值。我切换到glScale,现在一切都很好 第一个看起来像一个奇怪的剪辑,第二个看起来像你没有剔除面,也没有深度缓冲区,所以背景平面可能会画在前面的平面上。我看你的深度缓冲区是对的。它看起来很像我更新文章的第一个例子,当我更改glorth的zNear和zFar时,小故障看起来不同…如果可能的话,还可以使用剔除,而不仅仅是深度缓冲区。例如,使用立方体消隐将永远消除绘制一半曲面的需要(不考虑奇怪的FOV值或其他边缘情况)。使用深度缓冲区时,它们只是不显示,但它们要么被绘制(非常慢),要么至少被认为是被绘制的(也很慢)。