|further reading||more topics »|
|programs||3D theory||physics||maths||software||technology||about site||sitemap|
This toturial was written for me by Imran Khan (email@example.com,
This is one of the most important section of this Tutorial.In this section we will learn how to map a bitmap on a 3D object.Here we will Map a bitmap to a cube, we already draw in our previous section.
First of all we will define a variable on the top.
This Variable will hold the texture u want to map. If u want to map one texture then the array size will be one.If u want different texture, array size can be increase. Like if you want to use 3 texture on a single object.Then the declaration will be like
" GLunit texture; "
Now we will write a function which will load a BMP.
This Method takes filesname as argument and return a pointer. First we check whether the file name specified or not. If not then return NULL. Next we open the Bitmap file for reading ("r").Then we check whether the file exist or not. If yes then close the file and return the data from the Bitmap.
Now we will write a method which will call the above method to load the BMP and convert it into Texture.
First we Define a simple variable which we will
use for True/False. Next we create an image record that we can store our
bitmap in. The record will hold the bitmap width, height, and data. Then
we Clear the image record.Then we write a condition in which it will load
a BMP and return its width,height,data (record) and assign this record
to a variable (image record) above.If all goes fine we will set the status
to TRUE.Then we call a method " glGenTextures(1,&texture)
" which is use to tells OpenGL that we want to generate one texture
name.If you want more texture then u can increase the number.Next line
"glBindTexture(GL_TEXTURE_2D, texture) " tells OpenGL to
bind the named texture texture to a texture
glTexImage2D got 9 arguments which are as follows :
The next two lines tell OpenGL what type of filtering to use when the image is larger (GL_TEXTURE_MAG_FILTER) or stretched on the screen than the original texture, or when it's smaller (GL_TEXTURE_MIN_FILTER) on the screen than the actual texture. GL_LINEAR makes the texture look smooth.
Next lines use to free up the Memory.
Now we will change the "InitGL" method. i.e. It will load texture in the beginning.
First we try to load the texture " LoadGLTexture " . If it fails to load the texture it will return False. Then we enable 2D texture mapping.Next 2 lines we already described in " My First OpenGL Program ". Next line "glEnable(GL_DEPTH_TEST) " enables the depth test..Then we set the prespective view and return TRUE.
Now the final method "DrawGLScene" ...
We are familiar with first 4 lines. In line " glBindTexture(GL_TEXTURE_2D, texture) " , We bind the texture. we use index ZERO, coz we load the texture at index ZERO in "LoadGLTextures" method. Then we begin drawing the cube. Then we use a function "glTexCoord2f" as the texture is 2D, it got 2 arguments. 1st is x-axis ,(0.0f is the left side of the texture. 0.5f is the middle of the texture, and 1.0f is the right side of the texture. 2nd argument is y-axis (0.0f is the bottom of the texture. 0.5f is the middle of the texture, and 1.0f is the top of the texture.)
Now first we describe " Front Face ". we define 4 vertex along with there texture co-ordinates. As we know coordinate for bottom left will be (0,0) [ x-axis = (0.0f is the left side of the texture) , y-axis = (0.0f is the bottom of the texture) ] .
In the same way we describe vertices for each Face.
Now when we run the program, u will get this output.
|Correspondence about this page|
Book Shop - Further reading.
Where I can, I have put links to Amazon for books that are relevant to the subject, click on the appropriate country flag to get more details of the book or to buy it from them.
Commercial Software Shop
Where I can, I have put links to Amazon for commercial software, not directly related to this site, but related to the subject being discussed, click on the appropriate country flag to get more details of the software or to buy it from them.
This site may have errors. Don't use for critical systems.