Search code examples
c#openglcubeopentk

Weird OpenGL cube


I just tried to use VBOs. So I render a cube, and here's what's appening.

If I don't rotate it, everything is OK :

enter image description here

But when I rotate it, this thing appens : enter image description here enter image description here

It looks like the cube is translucid and... I don't really know, it's messing with my mind.

Here's my code :

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

Edit 1:

Looks like it is a problem of depth buffer. I tried to enable the DepthTest, but it still does the same thing.

Edit 2:

It might be coming from the way that I rotate the matrix...?

        GL.Ortho(-Zoom * ratio, Zoom * ratio, -Zoom, Zoom, 0, 100);

Solution

  • Allright I found the answer by myself. The problem came from the fact that I was using glOrtho to zoom, and somehow using wrong values. I switched to glScale and everything is good now!