You Like My Tutorials? Click The Banner To Keep 'Em Coming!

glGameDeveloper(): The Game Developer's Source for OpenGL Coding
Main Menu
Latest News
Archived News
Tutorials
Links
E-Mail Me

Other
Message Board
My Guestbook

Tutorials -- Texture Mapping
 


Texture Mapping Polygons Page 3 of 5
< Previous Jump to Page: 1 | 2 | 3 | 4 | 5 Next >

Drawing Polygons With a Texture

glBindTexture(GL_TEXTURE_2D, TEX_TILE);
glEnable(GL_TEXTURE_2D);
glColor4f(1, 1, 1, 1);

glBegin(GL_POLYGON);
	glTexCoord3f( 0,  0, 0); glVertex3f(-5,  5,  0);
	glTexCoord3f( 1,  0, 0); glVertex3f( 5,  5,  0);
	glTexCoord3f( 1,  1, 0); glVertex3f( 5, -5,  0);
	glTexCoord3f( 0,  1, 0); glVertex3f(-5, -5,  0);
glEnd();

Lets go through this code step by step. First we make the texture active in OpenGL. Then we glEnable the GL_TEXTURE_2D value. (If you know GL_TEXTURE_2D is already enabled from any previous glEnable command, take this line out.) Then we set our color to white so our texture shows up normal. If you set the color to something else, the polygon will end up textured that color.

Finally, we begin drawing a GL_POLYGON. Before each vertex is set, we set the current texture coordinates. This tells OpenGL which corner of the texture belongs to each vertex. So for the top left vertex, we want the top left corner of the texture, top right gets top right, and so on. You can also set the texcoords to values greater than one, which will cause the texture to repeat that many times. For example, set all the texcoords that are 1 to 10, and the texture will be drawn 100 times over the polygon.

Texture Mapping Polygons Page 3 of 5
< Previous Jump to Page: 1 | 2 | 3 | 4 | 5 Next >

This site best viewed with MS Internet Explorer 5.0 at 16-bit color and at least 800x600.

All Contents of this site are Copyright 1999, 2000 Joe Zimmerman. No part of this site (code, HTML, etc.) may be reproduced without my permission.

Click Here!