Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 12 Texture Mapping uploading of the texture to the video memory the application of the texture onto geometry.

Similar presentations


Presentation on theme: "1 Lecture 12 Texture Mapping uploading of the texture to the video memory the application of the texture onto geometry."— Presentation transcript:

1 1 Lecture 12 Texture Mapping uploading of the texture to the video memory the application of the texture onto geometry

2 2 Main steps Task: we have a raw RGB image data in a buffer and we want to apply it to our geometry in OpenGL First thing you have to do before OpenGL can use this raw texture data is upload it to the video memory. Once a texture is uploaded to the video memory it can be used throughout the time in which your application is running. Before a texture can be uploaded to the video memory there is some setup that must take place so OpenGL knows what to do with the image data that is passed to it.

3 3 Main Steps Below certain calls are given that need to be made in order to upload your texture. Note that these calls should be made once per texture when the application is started. The order is important.

4 4 Uploading the texture 1. Call glBindTexture. It tells OpenGL which texture "id" we will be working with. glBindTexture Example: glBindTexture (GL_TEXTURE_2D, 15); A texture "id" is just a number that you will use to access your textures. This call will make texture that is associated with the id of 15 the active texture. Any calls that have to do with OpenGL texture mapping will effect this texture.

5 5 Uploading the texture 2. Call glPixelStorei. It tells OpenGL how the data that is going to be uploaded is aligned. Recommended parameters are given below. Example: glPixelStorei (GL_UNPACK_ALIGNMENT, 1);

6 6 Uploading the texture 3. Call glTexParameteri. It sets the various parameters for the current OpenGL texture. (The parameters that are passed and their effects on the texture are an advanced topic.) Follow the example. Example: glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

7 7 Uploading the texture 4. Call glTexEnvf. It sets environment variables for the current texture, i.e., it tells how the texture will act when it is rendered into a scene. Example: glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); The GL_MODULATE attribute allows you to apply effects such as lighting and coloring to your texture. If you do not want lighting and coloring to effect your texture and you would like to display the texture unchanged when coloring is applied replace GL_MODULATE with GL_DECAL.

8 8 Uploading the texture 5. Call glTexImage2D. It uploads the texture to the video memory where it will be ready for us to use in our programs. Example: glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData); Parameters: 1) target - The target of this call, it will always be GL_TEXTURE_2D. 2) level - The level of detail number, this should be left at 0 for our purposes.

9 9 Uploading the texture 5. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData); Parameters: 3) internalformat - Internal components parameter. This tells OpenGL how many color components to represent internally from the texture that is uploaded. There are many symbolic constants for this parameter but the one which is most widely used is GL_RGB. 4 & 5) width & height - The width and height of the image data. These must be integers that are equal to 2n+2(border) for some integer n. What this basically means is that the texture width and height must be a power of two (2,4,8,16,32,63,128,256,512, etc).

10 10 Uploading the texture 5. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData); Parameters: 6) border - Image border, must be 0 or 1. 1 if you want a real border. 7) format - Format of the pixel data that will be uploaded. There are many constants which are accepted but GL_RGB is the value that is widely used.

11 11 Uploading the texture 5. glTexImage2D (GL_TEXTURE_2D, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData); Parameters: 8) type - Type of data that will be uploaded. Again there are several symbolic constants but the most used is GL_UNSIGNED_BYTE. 9) pixels - Pointer to the image data. This is the image data that will be uploaded to the video memory. Note that after your call to glTexImage2D you can free this memory since the texture is already uploaded into video memory.

12 12 Uploading the texture After you have done everything above the texture will me uploaded and ready to be applied to your geometry. The next section will discuss the application of the texture to geometry.

13 13 Applying a texture to geometry The process for applying a texture to geometry greatly depends on what type of data you are dealing with and how you would like things to run.

14 14 Applying a texture to geometry Remarks: 1 Make sure that you have enabled texturing. You do this with the glEnable (GL_TEXTURE_2D) call. 2 Make sure that you bind to a texture before you do any sort of glBegin/glEnd. You cannot bind to a texture in the middle of a begin/end pair. 3 Make sure that you specify a texture coordinate before each vertex that makes up a face. If you have 3 vertices the pattern for texture mapping the triangle would go like this: TexCoord; VertexCoord; TexCoord; VertexCoord; TexCoord; VertexCoord;

15 15 Applying a texture to geometry Remarks: 4 Make sure that you store your texture id's in variables, it makes things easier. 5 Make use of glGenTextures. It is an easy way to get a free texture id.

16 16 Applying a texture to geometry Example: how to texture a quad. This code assumes that texturing has been enabled and that there has been a texture uploaded with the id of 15. glBindTexture (GL_TEXTURE_2D, 15); glBegin (GL_QUADS); glTexCoord2f (0.0, 0.0); glVertex3f (0.0, 0.0, 0.0); glTexCoord2f (1.0, 0.0); glVertex3f (10.0, 0.0, 0.0); glTexCoord2f (1.0, 1.0); glVertex3f (10.0, 10.0, 0.0); glTexCoord2f (0.0, 1.0); glVertex3f (0.0, 10.0, 0.0); glEnd ();

17 17 Applying a texture to geometry Texture coordinates Remark: if we map a texture to a triangle, we have to specify three vertices of the texture.


Download ppt "1 Lecture 12 Texture Mapping uploading of the texture to the video memory the application of the texture onto geometry."

Similar presentations


Ads by Google