{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


lecture19-1 - CS 445 645 Introduction to Computer Graphics...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 445 / 645 Introduction to Computer Graphics Lecture 19 Texture Maps Texture Mapping Limited ability to generate complex surfaces with Limited geometry geometry Images can convey the illusion of geometry Images painted onto Images polygons is called texture mapping mapping Texture Maps Chapter 9 of Open GL Programming Guide (Red Chapter Book) Book) Images applied to polygons to enhance the visual Images effect of a scene effect • Rectangular arrays of data – Color, luminance, alpha Color, alpha – Components of array called texels Components texels Or in 3D, volumetric voxels Texture Mapping Texture map is an image, two-dimensional array of color Texture values (texels) values Texels are specified by texture’s (u,v) space At each screen pixel, texel can be used to substitute a At polygon’s surface property (color) polygon’s We must map (u,v) space to polygon’s (s, t) space T V U S Texture Mapping (u,v) to (s,t) mapping can be explicitly set at (u,v) vertices by storing texture coordinates with texture each vertex each How do we compute (u,v) to (s,t) mapping for How points in between points • Watch for aliasing • Watch for many to one mappings • Watch for perspective foreshortening effects and linear Watch interpolation interpolation Example Texture Map Applied to tilted polygon Example Texture Map glVertex3d (s, s, s) glTexCoord2d(1,1); glVertex3d (-s, -s, -s) glTexCoord2d(0, 0); Example Texture Map glVertex3d (s, s, s) glTexCoord2d(5, 5); Repeating textures vs. Clamped textures glVertex3d (s, s, s) glTexCoord2d(1, 1); The Art of 3D Computer Animation and Effects Isaac Kerlow Texture Coordinates Every polygon can have object coordinates and Every texture coordinates texture • Object coordinates describe where polygon vertices are on Object the screen the • Texture coordinates describe texel coordinates of each Texture vertex (usually 0 -> 1) vertex • Texture coordinates are interpolated along vertex-vertex Texture edges edges glTexCoord{1234}{sifd}(TYPE coords) Why 14 coords? s, t, r, and q (for homogeneous coordinates, an advanced topic) Textures Texture Object • An OpenGL data type that keeps textures resident in memory and An provides identifiers to easily access them provides • Provides efficiency gains over having to repeatedly load and reload a Provides texture texture • You can prioritize textures to keep in memory • OpenGL uses least recently used (LRU) if no priority is assigned Example use of Texture Read .bmp from file • Use Image data type – getc() and fseek() to read image x & y size fseek() – fread() fills the Imagedata memory with actual data red/green/blue values from .bmp red/green/blue • Note – malloc() Image->data to appropriate size – .bmp stores color in bgr order and we convert to rgb order Step 2 – create Texture Objects glGenTextures(1, &texture[texture_num]); • First argument tells GL how many Texture Objects to First create create • Second argument is a pointer to the place where Second OpenGL will store the names (unsigned integers) of the Texture Objects it creates Texture – texture[ ] is of type GLuint Step 3 – Specify which texture object is about to be defined Tell OpenGL that you are going to define the Tell specifics of the Texture Object it created specifics • glBindTexture(GL_TEXTURE_2D, texture[texture_num]); – Textures can be 1D and 3D as well Step 4 – Begin defining texture glTexParameter() • Sets various parameters that control how a texture is treated as it’s Sets applied to a fragment or stored in a texture object applied • // scale linearly when image bigger than texture // glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR); • // scale linearly when image smaller than texture glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR); GL_LINEAR); Step 5 – Assign image data • glTexImage2D(); GL_TEXTURE_2D (2D Texture) 0 (level of detail 0) 3 (3 components, RGB) image1->sizeX (size) image1->sizeY (size) 0 (no border pixel) GL_RGB (RGB color order) GL_UNSIGNED_BYTE (unsigned byte data) image1->data (pointer to the data)) glTexImage2D – Arg 1 GLenum target • GL_TEXTURE_{1|2|3}D • GL_PROXY_TEXTURE_2D – Provides queries for texture resources – Proceed with hypothetical texture use (GL won’t apply Proceed the texture) the – After query, call GLGetTexLevelParamter to verify After presence of required system components presence – Doesn’t check possibility of multiple texture interference glTexImage2D – Arg 2 GLint level • Used for Level of Detail (LOD) • LOD stores multiple versions of texture that can be used at LOD runtime (set of sizes) runtime • Runtime algorithms select appropriate version of texture – Pixel size of polygon used to select best texture – Eliminates need for error-prone filtering algorithms glTexImage2D – Arg 3 GLint internalFormat • GL defines 38 symbolic constants that describe which of R, GL G, B, and A are used in internal representation of texels G, • Provides control over things texture can do – High bit depth alpha blending – High bit depth intensity mapping – General purpose RGB • GL doesn’t guarantee all options are available on given GL hardware hardware glTexImage2D – Args 4-6 GLsizei width GLsizei height • Dimensions of texture image – Must be 2m + 2b (b=0 or 1 depending on border) – min, 64 x 64 GLint border • Width of border (1 or 0) – Border allows linear blending between overlapping textures – Useful when manually tiling textures glTexImage2D – Args 7 & 8 GLenum format • Describe how texture data is stored in input array – GL_RGB, GL_RGBA, GL_BLUE… GLenum type GLenum • Data size of array components – GL_SHORT, GL_BYTE, GL_INT… glTexImage2D – Arg 9 Const GLvoid *texels • Pointer to data describing texture map Step 6 – Apply texture Before defining geometry • glEnable(GL_TEXTURE_2D); • glBindTexture(GL_TEXTURE_2D, texture[0]); • glTexEnvf(GL_TEXTURE_ENV, glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); GL_TEXTURE_ENV_MODE, glTexEnv() First argument to function is always GL_TEXTURE_ENV GL_TEXTURE_ENV_MODE GL_DECAL (alpha blends texture with GL_DECAL poly color) poly GL_REPLACE (straight up GL_REPLACE replacement) replacement) GL_MODULATE (texture application is GL_MODULATE a function of poly lighting) function GL_BLEND (texture controls blending GL_BLEND with another color) with If GL_BLEND selected, second call to If glTexEnv() must specify GL_TEXTURE_ENV_COLOR GL_TEXTURE_ENV_COLOR 4-float array for R,G,B,A blend gluScaleImage() Alters the size of an image to meet the 2m size requirement of OpenGL requirement • Scaling performed by linear and box filtering glCopyTexImage2D() Use current frame buffer contents as texture Copy frame buffer to named texture location glTexSubImage2D() Replace a region of current working texture with a Replace smaller texture smaller SubImage need not adhere to 2m size limitation This is how you add data from your system’s This camera to GL environment camera glCopyTexSubImage2D • Frame buffer cut and paste possible too Bump Mapping Use textures to modify surface geometry Use texel values to modify surface normals of Use polygon polygon Texel values correspond to height field Texel height • Height field models a rough surface Partial derivative of bump map specifies change Partial to surface normal to Bump Mapping Displacement Mapping Bump mapped normals are inconsistent with actual Bump geometry. Problems arise (shadows). geometry. Displacement mapping actually affects the surface Displacement geometry geometry Mipmaps multum in parvo -- many things in a small place A texture LOD technique texture Prespecify a series of prefiltered texture maps of Prespecify decreasing resolutions decreasing Requires more texture storage Eliminates shimmering and flashing as objects Eliminates move (why?) (why?) Shimmering in mipmaps How do I reduce the size of this image by half? far near MIPMAPS With versus without MIPMAP MIPMAPS Arrange different versions into one block of Arrange memory memory gluBuild2DMipmaps Automatically constructs a family of textures from Automatically original texture size down to 1x1 original Advanced Mipmaps You can specify additional mipmap levels on You the fly the • MIN_LOD may reduce popping • MAX_LOD may reduce over compression You can specify min mipmap level • Useful for mosaicing (Alphabet on a texture) Filtering OpenGL tries to pick best mipmap level Question: Which texel corresponds to a particular pixel? Question: Which GL_NEAREST (Point Sampling) • Pick the texel with center nearest pixel GL_LINEAR (Bilinear Sampling) • Weighted average of 2x2 closest texels GL_NEAREST_MIPMAP_LINEAR • Average nearest texels from two mipmap levels GL_LINEAR_MIPMAP_LINEAR (Trilinear) • Average two averaged texels from two mipmaps ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online