HM's picture

Problems migrating OpenGL programs

Hi,

I´m new to Opentk. I´m trying to transform some of my old Opengl programs to Opentk to see the diferences between them. I have problems with this basic program, which draws a white cube with side= 10 at the origin. The camera is in (40, 30, -30), looking to (0,0,0). Nothing is shown, only the clear color fills the window. I can´t see where´s the problem, I´ve transformed instruction by instruction my C code. The only "big" change is the transformation of degrees in radians in the CreatePerspectiveFieldOfView instruction. Any idea is welcome.

using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics.OpenGL;
 
namespace OpenTKVBOShader
{
    class Program : GameWindow
    {
        string vertexShaderSource = @"
                                        #version 330
 
                                        layout (location = 0) in vec3 Position;
 
                                        uniform mat4 MVP;
 
                                        void main()
                                        {
                                            gl_Position = MVP*vec4(Position, 1.0f);
                                        }";
 
 
        string fragmentShaderSource = @"
                                        #version 330
 
                                        out vec3 outputColor;
 
                                        void main()
                                        {
	                                        outputColor = vec3(1.0f, 1.0f, 1.0f);
                                        }";
 
 
        int vbo, shaderProgramHandle, vertexShaderHandle, fragmentShaderHandle;
        int MVPLocation;
 
        void CreateVertexBuffer()
        {
            Vector3[] vertices = new Vector3[36];
            vertices[0] = new Vector3(-10.0f, -10.0f, -10.0f);
            vertices[1] = new Vector3(-10.0f, -10.0f, 10.0f);
            vertices[2] = new Vector3(-10.0f, 10.0f, 10.0f);
            vertices[3] = new Vector3(10.0f, 10.0f, -10.0f);
            vertices[4] = new Vector3(-10.0f, -10.0f, -10.0f);
            vertices[5] = new Vector3(-10.0f, 10.0f, -10.0f);
            vertices[6] = new Vector3(10.0f, -10.0f, 10.0f);
            vertices[7] = new Vector3(-10.0f, -10.0f, -10.0f);
            vertices[8] = new Vector3(10.0f, -10.0f, -10.0f);
            vertices[9] = new Vector3(10.0f, 10.0f, -10.0f);
            vertices[10] = new Vector3(10.0f, -10.0f, -10.0f);
            vertices[11] = new Vector3(-10.0f, -10.0f, -10.0f);
            vertices[12] = new Vector3(-10.0f, -10.0f, -10.0f);
            vertices[13] = new Vector3(-10.0f, 10.0f, 10.0f);
            vertices[14] = new Vector3(-10.0f, 10.0f, -10.0f);
            vertices[15] = new Vector3(10.0f, -10.0f, 10.0f);
            vertices[16] = new Vector3(-10.0f, -10.0f, 10.0f);
            vertices[17] = new Vector3(-10.0f, -10.0f, -10.0f);
            vertices[18] = new Vector3(-10.0f, 10.0f, 10.0f);
            vertices[19] = new Vector3(-10.0f, -10.0f, 10.0f);
            vertices[20] = new Vector3(10.0f, -10.0f, 10.0f);
            vertices[21] = new Vector3(10.0f, 10.0f, 10.0f);
            vertices[22] = new Vector3(10.0f, -10.0f, -10.0f);
            vertices[23] = new Vector3(10.0f, 10.0f, -10.0f);
            vertices[24] = new Vector3(10.0f, -10.0f, -10.0f);
            vertices[25] = new Vector3(10.0f, 10.0f, 10.0f);
            vertices[26] = new Vector3(10.0f, -10.0f, 10.0f);
            vertices[27] = new Vector3(10.0f, 10.0f, 10.0f);
            vertices[28] = new Vector3(10.0f, 10.0f, -10.0f);
            vertices[29] = new Vector3(-10.0f, 10.0f, -10.0f);
            vertices[30] = new Vector3(10.0f, 10.0f, 10.0f);
            vertices[31] = new Vector3(-10.0f, 10.0f, -10.0f);
            vertices[32] = new Vector3(-10.0f, 10.0f, 10.0f);
            vertices[33] = new Vector3(10.0f, 10.0f, 10.0f);
            vertices[34] = new Vector3(-10.0f, 10.0f, 10.0f);
            vertices[35] = new Vector3(10.0f, -10.0f, 10.0f);
 
            GL.GenBuffers(1, out vbo);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
                                   new IntPtr(vertices.Length * Vector3.SizeInBytes),
                                   vertices, BufferUsageHint.StaticDraw);
 
        }
 
        void CreateShaders()
        {
            shaderProgramHandle = GL.CreateProgram();
 
            vertexShaderHandle = GL.CreateShader(ShaderType.VertexShader);
            fragmentShaderHandle = GL.CreateShader(ShaderType.FragmentShader);
 
            GL.ShaderSource(vertexShaderHandle, vertexShaderSource);
            GL.ShaderSource(fragmentShaderHandle, fragmentShaderSource);
 
            GL.CompileShader(vertexShaderHandle);
            GL.CompileShader(fragmentShaderHandle);
            Console.WriteLine(GL.GetShaderInfoLog(vertexShaderHandle));
            Console.WriteLine(GL.GetShaderInfoLog(fragmentShaderHandle));
 
            GL.AttachShader(shaderProgramHandle, vertexShaderHandle);
            GL.AttachShader(shaderProgramHandle, fragmentShaderHandle);
            GL.LinkProgram(shaderProgramHandle);
            Console.WriteLine(GL.GetProgramInfoLog(shaderProgramHandle));
            GL.UseProgram(shaderProgramHandle);
 
            MVPLocation = GL.GetUniformLocation(shaderProgramHandle, "MVP");
        }
 
        protected override void OnLoad(EventArgs e)
        {
            GL.ClearColor(Color.Black);
            GL.Enable(EnableCap.DepthTest);
            GL.DepthFunc(DepthFunction.Less);
 
            CreateVertexBuffer();
            CreateShaders();
        }
 
        protected override void OnRenderFrame(FrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
            Matrix4 Projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45.0f), 4.0f / 3.0f, 0.1f, 100f);
            Matrix4 View = Matrix4.LookAt(new Vector3(40, 30, -30), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
            Matrix4 Model = Matrix4.Identity;
            Matrix4 MVP = Projection * View * Model;
 
            GL.UniformMatrix4(shaderProgramHandle, false, ref MVP);
 
            GL.EnableVertexAttribArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
 
            GL.DrawArrays(BeginMode.Triangles, 0, 12*3);
 
            GL.DisableVertexAttribArray(0);
 
            SwapBuffers();
        }
 
        public static void Main()
        {
            using (Program p = new Program())
            {
                p.Run(60);
            }
        }
    }
}

Comments

Comment viewing options

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

It´s strange, if I change the BeginMode.Triangles in GL.DrawArrays by BeginMode.Points a single point is drawn in the middle of the window.