16827 Computer Graphics 12 - Copy

Embed Size (px)

Citation preview

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    1/34

    Computer Graphics

    Texture Mapping

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    2/34

    Objectives

    Introduce Mapping Methods

    Texture Mapping

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    3/34

    Texture can either be two-dimensional or three-dimensional

    and is distinguished by visual and physical textures.

    1. Physical Texture: Physical texture is known as actual textureor tactile texture. It depicts the variations on a particular

    surface.

    Physical texture can be felt by touch. Fur, wood, grains, statues,

    sand, glass, and so on can be included in this type of texture.

    2. Visual Texture: Visual texture is the perception of having a

    physical texture. Every material has its own visual texture which

    needs to be considered before creating a composition. Textures

    are created by repetitive use of shapes and lines.

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    4/34

    Texture Mapping: Texture mapping makes use

    of photographical or procedural generatedtextures. These procedural generated patterns

    are made to appear very natural by subjecting

    them to noise or random variation. Thetexture mapping method involves scanning or

    digitizing the picture into another space, 2-D

    texture space.

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    5/34

    The Limits of Geometric Modeling

    Although graphics cards can render over 10

    million polygons per second, that number is

    insufficient for many phenomena

    Clouds

    Grass

    Terrain

    Skin

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    6/34

    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

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    7/34

    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

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    8/34

    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

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    9/34

    Texture Surface Patterns

    Texture mapping

    Consists in mapping patterns onto the

    geometric description of an object.

    TexturePattern to be mapped

    either an array of color values

    or as a procedure that

    modifies existing color

    or as an image from an image file

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    10/34

    Texture Mapping

    geometric model texture mapped

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    11/34

    Environment Mapping

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    12/34

    Bump Mapping

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    13/34

    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

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    14/34

    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

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    15/34

    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

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    16/34

    Texture Mapping

    parametric coordinates

    texture coordinates

    world coordinateswindow coordinates

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    17/34

    Mapping Functions

    Basic problem is how to find the maps

    Consider mapping from texture coordinates toa point a surface

    Appear to need three functionsx = x(s,t)

    y = y(s,t)

    z = z(s,t) But we really want

    to go the other way

    s

    t

    (x,y,z)

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    18/34

    OpenGL Texture Mapping

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    19/34

    Objectives

    Introduce the OpenGL texture functions and

    options

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    20/34

    Basic Strategy

    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 application3. specify texture parameters

    filtering

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    21/34

    Texture Mapping

    s

    t

    x

    y

    z

    image

    geometry display

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    22/34

    Texture Example

    The texture (below) is a

    256 x 256 image that has

    been mapped to a

    rectangular polygon whichis viewed in perspective

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    23/34

    Texture Mapping and the OpenGL

    Pipeline

    geometry pipelinevertices

    pixel pipelineimage

    fragment

    processor

    Images and geometry flow through

    separate pipelines that join during

    fragment processing

    complex textures do not affect geometric

    complexity

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    24/34

    Define a texture image from an array of

    texels(texture elements) in CPU memory

    Glubyte my_texels[512][512];

    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

    Specifying a Texture Image

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    25/34

    Define Image as a Texture

    glTexImage2D( target, level, components,w, h, border, format, type, texels);

    target: type of texture, e.g.GL_TEXTURE_2D

    level: used for mipmapping

    components: number of color components

    w, h: width and height oftexels in pixels

    border: used for smoothing

    format and type: describe texels

    texels: pointer to texel array

    glTexImage2D(GL_TEXTURE_2D, 0, 3, 512, 512, 0,GL_RGB, GL_UNSIGNED_BYTE, my_texels);

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    26/34

    Based on parametric texture coordinates

    glTexCoord*()specified at each vertex

    s

    t1, 1

    0, 1

    0, 0 1, 0

    (s, t) = (0.2, 0.8)

    (0.4, 0.2)

    (0.8, 0.4)

    A

    B C

    a

    bc

    Texture Space Object Space

    Mapping a Texture

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    27/34

    Typical Code

    glBegin(GL_POLYGON);

    glColor3f(r0, g0, b0); //if no shading used

    glNormal3f(u0, v0, w0); // if shading used

    glTexCoord2f(s0, t0);

    glVertex3f(x0, y0, z0);glColor3f(r1, g1, b1);

    glNormal3f(u1, v1, w1);

    glTexCoord2f(s1, t1);

    glVertex3f(x1, y1, z1);.

    .

    glEnd();Note that we can use vertex arrays to increase efficiency

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    28/34

    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 trapezoidshowing effects of

    bilinear interpolation

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    29/34

    Magnification and

    Minification

    Texture Polygon

    Magnification Minification

    PolygonTexture

    More than one texel can cover a pixel (minification) ormore 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

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    30/34

    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_NEAREST);

    Note that linear filtering requires a border of an

    extra texel for filtering at edges (border = 1)

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    31/34

    Applying Textures II

    1. specify textures in texture objects

    2. set texture filter

    3. set texture function

    4. enable texturing5. supply texture coordinates for vertex

    coordinates can also be generated

    Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    32/34

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    33/34

    void display(void) {glClearColor(1.0,1.0,0.0,0.0);

    glClear(GL_COLOR_BUFFER_BIT);

    glBegin(GL_TRIANGLES);glTexCoord2f(4, 8); glVertex2f(3, 3);

    glTexCoord2f(0.0, 0.0); glVertex2f(0, 0);

    glTexCoord2f(8, 0.0); glVertex2f(3, 0);

    glEnd();

    glFlush(); }int main(int argc,char** argv){

    glutInit(&argc,argv);

    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);

    glutInitWindowSize(600,600);

    glutInitWindowPosition(100,100);glutCreateWindow("Texture");

    glutDisplayFunc(display);

    glutReshapeFunc(reshape);

    glutMainLoop();

    } Madhulika (18010), Assistant Professor,LPU.

  • 8/12/2019 16827 Computer Graphics 12 - Copy

    34/34

    Thank You

    Madhulika (18010) Assistant Professor