MULTIMEDIA

iPhone 3D Programming : Blending and Augmented Reality - Shifting Texture Color with Per-Vertex Color

2/19/2011 4:32:37 PM
Sometimes you’ll need to uniformly tweak the alpha values across an entire texture. For example, you may want to create a fade-in effect or make a texture semitransparent for drawing a heads-up display (HUD).

With OpenGL ES 1.1, this can be achieved simply by adjusting the current vertex color:

glColor4f(1, 1, 1, alpha);

By default, OpenGL multiplies each component of the current vertex color with the color of the texel that it’s rendering. This is known as modulation, and it’s actually only one of many ways that you can combine texture color with per-vertex color .

If you’re using a texture with premultiplied alpha, then the vertex color should also be premultiplied. The aforementioned function call should be changed to the following:

glColor4f(alpha, alpha, alpha, alpha);

Sometimes you may want to throttle back only one color channel. For example, say your app needs to render some red and blue buttons and that all the buttons are identical except for their color. Rather than wasting memory with multiple texture objects, you can create a single grayscale texture and modulate its color, like this:

// Bind the grayscale button texture.
glBindTexture(GL_TEXTURE_2D, buttonTexture)

// Draw green button.
glColor4f(0, 1, 0, 1);
glDrawElements(...);

// Draw red button.
glColor4f(1, 0, 0, 1);
glDrawElements(...);

With ES 2.0, the modulation needs to be performed within the pixel shader itself:

varying lowp vec4 Color;
varying mediump vec2 TextureCoord;

uniform sampler2D Sampler;

void main(void)
{
gl_FragColor = texture2D(Sampler, TextureCoord) * Color;
}

Other  
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Extensions and Their Uses
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Caveats
  •  Building LOB Applications : Using Visual Studio 2010 WCF RIA Data Services Tooling
  •  Building LOB Applications : Implementing CRUD Operations in WCF Data Services
  •  iPhone 3D Programming : Blending and Augmented Reality - Wrangle Premultiplied Alpha
  •  iPhone 3D Programming : Blending and Augmented Reality - Blending Recipe
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 3)
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 2)
  •  Microsoft XNA Game Studio 3.0 : Controlling Color (part 1) - Games and Classes & Classes as Offices
  •  Microsoft XNA Game Studio 3.0 : Working with Colors
  •  iPhone 3D Programming : Textures and Image Capture - Creating Textures with the Camera
  •  iPhone 3D Programming : Textures and Image Capture - Dealing with Size Constraints
  •  Programming with DirectX : Game Math - Vectors
  •  iPhone 3D Programming : Textures and Image Capture - Generating and Transforming OpenGL Textures with Quartz
  •  iPhone 3D Programming : Textures and Image Capture - The PowerVR SDK and Low-Precision Textures
  •  Building LOB Applications : Using Visual Studio 2010 WCF Data Services Tooling
  •  Building LOB Applications : Accessing RESTful Data using OData
  •  Programming with DirectX : Additional Texture Mapping - Image Filters
  •  Microsoft XNA Game Studio 3.0 : Making a Game Program
  •  iPhone 3D Programming : Textures and Image Capture - Texture Compression with PVRTC
  •  
    Most View
    Acer C7 Chromebook - An Inexpensive Chromebook
    Prolink Pro 1301WE 13.3-Inch Mobile LED Monitor
    Java EE 6 with GlassFish 3 Application Server : Core JSTL tag library
    Visual Basic 2010 : Localizing Applications - Windows Forms Localization
    HP Envy 23 TouchSmart All-in-One
    Master Black-White Copying
    AOC MyStage E2343Fi 23" LED Monitor
    FXHome Hitfilm Standard
    Server 2008 : Deploying Physical Security
    Touch and Go (Part 1)
    Top 10
    NZXT Phantom 630 Modular Full Tower Case
    Rosewill Launches Armor Evolution Mid-Tower Case
    Windows 8 - An In-Depth Expert Review (Part 6)
    Windows 8 - An In-Depth Expert Review (Part 5)
    Windows 8 - An In-Depth Expert Review (Part 4)
    Windows 8 - An In-Depth Expert Review (Part 3)
    Windows 8 - An In-Depth Expert Review (Part 2)
    Windows 8 - An In-Depth Expert Review (Part 1)
    Kobo Mini: Does The World Need A Smaller E-Reader? (Part 2)
    Kobo Mini : Does The World Need A Smaller E-Reader? (Part 1)