奇怪的OpenGL多维数据集

时间:2014-06-05 17:41:47

标签: c# opengl cube opentk

我只是尝试使用VBO。所以我渲染一个立方体,这就是它的内容。

如果我不旋转它,一切正常:

enter image description here

但是当我旋转它时,这个东西出现了: enter image description here enter image description here

看起来立方体是透明的......我真的不知道,它让我的脑子一团糟。

这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

好吧,我自己找到了答案。问题来自于我使用glOrtho进行缩放,并以某种方式使用错误的值。我切换到glScale,现在一切都很好!