Textures - Caltech


Textures - Caltech

CS179: GPU Programming

Lecture 4: Textures

Original Slides by Luke Durant, Russel McClellan, Tamas Szalay




– What are textures?

– Traditional uses

– Alternative uses


Our data so far:

– Vertices with associated data

Position, color, etc.

– Triangles

Fragments in triangle interpolate vertex data

All our data is constant!

– ex. we can only have triangle be single color

– How could we attach pictures to triangles?


Images stored in GPU memory

Variety of storage types, usually floats

– Ex: RGBA for each pixel

Can vary in size, resolution

– Generally powers of 2

OpenGL supports 1D, 2D, and 3D textures

– 2D are probably the most “intuitive”


Access data via Texture Coordinates

– Referred to as (u, v), or (s, t, p, q)

– Range from 0 to 1

(0, 0) (1, 0)

(0, 1)

(1, 1)

Texturing in the Pipeline

Specify texcoords per vertex

Interpolated between verts (like other


Fragment color is original image sampled at

interpolated coordinate

Texturing Complex Models

Texturing in OpenGL

Create texture: glGenTextures

Bind (select) texture: glBindTexture

Set parameters: glTexParameter[f/i]

Set image data: glTexImage2D

Enable textures: glEnable(GL_TEXTURE_2D)

Set texture coords: glTexCoord2f

– Between glBegin and glEnd

As always, this will be provided!



Texture Parameters

glTexParameterf(GL_TEXTURE_2D, param,




– Sets behavior for coords outside of (0, 1)



– Sets inter-pixel sampling mode (linear is bilinear,

samples nearest 4 pixels) when zooming in or out


Unfortunately, glBindTexture only works for

one texture at a time

To use multiple textures, use glActiveTexture

– glActiveTexture(GL_TEXTUREn), replace n with


– Now gl calls affect texture n

Set coords using

glMultiTexCoord(GL_TEXTUREn, s, t, p, q)

Texturing in GLSL

Attribute texture coordinate inputs to vertex


– gl_MultiTexCoordN (N = 0,...,7)

gl_TexCoord[ ] is a set of writeable varyings

To read textures, use 2D sampler uniforms:

– uniform sampler2D tex;

Access using texture2D function:

– ex: vec4 color = texture2D(tex, gl_TexCoord[0].st)

GLSL Swizzling

In above example, we access gl_TexCoord[0].st

– .st outputs a vec2

Can access vec data like this, using a variety of


– vec.xyzw, vec.stpq, vec.rgba

– All mean the same thing

We can also mix them:

– If vec is a vec3 = (1, 2, 3):

– vec.xy = (1, 2) and vec.yx = (2, 1) are both valid!

– Output vec2's


On OpenGL we:

– Create texture

– Set data

– Set params

– Set uniforms

– Set texcoords

On GLSL side we:

– Use built-in texture coordinates and varyings

– Define samplers for uniforms for each texture

– Use texture2D functions to read data

Don't worry, plenty of practice in lab2

Side Note

Rendering complex models:

– We don't want to hardcode it

– Going through a for-loop to assign texCoords to verts

This is really bad/slow

Instead, use pointers and buffers:

– glVertexPointer, glTexCoordPointer, etc.

– Buffers work like textures, create and bind them

You don't need to know the details for this class,

but it is good to know

Uses of Texturing

Texturing pre-generated models

– Used in almost all video games since Quake..

– Pretty simple, so we wont cover here

Modify surface properties

– Ex. suppose we want to model a bottle with a label

– Have a texture that is 0 where the glass is and 1

where the label is

– Or, “cheat” and store this 0 or 1 in the alpha channel of

the color map

Most of using shaders/textures is “cheating”

Uses of Texturing

Visual effects

– Ex: blur texture

– Sample all points around a coordinate, and average

– Pretty fast

Height maps

– Can be used to model terrain with only a 2D texture

– You'll use this idea in lab 2

NVIDIA Cascades

– 3D textures to generate rock formations

Using Textures in Vertex Shaders

Similar to frag shaders, can have sampler2D

uniforms in vertex shader

– Can store all sorts of info in textures for verts

– We'll use this more in lab 3

Data from Textures

Uniform arrays are limited in size

– Can't be bigger than a few kb

To pass large amounts of data, we need


As mentioned, can sample in either vertex

shader or frag shader

More magazines by this user
Similar magazines