teaching machines

CS 455 Lecture 12 – Texture Mapping

March 10, 2015 by . Filed under cs455, lectures, spring 2015.

Agenda

While You’re Waiting

TODO

Texture Mapping

  1. Load in an image, noting its width, height, pixel data, and number of color channels. See the Image class in libtwodee or use your own. Shoot for dimensions that are powers of two. Otherwise, you may need to specify the byte alignment:
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  2. Create a texture, specifying its texture unit and texture parameters:
    Texture texture = new Texture(0);
    texture->Wrap(Texture::REPEAT);
    texture->Channels(Texture::RGB);
    texture->Magnify(Texture::MAGNIFY_NEAREST);
    texture->Minify(Texture::MINIFY_LINEAR);
    texture->Upload(image.GetWidth(), image.GetHeight(), image.GetPixels());
  3. Associate texture coordinates with each vertex either as a vertex attribute or computed in the shader.
  4. Add a sampler2D uniform to the shader where you’d like to do texture lookup:
    uniform sampler2D tex;
  5. In OnDraw or OnInitialize, register the texture with the sampler2D uniform:
    shader_program->SetUniform("tex", *texture);
  6. Retrieve the texture data using the texture2D function:
    vec3 rgb = texture2D(tex, ftexcoords).rgb;
    float alpha = texture2D(tex, ftexcoords).a;
    float red = texture2D(tex, ftexcoords).r;
  7. Incorporate the texture sample into your rendering in any way you please: as albedo, as an offset to the vertex data, as a mask to filter out fragments, and so on.

Haiku

on texture mapping
I’m know this place well
Just like the back of my hand
Where the map’s tattooed