Texture Mapping in OpenGL. Texture Mapping Imaging we are “pasting” a picture onto a model  Which part of the picture will be pasted onto which part.

Slides:



Advertisements
Similar presentations
Hofstra University1 Texture Motivation: to model realistic objects need surface detail: wood grain, stone roughness, scratches that affect shininess, grass,
Advertisements

1 Understanding of OpenGL TA: Dong Hyun Jeong Instructor : Dr. Kalpathi Subramanian Texture Mapping.
TEXTURE MAPPING JEFF CHASTINE 1. TEXTURE MAPPING Applying an image (or a texture ) to geometry 2D images (rectangular) 3D images (volumetric – such as.
OpenGL Texture Mapping
Texture Mapping. Typical application: mapping images on geometry 3D geometry (quads mesh) + RGB texture 2D (color-map) =
CH6 Texture. Before coding … 1/2 Prepare all texture images you need. BMP file is a good choice If you want to use TIFF file, maybe try this.
Texture Mapping OpenGl and Implementation Details CS
CSC345: Advanced Graphics & Virtual Environments
OpenGL Texture Mapping
OpenGL Texture Mapping Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
1 Lecture 12 Texture Mapping uploading of the texture to the video memory the application of the texture onto geometry.
OpenGL Texture Mapping April 16, Angel: Interactive Computer Graphics 3E © Addison-Wesley 2002 Basic Stragegy Three steps to applying a texture.
CS 4731: Computer Graphics Lecture 17: Texturing Emmanuel Agu.
Texture Mapping A way of adding surface details Two ways can achieve the goal:  Surface detail polygons: create extra polygons to model object details.
Texture Mapping. To add surface details… World of Warcraft, Blizzard Inc. More polygons (slow and hard to handle small details) Less polygons but with.
Computer Graphics Texture Mapping Eriq Muhammad Adams
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Texture Mapping + Texture Object = Texture Mapped Object.
2IV60 Computer Graphics set 10: Texture mapping Jack van Wijk TU/e.
2002 by Jim X. Chen: 1 Texture Lab At each rendered pixel, selected texels are used either to substitute for or to scale.
Texture Mapping. Scope Buffers Buffers Various of graphics image Various of graphics image Texture mapping Texture mapping.
Texture Mapping. Introduction What is Texture Mapping? Types of Texture Mapping –1D, 2D and 3D SDL and OpenGL.
Texture Mapping. Example Mappings Mapping Techniques Consider the problem of rendering a sphere in the examples The geometry is very simple - a sphere.
Computer Graphics OpenGL - Texture mapping. OpenGL Texture mapping zTexture Mapping allows us to glue an image on polygons. In this method we can produce.
Texture Mapping Course: Computer Graphics Presented by Fan Chen
Computer Graphics Ben-Gurion University of the Negev Fall 2012.
An Interactive Introduction to OpenGL Programming Ed Angel
CS 445 / 645 Introduction to Computer Graphics Lecture 19 Texture Maps Lecture 19 Texture Maps.
ECSE-4750 Computer Graphics Fall 2004 Prof. Michael Wozny TA. Abhishek Gattani TA. Stephen
CS380 LAB IV OpenGL Jonghyeob Lee Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Texture Mapping Fall, Textures Describe color variation in interior of 3D polygon  When scan converting a polygon, vary pixel colors according.
Lecture 27: Texture Mapping Li Zhang Spring 2008
And Some Extra Information From
Texture Mapping. 2 Motivation A typical modern graphics card can handle 10s of millions of polygons a second. How many individual blades of grass are.
OpenGL Texture Mapping. 2 Objectives Introduce the OpenGL texture functions and options.
CS 480/680 Computer Graphics OpenGL Texture Mapping Dr. Frederick C Harris, Jr. Fall 2011.
Texture Mapping Software College, Shandong University Instructor: Zhou Yuanfeng
Texture Mapping Drawing Pictures on Polygons. Texture Mapping.
TEXTURES & OTHER GOODIES Computer Graphics. glTexCoord2f(...); + =
111/17/ :24 UML Solution Involves Selection of Discrete Representation Values.
2 COEN Computer Graphics I Evening’s Goals n Discuss displaying and reading image primitives n Describe texture mapping n Discuss OpenGL modes and.
CH8 Frame Buffer Object 1. Introduction Name OpenGL :Frame Buffer Object DirectX:Render Target Usage Render to Texture 2.
Texture Mapping. 2 3 Loading Textures void glTexImage2D (GLenum target, GLint level, GLint internalformat, GLsizei width, GLsizei height, GLint border,
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
OpenGL Programming Guide : Texture Mapping Yoo jin wook Korea Univ. Computer Graphics Lab.
CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment.
Texture Mapping. checker.c Texture-Mapped Squares.
1 Graphics CSCI 343, Fall 2015 Lecture 25 Texture Mapping.
CH6 Texture.
Texture Mapping and NURBS Week 7 David Breen Department of Computer Science Drexel University Based on material from Ed Angel, University of New Mexico.
Details of Texture Mapping Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, December 1, 2003.
CH6 Texture. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture blending Per Fragment.
CH6 Texture. Before coding … 1/2 Prepare all texture images you need. BMP file is a good choice If you want to load other format, you can try the FreeImage.
CS425 © 2003 Ray S. Babcock Pixels and Bitmaps ● OpenGL allows us to work directly with bits and groups of bits, or pixels, which flow down a parallel.
Texture Mapping CEng 477 Introduction to Computer Graphics.
Texture Mapping Fall, 2016.
Texture Mapping 고려대학교 컴퓨터 그래픽스 연구실 kucg.korea.ac.kr.
OpenGL Texture Mapping
Advanced Graphics Algorithms Ying Zhu Georgia State University
Advanced Graphics Algorithms Ying Zhu Georgia State University
OpenGL Texture Mapping
Introduction to Computer Graphics with WebGL
Computer Graphics, Lee Byung-Gook, Dongseo Univ.
3D Game Programming Texture Mapping
Computer Graphics Practical Lesson 6
OpenGL Texture Mapping
OpenGL Texture Mapping
Programming Textures Lecture 15 Fri, Sep 28, 2007.
3D Game Programming Texture Mapping
OpenGL Texture Mapping
Presentation transcript:

Texture Mapping in OpenGL

Texture Mapping Imaging we are “pasting” a picture onto a model  Which part of the picture will be pasted onto which part of the model?  How do we combine the picture with the model’s existing material and lighting?

Texture Mapping Note the darker parts on the wing

Introduction “Which part of the picture will be pasted onto which part of the model?” is actually  For a texel: Which pixels will have the color of this texel?  For a pixel: Which texels should I lookup in order to find out my color? Forward mapping vs backward mapping

Consider A Pipeline

Introduction Backward mapping is suitable for the pipeline  The data after rasterization are pixels  Thus a per-pixel processing fashion is adopted The pipeline will find out proper texels the pixel mapped to…and decide the final result…if it have enough information  What are the information needed?

What is a texture? Textures are arrays of data  Ex. Color, luminance, alpha, normal…  The individual elements in a texture array are called texels  1D, 2D, 3D We will focus on 2D textures

Consider Texture Mapping How do we construct a texture? Which texture the texel comes from?  Texture object How do we map the pixels to the texels?  Texture coordinates How the information on the texels interacts with the pixel?  Replace? Blend?... What if a pixel is mapped onto multiple texels? And on the contrary?  Aliasing: magnification and minification

How to use textures in OpenGL Steps in Texture Mapping  Specify a texture  Enable texture mapping  Indicate how the texture is applied to objects  Draw the scene

How to use textures in OpenGL Preparing:  Prepare the image as an continuous memory chunk Generate it or using some external libraries  Create a texture from the image What information the memory carries?  RGB? RGBA? Height/width Data format?  float? unsigned int?

Texture Example 1/4 We now generate an image, which will be later used as a texture // the checker image. Note when we declare a 2D array with fixed size, // it is actually a continuous memory ordered in a row major fashion static GLubyte checkImage0[256][256][4]; // initially generate a checker image by code void makeCheckImage(void) { GLubyte c; for(int i=0; i<256; i++) for(int j=0; j<256; j++) { c = ((((i&0x20)==0)^((j&0x20))==0))*255; checkImage0[i][j][0] = checkImage0[i][j][1] = checkImage0[i][j][2] = c; checkImage0[i][j][3] = 255; }

How to use textures in OpenGL Setting:  Enable texture functionality Can be enabled/disabled anytime glEnable(GL_TEXTURE_2D)  Specify how to apply the texture on pixels Replace/Blend/Modulate/Decal glTexEnv(…)  Other parameters filters

Texture Example 2/4 void GL_init(void) { makeCheckImage(); // specify the texture with image (which is a pointer as the last parameter) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage0); // how texture interacted with existing pixel colors glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); // filters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // enable 2D texture glEnable(GL_TEXTURE_2D); }

How to use textures in OpenGL Render  Texture coordinate Assign every vertex a texture coordinate Or Let OpenGL generate texture coordinates for you

How to use textures in OpenGL //an example of manully assigning texture coordinates glBegin(GL_TRIANGLES); glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0); glTexCoord2f(1.0, 0.0); glVertex2f(1.0, -1.0); glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 1.0); glEnd();

Looking into the API Now we have rough ideas in texture mapping, let’s go though interfaces related to texture mapping in OpenGL

Specify the Texture 1/7 Define a 2D texture.  void glTexImage2D(GLenum target, GLint level, GLint internalFormat, GLsizei width, GLsizei height, Glint border, GLenum format, GLenum type, const GLvoid *pixels); See OpenGL Programming Guide for more detail.

Specify the Texture 2/7 target : GL_TEXTURE_2D, GL_PROXY_TEXTURE_1D level : resolution level of the texture (for mipmap) InternalFormat : indicate which of the R, G, B, and A or other components are selected for use in describing the texels of an image. (GL_RGB, GL_ALPHA, GL_RGBA, …) width and height : size of the texture  Must be power of 2.

Specify the Texture 4/7  In OpenGL, the length and width of the texture array must be power of 2  For texture which size is not the power of 2 use glScaleImage() to scale the texture image. Linear interpolation and box filtering. See OpenGL Programming Guide for more detail

Specify the Texture 5/7 Modifying the texture data  void glTexSubImage2D(GLenum target, GLint level, GLint xoffset, GLint yoffset, GLsizei width, GLsizei height, GLenum format, GLenum type, const GLvoid *pixels); Define a 2D texture image to replace all or part of a contiguous subregion. xoffset and yoffset : specify the texels offset in x and y direction. Other parameters are the same as glTexImage2D().

Specify the Texture 6/7  Also, the frame buffer can be used as source of image data to modify the texture by glCopyTexSubImage2D().

Specify the Texture 7/7 One-Dimensional Textures  glTexImage1D();  glCopyTexImage1D();  glTexSubImage1D();  glCopyTexSubImage1D(); The parameters are the same as those of 2D.

Apply texture to objects 1/2  Using glTexEnv() to indicate how textures are applied to objects (how the texels are combined with the original pixels). void glTexEnv{if}[v](GLenum target, GLenum pname, TYPE param);  target must be GL_TEXTURE_ENV  If pname is GL_TEXTURE_ENV_MODE, param can be GL_DECAL, GL_REPLACE, GL_MODULATE, GL_BLEND  See next page for how it works  If pname is GL_TEXTURE_ENV_COLOR, param is an array of R, G, B, A components.

Apply texture to objects 2/2 Base Internal FormatGL_REPLACEGL_MODULATE GL_ALPHAC = Cf, A = AtC = Cf, A = AfAt GL_LUMINANCEC = Lt, A = AfC = CfLt, A = Af GL_LUMINANCE_ALPHAC = Lt, A = AtC = CfLt, A = AfAt GL_INTENSITYC = It, A = ItC = CfIt, A = AfIt GL_RGBC = Ct, A = AfC = CfCt, A = Af GL_RGBAC = Ct, A = AtC = CfCt, A = AfAt Base Internal FormatGL_DECALGL_BLEND GL_ALPHAUndefinedC = Cf, A = AfAt GL_LUMINANCEUndefinedC = Cf(1-Lt)+CcLt, A = Af GL_LUMINANCE_ALPHAUndefinedC = Cf(1-Lt)+CcLt, A = AfAt GL_INTENSITYUndefinedC = Cf(1-It)+CcIt, A = Af(1-It)+AcIt GL_RGBC = Ct, A = AfC = Cf(1-Ct)+CcCt, A = Af GL_RGBAC = Cf(1-At)+CtAt, A = AtC = Cf(1-Ct)+CcCt, A = AfAt Xc indicates the values assigned with GL_TEXTURE_ENV_COLOR. Xt is the values form texture, Xf is the values in the color buffers. C is the final output color value and A is the output alpha value.

Assign texture coordinates void glTexCoord{1234}{sifd}[v](TYPEcoords);  Uses like the glColor(), glNormal(), … described before.  The texture coordinates are floating points between 0.0 and 1.0.  Ex. glBegin(GL_TRIANGLES); glTexCoord2f(0.0, 0.0); glVertex2f(-1.0, -1.0); glTexCoord2f(1.0, 0.0); glVertex2f(1.0, -1.0); glTexCoord2f(0.0, 1.0); glVertex2f(0.0, 1.0); glEnd();

Texture Object 1/4 OpenGL 1.1 or later. A faster way to switch between textures  You don’t have to frequently TexImage(…) Steps of texture objects  Generate texture names.  Initially bind texture object to texture data.  Set properties for each texture object.  When rendering, just rebind the texture object and render the objects.

Texture Object 2/4 Generate texture names  void glGenTextures(GLsizei n, GLuint *textureNames); Return n number of unused names for texture objects in array textureNames.  GLboolean glIsTexture(GLuint textureName); Return true if textureName is currently bind to some texture object, and false otherwise.

Texture Object 3/4  void glBindTexture(GLenum target, GLuint textureName); target is GL_TEXTURE_2D or GL_TEXTURE_1D. textureName is the name generated by glGenTextures();  void glDeleteTextures(GLsizei n, const GLuint *textureNames); Delete n texture objects, named by elements in the array textureNames.

Texture Object 4/4 GLuint texObject[2];// texture object glGenTextures(2, texObject);// generate a new texture object // Bind Texture 1 glBindTexture(GL_TEXTURE_2D, texObject[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, image1); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); // Bind Texture 2 glBindTexture(GL_TEXTURE_2D, texObject[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, image2); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_BLEND); // Render a sphere using texture 1: glBindTexture(GL_TEXTURE_2D, texObject[0]); glutSolidSphere(1.0, 16, 16); // Render a teapot using texture 2: glBindTexture(GL_TEXTURE_2D, texObject[0]); glutSolidTeapot(1.0); …

Filtering 1/3 The problem  After being mapped to a polygon and transformed into screen coordinates, the texels rarely correspond to individual pixels of the final screen image.

Filtering 2/3 Two cases of problem  Magnification  Minification

Filtering 3/3  glTexParameter{if}(Glenum target, Glenum pname, TYPE param); target : GL_TEXTURE_2D, GL_TEXTURE_1D pname : GL_TEXTURE_MAG_FILTER, GL_TEXTURE_MIN_FILTER param : GL_NEAREST, GL_LINEAR, …  To apply mipmap, using the following parameters (only for GL_MIN_FILTER) : GL_NEAREST_MIPMAP_NEAREST GL_NEAREST_MIPMAP_LINEAR GL_LINEAR_MIPMAP_LINEAR GL_LINEAR_MIPMAP_NEAREST See OpenGL Programming Guide for more detail.

Mipmap 1/5 The Problem Without MipmapWith Mipmap

Mipmap 2/5 Mipmap  A series of prefiltered texture maps of decreasing resolutions.

Mipmap 3/5  Call the glTexImage2D() with different level, width, height and image.  OpenGL automatically determines which texture map to use based on the size (pixels) of the objects being mapped.  To use mipmap, all sizes of texture in powers of 2 between largest size and 1x1 map are required.

Mipmap 4/5 Ex. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 8, 8, 0, GL_RGB, GL_FLOAT, checkImage0); glTexImage2D(GL_TEXTURE_2D, 1, GL_RGB, 4, 4, 0, GL_RGB, GL_FLOAT, checkImage0); glTexImage2D(GL_TEXTURE_2D, 2, GL_RGB, 2, 2, 0, GL_RGB, GL_FLOAT, checkImage0); glTexImage2D(GL_TEXTURE_2D, 3, GL_RGB, 1, 1, 0, GL_RGB, GL_FLOAT, checkImage0); For a 8x8 texture, 4 levels mipmaps are required.

Mipmap 5/5  Automatic Mipmap generation Construct the highest level (level-0) map Use gluBuild2DMipmaps().  int gluBuild2DMipmaps(GLenum target, GLint components, GLint width, Glint height, GLenum format, GLenum type, void* data);  Parameters are the same as glTexImage2D().

Repeating and Clamping 1/3 Texture coordinate is defined between 0.0 – 1.0  What if we assign coordinates greater than 1.0? glTexParameter*() can also used to define how values beyond the range will be treated.  void glTexParameter{if}(GLenum target, GLenum pname, TYPE param); target : GL_TEXTURE_2D, GL_TEXTURE_1D pname : GL_TEXTURE_WARP_S, GL_TEXTURE_WARP_T

Repeating and Clamping 2/3 pnameparam GL_TEXTURE_WARP_SGL_REPEAT, GL_CLAMP GL_TEXTURE_WARP_TGL_REPEAT, GL_CLAMP GL_TEXTURE_MAG_FILTERGL_NEAREST, GL_LINEAR GL_TEXTURE_MAG_FILTERGL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST GL_TEXTURE_BORDER_COLORcolor value GL_TEXTURE_PRIORITYpriority of current texture image between 0.0 – 1.0

Repeating and Clamping 3/3 Repeat both S, T Clamp both S, T Repeat T but Clamp S glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex2f(-2.0, -1.0); glTexCoord2f(0.0, 3.0); glVertex2f(-2.0, 1.0); glTexCoord2f(0.0, 3.0); glVertex2f(0.0, 1.0); glTexCoord2f(3.0, 0.0); glVertex2f(0.0, -1.0); glEnd();

Texture Sample 1/3 void GL_init(void) { makeCheckImage(); glClearColor (0.0, 0.0, 0.0, 0.0); glGenTextures(1, &texObject);// generate a new texture object glBindTexture(GL_TEXTURE_2D, texObject);// bind texture object for later use // repeat texture glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // filter glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST_MIPMAP_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); // specify the texture with image glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage0); // automatically generate mipmaps gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, 256, 256, GL_RGBA, GL_UNSIGNED_BYTE, checkImage0); // set texture function glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); // enable 2D texture glEnable(GL_TEXTURE_2D); }

Texture Sample 2/3 void GL_display(void) { glClear(GL_COLOR_BUFFER_BIT); // bind the texture object for rendering glBindTexture(GL_TEXTURE_2D, texObject); glBegin(GL_QUADS);// assign texture coordinate and render it glTexCoord2f(0.0, 0.0);glVertex3f( , , ); glTexCoord2f(0.0, 10.0);glVertex3f(1000.0, , ); glTexCoord2f(10.0, 10.0);glVertex3f(1000.0, , 0.0); glTexCoord2f(10.0, 0.0);glVertex3f( , , 0.0); glEnd(); glFlush(); }

Texture Sample 3/3

Automatic Texture-Coordinate Generation 1/4  void glTexGen{ifd}[v](GLenum coord, GLenum pname, TYPEparam); coord can be GL_S, GL_T, GL_R, GL_Q pname param GL_TEXTURE_GEN_MODEGL_OBJECT_LINEAR, GL_GL_EYE_LINEAR, GL_SPHERE_MAP GL_OBJECT_PLANEPlane coefficient array GL_EYE_PLANEPlane coefficient array

Automatic Texture-Coordinate Generation 2/4  Plane coefficient array Generated plane = p1X + p2Y + p3Z + p4W p1, p2, p3, p4 are components of plane coefficient array.  GL_TEXTURE_GEN_MODE (a) GL_OBJECT_LINEAR with plane x = 0 (b) GL_OBJECT_LINEAR with plane x+y+z = 0 (c) GL_EYE_LINEAR with plane x = 0

Automatic Texture-Coordinate Generation 3/4 Ex. Environment Map  Apply the following code glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP); glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); The GL_SPHERE_MAP constant creates the proper texture coordinates for the environment mapping

Automatic Texture-Coordinate Generation 4/4 Sphere MapObject with Environment Map

Any Question ?