OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter...
-
Upload
myra-morgan -
Category
Documents
-
view
325 -
download
4
Transcript of OpenGL Texturing. Content Texture target Texture environment Texture coordinate Texture parameter...
Content
Texture targetTexture environmentTexture coordinateTexture parameter (filters, wrap)Texture objectTexture transformationTexGen
MultitextureApplications
Light map 3D textures Projective texture Environment map Specular map
2Fall 2013 Revised
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 mappingDraw the scene, supplying both texture and geometric coordinates
Works only in RGB mode
3Fall 2013 Revised
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
2m + 2(border bit) w and h can be different
There are new extensions that removes this restriction
5Fall 2013 Revised
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
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
Texture Environment Modes
GL_REPLACEGL_MODULATE (default)GL_DECALGL_BLEND
New environment modes:GL_ADD: Cv = Cf + Ct
GL_COMBINE (ARB, see here)
8Fall 2013 Revised
GL_MODULATE
Color of polygon affects the display of texture
Tree: (r,g,b,a) acutout = 0Polygon: (1,0,0) 9Fall 2013 Revised
GL_BLEND
Use with:glTexEnvfv (GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, colorPtr)
Cc: texture environment color
10Fall 2013 Revised
GL_REPLACE
Appearance solely determined by texture
FOR TEXTURE CUT-OUTS
Tree: (r,g,b,a) acutout = 0Polygon: (1,0,0) 11Fall 2013 Revised
GL_DECAL
Cp: replace
RGB: DECAL=REPLACETree: (r,g,b,a) acutout = 0Polygon: (1,0,0) 12Fall 2013 Revised
Texture + Lighting
To show fragment color, use GL_MODULATEApply specular color AFTER texture mapping:
glLightModeli (GL_LIGHT_MODEL_COLOR_CONTROL, GL_SEPARATE_SPECULAR_COLOR);
See FAQ 21.040
GL_SINGLE_COLOR
GL_SEPARATE_SPECULAR_COLOR
13Fall 2013 Revised
Texture Coordinates
Texture coordinate: Associate texture location (in texture
space) with vertices in the polygonglTexCoord2i (s, t); glVertex2i (x, y);
Order cannot be reversed![Think of TexCoord as state
assignment]
14Fall 2013 Revised
Texture Coordinates of Quadrics
Most quadric primitives have default setting for texture coordinatesTo turn on the default setting: gluQuadricTexture
(qobj, GL_TRUE)
15Fall 2013 Revised
Deeper Look into Texturing
17
Each fragment got its texture coordinates from interpolationThen via table lookup, obtain its (interpolated) color values.
Fall 2013 Revised
rasterization
Polygon (in screen space) and texture
coordinates
(1,1)
(1,0)
(0,.75) (1,1)
(1,0)
(0,.75)
Texture map (4x4)
nearestlinear
Interpolate (s,t) and lookup (r,g,b,a) in the texture map
Filters
Texture Access and Lookup
18Fall 2013 Revised
Filters: Magnification & Minification
Nature of problem: Mismatch between texels and pixels
1
Pixels
TexelsPixels
Magnification
Minification
19Fall 2013 Revised
Options
MagnificationGL_NEARESTGL_LINEAR
MinificationGL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEARESTGL_LINEAR_MIPMAP_NEARESTGL_NEAREST_MIPMAP_LINEARGL_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
Magnification OptionsGL_LINEAR Interpolate the
texels More time
consuming but more accurate
GL_NEAREST Snap to the
nearest texel
21Fall 2013 Revised
MipmappingThe 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.
2m2n
m, n∈+
2m-12n-1
d = the Level Of Detail (LOD)
Level 0
Level 1
Level 2
Level 3
Level 0 Texture
If a pixel covers2222 texels,go to level 2
and get a texel.2121,level 1
Mipmapping was invented by Lance Williams in 1983 and is described in his paper Pyramidal parametrics.
24Fall 2013 Revised
Minification OptionsglTexParameteri( 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
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
Parameters for Wrapping
GL_CLAMP use the border
texel Diagonal: corner
texel
GL_REPEAT repeat the texels
in [0,1]
29Fall 2013 Revised
Texture Object
A texture object stores texture data and makes it readily availableYou can now control many textures and go back to textures that have been previously loaded into your texture resourcesUsing 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
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
texbind.c
Once created, a named texture may be re-bound to the target of the matching dimensionality as often as needed. It is usually 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
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
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
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
Automatic TEXture Coordinate GENeration
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
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
TexGen: Object/Eye_Linearfor a vertex with object coordinates (xo,yo,zo,wo), generated coordinate = p1xo + p2yo + p3zo + p4wo
Meaning: when p1, p2, p3 are normalized, this coord corresponds to signed distanceSimilarly, EYE_LINEAR applies to eye coordinates
(0,0,-1,0)(xo,yo,zo,wo)
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
Eye_linear (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’ = MV, let’s call V’ = (xe, ye, ze, we). 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’xe + B’ye + C’ze + D’we.
41Fall 2013 Revised
Eye_linear Summary
e
e
e
e
o
o
o
o
e
e
e
e
w
z
y
x
dcbatex
Mdcbadcba
M
w
z
y
x
M
w
z
y
x
coord
matrix modelview:,
1
42Fall 2013 Revised
SphereMap
Another mode of TexGen, SphereMap, will be explain in the “Environment Map” note.
43Fall 2013 Revised
Glew – Extension WranglerInclude <gl/glew.h> before <gl/glut.h>Remember to add glewInit() After the first glutWindow is created
Error MessageVersion 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
(First) ARB-approved extension (1998)Support up to 32 texture units*Texture matrix and texgen commands affect the active texture environment
ARB_multitexture (ref)
* glGetIntegerv (GL_MAX_TEXTURE_UNITS, &units); My platform has only 4! OpenGL 3.1 requires minimum of 16 texture units48Fall 2013 Revised
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
TexGen with MultitextureWhen 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
Dynamic Texture
CopyTexSubImage2DReplace a rectangular portion of 2D texture with pixels from the current READ_BUFFERThis 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
glTexSubImage2D
Replace part of the original image as new subimageWidth and size of image need not be 2n
E.g., dynamic texture
glTexSubImage2D (target, level, xoffset, yoffset, width, height, format, type, pixels)
glTexSubImage2D (target, level, xoffset, yoffset, width, height, format, type, pixels)
53Fall 2013 Revised
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); doc
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
Fall 2013 Revised 57
Light MapsQuake (雷神之錘 , id software) was the first computer game to use light map
Spherical specular map obtained on a highly tesselated sphere`
Apply this spheremap as “specular texture” on the teapot
Specular Map
59Fall 2013 Revised