440 likes | 663 Views
Texture Mapping. Kurt Akeley CS248 Lecture 10 25 October 2007 http://graphics.stanford.edu/courses/cs248-07/. Texture mapping demo. Texture mapping. Paints images onto triangles Paints images onto points, lines, and other images. Complete OpenGL pipeline. Pixel pipeline. Application.
E N D
Texture Mapping Kurt Akeley CS248 Lecture 10 25 October 2007 http://graphics.stanford.edu/courses/cs248-07/
Texture mapping Paints images onto triangles Paints images onto points, lines, and other images
Complete OpenGL pipeline Pixel pipeline Application Vertex pipeline Pixel assembly(unpack) Vertex assembly Vertex operations Texture mapping ties the vertex and pixel pipelines together Primitive assembly Pixel operations Primitive operations Pixel pack Rasterization Texture memory Fragment operations Application Framebuffer Display
Texture mapping Paints images onto triangles Paints images onto points, lines, and other images Ties the vertex and pixel pipelines together • Rendered images can be used as textures • To modify the rendering of new images • That can be used as textures … Implements general functions of one, two, or three parameters • Specified as 1-D, 2-D, or 3-D tables (aka texture images) • With interpolated (aka filtered) lookup Drives the hardware architecture of GPUs • Multi-thread latency hiding • “shader” programmability Adds many capabilities to OpenGL • Volume rendering • Alternate color spaces • Shadows …
Complete OpenGL pipeline Pixel pipeline Application Vertex pipeline Pixel assembly(unpack) Vertex assembly Vertex operations Primitive assembly Pixel operations Primitive operations Pixel pack Rasterization Texture memory Fragment operations Application Framebuffer Display
Fundamentals of texture mapping Texture mapping requires specification of • The texture image • The mapping from object to texture coordinates • The sampling mechanism • The application of the resulting value(s)
OpenGL textured-quad code LoadTexture(“Textures\\rooster.tga”, 1); // lots hidden here glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 1); glClearColor(1, 1, 1, 1); // white glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glOrtho(0, 100, 0, 100, -1, 1); glColor3f(1, 1, 1); // white glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(0, 0); glVertex2i(11, 31); glTexCoord2f(0, 1); glVertex2i(37, 71); glTexCoord2f(1, 0); glVertex2i(91, 38); glTexCoord2f(1, 1);glVertex2i(65, 71); glEnd(); glFlush(); Texture mapping is not corrected for perspective!
struct { float r,g,b,a;} pixel; Texture image specification Unpack from memory format into canonical format (pixel structures) Pixel pipeline Application Vertex pipeline Pixel assembly(unpack) Vertex assembly Vertex operations Scale and offset Color table lookup Optional: Convolution Histogram Min/max computation Primitive assembly Pixel operations Primitive operations Pixel pack Rasterization Texture memory Fragment operations Application Framebuffer Display
struct { float xo,yo,zo,wo; float nxo,nyo,nzo; float r,g,b,a; float so,to,ro,qo;} vertex; Vertex assembly glColor3f(1,1,1); glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(0, 0); glVertex2i(11, 31); Pixel pipeline Application Vertex pipeline Pixel assembly(unpack) Vertex assembly Vertex operations Primitive assembly Pixel operations struct { float 11,31,0,1; float 0,0,1; float 1,1,1,1; float 0,0,0,1;} vertex; Primitive operations Pixel pack Rasterization Texture memory Fragment operations Application Framebuffer Display
struct { float xc,yc,zc,wc; float r,g,b,a; float sc,tc,rc,qc;} clipvertex; Vertex operations Pixel pipeline Application Vertex pipeline Pixel assembly(unpack) Vertex assembly Vertex operations Primitive assembly Pixel operations Primitive operations Pixel pack Rasterization Texture memory Fragment operations Application Framebuffer Display
struct { float xc,yc,zc,wc; float r,g,b,a; float sc,tc,rc,qc;} clipvertex;struct { clipvertex v0,v1,v2;} triangle; Primitive assembly Pixel pipeline Application Vertex pipeline Pixel assembly(unpack) Vertex assembly Vertex operations Primitive assembly Pixel operations Primitive operations Pixel pack Rasterization Texture memory Fragment operations Application Framebuffer Display
struct { float xw,yw,zw,wc; float r,g,b,a; float sc,tc,rc,qc;} winvertex;struct { winvertex v0,v1,v2;} triangle; Primitive operations Pixel pipeline Application Vertex pipeline Pixel assembly(unpack) Vertex assembly Vertex operations Primitive assembly Pixel operations Primitive operations Pixel pack Rasterization Texture memory Fragment operations Application Framebuffer Display
struct { float xw,yw,zw,wc; float r,g,b,a; float sc,tc,rc,qc;} winvertex;struct { winvertex v0,v1,v2;} triangle; Rasterization Pixel pipeline Application Vertex pipeline Pixel assembly(unpack) Vertex assembly Vertex operations Primitive assembly Pixel operations Primitive operations Pixel pack Rasterization Texture memory Fragment operations Application Framebuffer Display
a2 a0 a1 Perspective-correct attribute evaluation (x1, y1, w1, f1) (x, y, f ) All w’s are wc’s (x0, y0, w0, f0) (x2, y2, w2, f2)
a2 a0 a1 Perspective-correct tex coord evaluation Texture coordinates homogenized here Supports image remapping (from q-specified warp to render-specified warp) (x1, y1, w1, s1) (x, y, s ) wc’s, sc’s, qc’sxw’s, yw’s (x0, y0, w0, s0) (x2, y2, w2, s2)
Rasterization Pixel pipeline Application Vertex pipeline Pixel assembly(unpack) Vertex assembly Vertex operations struct { float xw,yw,zw; float r,g,b,a; float sw,tw,rw;} fragment; Primitive assembly Pixel operations Primitive operations Pixel pack Rasterization Texture memory Fragment operations Application Framebuffer Display
Fragment operations Pixel pipeline Application Vertex pipeline Pixel assembly(unpack) Vertex assembly Vertex operations struct { float xw,yw,zw; float r,g,b,a; float sw,tw,rw;} fragment; Primitive assembly Pixel operations Primitive operations Pixel pack Rasterization Texture memory Fragment operations Application Framebuffer Display
Texture lookup 1 struct { float xw,yw,zw; float r,g,b,a; float sw,tw,rw;} fragment; tw 0 struct { float rt,gt,bt,at;} color; 0 sw 1 If sw or tw is outside the range [0, 1]:- Clamp to edge color- Clamp to border color- Wrap repeatedly- Wrap with mirror reflections
Texture application 1 struct { float xw,yw,zw; float r,g,b,a; float sw,tw,rw;} fragment; tw Alternatives include: - GL_REPLACE - GL_BLEND - GL_ADD 0 struct { float rt,gt,bt,at;} color; 0 sw 1 struct { float xw,yw,zw; float r’,g’,b’,a’;} fragment; GL_MODULATE:
Fragment / framebuffer operations Pixel pipeline Application Vertex pipeline Pixel assembly(unpack) Vertex assembly Vertex operations struct { float xw,yw,zw; float r’,g’,b’,a’;} fragment; Primitive assembly Pixel operations Primitive operations Pixel pack Rasterization Texture memory Fragment operations Application Framebuffer Display
OpenGL textured-quad code LoadTexture(“Textures\\rooster.tga”, 1); // lots hidden here glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 1); glClearColor(1, 1, 1, 1); // white glClear(GL_COLOR_BUFFER_BIT); glLoadIdentity(); glOrtho(0, 100, 0, 100, -1, 1); glColor3f(1, 1, 1); // white glBegin(GL_TRIANGLE_STRIP); glTexCoord2f(0, 0); glVertex2i(11, 31); glTexCoord2f(0, 1); glVertex2i(37, 71); glTexCoord2f(1, 0); glVertex2i(91, 38); glTexCoord2f(1, 1);glVertex2i(65, 71); glEnd(); glFlush(); run
Texel coordinates 256 2m 1 struct { float xw,yw,zw; float r,g,b,a; float sw,tw,rw;} fragment; v tw 256 0 0 struct { float rt,gt,bt,at;} color; sw 0 1 u 0 2n For this image n = m = 256
Image resampling We are resampling the texture image at the pixel rate We have seen resampling before …
Supersample antialiasing Supersampling algorithm: • Over-sample, e.g., at 4x the pixel rate • Reconstruct at the over-sampled rate • Band-limit to match the pixel rate • Resample at the pixel rate to yield pixel values • Reconstruct to display
Texture resampling (to avoid aliasing) Texture resampling algorithm: • Over-sample, e.g., at 4x the pixel rate • Reconstruct at the texture-sample rate • Band-limit to match the pixel rate • Resample at the pixel rate to yield pixel values • Reconstruct to display
Supersampling optimizations The over-sample reconstruction convolution and the band-limiting convolution steps can be combined: • Convolution is associative • (f*g) * h=f * (g * h) • And g and h are constant • f * (g * h) = f * filter The filter convolution can be reduced to a simple weighted sum of sample values: • The result is sampled at pixel rate • So only values at pixel centers are needed • These are weighted sums of the 4x samples
Texture resampling optimizations The reconstruction convolution and the band-limiting convolution steps can be combined: • Convolution is associative • (f*g) * h=f * (g * h) • And g and h are constant • f * (g * h) = f * filter The filter convolution can be reduced to a simple weighted sum of sample values: • The result is sampled at pixel rate • So only values at pixel centers are needed • These are weighted sums of the texels How many texels need to be summed to get a good answer?
Size of the filter n typically differs for every fragment!
Magnification One pixel corresponds to less than one texel Resulting image is “larger” (more pixels than corresponding texels) Efficient to implement: Rule 1
Minification One pixel corresponds to many texels Resulting image is “smaller” (fewer pixels than corresponding texels) Difficult to implement efficiently Could require visiting every texel in the entire texture image!
Two approaches to antialiasing Supersampling Pre-filtering
MIPmap pre-filtering Prefilter repeatedly to ½ resolution Reduce resolution equally in all dimensions Stop at a single texel
MIPmap filtering Choose the pre-filtered images that most closely match the pixel sample rate Sample each of these images Combine the resulting values with a MIPmap filter whose weights are determined by the fractional value of p Notes: • Any filter can be chosen for the in-image sampling • Usually a bilinear filter is chosen for the MIPmap filter • Hence two pre-filtered images are chosen and sampled • Choose the single nearest MIPmap level improves preformance • But violates rule 1 ! • Four or more MIPmap levels can also be chosen
Tri-linear filtering A horrible term! Usually means sampling a 2-D MIPmapped texture image using bilinear interpolation for both the in-image filtering and the MIPmap filtering OpenGL allows separate specification of in-image and MIPmap filters
2-D and 3-D textures Spatial rates typically differ in different dimensions: This conservative approach will result in image blurring Can we do better?
Anisotropic texture filtering Combine pre-filtering and supersampling Typical algorithm: • Pre-filter to MIPmap images • Supersample the pixel area • For each sample perform a full MIPmap sample operation • Combine the MIPmap samples using pixel-customized filter weights
Summary Texture mapping is a powerful, general-purpose mechanism • It’s not just painting pictures onto triangles! Texture mapping requires specification of • The texture image • The mapping from object to texture coordinates • The sampling mechanism • The application of the resulting value(s) Of these, sampling is the greatest challenge • Use pre-filtering and supersampling approaches to • Avoid aliasing • Avoid blurring • Maintain high performance
Assignments Tuesday 30 October • Kurt will be out of town • Saturday morning until Tuesday late • Lecture with be given by Andrew Adams Reading assignment for Tuesday’s class • Work through the tutorial at: • http://www.lighthouse3d.com/opengl/glsl/ Project 3: • Write a computer-graphics game • Will be assigned this coming Tuesday Office hours today ?