0

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);
Phito
  • 281
  • 1
  • 2
  • 12
  • 4
    The first looks like a strange clipping, the second looks like you don't cull the faces and don't have a depth buffer, so the background planes may be drawn on top of the front ones. – Sami Kuhmonen Jun 05 '14 at 17:47
  • I looks like you're right for the depth buffer. It looks a lot like the first example on [this page](http://open.gl/depthstencils) – Phito Jun 05 '14 at 18:05
  • I updated the post, when I change the zNear and zFar of glOrtho the glitch looks different... – Phito Jun 05 '14 at 18:48
  • 1
    Also use culling, if possible, not just depth buffer. For example with cubes culling will remove the need to draw half the surfaces, always (not considering strange FOV values or other fringe cases). With depth buffer they're just not shown, but they are either drawn (very slow) or at least considered to be drawn (slow also). – Sami Kuhmonen Jun 06 '14 at 05:40

1 Answers1

0

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!

Phito
  • 281
  • 1
  • 2
  • 12