CS179: GPU Programming Lecture 4: Textures Original Slides by Luke Durant, Russel McClellan, Tamas Szalay
Today Recap Textures What are textures? Traditional uses Alternative uses
Recap 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?
Textures 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
Textures 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 varyings) 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!
Mipmaps
Mipmaps
Texture Parameters gltexparameterf(gl_texture_2d, param, value) GL_TEXTURE_WRAP_S/T {GL_REPEAT, GL_CLAMP} Sets behavior for coords outside of (0, 1) GL_TEXTURE_MIN/MAG_FILTER {GL_LINEAR, GL_NEAREST} Sets inter-pixel sampling mode (linear is bilinear, samples nearest 4 pixels) when zooming in or out
Multitexturing Unfortunately, glbindtexture only works for one texture at a time To use multiple textures, use glactivetexture glactivetexture(gl_texturen), replace n with 0,1,... 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 shader 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 abbreviations: 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
Summary 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 textures As mentioned, can sample in either vertex shader or frag shader