nicstop's picture

Help please

Hello. Can somebody explain why nothing is drawing to the screen?

private VertexC1P3[] Shape = null;
        private uint vboId;
 
        private void CreateShape()
        {
            this.Shape = new VertexC1P3[4];
            this.Shape[0] = new VertexC1P3(new ColorRgba(1, 1, 0, 1), new Vector3(50, 50, 0));
            this.Shape[1] = new VertexC1P3(new ColorRgba(1, 1, 0, 1), new Vector3(50, -50, 0));
            this.Shape[2] = new VertexC1P3(new ColorRgba(1, 1, 0, 0), new Vector3(-50, -50, 0));
            this.Shape[3] = new VertexC1P3(new ColorRgba(1, 1, 1, 0), new Vector3(-50, 50, 0));
        }
 
        private void GenVbo()
        {
            GL.GenBuffers(1, out this.vboId);
            GL.BindBuffer(BufferTarget.ArrayBuffer, this.vboId);
 
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(this.Shape.Length * VertexC1P3.Size), this.Shape, BufferUsageHint.StreamDraw);
 
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);  
        }
 
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);
 
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            GL.Viewport(0, 0, this.Width, this.Height);
 
            GL.BindBuffer(BufferTarget.ArrayBuffer, this.vboId);
 
            GL.EnableClientState(ArrayCap.ColorArray);
            GL.EnableClientState(ArrayCap.VertexArray);
 
            GL.ColorPointer(4, ColorPointerType.UnsignedByte, VertexC1P3.Size, VertexC1P3.ColorOffset);
            GL.VertexPointer(3, VertexPointerType.Float, VertexC1P3.Size, VertexC1P3.PositionOffset);
 
            GL.DrawArrays(PrimitiveType.Quads, 0, this.Shape.Length);
 
            GL.DisableClientState(ArrayCap.ColorArray);
            GL.DisableClientState(ArrayCap.VertexArray);
 
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
 
            this.SwapBuffers();
        }
 
        [StructLayout(LayoutKind.Sequential)]
        public struct VertexC1P3
        {
            public ColorRgba Color;
            public Vector3 Position;
 
            public VertexC1P3(ColorRgba color, Vector3 position)
            {
                this.Color = color;
                this.Position = position;
            }
 
            public const int ColorOffset = 0;
 
            public const int PositionOffset = ColorOffset + sizeof(byte) * 4;
 
            public const int Size = PositionOffset + sizeof(float) * 3;
        }
 
        [StructLayout(LayoutKind.Sequential)]
        public struct ColorRgba
        {
            public byte R;
 
            public byte G;
 
            public byte B;
 
            public byte A;
 
            public ColorRgba(byte r, byte g, byte b, byte a)
            {
                this.R = r;
                this.G = g;
                this.B = b;
                this.A = a;
            }
        }

Comments

Comment viewing options

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

If you are not using a shader, you need to setup a projection matrix.

A complete working example is available here: https://github.com/opentk/opentk/blob/develop/Source/Examples/OpenGL/1.x...

            GL.Viewport(0, 0, Width, Height);
 
            float aspect_ratio = Width / (float)Height;
            Matrix4 perpective = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspect_ratio, 1, 64);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref perpective);
 
            Matrix4 lookat = Matrix4.LookAt(0, 5, 5, 0, 0, 0, 0, 1, 0);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref lookat);
nicstop's picture

Thank you!

flopoloco's picture

This code looks OK, but I can't figure out what is the problem.

Perhaps might be the Matrix case for sure as Fiddler said.

(For the record you can display shapes right away on the screen, as long as there is no depth coordinates involved and the shape coordinates limited in ranges between -1 and +1).

Also another fact, for modern OpenGL (shader based rendering) is proper to use AttribPointers instead of VertexPointer/ColorPointer/etc.
http://www.opentk.com/node/2293