Texture Mapping in OpenGL

Click here to load reader

download Texture Mapping in OpenGL

of 48

  • date post

  • Category


  • view

  • download


Embed Size (px)


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. - PowerPoint PPT Presentation

Transcript of Texture Mapping in OpenGL

  • Texture Mapping in OpenGL

  • Texture MappingImaging we are pasting a picture onto a modelWhich part of the picture will be pasted onto which part of the model?How do we combine the picture with the models existing material and lighting?

  • Texture MappingNote the darker parts on the wing

  • IntroductionWhich part of the picture will be pasted onto which part of the model? is actuallyFor 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

  • IntroductionBackward mapping is suitable for the pipelineThe data after rasterization are pixelsThus a per-pixel processing fashion is adoptedThe pipeline will find out proper texels the pixel mapped toand decide the final resultif it have enough informationWhat are the information needed?

  • What is a texture?Textures are arrays of dataEx. Color, luminance, alpha, normalThe individual elements in a texture array are called texels1D, 2D, 3DWe will focus on 2D textures

  • Consider Texture MappingHow do we construct a texture?Which texture the texel comes from?Texture objectHow do we map the pixels to the texels?Texture coordinatesHow 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 OpenGLSteps in Texture MappingSpecify a textureEnable texture mappingIndicate how the texture is applied to objectsDraw the scene

  • How to use textures in OpenGLPreparing:Prepare the image as an continuous memory chunkGenerate it or using some external librariesCreate a texture from the imageWhat information the memory carries? RGB? RGBA? Height/widthData format? float? unsigned int?

  • Texture Example1/4We 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 fashionstatic GLubyte checkImage0[256][256][4];// initially generate a checker image by codevoid makeCheckImage(void){GLubyte c;for(int i=0; i
  • How to use textures in OpenGLSetting:Enable texture functionalityCan be enabled/disabled anytimeglEnable(GL_TEXTURE_2D)Specify how to apply the texture on pixelsReplace/Blend/Modulate/DecalglTexEnv()Other parametersfilters

  • Texture Example2/4void 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 colorsglTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);// filtersglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);// enable 2D textureglEnable(GL_TEXTURE_2D);}

  • How to use textures in OpenGLRenderTexture coordinateAssign every vertex a texture coordinateOrLet OpenGL generate texture coordinates for you

  • How to use textures in OpenGL//an example of manully assigning texture coordinatesglBegin(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 APINow we have rough ideas in texture mapping, lets go though interfaces related to texture mapping in OpenGL

  • Specify the Texture1/7Define 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 Texture2/7target : GL_TEXTURE_2D, GL_PROXY_TEXTURE_1Dlevel : 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 textureMust be power of 2.

  • Specify the Texture4/7In OpenGL, the length and width of the texture array must be power of 2For 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 Texture5/7Modifying the texture datavoid 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 Texture6/7Also, the frame buffer can be used as source of image data to modify the texture by glCopyTexSubImage2D().

  • Specify the Texture7/7One-Dimensional TexturesglTexImage1D();glCopyTexImage1D();glTexSubImage1D();glCopyTexSubImage1D();The parameters are the same as those of 2D.

  • Apply texture to objects1/2Using 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_ENVIf pname is GL_TEXTURE_ENV_MODE, param can be GL_DECAL, GL_REPLACE, GL_MODULATE, GL_BLENDSee next page for how it worksIf pname is GL_TEXTURE_ENV_COLOR, param is an array of R, G, B, A components.

  • Apply texture to objects2/2Xc 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 coordinatesvoid 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 Object1/4OpenGL 1.1 or later.A faster way to switch between texturesYou dont have to frequently TexImage()Steps of texture objectsGenerate 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 Object2/4Generate texture namesvoid 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 Object3/4void 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 Object4/4GLuint texObject[2];// texture object

    glGenTextures(2, texObject);// generate a new texture object

    // Bind Texture 1glBindTexture(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 2glBindTexture(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);

  • Filtering1/3The problemAfter being mapped to a polygon and transformed into screen coordinates, the texels rarely correspond to individual pixels of the final screen image.

  • Filtering2/3Two cases of problemMagnificationMinification

  • Filtering3/3glTexParameter{if}(Glenum target, Glenum pname, TYPE param);target : GL_TEXTURE_2D, GL_TEXTURE_1Dpname : GL_TEXTURE_MAG_FILTER, GL_TEXTURE_MIN_FILTERparam : 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_NEARESTSee OpenGL Programming Guide for more detail.

  • Mipmap1/5The Problem

  • Mipmap2/5MipmapA series of prefiltered texture maps of decreasing resolutions.

  • Mipmap3/5Call 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.

  • Mipmap4/5Ex.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.

  • Mipmap5/5Automatic Mipmap generationConstruct 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().