Pages

Setting up OpenTK

I am going to try and make this as simple as i can since I'm new to making tutorials so hopefully everything goes well.

I'm using Visual Studio as my IDE, If you don't use it i recommend you do so everything is relevant to this tutorial, You can download Visual Studio Express C# from the link below.
Visual Studio Express

Next you will need OpenTK from their website, I recommend downloading a nightly build so you get all the newest features and help with issue tracking if you are into that.
OpenTK Website, Latest Build

Setting up the Project
Open up your version of Visual Studio and click New Project on the left pane
or (File > New > Project).

In this window you want to select the default 'Windows Forms Application' if not already selected,
Then name the project, Alter the location if you so choose, And click OK.

Now we have the project ready to go, the first thing you want to do is Right-Click References in the right pane, and select Add Reference this will open a new window, Click the leftmost tab '.Net' and scroll till you see OpenTK (May need to load before it shows up) there are two other references here
OpenTK.Compatibility and OpenTK.GLControl don't bother with these they wont be needed.

Now that the reference is added to are list we can go ahead Right-Click Form1.cs > Delete as we are not going to be using the windows forms system, with that out of the way we can start coding :D

Initial Window Setup
Ok in your Solution Explorer Right-Click your project and Add > Class this will open a window, where you can name the class, I'm going to name it Main.cs and then click Add it will show up in the Solution Explorer.

Onto Program.cs where we will reference main and get the program running, below is how it should look.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace Tutorial
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            using (Main main = new Main())
            {
                main.Run(60.0);
            }
        }
    }
}  
The parameter 60.0 in Run() is for OpenTK.GameWindow - how many intervals before we update, This is are Updates Per-Second not Frames Per-Second we don't want to limit FPS.
Now you can save Program.cs (CTRL+S) and open up Main.cs where we get to the guts of are GL program.

Are Main class is pretty bare bones at the moment there are a few things we need to do here.
reference OpenTK and extending the GameWindow, initializing as a 800x600 window.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
namespace Tutorial
{
    class Main : GameWindow
    {
        public Main()
            : base(800, 600)
        {
            Title = "My OpenTK Window";
        }
    }
}

Now we override some methods from the base class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace Tutorial
{
    class Main : GameWindow
    {
        public Main()
            : base(800, 600)
        {
            Title = "My OpenTK Window";
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //This is where you load all of your content
            //Images, Models, Fonts, Sounds.
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            //When the NativeWindow is resized this is called
            //This is where we define are viewport and aspect ratio
        }

        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
            //This is where you handle all of your game logic
            //Jumping, Running, Attacking, Playing Sounds...
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);
            //And this is where you render all of the images
            //models, fonts or anything that you want to show onscreen
        }
    }
}

Basics and Viewport
Here is a basic setup, drawing a quad.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;

namespace Tutorial
{
    class Main : GameWindow
    {
        public Main()
            : base(800, 600)
        {
            Title = "My OpenTK Window";
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //This is the base color of are renderer
            //this color will fill the buffer after clearing it in OnRenderFrame
            GL.ClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            //Setting up a viewport using the native windows Width and Height
            //you can have multiple viewports for splitscreen but for now just one.
            GL.Viewport(0, 0, Width, Height);

            //Get the aspect ratio of the screen
            double aspect_ratio = Width / (double)Height;
            //Field of view of are camera
            float fov = 1.0f;
            //The nearest the camera can see, want to keep this number >= 0.1f else visible clipping ensues
            float near_distance = 1.0f;
            //The farthest the camera can see, depending on how far you want to draw this can be up to float.MaxValue
            float far_distance = 1000.0f;
            
            //Now we pass the parameters onto are matrix
            OpenTK.Matrix4 perspective_matrix =
               OpenTK.Matrix4.CreatePerspectiveFieldOfView(fov, (float)aspect_ratio, near_distance, far_distance);

            //Then we tell GL to use are matrix as the new Projection matrix.
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref perspective_matrix);
        }

        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            //Clear the current buffers
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            //Swap to the modelview so we can draw all of are objects
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();

            //Now we draw something fancy... a quad
            //before that we have to set the coords they are currently (0, 0, 0) and are object
            //will not be visible to us.
            GL.Translate(0, 0, -5);

            //Set the current color to red in this case
            GL.Color3(System.Drawing.Color.Red);

            //Tell GL that we are going to start drawing
            GL.Begin(BeginMode.Quads);
            //now we setup the vertices in counter clockwise order since that
            //is GL's default.
            GL.Vertex2( 1,  1);
            GL.Vertex2(-1,  1);
            GL.Vertex2(-1, -1);
            GL.Vertex2( 1, -1);
            GL.End();

            //Present to the front buffer
            SwapBuffers();
        }
    }
}

And i believe that's all it takes to a basic setup.
:D Hope it helped.

Here are some link's for the OpenTK documentation and API reference.

2 comments :

  1. Why use GlBegin() and GlEnd()?

    Please use Shader ( new version of opengl es or normal )

    Thanks!

    ReplyDelete
  2. Why use GlBegin() and GlEnd()?

    Please use Shader ( new version of opengl es or normal )

    Thanks!

    ReplyDelete