Thursday, 4 August 2011

Adding an object

So from the last post we have a working OpenTK control called glControl1 which is just a simple black window, nothing very exciting but now we are going to add an object. I am not really a fan of the simple triangle as I want to get to the end stage where there are nice 3d objects, small steps are needed though.
Ok so one of the simplest objects is the humble cube :)
From the last post add a call to a new method called render() into the glControl1_Paint method like this

And create the following render() method, you will notice that I and Pushing and Popping the matrix quite a bit, this is so that I can change the location of each object I draw and always start from the same location, I will be using the GL.Translate(x,y,z); So far so good, we have a control and we are ready to add an object and view it.
Add the following 2 lines into the render() method where the comment is (right in the middle)


The GL.Translate command sets our starting position for the object, remember we are using perspective view so our viewpoint is straight down the center of the screen, and we are drawing the object in the distance by 10 units so that we can see the whole object.
The setupViewport() method set the z plane to "1f, 1000.0f" with 1f being the zNear and 1000.0f being the zFar distance that is visible.
Now add the DrawCube() method, I have taken this out of the render() method just so that the code is a little neater.

And there we have it a 3d cube drawn with OpenGL, dosent look much I know but its ready to be moved and rotated and generally modified :)
Try adding a rotation to the cube with the following command:

GL.Rotate(rotation1, Vector3.UnitX);
Add this to the render() method just after the translation and also add the event, details are in the OpenTK documentation, or I will add this into the next post :)
more to come....

No comments:

Post a Comment