MULTIMEDIA

iPhone 3D Programming : Textures and Image Capture - Texture Coordinates Revisited

1/29/2011 7:05:21 PM
Recall that texture coordinates are defined such that (0,0) is the lower-left corner and (1,1) is the upper-right corner. So, what happens when a vertex has texture coordinates that lie outside this range? The sample code is actually already sending coordinates outside this range. For example, the TextureCount parameter for the sphere is (20,35).

By default, OpenGL simply repeats the texture at every integer boundary; it lops off the integer portion of the texture coordinate before sampling the texel color. If you want to make this behavior explicit, you can add something like this to the rendering engine code:

glBindTexture(GL_TEXTURE_2D, m_gridTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

The wrap mode passed in to the third argument of glTexParameteri can be one of the following:

GL_REPEAT

This is the default wrap mode; discard the integer portion of the texture coordinate.

GL_CLAMP_TO_EDGE

Select the texel that lies at the nearest boundary.

GL_MIRRORED_REPEAT[5]

If the integer portion is an even number, this acts exactly like GL_REPEAT. If it’s an odd number, the fractional portion is inverted before it’s applied.

Figure 1 shows the three wrap modes. From left to right they’re repeat, clamp-to-edge, and mirrored. The figure on the far right uses GL_REPEAT for the S coordinate and GL_CLAMP_TO_EDGE for the T coordinate.

Figure 1. Four texture wrap configurations


What would you do if you wanted to animate your texture coordinates? For example, say you want to gradually move the grasshoppers in Figure 5-2 so that they scroll around the Möbius strip. You certainly wouldn’t want to upload a new VBO at every frame with updated texture coordinates; that would detrimental to performance. Instead, you would set up a texture matrix.  The following snippet shows how you’d set up a texture matrix with ES 1.1 (with ES 2.0, there’s no built-in texture matrix, but it’s easy to create one with a uniform variable):

glMatrixMode(GL_TEXTURE);
glTranslatef(grasshopperOffset, 0, 0);

In this book, we use the convention that (0,0) maps to the upper-left of the source image (Figure 5-3) and that the Y coordinate increases downward.

By the Spec

Our texture coordinate convention is not what you’d find in the OpenGL specification from Khronos, which proclaims (0,0) to be the lower-left corner. However, the spec also says that when uploading an image with glTexImage2D, it should be arranged in memory such that the first row of data corresponds to the bottom row in the image. In all of the methods we’re using to decode the image, the top row of data comes first.


By the way, saying that (1,1) maps to a texel at the far corner of the texture image isn’t a very accurate statement; the corner texel’s center is actually a fraction, as shown in Figure 2. This may seem pedantic, but you’ll see how it’s relevant when we cover filtering, which comes next.

Figure 2. Texture coordinates in a 4x4 texture


Other  
  •  Programming with DirectX : Additional Texture Mapping - Sprites
  •  Programming with DirectX : Additional Texture Mapping - Alpha Mapping
  •  Microsoft XNA Game Studio 3.0 : Writing Your First Program (part 2) - Running the Same XNA Game on Different Devices
  •  Microsoft XNA Game Studio 3.0 : Writing Your First Program (part 1)
  •  Programming with DirectX : Shading and Surfaces - Additional Texturing Topics
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 4) - Enabling Textures with ES2::RenderingEngine
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 3) - Enabling Textures with ES1::RenderingEngine
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 2) - Generating Texture Coordinates
  •  iPhone 3D Programming : Adding Textures to ModelViewer (part 1) - Enhancing IResourceManager
  •  Programming with DirectX : Shading and Surfaces - Implementing Texture Mapping (part 2) - Multi Texture Demo
  •  Programming with DirectX : Shading and Surfaces - Implementing Texture Mapping (part 1) - 2D Texture Mapping Demo
  •  Building Out Of Browser Silverlight Applications - Using COM Interoperability and File System Access
  •  Building Out Of Browser Silverlight Applications - Controlling the Application Window
  •  iPhone 3D Programming : Adding Depth and Realism - Loading Geometry from OBJ Files
  •  iPhone 3D Programming : Adding Depth and Realism - Better Wireframes Using Polygon Offset
  •  Programming with DirectX : Textures in Direct3D 10 (part 2)
  •  Programming with DirectX : Textures in Direct3D 10 (part 1) - Textures Coordinates
  •  Programming with DirectX : Shading and Surfaces - Types of Textures
  •  iPhone 3D Programming : Adding Shaders to ModelViewer (part 2)
  •  iPhone 3D Programming : Adding Shaders to ModelViewer (part 1) - New Rendering Engine
  •  
    Most View
    Installing HP-UX : Loading Patches
    LG LM9600 47” Smart TV - A New Direction
    HP Photosmart 7520 e-All-in-One
    Acer CloudMobile - Ambitious Android Phone (Part 2)
    What To Do With An Old Mac (Part 1)
    Deploying to an iPhone, Debugging, and Testing : Distributing Your Application
    How To Choose A Printer (Part 2)
    Nvidia GeForce GTX Titan 6 GB Graphics Card Review (Part 1)
    Appetizing Art - The Complete Picture (Part 2)
    How To Deal With Windows And Network-Related Problems (Part 2)
    Top 10
    The NZXT Kraken X40 Compact Liquid Cooler Review (Part 3)
    The NZXT Kraken X40 Compact Liquid Cooler Review (Part 2)
    T-Mobile’s Samsung Galaxy Note II Review (Part 6)
    T-Mobile’s Samsung Galaxy Note II Review (Part 5)
    T-Mobile’s Samsung Galaxy Note II Review (Part 4)
    T-Mobile’s Samsung Galaxy Note II Review (Part 3)
    T-Mobile’s Samsung Galaxy Note II Review (Part 2)
    T-Mobile’s Samsung Galaxy Note II Review (Part 1)
    Sony Cybershot DSC-TF1 - Affordable Water-Resistant Camera
    Buffalo MiniStation Slim 500GB External Hard Drive