Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 27: Texture Mapping Li Zhang Spring 2008

Similar presentations


Presentation on theme: "Lecture 27: Texture Mapping Li Zhang Spring 2008"— Presentation transcript:

1 Lecture 27: Texture Mapping Li Zhang Spring 2008
CS559: Computer Graphics Lecture 27: Texture Mapping Li Zhang Spring 2008 Many slides from Ravi Ramamoorthi, Columbia Univ, Greg Humphreys, UVA and Rosalee Wolfe, DePaul tutorial teaching texture mapping visually, Jingyi Yu, U Kentucky.

2 Today Continue on Texture mapping Reading Redbook: Ch 9
(highly recommended) Moller and Haines: Real-Time Rendering, 3e, Ch 6 Linux: /p/course/cs559-lizhang/public/readings/6_texture.pdf Windows: P:\course\cs559-lizhang\public\readings\6_texture.pdf (optional) Shirley: Ch 11.4 – 11.8

3 Simple OpenGL Example public void Draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslated(centerx, centery, depth); glMultMatrixf(Rotation); : // Draw Front of the Cube glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glTexCoord2d(0, 1); glVertex3d( 1.0, 1.0, 1.0); glTexCoord2d(1, 1); glVertex3d(-1.0, 1.0, 1.0); glTexCoord2d(1, 0); glVertex3d(-1.0,-1.0, 1.0); glTexCoord2d(0, 0); glVertex3d( 1.0,-1.0, 1.0); glEnd(); glDisable(GL_TEXTURE_2D); glFlush(); } Specify a texture coordinate at each vertex (s, t) Canonical coordinates where s and t are between 0 and 1 glTexCoord works like glColor

4 Initializing Texture Mapping
static GLubyte image[64][64][4]; static GLuint texname; void init(void) { glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); //load in or generate image; glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glGenTextures(1, &texName); glBindTexture(GL_TEXTURE_2D, texName); 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); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); } Level index in the Pyramid

5 OpenGL Texture Peculiarities
The width and height of Textures in OpenGL must be powers of 2 The parameter space of each dimension of a texture ranges from [0,1) regardless of the texture’s actual size. The behavior of texture indices outside of the range [0,1) is determined by the texture wrap options. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Draw Front of the Cube glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glTexCoord2d(-1, 2); glVertex3d( 1.0, 1.0, 1.0); glTexCoord2d(2, 2); glVertex3d(-1.0, 1.0, 1.0); glTexCoord2d(2, -1); glVertex3d(-1.0,-1.0, 1.0); glTexCoord2d(-1, -1); glVertex3d( 1.0,-1.0, 1.0); glEnd(); glDisable(GL_TEXTURE_2D); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

6 http://www.xmission.com/~nate/tutors.html Rotate the rectangle in 3D
Show different texture image change texture coord Explain effect Change Texture funciton option

7 Texture Function glTexEnv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE) C = Ct, A = Af t: texture f:fragment glTexEnv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) C = Cf*Ct, A = Af t: texture f:fragment Use checker board

8 http://www.xmission.com/~nate/tutors.html Rotate the rectangle in 3D
Show different texture image change texture coord Explain effect Change Texture funciton option

9 More texture function options

10 Using one texture for different objects
static GLubyte image[64][64][4]; static GLuint texname; void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texName); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0); glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f( , 1.0, ); glTexCoord2f(1.0, 0.0); glVertex3f( , -1.0, ); glEnd(); glFlush(); glDisable(GL_TEXTURE_2D); }

11 Using several texture images
static GLubyte image0[64][64][4], image1[64][64][4]; static GLuint texname[2]; void init(void) { glGenTextures(2, texName); glBindTexture(GL_TEXTURE_2D, texName[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, image0); glBindTexture(GL_TEXTURE_2D, texName[1]); GL_RGBA, GL_UNSIGNED_BYTE, image1); }

12 Using several texture images
Swithcing using glBindTexture(GL_TEXTURE_2D, texName); void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindTexture(GL_TEXTURE_2D, texName[0]); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3f(-2.0, -1.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(-2.0, 1.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 1.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -1.0, 0.0); glEnd(); glBindTexture(GL_TEXTURE_2D, texName[1]); glTexCoord2f(0.0, 0.0); glVertex3f(1.0, -1.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(1.0, 1.0, 0.0); glTexCoord2f(1.0, 1.0); glVertex3f( , 1.0, ); glTexCoord2f(1.0, 0.0); glVertex3f( , -1.0, ); glFlush(); }

13 OpenGL Mipmap Incorporating MIPmapping into OpenGL applications is surprisingly easy. The gluBuildMipmaps() utility routine will automatically construct a mipmap from a given texture buffer. It will filter the texture using a simple box filter and then subsample it by a factor of 2 in each dimension. It repeats this process until one of the texture’s dimensions is 1. Each texture ID, can have multiple levels associated with it. GL_LINEAR_MIPMAP_LINEAR trilinearly interploates between texture indices and MIPmap levels. Other options include GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, and GL_LINEAR_MIPMAP_NEAREST. // Boilerplate Texture setup code glTexImage2D(GL_TEXTURE_2D, 0, 4, texWidth, texHeight, 0, GL_RGBA,GL_UNSIGNED_BYTE, data); gluBuild2DMipmaps(GL_TEXTURE_2D, 4, texWidth, texHeight, GL_RGBA, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); GL_LINEAR_MIPMAP_LINEAR glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); OpenGL also provides a facility for specifying the MIPmap image at each level using multiple calls to the glTexImage*D() function. This approach provides more control over filtering in the MIPmap construction.

14 Initializing Texture Mapping
Red book chapter on Texture mapping, Example 9-1 All Red book example source code can be found at Course Tutorial 10,

15 Project 3

16

17 Texture animation Basic idea: treat texture coordinate like color
Moving water texture to simulate flow zoom, rotation, and shearing image on a surface

18 Other Issues with Textures
Tedious to specify texture coordinates for every triangle Textures are attached to the geometry Can't use just any image as a texture The "texture" can't have projective distortions Reminder: linear interpolation in image space is not equivalent to linear interpolation in 3-space (This is why we need "perspective-correct" texturing). The converse is also true. Makes it hard to use pictures as textures

19 Projective Textures Treat the texture as a light source (like a slide projector) No need to specify texture coordinates explicitly A good model for shading variations due to illumination (cool spotlights) A fair model for view-dependent reflectance (can use pictures)

20 The Mapping Process This is the same process, albeit with an additional transform, as perspective correct texture mapping. Thus, we can do it for free! Almost.

21 What transformation do we need?
OpenGL is able to insert this extra projection transformation for textures by including another matrix stack, called GL_TEXTURE. Also we can use 3d vertex coordinate as texture coordinates The transform we want is: This matrix undoes the world-to-eye transform on the MODEL_VIEW matrix stack., so the projective texture can be specified in world coordinates. Note: If you specify your lights in eye-space then this matrix is identity. This matrix positions the projector in the world, much like the viewing matrix positions the eye within the world. (HINT, you can use gluLookAt() to set this up if you want. This matrix specifies the frustum of the projector . It is a non-affine, projection matrix. You can use any of the projection transformations to establish it, such as glFrustum(), glOrtho() or gluPerspective () . This extra matrix maps from normalized device coordinates ranging from [-1,1] to valid texture coordinates ranging from [0,1].

22 OpenGL Example Here is a code fragment implementing projective textures in OpenGL // The following information is associated with the current active texture // Basically, the first group of setting says that we will not be supplying texture coordinates. // Instead, they will be automatically established based on the vertex coordinates in “EYE-SPACE” // (after application of the MODEL_VIEW matrix). glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, (int) GL_EYE_LINEAR); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, (int) GL_EYE_LINEAR); glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, (int) GL_EYE_LINEAR); glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, (int) GL_EYE_LINEAR); // These calls initialize the TEXTURE_MAPPING function to identity. We will be using // the Texture matrix stack to establish this mapping indirectly. float [] eyePlaneS = { 1.0f, 0.0f, 0.0f, 0.0f }; float [] eyePlaneT = { 0.0f, 1.0f, 0.0f, 0.0f }; float [] eyePlaneR = { 0.0f, 0.0f, 1.0f, 0.0f }; float [] eyePlaneQ = { 0.0f, 0.0f, 0.0f, 1.0f }; glTexGenfv(GL_S, GL_EYE_PLANE, eyePlaneS); glTexGenfv(GL_T, GL_EYE_PLANE, eyePlaneT); glTexGenfv(GL_R, GL_EYE_PLANE, eyePlaneR); glTexGenfv(GL_Q, GL_EYE_PLANE, eyePlaneQ);

23 GL_OBJECT_LINEAR vs GL_EYE_LINEAR

24 OpenGL Example (cont) The following code fragment is inserted into Draw( ) or Display( ) if (projTexture) { glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glEnable(GL_TEXTURE_GEN_R); glEnable(GL_TEXTURE_GEN_Q); projectTexture(); } // … draw everything that the texture is projected onto if (projTexture) { glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_GEN_S); glDisable(GL_TEXTURE_GEN_T); glDisable(GL_TEXTURE_GEN_R); glDisable(GL_TEXTURE_GEN_Q); }

25 OpenGL Example (cont) Here is where the extra “Texture” transformation on the vertices is inserted. private void projectTexture() { glMatrixMode(GL_TEXTURE); glLoadIdentity(); glTranslated(0.5, 0.5, 0.5); // Scale and bias the [-1,1] NDC values glScaled(0.5, 0.5, 0.5); // to the [0,1] range of the texture map gluPerspective(15, 1, 5, 7); // projector "projection" and view matrices gluLookAt(lightPosition[0],lightPosition[1],lightPosition[2], 0,0,0, 0,1,0); glMatrixMode(GL_MODELVIEW); } How to know where the light is to project an captured image? CS766 Computer Vision 4/23/2017

26 Outline Types of mappings Interpolating texture coordinates
Texture Resampling Texture mapping in OpenGL Broader use of textures

27 Modulation textures Map texture values to scale factor Wood texture

28 Illumination Maps Quake introduced illumination maps or light maps to capture lighting effects in video games Texture map: Light map Texture map + light map:

29 Bump Mapping Texture = change in surface normal!
Sphere w/ diffuse texture and swirly bump map Sphere w/ diffuse texture Swirly bump map

30 Bump map respresentation
High values as texel values need to converted to normal (bu, bv) as texel values What happens if the light is head on? Figure 6.22 from RTR book

31 More Bump Map Examples Since the actual shape of the object does not change, the silhouette edge of the object will not change. Bump Mapping also assumes that the Illumination model is applied at every pixel (as in Phong Shading or ray tracing). 4/23/2017 Lecture 20

32 One More Bump Map Example
4/23/2017 Lecture 20

33 Dot product normal map (r,g,b) encodes (nx,ny,nz)
Figure 6.24 from RTR book

34 Bump map in shading X + = Figure 6.26 from RTR book

35 Displacement Mapping Texture maps can be used to actually move surface points. This is called displacement mapping. How is this fundamentally different than bump mapping? Bump map does not have occlusion effects.

36 Rendering Displacement map is tricky
How to find the intersection? See RTR book Figure 6.29 from RTR book

37 Bump map vs Displacement map
Figure 6.24 and 30 from RTR book


Download ppt "Lecture 27: Texture Mapping Li Zhang Spring 2008"

Similar presentations


Ads by Google