Wednesday, 17 August 2011

Adding a Texture

Adding a texture to the cube involves us loading a bitmap image and adding this to the cube object.

The last blog entry on OpenTK contained details of how to add rotation to a wireframe cube, this entry adds textures to this cube.

I used the "Open Toolkit" logo as my texture (you can use anything you like) a reason for using this is so that we can see how to rotate the texture on the cube within our syntax.

So here is the routine to load our texture, this simply loads the entire texture into the array "Texture[0]" at position "0", I am only loading a single texture but using an array you can load multiple textures for use on different objects.
The string variable "FileName" is a reference to the texture file:
String TextureName = "C:\\Code\\Blog3_Texture\\texture\\logo.jpg";
Call the loadtexture() method from the "glControl1_Load" method just after you have called SetupViewport()


Modify the render method to draw Polygonmode.Fill rather than Lines that we had previously and bind the texture to the object.


Now all that's left is to modify the Quads that we have so that we have GL.TexCoord2 to bind the texture to:



That's it we now have a cube object with a texture.

Now if you notice that any of the textures are not pointing in the right direction on your cube you can rotate them by changing the GL.TexCoord2 of the face.

Using just the front face so we can see the difference make the following change:


You should now see that the front face of the cube has rotated 90degrees, this is only the texture though.

A short entry will come next to change the code so that we have keyboard control over the rotation.

No comments:

Post a Comment