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". In the "Solution Explorer" pane, rightclick "References" and select to "Add Reference". In the Browse tab, select OpenTK and click OK.
OpenTK depends on System.Drawing, so you also need to add a reference to System.Drawing.dll. In the "Solution Explorer" pane, rightclick "References" and select to "Add Reference". In the .NET tab, select System.Drawng and click OK.
Now open the folder you just installed OpenTK to. Inside, you will find a file called Source\QuickStart\Game.cs. It contains a basic framework to get your started with OpenTK. Copy the contents of this file to the Program.cs file created by the IDE in your new project. Scroll to the bottom: the
Main() method is where everything begins.
Now, press F5 to run the project. A window with a colored triangle will show up - not very interesting, is it? Press escape to close it.
[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:
OnLoad: this is the place to load resources from disk, like images or music.
OnUpdateFrame: this is a suitable place to handle input, update object positions, run physics or AI calculations.
OnRenderFrame: this contains the code that renders your graphics. It typically begins with a call to
GL.Clear()and ends with a call to
OnResize: 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:
- 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.
- Make the triangle change colors when you press a key (OnUpdateFrame and OnRenderFrame methods).
- 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).
- Use a for-loop to render many triangles arranged on a plane (OnRenderFrame method).
- 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.
- 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:
Matrix4 classes for camera manipulations.
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.
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!