Example #1
0
        private void SimpleCube_Load(object sender, EventArgs e)
        {
            DoubleBuffered = false;
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
            SetStyle(ControlStyles.Opaque, true);


            AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            Size          = new Size(800, 600);

            _WGLContext = Ratchet.Drawing.OpenGL.WGL.CreateContext(this);
            _Context    = Ratchet.Drawing.OpenGL.glContext.CreateContextFromWGLContext(_WGLContext);
            _Context.MakeCurrent();
            _Context.Enable(Ratchet.Drawing.OpenGL.glContext.Capability.GL_DEBUG_OUTPUT);
            _Context.DebugMessageCallback((int source, int id, int type, int severity, string message) =>
            {
                Console.WriteLine(message);
            });
            _VertexBuffer = _Context.GenBuffer();
            _VertexBuffer.BindBuffer(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ARRAY_BUFFER);
            float[] triangles = new float[] { -0.5f, -0.5f, -0.5f, 1.0f, // Vertex 0 Position
                                              1.0f, 1.0f,

                                              0.5f, -0.5f, -0.5f, 1.0f, // Vertex 1 Position
                                              0.0f, 1.0f,

                                              0.5f, 0.5f, -0.5f, 1.0f,  // Vertex 2 Position
                                              0.0f, 0.0f,

                                              -0.5f, 0.5f, -0.5f, 1.0f,  // Vertex 3 Position
                                              1.0f, 0.0f,

                                              -0.5f, -0.5f, 0.5f, 1.0f, // Vertex 4 Position
                                              1.0f, 1.0f,

                                              0.5f, -0.5f, 0.5f, 1.0f, // Vertex 5 Position
                                              0.0f, 1.0f,

                                              0.5f, 0.5f, 0.5f, 1.0f,  // Vertex 6 Position
                                              0.0f, 0.0f,

                                              -0.5f, 0.5f, 0.5f, 1.0f,  // Vertex 7 Position
                                              1.0f, 0.0f, };
            _VertexBuffer.BufferData(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ARRAY_BUFFER, sizeof(float) * triangles.Length, triangles, Ratchet.Drawing.OpenGL.glBuffer.Usage.GL_STATIC_DRAW);

            _IndexBuffer = _Context.GenBuffer();
            _IndexBuffer.BindBuffer(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ELEMENT_ARRAY_BUFFER);
            ///        7 --- 6
            ///       /     /|
            ///      3 --- 2 5
            ///      |     |/
            ///      0 --- 1
            _IndexBuffer.BufferData(Ratchet.Drawing.OpenGL.glBuffer.BindTarget.GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * 36, new int[] { 0, 1, 2, 0, 2, 3,
                                                                                                                                      4, 5, 6, 4, 6, 7,
                                                                                                                                      0, 1, 4, 1, 4, 5,
                                                                                                                                      2, 7, 3, 2, 7, 6,
                                                                                                                                      1, 6, 2, 1, 6, 5,
                                                                                                                                      0, 7, 3, 0, 7, 4, }, Ratchet.Drawing.OpenGL.glBuffer.Usage.GL_STATIC_DRAW);
            var vertexShader   = _Context.CreateShader(Ratchet.Drawing.OpenGL.glShader.Type.GL_VERTEX_SHADER, vertexShaderSource);
            var fragmentShader = _Context.CreateShader(Ratchet.Drawing.OpenGL.glShader.Type.GL_FRAGMENT_SHADER, fragmentShaderSource);

            _Shader = _Context.CreateProgram();
            _Shader.AttachShader(vertexShader);
            _Shader.AttachShader(fragmentShader);
            _Shader.LinkProgram();

            _Context.Viewport(0, 0, Width, Height);

            _Projection = mat4.Perspective(2.8f, (float)Width / (float)Height, 0.01f, 10.0f);
            _Lookat     = mat4.LookAt(new vec3(0.0f, 0.2f, -2.0f), new vec3(0.1f, 0.1f, 0.1f), new vec3(0.0f, 1.0f, 0.0f));

            _Context.Enable(Ratchet.Drawing.OpenGL.glContext.Capability.GL_DEPTH_TEST);
            _Context.DepthFunc(Ratchet.Drawing.OpenGL.glContext.DepthFuncion.GL_LEQUAL);
        }