CsharpCoder's picture

Cannot see the Triangle from VertexArray

Hi,
I'm a beginner and I need help to solve this problem.
It's my first VertexArray test. When i use my float VertexArray to draw a triangle, i cannot see it. But when i use my array in the immediate mode, i see it!
Here the code:

class MyGameWindow : GameWindow
    {
        private float[] vertexArray;
        private Matrix4 projection;
 
        protected override void OnLoad(EventArgs e)
        {
            GL.Enable(EnableCap.VertexArray);
            GL.Enable(EnableCap.DepthTest);
            GL.ClearColor(0.5f, 0.5f, 0.5f, 1f);
 
            vertexArray = new float[9];
 
            vertexArray[0] = 0f;
            vertexArray[1] = 1f;
            vertexArray[2] = -5f;
 
            vertexArray[3] = 1f;
            vertexArray[4] = -1f;
            vertexArray[5] = -5f;
 
            vertexArray[6] = -1f;
            vertexArray[7] = -1f;
            vertexArray[8] = -5f;
 
            projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45f), (float)this.Width / (float)this.Height, 0.1f, 1000f);
 
            base.OnLoad(e);
        }
 
        protected override void OnResize(EventArgs e)
        {
            GL.Viewport(0, 0, this.Width, this.Height);
 
            projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45f), (float)this.Width / (float)this.Height, 0.1f, 1000f);
 
            base.OnResize(e);
        }
 
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref projection);
 
 
            GL.EnableClientState(ArrayCap.VertexArray);
 
            GL.VertexPointer<float>(3, VertexPointerType.Float, 3 * System.Runtime.InteropServices.Marshal.SizeOf(typeof(float)), ref vertexArray[0]);
            GL.DrawArrays(BeginMode.Triangles, 0, 1);
 
            GL.EnableClientState(ArrayCap.VertexArray);
 
            /* Here the immediate mode
            GL.Begin(BeginMode.Triangles);
                GL.Vertex3(vertexArray[0], vertexArray[1], vertexArray[2]);
                GL.Vertex3(vertexArray[3], vertexArray[4], vertexArray[5]);
                GL.Vertex3(vertexArray[6], vertexArray[7], vertexArray[8]);
            GL.End();
            */
 
            SwapBuffers();
        }
    }

Thanks for your help!
This is a good forum. Respect!


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
c2woody's picture

The count parameter of glDrawArrays specifies the number of vertices, not primitives (so use 3 instead of 1 there). See
http://www.opengl.org/sdk/docs/man/xhtml/glDrawArrays.xml
for example.

Any way, also have a look at the VertexArray example in the OpenTK sources, especially how GL.VertexPointer is called there (zero-stride for tightly packed structures, Vector3-array for the vertices).

the Fiddler's picture

See http://www.opentk.com/doc/graphics/geometry/vertex-arrays for some additional information.

In short, you cannot use client storage in a garbage collected environment without some extra precautions. You should always use server storage instead, i.e. vertex buffer objects or display lists.

Vertex Buffer Objects, specifically, are very easy to use (just three additional lines of code in your snippet, to generate, bind and upload the buffer). Check the Vertex Buffer Object samples in the example launcher.

CsharpCoder's picture

I looked in the source from the OpenTK examples.
The mistake was to call GL.VertexPointer() with a reference and GL.DrawArrays() with the number one.
Here are right calls.

GL.VertexPointer<float>(3, VertexPointerType.Float, 0, vertexArray);
GL.DrawArrays(BeginMode.Triangles, 0, 3);

To use the vertexarrays was only a test. After I use VBOs. Thanks.

Cheers!