1 / 43

Texture Mapping

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.

Download Presentation

Texture Mapping

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Texture Mapping Kurt Akeley CS248 Lecture 10 25 October 2007 http://graphics.stanford.edu/courses/cs248-07/

  2. Texture mapping demo

  3. Texture mapping Paints images onto triangles Paints images onto points, lines, and other images

  4. 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

  5. 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 …

  6. 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

  7. 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)

  8. The OpenGL Pipeline(texture mapping example)

  9. 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!

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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)

  17. 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)

  18. 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

  19. 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

  20. 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

  21. 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:

  22. 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

  23. 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

  24. Texture Sampling

  25. 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

  26. Image resampling We are resampling the texture image at the pixel rate We have seen resampling before …

  27. 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

  28. 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

  29. 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

  30. 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?

  31. Size of the filter n typically differs for every fragment!

  32. Magnification One pixel corresponds to less than one texel Resulting image is “larger” (more pixels than corresponding texels) Efficient to implement: Rule 1

  33. 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!

  34. Two approaches to antialiasing Supersampling Pre-filtering

  35. MIPmap pre-filtering Prefilter repeatedly to ½ resolution Reduce resolution equally in all dimensions Stop at a single texel

  36. 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

  37. 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

  38. 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?

  39. RIPmap pre-filtering

  40. 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

  41. 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

  42. 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 ?

  43. End

More Related