Download presentation
Presentation is loading. Please wait.
Published byMyra Morgan Modified over 9 years ago
1
OpenGL Texturing
2
Content Texture target Texture environment Texture coordinate Texture parameter (filters, wrap) Texture object Texture transformation TexGen Multitexture Applications Light map 3D textures Projective texture Environment map Specular map 2Fall 2013 Revised
3
Steps in OpenGL Texture Mapping Specify (create) the texture Set texture parameters: Indicate how the texture is to be applied to each pixel Enable texture mapping Draw the scene, supplying both texture and geometric coordinates Works only in RGB mode 3Fall 2013 Revised
4
Texture Targets glTexImage1D() glTexImage2D() glTexImage3D() 4Fall 2013 Revised
5
Two Dimensional Texture glTexImage2D (GLenum target, GLint level, GLint components, GLsizei width, GLsizei height, GLint border, GLenum format, GLenum type, const GLvoid *pixels) target : GL_TEXTURE_2D level : LOD number (0: base image) components : number of color components (1|2|3|4) Format : pixel data format Border: 0 or 1 Width & height 2 m + 2(border bit) w and h can be different There are new extensions that removes this restriction 5Fall 2013 Revised
6
glTexImage*D supplement In GL version 1.1 or greater, pixels may be a null pointer. In this case texture memory is allocated to accommodate a texture of width width and height height. You can then download subtextures to initialize this texture memory. The image is undefined if the user tries to apply an uninitialized portion of the texture image to a primitive. 6Fall 2013 Revised
7
Setting Texture Environment glTexEnv{if}{v}(GLenum target, GLenum pname, TYPEparam); Setting how textures are to be interpreted: Target : GL_TEXTURE_ENV Pname: GL_TEXTURE_ENV_MODE Param: modes (DECAL|REPLACE|MODULATE|BLEND|ADD|…) 7Fall 2013 Revised
8
Texture Environment Modes GL_REPLACE GL_MODULATE (default) GL_DECAL GL_BLEND New environment modes: GL_ADD: C v = C f + C t GL_COMBINE (ARB, see here)here 8Fall 2013 Revised
9
GL_MODULATE Color of polygon affects the display of texture Tree: (r,g,b,a) a cutout = 0 Polygon: (1,0,0) 9Fall 2013 Revised
10
GL_BLEND Use with: glTexEnvfv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, colorPtr) C c : texture environment color 10Fall 2013 Revised
11
GL_REPLACE Appearance solely determined by texture FOR TEXTURE CUT-OUTS Tree: (r,g,b,a) a cutout = 0 Polygon: (1,0,0) 11Fall 2013 Revised
12
GL_DECAL Cp: replace RGB: DECAL=REPLACE Tree: (r,g,b,a) a cutout = 0 Polygon: (1,0,0) 12Fall 2013 Revised
13
Texture + Lighting To show fragment color, use GL_MODULATE Apply specular color AFTER texture mapping: glLightModeli (GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR); See FAQ 21.040FAQ 21.040 GL_SINGLE_COLOR GL_SEPARATE_SPECULAR_COLOR 13Fall 2013 Revised
14
Texture Coordinates Texture coordinate: Associate texture location (in texture space) with vertices in the polygon glTexCoord2i (s, t);glVertex2i (x, y); Order cannot be reversed! [Think of TexCoord as state assignment] 14Fall 2013 Revised
15
Texture Coordinates of Quadrics Most quadric primitives have default setting for texture coordinates To turn on the default setting: gluQuadricTexture (qobj, GL_TRUE) 15Fall 2013 Revised
16
Ex: Textures on Quadrics 16Fall 2013 Revised
17
Deeper Look into Texturing 17 Each fragment got its texture coordinates from interpolation Then via table lookup, obtain its (interpolated) color values. Fall 2013 Revised
18
rasterization Polygon (in screen space) and texture coordinates (1,1) (1,0) (0,.75) (1,1) (1,0) (0,.75) Texture map (4x4) nearest linear Interpolate (s,t) and lookup (r,g,b,a) in the texture map Filters Texture Access and Lookup 18Fall 2013 Revised
19
Filters: Magnification & Minification Nature of problem: Mismatch between texels and pixels 19Fall 2013 Revised
20
Options Magnification GL_NEAREST GL_LINEAR Minification GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST GL_LINEAR_MIPMAP_NEAREST GL_NEAREST_MIPMAP_LINEAR GL_LINEAR_MIPMAP_LINEAR Chooses the mipmap that most closely matches the size of the pixel being textured Chooses the two mipmaps that most closely match the size of the pixel being textured Nearest: in Manhattan distance to the center of the pixel 20Fall 2013 Revised
21
Magnification Options GL_LINEAR Interpolate the texels More time consuming but more accurate GL_NEAREST Snap to the nearest texel 21Fall 2013 Revised
22
Problems of Minification Can use nearest neighbor or bilinear interpolation,but these two may cause severe aliasing problems Nearest NeighborBilinear Interpolation or ?? Texels Pixels No Influence 22Fall 2013 Revised
23
Problem with Minification without mipmap with mipmap 23Fall 2013 Revised
24
Mipmapping The most popular method of anti-aliasing for textures ‘Mip’ stands for “multum in parvo” = “many things in a small place” The texture is downsampled to a quarter of the original area. Mipmapping was invented by Lance Williams in 1983 and is described in his paper Pyramidal parametrics. 24Fall 2013 Revised
25
[Point Sampled Texture Aliasing] Note that the back row is a very poor representation of the true image Texture map Polygon far from the viewer in perspective projection Rasterized and textured 25Fall 2013 Revised
26
Minification Options glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR, GL_NEAREST, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_NEAREST_MIPMAP_NEAREST); Remarks: Mipmap selection is done per pixel Using not-yet-ready mipmap disables the texture mapping function. Remarks: Mipmap selection is done per pixel Using not-yet-ready mipmap disables the texture mapping function. 26Fall 2013 Revised
27
Mipmap Generation gluBuild2DMipmaps Storage overhead 27Fall 2013 Revised
28
Texture Wrap When the texture coordinates are outside [0,1] glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,param); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,param); param: GL_CLAMP|GL_REPEAT 28Fall 2013 Revised
29
Parameters for Wrapping GL_CLAMP use the border texel Diagonal: corner texel GL_REPEAT repeat the texels in [0,1] 29Fall 2013 Revised
30
Texture Object A texture object stores texture data and makes it readily available You can now control many textures and go back to textures that have been previously loaded into your texture resources Using texture objects is usually the fastest way to apply textures, resulting in big performance gains it is almost always much faster to bind (reuse) an existing texture object than it is to reload a texture image using glTexImage*D() 30Fall 2013 Revised
31
Texture Object API glGenTextures Allocate n textures (integer identifiers) glBindTexture Bind the current texture to specified identifier glDeleteTextures Free the allocated texture identifiers Reverse of glGenTextures 31Fall 2013 Revised
32
checker.c 32Fall 2013 Revised
33
texbind.c Once created, a named texture may be re-bound to the target of the matching dimensionality as often as needed. It isusually much faster to use glBindTexture to bind an existing named texture to one of the texture targets than it is to reload the texture image using glTexImage1D, glTexImage2D, or glTexImage3D. 33Fall 2013 Revised
34
Texture Objects Texture properties saved in texture objects: Minification and magnification filters, wrapping modes, border color and texture priority When a texture object is bound again, one may edit the contents of the bound texture object. Any commands that change the above properties will change the currently bound texture as well as the current texture state. Texture environment, texgen, etc. are NOT stored in texture objects These settings need to be repeated after texture binding. 34Fall 2013 Revised
35
Texture Loader (PNG, glpng.rar) Features Build mipmap Load and bind 2D textures Load image data (image loader) Handle transparent image (cut-out texture) Important API id = pngBind(filename, mipmap, trans, info, wrapst, minfilter, magfilter) pngSetStencil(red, green, blue) pngSetStandardOrientation(1) OpenGL: origin (0,0) is at lower left corner pngLoad(filename, mipmap, trans, info) TEXTURE ID IS RETURNED TO YOU BY PNG LOADER 35Fall 2013 Revised
36
Texture Transforms Texture coordinates are multiplied by a 4 by 4 matrix before any texture mapping occurs. Texture animation: using this, you can make the texture slide over the surface, rotate around it, stretch and shrink, … All matrix operations apply: Push/Pop/Mult/ … 36Fall 2013 Revised
37
Texture Suite cloud explode smoke water fire 37Fall 2013 Revised
38
Automatic TEX ture Coordinate GEN eration void glTexGen{ifd}{v}(GLenum coord, GLenum pname, TYPEparam); Coord: GL_S, GL_T, GL_R, GL_Q Pname: GL_TEXTURE_GEN_MODE Type: GL_OBJECT_LINEAR, GL_EYE_LINEAR, or GL_SPHERE_MAP Used when the tex coords are too cumbersome to specify, or will be changed with time. Texture environment, texgen, etc. are NOT stored in texture objects. 38Fall 2013 Revised
39
1D Texture According to Height Continuous color gradation (0,1/32,2/32, …, 31/32) Discrete white lines (0,0,0,1,0,0,0,1, … ) 39Fall 2013 Revised
40
TexGen: Object/Eye_Linear for a vertex with object coordinates (x o,y o,z o,w o ), generated coordinate = p 1 x o + p 2 y o + p 3 z o + p 4 w o Meaning: when p 1, p 2, p 3 are normalized, this coord corresponds to signed distance Similarly, EYE_LINEAR applies to eye coordinates (0,0,-1,0) (x o,y o,z o,w o ) Initially, all texture generation functions are set to GL_EYE_LINEAR and are disabled. Both s plane equations are (1, 0, 0, 0), both t plane equations are (0, 1, 0, 0), and all r and q plane equations are (0, 0, 0, 0). 40Fall 2013 Revised
41
Eye_linear (ref)ref Computing this distance in eye coordinates is a little tricky, since the plane and vertex must first be transformed to eye coordinates. if M is the modelview matrix at the time glTexGenfv( GLX, GL EYE PLANE, plane ) is called, then the transformed vertex V’ is V’ = M V, let’s call V’ = ( x e, y e, z e, w e ). The plane must also be transformed to get a new plane A’x + B’y + C’z + D’w = 0 We get (A’,B’,C’,D’) = (A,B,C,D)M −1 and M is the modelview matrix when glTexGen is invoked. The texture coordinate is then computed as A’x e + B’y e + C’z e + D’w e. 41Fall 2013 Revised
42
Eye_linear Summary 42Fall 2013 Revised
43
SphereMap Another mode of TexGen, SphereMap, will be explain in the “Environment Map” note. 43Fall 2013 Revised
44
Multi-Texture What this is about … Extension Wrangler (GLEW)GLEW 44Fall 2013 Revised
45
Glew – Extension Wrangler Include before Remember to add glewInit() After the first glutWindow is created
46
Error Message Version of glew library Sometimes you’ll see this error message when you run the program It is because the static library (in the EXE) and the DLL files are not the same version. Recompiling the application solves the problem
47
Usages of Multitexture 47Fall 2013 Revised
48
(First) ARB-approved extension (1998) Support up to 32 texture units* Texture matrix and texgen commands affect the active texture environment ARB_multitexture (ref)ref * glGetIntegerv (GL_MAX_TEXTURE_UNITS, &units); My platform has only 4! OpenGL 3.1 requires minimum of 16 texture units 48Fall 2013 Revised
49
Details OpenGL's multitexture support requires that every texture unit be fully functional and maintain state that is independent of any other texture units. Each texture unit has its own texture coordinate generation state, texture matrix state, texture enable state, and texture environment state. However, each texture unit within an OpenGL context shares the same set of texture objects. 49Fall 2013 Revised
50
Multi Texture 50Fall 2013 Revised
51
TexGen with Multitexture When the GL_ARB_multitexture extension is supported, glTexGen set the texture generation parameters for the currently active texture unit, selected with glActiveTexture. TexUnit0: texture2D;(s,t) given TexUnit1: texture1D; texGen 51Fall 2013 Revised
52
Dynamic Texture CopyTexSubImage2D Replace a rectangular portion of 2D texture with pixels from the current READ_BUFFER This involves a read- back from GPU to CPU. Can you think of a way to move the dynamic texture around?! A better way of doing it: Render-to-texture (RTT) with framebuffer object (FBO) 52Fall 2013 Revised
53
glTexSubImage2D Replace part of the original image as new subimage Width and size of image need not be 2 n E.g., dynamic texture glTexSubImage2D (target, level, xoffset, yoffset, width, height, format, type, pixels) 53Fall 2013 Revised
54
Other Related Functions void glCopyTexImage2D(target, level, internalformat, x, y, width, height, border); defines a two-dimensional texture image with pixels from the current GL_READ_BUFFER. void glCopyTexSubImage2D(target, level, xoffset, yoffset, x, y, width, height); docdoc replaces a rectangular portion of a two-dimensional texture image with pixels from the current GL_READ_BUFFER (rather than from main memory, as is the case for glTexSubImage2D). void glGetTexImage(target, level, format, type, void *pixels ); Get the content in the texture target into the array Similar to glCopyPixels 54Fall 2013 Revised
55
3D Textures 55Fall 2013 Revised
56
3D Perlin noise … 56Fall 2013 Revised
57
57 Light Maps Quake ( 雷神之錘, id software) was the first computer game to use light map
58
Fall 2013 Revised58
59
Spherical specular map obtained on a highly tesselated sphere` Apply this spheremap as “specular texture” on the teapot Specular Map 59Fall 2013 Revised
60
A more “cartoon-ish” specular map Fall 2013 Revised60
61
61Fall 2013 Revised
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.