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]
Open the zip you downloaded and extract it to a folder of your choosing. I usually create a 'Projects' folder on my desktop or in my documents but any folder will do.
[Step 2: Use OpenTK]
Open the folder you just extracted. Inside, you will find three solutions: OpenTK.sln, Generator.sln and QuickStart.sln. The first two contain the OpenTK source code - no need to worry about them right now. The QuickStart solution is what we are interested in.
Double-click QuickStart.sln. This will launch your .Net IDE (don't have a .Net IDE? Check out MonoDevelop or Visual Studio Express).
Take a few moments to take in the contents of the QuickStart project:
- Game.cs: this contains the code for the game. Scroll to the bottom: the
Main()method is where everything begins. - References: click on the '+' sign to view the project references. The 'OpenTK' reference is the only one you need in order to use OpenTK.
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 toGL.Clear()and ends with a call toSwapBuffers.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: 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.
[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!
- Printer-friendly version
- Login or register to post comments


Comments
Re: Chapter 0: Learn OpenTK in 15'
Great tutorial!
(Actually it got me thinking: how do I create a fixed-size GameWindow..? Eg. important if creating a retro-game/platformer. Also important if you have a fixed-aspect-ratio heads-up display in 3d games, eg. a cockpit in an airplane. Is it possible? Haven't tested GameWindow in a couple of months now..)
Re: Chapter 0: Learn OpenTK in 15'
Use the WindowBorder property:
WindowBorder = WindowBorder.Fixed;You can also disable the border completely with
WindowBorder.Hidden. In general, this is not a good idea but some applications have managed to make it work: Google Chrome and the ZSNES emulator, for example.Re: Chapter 0: Learn OpenTK in 15'
I want to start out by saying thanks to all that have made OpenTK possible and the above is a great tutorial....However I have encountered issues =( I followed the instructs after downloading (if it helps any what I DLed was "opentk-1.0.0-rc1.zip"). When I got to step 2, I double-clicked QuickStart.sln as instructed it launched my .Net IDE (which is MonoDevelop) and it seemed all was going according to plan. Well, when I took a look at the 'OpenTK' reference, it was highlighted in red and had a yellow triangle with an "!" in it with the words "Assembly not found"....Me, being who I am, went ahead and ran it (F5) for a better explaination ...Here's what I got for the build output.
Building: QuickStart (Debug)
Building Solution QuickStart
Building: QuickStart (Debug)
Performing main compilation...
/usr/bin/gmcs "/out:/home/demidione/Downloads/opentk-1.0.0-rc1/Binaries/QuickStart/Debug/QuickStart.exe" "/r:System.dll" "/r:System.Drawing.dll" /noconfig /nologo /warn:4 /debug:+ /debug:full /optimize- /codepage:utf8 /define:"DEBUG;TRACE" /t:winexe "/home/demidione/Downloads/opentk-1.0.0-rc1/Source/QuickStart/Game.cs"
Compilation failed: 6 error(s), 0 warnings
/home/demidione/Downloads/opentk-1.0.0-rc1/Source/QuickStart/Game.cs(5,7): error CS0246: The type or namespace name `OpenTK' could not be found. Are you missing a using directive or an assembly reference?
/home/demidione/Downloads/opentk-1.0.0-rc1/Source/QuickStart/Game.cs(6,7): error CS0246: The type or namespace name `OpenTK' could not be found. Are you missing a using directive or an assembly reference?
/home/demidione/Downloads/opentk-1.0.0-rc1/Source/QuickStart/Game.cs(7,7): error CS0246: The type or namespace name `OpenTK' could not be found. Are you missing a using directive or an assembly reference?
/home/demidione/Downloads/opentk-1.0.0-rc1/Source/QuickStart/Game.cs(8,7): error CS0246: The type or namespace name `OpenTK' could not be found. Are you missing a using directive or an assembly reference?
/home/demidione/Downloads/opentk-1.0.0-rc1/Source/QuickStart/Game.cs(9,7): error CS0246: The type or namespace name `OpenTK' could not be found. Are you missing a using directive or an assembly reference?
/home/demidione/Downloads/opentk-1.0.0-rc1/Source/QuickStart/Game.cs(10,7): error CS0246: The type or namespace name `OpenTK' could not be found. Are you missing a using directive or an assembly reference?
Build complete -- 6 errors, 1 warning
---------------------- Done ----------------------
Assembly 'OpenTK' not found. Make sure that the assembly exists in disk. If the reference is required to build the project you may get compilation errors.
Build: 6 errors, 1 warning
Can anyone help me figure out exactly what's going on at this point and how I can go about fixing the problem? Thanks in advance =)
Re: Chapter 0: Learn OpenTK in 15'
Try adding the reference manually (right click on references -> add reference and navigate to opentk-1.0.0-rc1/Binaries/OpenTK/Release/OpenTK.dll). This should work correctly in the next release.
Edit: Quickstart.sln should also contain a file named "OpenTK.dll.config". If it does not, you should add this and set it to "copy to output directory" (you can find this file in the same directory as OpenTK.dll). Again, this should be fixed in the next release.
Re: Chapter 0: Learn OpenTK in 15'
Yay, it worked =)
Building: QuickStart (Debug)
Building Solution QuickStart
Building: QuickStart (Debug)
Performing main compilation...
/usr/bin/gmcs "/out:/home/demidione/Downloads/opentk-1.0.0-rc1/Binaries/QuickStart/Debug/QuickStart.exe" "/r:System.dll" "/r:System.Drawing.dll" "/r:/home/demidione/Downloads/opentk-1.0.0-rc1/Binaries/OpenTK/Release/OpenTK.dll" /noconfig /nologo /warn:4 /debug:+ /debug:full /optimize- /codepage:utf8 /define:"DEBUG;TRACE" /t:winexe "/home/demidione/Downloads/opentk-1.0.0-rc1/Source/QuickStart/Game.cs"
Build complete -- 0 errors, 0 warnings
---------------------- Done ----------------------
Build successful.
Ty Ty =)