Thursday, 4 August 2011

Getting started.

So how to start with OpenTK:

As I am very much a new programmer and I find some of the very easy and simple things a challenge I am going to assume nothing and include as many steps as I possibly can, you can always skip ahead if I bore you :)

Much of my information has come from the OpenTK website and following the articles and help along with regular help and advice from Robin.

The OpenTK getting started tutorial from the documentation is here: http://www.opentk.com/doc/chapter/2/glcontrol

I grabbed a copy of Visual Studio Express from the Microsoft website http://www.microsoft.com/visualstudio/en-us/products/2010-editions/visual-csharp-express
Installed and registered the product, its free.

Downloaded and installed OpenTK http://www.opentk.com/

There are 3 dll's that are needed which are installd to the "OpenTK\1.0\Binaries\OpenTK\Release" folder

OpenTK.Compatibility.dll OpenTK.dll OpenTK.GLControl.dll

Start Microsoft Visual C# 2010 Express and start a new "Windows Forms Application" Add References to the 3 dll's by right clicking on References and clicking Add reference which can be found under the Solutions Explorer window on the right of the screen. Click the browse tab and find the 3 dll's listed above, you can add multiple dll's at once by using either the Ctrl or the Shift key to multiple select.

If you have added the dll's and the OpenTK.GLControl.dll correctly you will find that there is now a GLControl available for use under the Toolbox/General option which can be found on the far left hand side of the main window. Add a new GLControl to your form and resize it to the size of your choice, this is your openGL window where all the magic will happen so its a good idea to make it a suitable size, its name can be seen in the properties window as glControl1.

Now double click on your glControl1 and you will be taken to the Form1.cs page and into the glControl1_load method, we are going to use this to enable our control and setup our viewpoint, but first we need to add the references for our dll's:

Add the following `using` statements to the top section of your code:

Now add the following code to your glControl1_Load method This is my SetupViewport method, this differs a little bit from the example in the documentation as I am using Projection perspective rather than the examples orthographic projection, there is a very good description of them here which is probably worth reading, there is a lot of maths involved but don't worry to much about it, just know that the positions of your object will need to be moved around until they appear in the visible area.

Now the paint method Finally we need to add the Paint event for the glControl1, to do this go back to the Form1 design view and click on the glControl1 object, now in the properties window (bottom right of the screen) click the event tab, scroll down to the Paint option and select the glControl1_paint method from the drop down list.

We now have an OpenGL control ready and waiting for us to draw something :)

No comments:

Post a Comment