Presentation is loading. Please wait.

Presentation is loading. Please wait.

Details of Texture Mapping Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, December 1, 2003.

Similar presentations


Presentation on theme: "Details of Texture Mapping Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, December 1, 2003."— Presentation transcript:

1 Details of Texture Mapping Glenn G. Chappell CHAPPELLG@member.ams.org U. of Alaska Fairbanks CS 381 Lecture Notes Monday, December 1, 2003

2 1 Dec 2003CS 3812 Review: Mapping Techniques [1/3] A number of rendering techniques come under the heading of “mapping”. Mapping generally refers to color generation at the fragment level, using some kind of look-up table, indexed by properties of the fragment. Note: An image can be a look-up table, right? Mapping techniques are a way of generating complex, detailed images cheaply. “Cheaply” has two meanings here. Not very many polygons. Without much work on the part of the modeler.

3 1 Dec 2003CS 3813 Review: Mapping Techniques [2/3] Texture Mapping We paint an image on a polygon. Each vertex gets “texture coordinates”. These are interpolated across each polygon. A fragment’s texture coordinates are used to look up its color in an image (a “texture”). More on texture mapping later. Environment Mapping This is a type of texture mapping in which texture coordinates are generated based on the normal vector. Our texture is a picture of the scene around the object. We compute texture coordinates so as to simulate mirror-like reflection.

4 1 Dec 2003CS 3814 Review: Mapping Techniques [3/3] Bump Mapping Here, our look-up table holds not colors but normals. After looking up a normal, we do the usual lighting computation at the fragment level. Result: a bumpy-looking surface. With a smooth-looking silhouette.  Since OpenGL does not do per-fragment lighting, it does not support bump mapping very well. On an OpenGL-based system, bump mapping can be done by: Using a shader language. Bypassing OpenGL and rendering an image ourselves.

5 1 Dec 2003CS 3815 Review: Texture Mapping [1/5] Again, a texture is an image that can be painted on a polygon. Uses of Texturing Allowing images to be placed in a 3-D world. Adding “cheap” detail to a scene. Rendering objects with a realistic appearance: Skin. Bricks on walls. Wood. Stone. Etc. … Special effects: Shadowing. A scene containing a movie projector. Simulating reflections (environment mapping).

6 1 Dec 2003CS 3816 Review: Texture Mapping [2/5] How Texturing Works The image and geometry pipelines work together. Textures are stored in the same way as normal images. Minor restriction: Width & height must be powers of 2. Each vertex is given two sorts of coordinates: Texture coordinates. Then, ordinary vertex coordinates. Texture coordinates tell where a vertex lies in the texture. During rasterization, fragment colors are taken from the texture image.

7 1 Dec 2003CS 3817 Review: Texture Mapping [3/5] FragmentsVertex Pixmap Fragment Pixmap Vertex Processing Rasterization Fragment Processing Frame Buffer Vertex Data Pixel Processing Pixel Data (1) Texture enters here …(2) … and stops here. (3) Geometry [vertex coordinates + texture coordinates] enters here. (5) The color of each fragment generated depends on its texture coordinates and the color in the texture at the corresponding spot. (6) Result: textured polygon. (4) Texture coordinates accompany a vertex, but are not affected by model/view, projection.

8 1 Dec 2003CS 3818 Review: Texture Mapping [4/5] Texture coordinates are specified inside glBegin - glEnd pairs. Specify texture coordinates with glTexCoord *. Then, specify ordinary vertex coordinates with glVertex *. Example: glBegin(GL_TRIANGLES); glTexCoord2d(0., 0.); glVertex(2., 5.); glTexCoord2d(1., 0.); glVertex(3., 7.); glTexCoord2d(0., 1.); glVertex(1., 10.); glEnd();

9 1 Dec 2003CS 3819 Review: Texture Mapping [5/5] Texture coordinates are not transformed by the model/view, projection, and viewport transformations. However, they do pass through the texture transformation. This transformation is handled just like model/view and projection. Matrix mode is GL_TEXTURE.

10 1 Dec 2003CS 38110 Details of Texture Mapping: Introduction & Notes Now we look at texturing in more detail. Resources: Sample code is on the web page ( simpletexture.cpp, texture.cpp ). You may also want to look at the red book, chapter 9. The designers of OpenGL revised texturing in the 1.1 release. We will discuss the revised interface. Thus, our sample code will not work under OpenGL 1.0. We will concentrate on 2-D texturing. OpenGL actually supports 1-D, 2-D, and 3-D textures. 3-D textures require OpenGL 1.2 or later. Or extensions to OpenGL 1.1. 1-D & 3-D are handled similarly to 2-D.

11 1 Dec 2003CS 38111 Details of Texture Mapping: Overview Topics Initializing textures (brief summary). Details of texture initialization: Using texture objects. Creation. Binding. Setting Texture-Related States. Function glTexParameter *. Function glTexEnv *. Telling OpenGL about a texture: glTexImage2D. Drawing with a texture.

12 1 Dec 2003CS 38112 Details of Texture Mapping: Initializing Textures To initialize textures (in the init function?): Put the image(s) into an array or arrays. Use glPixelStore * to set unpack alignment, if required. This is just like what you did with glDrawPixels, except that the array dimensions must be powers of 2. Create a texture object or objects. Use glGenTextures and glBindTexture. These work much like display lists. This is not actually needed if there is only one texture, but it simplifies things if & when you add a second texture. Indicate the properties of the texture object. Use glTexParameter * & glTexEnv * to set various states. Use glTexImage2D to turn the array data into a texture.

13 1 Dec 2003CS 38113 Details of Texture Mapping: Texture Objects — Creation Texture objects are not needed if there is only one texture. But use them anyway. Texture objects are created using glGenTextures. Parameters: Number of texture names to create. Array of GLuint ’s (or GLuint * ) to hold the names. Example Globals: const int num_textures = 7; // Number of textures GLuint texnames[num_textures]; In init function: glGenTextures(num_textures, texnames);

14 1 Dec 2003CS 38114 Details of Texture Mapping: Texture Objects — Binding To use a texture object, we bind it to a target. Target = GL_TEXTURE_2D when we use 2-D textures. Something like this: glBindTexture(GL_TEXTURE_2D, texnames[2]); After this, we refer to the texture by its target. All references to the target actually refer to the named texture object. So, if you have just one texture, then generate one name, bind it, and forget about it. If you have multiple textures: Generate all the names. For each texture, bind it, then initialize it. Just before using a texture (in display routine), bind it.

15 1 Dec 2003CS 38115 Details of Texture Mapping: States — glTexParameter * The most important command for setting texture-related states is glTexParameter *. We mainly use glTexParameteri. See simpletexture.cpp and texture.cpp for examples. The “wrap” parameters determine what happens when a texture coordinate is greater than 1 or less than 0. Coordinates are s and t. Use GL_REPEAT to repeat the image again: from 1 to 2, 2 to 3, etc. Using GL_CLAMP means coordinates greater than 1 are set to 1; coordinates less than 0 are set to 0. The “mag filter” and “min filter” parameters determine what happens when a texture lookup does not hit a texel exactly. Use GL_NEAREST to get the color of the nearest texel. Use GL_LINEAR to lirp between texel colors. Look in the doc’s for glTexParameter * for more.

16 1 Dec 2003CS 38116 Details of Texture Mapping: States — glTexEnv * Another function for setting texture states is glTexEnv *. It determines how the color in a texture image affects the color of a fragment. We will call it as follows: glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, parameter_value ); Options for parameter_value above: Option GL_REPLACE does the obvious thing: the fragment color is the color is what is read from the texture image. Option GL_MODULATE multiplies the texture image color by the color from the geometry pipeline to get the fragment color. Look in the doc’s for glTexEnv * for more.

17 1 Dec 2003CS 38117 Details of Texture Mapping: Using glTexImage2D [1/3] We tell OpenGL about the texture itself using glTexImage2D. This command deals with an image in an array, just like glDrawPixels and glReadPixels. So it needs the same 5 parameters to describe the image array. Width, height, format (RGB, RGBA, etc.), type ( GLubyte ?), pixels (pointer to array). Once we have called glTexImage2D, OpenGL is done with our array. So we can reuse it for another texture. There is also glTexImage1D and (in OpenGL 1.2 or later) glTexImage3D. These work much the same as the 2-D version.

18 1 Dec 2003CS 38118 Details of Texture Mapping: Using glTexImage2D [2/3] Function glTexImage2D has 9 parameters: Target: must be GL_TEXTURE_2D. I do not know the reason for this redundancy. Level: Use zero … … unless you are giving the image data in multiple resolutions (read about it in the Red Book). Components: What to do with the data ( GL_RGB or GL_RGBA ). Width: image width, as in glDrawPixels. Height: image height, as in glDrawPixels. Border: Use zero … … unless the image has a border (read about it in the Red Book). Format: as in glDrawPixels ( GL_RGB or GL_RGBA ). Type: as in glDrawPixels ( GL_UNSIGNED_BYTE, probably). Pixels: pointer to image data, as in glDrawPixels.

19 1 Dec 2003CS 38119 Details of Texture Mapping: Using glTexImage2D [3/3] Summary: Your call to glTexImage2D will look like this, unless you are feeling brave: glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, // GL_RGBA if alpha stored in array img_width, img_height, 0, GL_RGB, // GL_RGBA if alpha stored in array GL_UNSIGNED_BYTE, the_image); // <-- Name of your array here

20 1 Dec 2003CS 38120 Details of Texture Mapping: Drawing With a Texture To use a texture (in the display function): Enable the texture. Do glEnable(GL_TEXTURE_2D); If you have multiple textures, you may also need to call glBindTexture, to specify which texture to use. When you draw, specify texture coordinates. Do glTexCoord2 * before each glVertex *. The call to glTexCoord * must come before glVertex *. Reason: glVertex * triggers the sending of vertex data to the pipeline. Only states set before this call affect the data that is sent. If you are going to draw something without texturing (like text instructions?), disable the texture. Do glDisable(GL_TEXTURE_2D);


Download ppt "Details of Texture Mapping Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, December 1, 2003."

Similar presentations


Ads by Google