Chapter 0: Learn OpenTK in 15'

So, you have downloaded the latest version of OpenTK - what now?

This is a short tutorial that will help you get started with OpenTK in 3 simple steps.

[Step 1: Installation]

Run the installer you downloaded. It will ask where you want OpenTK to be installed. Any folder will do.

[Step 2: Use OpenTK]

Create a new project in your .NET IDE (don't have a .NET IDE? Check out MonoDevelop or Visual Studio Express). Make it of type "Console Application".

Now, add a reference to OpenTK.dll: In your new project, right click "References" and select "Add Reference". Locate OpenTK.dll and add it.

Repeat this process for System.Drawing.dll. OpenTK depends on this.

Now you are ready to add some graphics! Open Program.cs and copy-paste the following code:

using System;
using System.Drawing;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;
 
namespace Example
{
    class MyApplication
    {
        [STAThread]
        public static void Main()
        {
            using (var game = new GameWindow())
            {
                game.Load += (sender, e) =>
                {
                    // setup settings, load textures, sounds
                    game.VSync = VSyncMode.On;
                };
 
                game.Resize += (sender, e) =>
                {
                    GL.Viewport(0, 0, game.Width, game.Height);
                };
 
                game.UpdateFrame += (sender, e) =>
                {
                    // add game logic, input handling
                    if (game.Keyboard[Key.Escape])
                    {
                        game.Exit();
                    }
                };
 
                game.RenderFrame += (sender, e) =>
                {
                    // render graphics
                    GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
 
                    GL.MatrixMode(MatrixMode.Projection);
                    GL.LoadIdentity();
                    GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 4.0);
 
                    GL.Begin(PrimitiveType.Triangles);
 
                    GL.Color3(Color.MidnightBlue);
                    GL.Vertex2(-1.0f, 1.0f);
                    GL.Color3(Color.SpringGreen);
                    GL.Vertex2(0.0f, -1.0f);
                    GL.Color3(Color.Ivory);
                    GL.Vertex2(1.0f, 1.0f);
 
                    GL.End();
 
                    game.SwapBuffers();
                };
 
                // Run the game at 60 updates per second
                game.Run(60.0);
            }
        }
    }
}

Build and run this application. You should see a window with a triangle inside. Press escape to exit.

[Step 3: Play]

Now it's time to start playing with the code. This is a great way to learn OpenGL and OpenTK at the same time.

Every OpenTK game will contain 4 basic methods:

  1. Load: this is the place to load resources from disk, like images or music.
  2. UpdateFrame: this is a suitable place to handle input, update object positions, run physics or AI calculations.
  3. RenderFrame: this contains the code that renders your graphics. It typically begins with a call to GL.Clear() and ends with a call to SwapBuffers.
  4. Resize: this method is called automatically whenever your game window changes size. Fullscreen applications will typically call it only once. Windowed applications may call it more often. In most circumstances, you can simply copy & paste the code from Game.cs.

Why don't you try modifying a few things? Here are a few suggestions:

  1. Change the colors of the triangle or the window background (OnLoad and OnRenderFrame methods). Hint: use GL.Color4() to control the triangle color and GL.ClearColor() to control the background color.
  2. Make the triangle change colors when you press a key (OnUpdateFrame and OnRenderFrame methods).
  3. Make the triangle move across the screen. Use the arrow keys or the mouse to control its position (OnUpdateFrame). Hint: use Matrix4.CreateTranslation() to create a translation matrix and call GL.LoadMatrix() to load it (OnRenderFrame).
  4. Use a for-loop to render many triangles arranged on a plane (OnRenderFrame method).
  5. Rotate the camera so that the plane above acts as ground (OnRenderFrame method). Hint: use Matrix4.LookAt() to create a modelview matrix and use GL.LoadMatrix() to load it.
  6. Use the keyboard and mouse to walk on the ground. Make sure you can't fall through it! (OnUpdateFrame and OnRenderFrame methods).

Some things you might find useful: Vector2, Vector3, Vector4 and Matrix4 classes for camera manipulations. Mouse and Keyboard properties for interaction with the mouse and keyboard, respectively. Joysticks property for interaction with joystick devices.

Don't be afraid to try things and see the results. OpenTK lends itself to explorative programming - even if something breaks, the library will help you pinpoint the cause of the error.

Check out the examples at: https://github.com/opentk/opentk/tree/master/Source/Examples

[Step: next]

There's a lot of functionality that is not visible at first glance: audio, advanced OpenGL, display devices, support for GUIs through GLControl... Then there's the subject of proper engine and game design, which could cover a whole book by itself.

Hopefully, you'll have gained a feel of the library by now and you'll be able to accomplish more complex tasks. You might wish to consult the complete documentation for the more advanced aspects of OpenTK and, of course, don't hesitate to post at the forums if you hit any roadblocks!


Comments

Comment viewing options

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

This is nice. However I have a question. I am working on Windows 7 but would like to create a graphics program that will run in both windows and Linux Desktop so I would like to try it with c# mono. When i try create a project in vs2008 using the sample here, it works and it runs successfully. But if i run the sample here in Xamarin Studio 4.0 (still in Windows 7), and when I run it, the compiler throws me an error which looks like its trying to look for x11 (libx11). What setting is missing when the project is loaded in Xamarin Studio. Is there a way to make it work seamless across desktop environments of windows and linux?

thank you

the Fiddler's picture

Yes, it is possible and fully supported. Please start a post in the forums :)

aronas's picture

VB Version (VS Express 2013)

Imports System
Imports System.Drawing
Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Imports OpenTK.Input
 
Module Main
    Private WithEvents game As New GameWindow
 
    Public Sub Main()
        game.Run(60.0)
    End Sub
 
    Private Sub game_Load(sender As Object, e As EventArgs) Handles game.Load
        game.VSync = VSyncMode.On
    End Sub
 
    Private Sub game_RenderFrame(sender As Object, e As FrameEventArgs) Handles game.RenderFrame
        GL.Clear(ClearBufferMask.ColorBufferBit Or ClearBufferMask.DepthBufferBit)
 
        GL.MatrixMode(MatrixMode.Projection)
        GL.LoadIdentity()
        GL.Ortho(-1.0, 1.0, -1.0, 1.0, 0.0, 4.0)
 
        GL.Begin(PrimitiveType.Triangles)
 
        GL.Color3(Color.MidnightBlue)
        GL.Vertex2(-1.0F, 1.0F)
        GL.Color3(Color.SpringGreen)
        GL.Vertex2(0.0F, -1.0F)
        GL.Color3(Color.Ivory)
        GL.Vertex2(1.0F, 1.0F)
 
        GL.End()
 
        game.SwapBuffers()
    End Sub
 
    Private Sub game_Resize(sender As Object, e As EventArgs) Handles game.Resize
        GL.Viewport(0, 0, game.Width, game.Height)
    End Sub
 
    Private Sub game_UpdateFrame(sender As Object, e As FrameEventArgs) Handles game.UpdateFrame
        If game.Keyboard(Key.Escape) Then game.Exit()
    End Sub
End Module