|
|
| 7.
TEXTURE MAPPING |
|
|
|
| Texture
Mapping
Basically, it is a process
to paste an image or texture onto the polygon.
Now, let's see how it is
done in OpenGL. |
|
|
1. |
Create a new project as in tutorial "3.
Start Programming With OpenGL", but when it comes at step
number 15, besides adding "glut32.lib" only, you also have
to add "glaux.lib". Put it in front of
"glut32.lib".
|
|
2. |
After you have finished all the step, and
begin to write your program, create a new folder inside the Project
folder. name it as "Texture".
|
|
3. |
Save this bitmap file named "rock.bmp"
into the "Texture" folder.
|
|
4.
|
Copy this program
inside your source file, then compile, build and run it.
|
|
5.
|
You should see this output after you
execute it:
Now, let's go through the program
walkthrough.
|
| 6. |
Firstly, at line 9,
include the GLAUX library, just below the include GLUT
statement.
|
| 7. |
At line 11, initialize
the storage for the texture. If only one texture is used, put 1.
If you use two textures, put the value 2 inside, and so on.
|
|
8.
|
Now, we jump straight to the Init
function in line 32. Inside it, we start with loading the texture
we want to used inside the program (from line 34 to line 54).
|
| 9. |
At line 35, AUX_RGBImageRec
is used to create the storage space for the texture. The value
inside depends on how many textures we want to used (same as
Step no. 7).
|
AUX_RGBImageRec
*TexImg[1]; |
|
| 10. |
At line 36, memset
function is used to set the pointer to null..
|
memset(TexImg,0,sizeof(void *)*1); |
|
| 11. |
At line 37, glGenTextures
function is used to generate the texture.
|
glGenTextures(1, texmap); |
|
| 12. |
At line 39, auxDIBImageLoad
function is used to load the bitmap file named
"rock.bmp" from Step no. 3, and store it inside
TexImg[0].
|
TexImg[0]= auxDIBImageLoad("Texture/rock.bmp"); |
|
| 13. |
At line 40, glBindTexture
function is used to create a texture object for a texture image.
|
glBindTexture(GL_TEXTURE_2D,
texmap[0]); |
|
| 14. |
At line 41 to 49, glTexImage2D
function is used to define a two-dimensional texture.
glTexImage2D(GL_TEXTURE_2D,
<--
The Target Parameter
0,
<--
The Level
3,
<--
The Internal
Format
TexImg[0]->sizeX,
<--
Width
Of Texture Image
TexImg[0]->sizeY,
<--
Height
Of Texture Image
0,
<--
Width
Of Border
GL_RGB,
<--
Texture
Image Format
GL_UNSIGNED_BYTE, <--
Texture
Image Data Type
TexImg[0]->data);
<--
Texture
Image Data |
|
| 15. |
At line 50 to 51, glTexParameteri
function is used to specify the minification and magnification
filtering methods.
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
|
|
| 16. |
At line 52 to 53, free
function is used to free the texture image memory and the image
structure.
free(TexImg[0]->data);
free(TexImg[0]);
|
|
| 17. |
At line 56, glEnable(GL_TEXTURE_2D)
function is used to enable the texture mapping. Without this,
all the preparation before this to do the texture mapping is
useless.
|
| 18. |
Now, we go to the Display
function in line 13. At line 20, we called the glBindTexture
function to use the texture we want to associate with the
object.
|
glBindTexture(GL_TEXTURE_2D,
texmap[0]); |
|
| 19. |
From line 22 to 25,
we called the glTexCoord2f function to set the current
texture coordinates. The texture coordinate will be associate
with the vertex.
|
glTexCoord2f(0.0f, 0.0f);
|
|
|
20.
|
How about if it's involved two or more
textures? Well, it just using the same process, with a little bit
of modifications. You can look and study at this extended program
which involved two textures. Before you run it, make sure you have
copy this second texture named "grass.bmp"
into the Texture folder.
|
|
21.
|
After you run it, you should see these
two squares sitting next to each other with different
textures:
|
|
22.
|
Now you can try experiment with
different types of texture mapping technique. This is just the
basic one for starting point. The texture mapping areas are really
huge, complex and exciting to be explore. Good luck!
|
|
|
|
|
PREVIOUS INDEX NEXT
|
|
|