Texture Mapping. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Similar documents
Buffers. Angel and Shreiner: Interactive Computer Graphics 7E Addison-Wesley 2015

Lecture 19: OpenGL Texture Mapping. CITS3003 Graphics & Animation

CISC 3620 Lecture 7 Lighting and shading. Topics: Exam results Buffers Texture mapping intro Texture mapping basics WebGL texture mapping

CS452/552; EE465/505. Texture Mapping in WebGL

Fog example. Fog is atmospheric effect. Better realism, helps determine distances

Objectives. Texture Mapping and NURBS Week 7. The Limits of Geometric Modeling. Modeling an Orange. Three Types of Mapping. Modeling an Orange (2)

OpenGL Texture Mapping. Objectives Introduce the OpenGL texture functions and options

CS 432 Interactive Computer Graphics

CS212. OpenGL Texture Mapping and Related

Computer Graphics. Three-Dimensional Graphics VI. Guoying Zhao 1 / 73

CS 5600 Spring

Introduction to Computer Graphics with WebGL

Discrete Techniques. 11 th Week, Define a buffer by its spatial resolution (n m) and its depth (or precision) k, the number of

Surface Rendering. Surface Rendering

Buffers, Textures, Compositing, and Blending. Overview. Buffers. David Carr Virtual Environments, Fundamentals Spring 2005 Based on Slides by E.

Graphics. Texture Mapping 고려대학교컴퓨터그래픽스연구실.

INF3320 Computer Graphics and Discrete Geometry

CS4621/5621 Fall Basics of OpenGL/GLSL Textures Basics

CSE528 Computer Graphics: Theory, Algorithms, and Applications

Texturas. Objectives. ! Introduce Mapping Methods. ! Consider two basic strategies. Computação Gráfica

Cap. 3 Textures. Mestrado em Engenharia Informática (6931) 1º ano, 1º semestre

CSE 167: Introduction to Computer Graphics Lecture #7: Textures. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2018

Lecture 07: Buffers and Textures

CSE 167: Introduction to Computer Graphics Lecture #8: Textures. Jürgen P. Schulze, Ph.D. University of California, San Diego Spring Quarter 2016

Today. Texture mapping in OpenGL. Texture mapping. Basic shaders for texturing. Today. Computergrafik

CS335 Graphics and Multimedia. Slides adopted from OpenGL Tutorial

CT5510: Computer Graphics. Texture Mapping

Computergrafik. Matthias Zwicker Universität Bern Herbst 2016

Texture Mapping. Mike Bailey.

CSE 167: Introduction to Computer Graphics Lecture #8: Textures. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2017

CS179: GPU Programming

Texture Mapping. Computer Graphics, 2015 Lecture 9. Johan Nysjö Centre for Image analysis Uppsala University

GRAFIKA KOMPUTER. ~ M. Ali Fauzi

TSBK 07! Computer Graphics! Ingemar Ragnemalm, ISY

Texturing. Slides done bytomas Akenine-Möller and Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

INF3320 Computer Graphics and Discrete Geometry

Lecture 22 Sections 8.8, 8.9, Wed, Oct 28, 2009

Discussion 3. PPM loading Texture rendering in OpenGL

Computer Graphics Texture Mapping

CS452/552; EE465/505. Image Processing Frame Buffer Objects

Lecture 5 3D graphics part 3

SUMMARY. CS380: Introduction to Computer Graphics Texture Mapping Chapter 15. Min H. Kim KAIST School of Computing 18/05/03.

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

Texture mapping. Computer Graphics CSE 167 Lecture 9

Computergraphics Exercise 15/ Shading & Texturing

-=Catmull's Texturing=1974. Part I of Texturing

三維繪圖程式設計 3D Graphics Programming Design 第七章基礎材質張貼技術嘉大資工系盧天麒

Lighting and Texturing

Textures. Texture Mapping. Bitmap Textures. Basic Texture Techniques

CSE 167: Lecture 11: Textures 2. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2011

CSE 167: Introduction to Computer Graphics Lecture #9: Textures. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2013

Applying Textures. Lecture 27. Robb T. Koether. Hampden-Sydney College. Fri, Nov 3, 2017

Texture and other Mappings

Texture Mapping. Texture Mapping. Map textures to surfaces. Trompe L Oeil ( Deceive the Eye ) The texture. Texture map

Computer Graphics. Bing-Yu Chen National Taiwan University

CMSC 425: Lecture 12 Texture Mapping Thursday, Mar 14, 2013

Introduction to Computer Graphics with WebGL

Shading. Flat shading Gouraud shading Phong shading

Announcements. Written Assignment 2 is out see the web page. Computer Graphics

Steiner- Wallner- Podaras

Shading/Texturing. Dr. Scott Schaefer

Texturing. Slides done by Tomas Akenine-Möller and Ulf Assarsson Department of Computer Engineering Chalmers University of Technology

Grafica Computazionale

Texture Mapping and Sampling

Stored Texture Shaders

General Purpose computation on GPUs. Liangjun Zhang 2/23/2005

Computer Graphics (CS 543) Lecture 8c: Environment Mapping (Reflections and Refractions)

CPSC / Texture Mapping

Preparing for Texture Access. Stored Texture Shaders. Accessing Texture Maps. Vertex Shader Texture Access

OUTLINE. Implementing Texturing What Can Go Wrong and How to Fix It Mipmapping Filtering Perspective Correction

Overview. Goals. MipMapping. P5 MipMap Texturing. What are MipMaps. MipMapping in OpenGL. Generating MipMaps Filtering.

CSE 167: Introduction to Computer Graphics Lecture #6: Lights. Jürgen P. Schulze, Ph.D. University of California, San Diego Fall Quarter 2014

+ = Texturing: Glue n-dimensional images onto geometrical objects. Texturing. Texture magnification. Texture coordinates. Bilinear interpolation

Computer Graphics Texture Mapping

Texture Mapping 1/34

Imaging and Raster Primitives

CS GPU and GPGPU Programming Lecture 11: GPU Texturing 1. Markus Hadwiger, KAUST

CS 130 Final. Fall 2015

Texturing Theory. Overview. All it takes is for the rendered image to look right. -Jim Blinn 11/10/2018

Building Models. Prof. George Wolberg Dept. of Computer Science City College of New York

Texture Mapping CSCI 4229/5229 Computer Graphics Fall 2016

Texture Mapping and Special Effects

Copyright Khronos Group 2012 Page 1. Teaching GL. Dave Shreiner Director, Graphics and GPU Computing, ARM 1 December 2012

Most device that are used to produce images are. dots (pixels) to display the image. This includes CRT monitors, LCDs, laser and dot-matrix

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Texture Mapping 1/34

CS GPU and GPGPU Programming Lecture 12: GPU Texturing 1. Markus Hadwiger, KAUST

CS GPU and GPGPU Programming Lecture 16+17: GPU Texturing 1+2. Markus Hadwiger, KAUST

ก ก ก.

Tutorial 3: Texture Mapping

Textures. Texture coordinates. Introduce one more component to geometry

Magnification and Minification

C P S C 314 S H A D E R S, O P E N G L, & J S RENDERING PIPELINE. Mikhail Bessmeltsev

Computer Graphics CS 543 Lecture 4 (Part 2) Building 3D Models (Part 2)

CHAPTER 1 Graphics Systems and Models 3

CS 450: COMPUTER GRAPHICS TEXTURE MAPPING SPRING 2015 DR. MICHAEL J. REALE

CS 354R: Computer Game Technology

Shaders. Slide credit to Prof. Zwicker

Methodology for Lecture

Lecture 5. Scan Conversion Textures hw2

Transcription:

Texture Mapping CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science 1

Objectives Introduce Mapping Methods - Texture Mapping - Environment Mapping - Bump Mapping Consider basic strategies - Forward vs backward mapping - Point sampling vs area averaging 2

The Limits of Geometric Modeling Although graphics cards can render over 20 million polygons per second, that number is insufficient for many phenomena - Clouds - Grass - Terrain - Skin 3

Modeling an Orange Consider the problem of modeling an orange (the fruit) Start with an orange-colored sphere - Too simple Replace sphere with a more complex shape - Does not capture surface characteristics (small dimples) - Takes too many polygons to model all the dimples 4

Modeling an Orange (2) Take a picture of a real orange, scan it, and paste onto simple geometric model - This process is known as texture mapping Still might not be sufficient because resulting surface will be smooth - Need to change local shape - Bump mapping 5

Three Types of Mapping Texture Mapping - Uses images to fill inside of polygons Environment (reflection mapping) - Uses a picture of the environment for texture maps - Allows simulation of highly specular surfaces Bump mapping - Emulates altering normal vectors during the rendering process 6

Texture Mapping geometric model texture mapped 7

Environment Mapping 8

Bump Mapping 9

Where does mapping take place? Mapping techniques are implemented at the end of the rendering pipeline - Very efficient because few polygons make it past the clipper Vertices G eometry processing Rasteriz ation Fra gment processing Frame buffer Pixels Pixel processing 1 0

Is it simple? Although the idea is simple---map an image to a surface---there are 3 or 4 coordinate systems involved 2D image 3D surface 1 1

Coordinate Systems Parametric coordinates - May be used to model curves and surfaces Texture coordinates - Used to identify points in the image to be mapped Object or World Coordinates - Conceptually, where the mapping takes place Window Coordinates - Where the final image is really produced 1 2

Texture Mapping parametric coordinates texture coordinates world coordinates window coordinates 1 3

Mapping Functions Basic problem is how to find the maps Consider mapping from texture coordinates to a point a surface Appear to need three functions x = x(s,t) y = y(s,t) z = z(s,t) But we really want to go the other way t s (x,y,z) 1 4

Backward Mapping We really want to go backwards - Given a pixel, we want to know to which point on an object it corresponds - Given a point on an object, we want to know to which point in the texture it corresponds Need a map of the form s = s(x,y,z) t = t(x,y,z) Such functions are difficult to find in general 1 5

Two-part mapping One solution to the mapping problem is to first map the texture to a simple intermediate surface Example: map to cylinder 1 6

Cylindrical Mapping parametric cylinder x = r cos 2π u y = r sin 2πu z = v/h maps rectangle in u,v space to cylinder of radius r and height h in world coordinates s = u t = v maps from texture space 1 7

Spherical Map We can use a parametric sphere x = r cos 2πu y = r sin 2πu cos 2πv z = r sin 2πu sin 2πv in a similar manner to the cylinder but have to decide where to put the distortion Spheres are used in environmental maps 1 8

Box Mapping Easy to use with simple orthographic projection Also used in environment maps 1 9

Second Mapping Map from intermediate object to actual object - Normals from intermediate to actual - Normals from actual to intermediate - Vectors from center of intermediate actual intermediate 2 0

Aliasing Point sampling of the texture can lead to aliasing errors miss blue stripes point samples in u,v (or x,y,z) space point samples in texture space 2 1

Area Averaging A better but slower option is to use area averaging preimage pixel Note that preimage of pixel is curved 2 2

Go to ART Texture Mapping slides 2 3

OpenGL Texture Mapping CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science 2 4

Objectives Introduce the OpenGL texture functions and options 2 5

Basic Stragegy Three steps to applying a texture 1. specify the texture read or generate image assign to texture enable texturing 2. assign texture coordinates to vertices Proper mapping function is left to application 3. specify texture parameters wrapping, filtering 2 6

Texture Mapping y z x geometry display t image s 2 7

Texture Example The texture (below) is a 256 x 256 image that has been mapped to a rectangular polygon which is viewed in perspective 2 8

Texture Mapping and the OpenGL Pipeline Images and geometry flow through separate pipelines that join during fragment processing - complex textures do not affect geometric complexity vertices image geometry pipeline pixel pipeline fragment processor 2 9

Specifying a Texture Image Define a texture image from an array of texels (texture elements) in CPU memory Glubyte my_texels[512][512][3]; Define as any other pixel map - Scanned image - Generate by application code Enable texture mapping - glenable(gl_texture_2d) - OpenGL supports 1-4 dimensional texture maps 3 0

Define Image as a Texture glteximage2d( target, level, internalformat, w, h, border, format, type, texels ); target: type of texture, e.g. GL_TEXTURE_2D level: used for mipmapping (discussed later) internalformat: how texel is stored w, h: width and height of texels in pixels border: used for smoothing (discussed later) format and type: describe texels texels: pointer to texel array glteximage2d(gl_texture_2d, 0, GL_RGB, 512, 512, 0, GL_RGB, GL_UNSIGNED_BYTE, my_texels); 3 1

Mapping a Texture Application or shaders define texture coordinates Texture coordinates specified at each vertex t 0, 1 Texture Space a 1, 1 Object Space (s, t) = (0.2, 0.8) A c b 0, 0 1, 0 s (0.4, 0.2) B C (0.8, 0.4) 3 2

Typical Code Application sending texture coordinates offset = 0; GLuint vposition = glgetattriblocation( program, "vposition" ); glenablevertexattribarray( vposition ); glvertexattribpointer( vposition, 4, GL_FLOAT, GL_FALSE, 0,BUFFER_OFFSET(offset) ); offset += sizeof(points); GLuint vtexcoord = glgetattriblocation( program, "vtexcoord" ); glenablevertexattribarray( vtexcoord ); glvertexattribpointer( vtexcoord, 2,GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offset) ); 3 3

Interpolation OpenGL uses interpolation to find proper texels from specified texture coordinates Can be distortions good selection of tex coordinates poor selection of tex coordinates texture stretched over trapezoid showing effects of bilinear interpolation 3 4

Texture Parameters OpenGL has a variety of parameters that determine how texture is applied - Wrapping parameters determine what happens if s and t are outside the (0,1) range - Filter modes allow us to use area averaging instead of point samples - Mipmapping allows us to use textures at multiple resolutions - Environment parameters determine how texture mapping interacts with shading 3 5

Wrapping Mode Clamping: if s,t > 1 use 1, if s,t <0 use 0 Wrapping: use s,t modulo 1 gltexparameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ) gltexparameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) t texture s GL_REPEAT wrapping GL_CLAMP wrapping 3 6

Magnification and Minification More than one texel can cover a pixel (minification) or more than one pixel can cover a texel (magnification) Can use point sampling (nearest texel) or linear filtering ( 2 x 2 filter) to obtain texture values Texture Magnification Polygon Texture Minification Polygon 3 7

Filter Modes Modes determined by - gltexparameteri( target, type, mode ) gltexparameteri(gl_texture_2d, GL_TEXURE_MAG_FILTER, GL_NEAREST); gltexparameteri(gl_texture_2d, GL_TEXURE_MIN_FILTER, GL_LINEAR); Note that linear filtering requires a border of an extra texel for filtering at edges (border = 1) 3 8

Mipmapped Textures Mipmapping allows for prefiltered texture maps of decreasing resolutions Lessens interpolation errors for smaller textured objects Declare mipmap level during texture definition glteximage2d( GL_TEXTURE_*D, level, ) Have OpenGL make your mipmap glgeneratemipmap(target) Mipmaps invoked by setting sampling gltexparameteri(gl_texture_2d, GL_TEXURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); 3 9

Example point sampling linear filtering mipmapped point sampling mipmapped linear filtering 4 0

Using Texture Objects 1. specify textures in texture objects 2. set texture filter 3. set texture wrap mode 4. bind texture object 5. enable texturing 6. supply texture coordinates for vertex - coordinates can also be generated 4 2

Other Texture Features Environment Maps - Start with image of environment through a wide angle lens Can be either a real scanned image or an image created in OpenGL - Use this texture to generate a spherical map - Alternative is to use a cube map Multitexturing - Apply a sequence of textures through cascaded texture units 4 3

Checkerboard Texture GLubyte image[64][64][3]; // Create a 64 x 64 checkerboard pattern for ( int i = 0; i < 64; i++ ) { for ( int j = 0; j < 64; j++ ) { GLubyte c = (((i & 0x8) == 0) ^ ((j & 0x8) == 0)) * 255; image[i][j][0] = c; image[i][j][1] = c; image[i][j][2] = c; 4 4

Adding Texture Coordinates void quad( int a, int b, int c, int d ) { quad_colors[index] = colors[a]; points[index] = vertices[a]; tex_coords[index] = vec2( 0.0, 0.0 ); index++; quad_colors[index] = colors[a]; points[index] = vertices[b]; tex_coords[index] = vec2( 0.0, 1.0 ); Index++; // other vertices } 4 5

Texture Object GLuint textures[1]; glgentextures( 1, textures ); glbindtexture( GL_TEXTURE_2D, textures[0] ); glteximage2d( GL_TEXTURE_2D, 0, GL_RGB, TextureSize, TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image ); gltexparameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); gltexparameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); gltexparameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); gltexparameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glactivetexture( GL_TEXTURE0 ); 4 6

Linking with Shaders GLuint vtexcoord = glgetattriblocation( program, "vtexcoord" ); glenablevertexattribarray( vtexcoord ); glvertexattribpointer( vtexcoord, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(offset) ); // Set the value of the fragment shader texture sampler variable // ("texture") to the the appropriate texture unit. In this case, // zero, for GL_TEXTURE0 which was previously set by calling // glactivetexture(). gluniform1i( glgetuniformlocation(program, "texture"), 0 ); 4 7

Vertex Shader Usually vertex shader will output texture coordinates to be rasterized Must do all other standard tasks too - Compute vertex position - Compute vertex color if needed in vec4 vposition; //vertex position in object coordinates in vec4 vcolor; //vertex color from application in vec2 vtexcoord; //texture coordinate from application out vec4 color; //output color to be interpolated out vec2 texcoord; //output tex coordinate to be interpolated 4 8

Applying Textures Textures are applied during fragment shading by a sampler Samplers return a texture color from a texture object in vec4 color; //color from rasterizer in vec2 texcoord; //texure coordinate from rasterizer uniform sampler2d texture; //texture object id from application void main() { gl_fragcolor = color * texture2d( texture, texcoord ); } 4 9

Using Textures Texture value may be used in ANY of the components of the shading formula For example - Diffuse color - Specular color - Ambient color - Shininess - Normals - Alpha Or as a decal, or mask or blended in 5 0

Suggestions for HW9 First add texture coordinates [u,v] to your vertices Just use the [u,v] pair used to compute the vertex on the Bezier patch Use a variant of the code in slides 45 -> 49 to define your texture object in your application and fragment shader Note that the texture coordinates get passed through to the fragment shader, where they are used to sample the 2D texture 5 1

Suggestions for HW9 To test if your texture coordinates are getting to the shaders properly, you could simply map the coordinates to the red & green channels of the fragment's color This is one way to visualize the texture coordinates values The color retrieved from the texture map should be used for the material s diffuse color in your Phong shading calculation in your fragment shader 5 2

Suggestions for HW9 For 3D procedural texturing I would suggest that your mapping from 3D Cartesian space to RGB color space be based on a location specified in world, NOT camera, coordinates. So you will need to pass the location of the vertices in world coordinates to the fragment shader 5 3

Suggestions for HW9 Write your procedural texture function in your fragment shader based on the interpolated value of the vertex location The color computed by your procedure should be used for the material s diffuse color in your Phong shading calculation in your fragment shader For the 3D procedural texturing you will only need to change your vertex and fragment shaders. I don't think that you will need to change any of your HW6 application code 5 4