1 of 41

Texturing

Instructor: Christopher Rasmussen (cer@cis.udel.edu)

Course web page:

https://goo.gl/th9HB2

March 8, 2018 ❖ Lecture 10

2 of 41

Outline

  • Lighting in GLSL
  • Texture mapping
  • Bump, displacement mapping
  • HW #2

3 of 41

Lighting details

  • What do we need to “light” a piece of surface?
    • Normal n = (nx, ny, nz)
    • Light direction l = (lx, ly, lz) (computed from surface & light positions)
    • View direction v = (vx, vy, vz) (computed from surface and eye positions)
    • Surface properties & light colors
      • Specular, diffuse, ambient

from Hill

  • Lighting’s place in the pipeline
    • Must do before perspective division (in world or camera coordinates) because of nonlinear distortion of z

4 of 41

Shading methods: Notes

  • Flat: Compute ctotal at one vertex per polygon, use same value for every pixel in polygon
    • Infinite viewpoint v/light l: Same value for all vertices in scene
  • Gouraud: Compute different ctotal at each vertex of a polygon, linearly interpolate to interior pixels
    • Different vertex colors because l, v, r , and possibly n are different at each vertex
  • Phong: Interpolate normals from polygon vertices to interior, recompute ctotal at each pixel
    • Interpolation changes length of normals, so be sure to normalize them to unit length before computing ctotal

5 of 41

Tutorial #8:

Setting up in OpenGL: Normals

Just like vertex position and color VBOs…

GLuint normalbuffer;�glGenBuffers(1, &normalbuffer);�glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);�glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals[0], GL_STATIC_DRAW);

// 3rd attribute buffer : normals glEnableVertexAttribArray(2);� glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);� glVertexAttribPointer(� 2, // attribute 3, // size GL_FLOAT, // type GL_FALSE, // normalized? 0, // stride� (void*)0 // array buffer offset

);

6 of 41

Tutorial #8:

Setting up in OpenGL: Light

// position

GLuint LightID = glGetUniformLocation(programID, "LightPosition_worldspace");

glm::vec3 lightPos = glm::vec3(4,4,4);

glUniform3f(LightID, lightPos.x, lightPos.y, lightPos.z);

7 of 41

Tutorial #8: Vertex Shader variables

// Input vertex data, different for all executions of this shader.

layout(location = 0) in vec3 vertexPosition_modelspace;

layout(location = 1) in vec2 vertexUV;

layout(location = 2) in vec3 vertexNormal_modelspace;

// Output data ; will be interpolated for each fragment.

out vec2 UV;

out vec3 Position_worldspace;

out vec3 Normal_cameraspace;

out vec3 EyeDirection_cameraspace;

out vec3 LightDirection_cameraspace;

// Values that stay constant for the whole mesh.

uniform mat4 MVP;

uniform mat4 V;

uniform mat4 M;

uniform vec3 LightPosition_worldspace;

8 of 41

Tutorial #8 (modified): Fragment Shader

#version 330 core

// Interpolated values from the vertex shader

in vec3 Normal_cameraspace;

in vec3 EyeDirection_cameraspace;

in vec3 LightDirection_cameraspace;

// Ouput data

out vec3 color;

9 of 41

Tutorial #8 (modified): Fragment Shader

void main() {

vec3 LightColor = vec3(1,1,1); // Light properties

// Material properties

vec3 MaterialDiffuseColor = vec3(0.3,0.3,0.3);

vec3 MaterialAmbientColor = vec3(0.03,0.03,0.03);

vec3 MaterialSpecularColor = vec3(0.3,0.3,0.3);

// Normal of the computed fragment, in camera space

vec3 n = normalize( Normal_cameraspace );

// Direction to the light from this fragment

vec3 l = normalize( LightDirection_cameraspace );

10 of 41

Tutorial #8 (modified): Fragment Shader

// foreshortening factor

float cosTheta = clamp( dot( n,l ), 0,1 );

// Eye vector (fragment to camera) &

// direction in which light is reflected

vec3 E = normalize(EyeDirection_cameraspace);

vec3 R = reflect(-l,n);

// specular parameters

float cosAlpha = clamp( dot( E,R ), 0,1 );

float specExp = 25;

// final color

color = MaterialAmbientColor +

MaterialDiffuseColor * LightColor * cosTheta +

MaterialSpecularColor * LightColor * pow(cosAlpha,specExp);

}

11 of 41

What is Texture Mapping?

  • Apply a 2-D image to a 3-D object

http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube

12 of 41

What is Texture Mapping?

  • Spatially-varying modification of surface appearance at the pixel level
  • Characteristics
    • Color
    • Transparency

from Hill

13 of 41

Texture mapping applications: Text!

http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-11-2d-text

14 of 41

Texture mapping applications: Billboards

from Akenine-Moller & Haines

Also called ”impostors”: Image-aligned polygons in 3-D

from www.massal.net/projects

15 of 41

Why use texture mapping?

  • More detail without the cost of more complicated geometry
    • Modeling
    • Display

16 of 41

Why use texture mapping?

  • More detail without the cost of more complicated geometry
    • Modeling
    • Display
  • Layer multiple texture maps → Can modulate color + other surface characteristics

17 of 41

Why use texture mapping?

  • More detail without the cost of more complicated geometry
    • Modeling
    • Display
  • Layer multiple texture maps → Can modulate color + other surface characteristics
  • “Lookup table” for precomputed quantities
    • Lighting
    • Shadows
    • Etc.

18 of 41

What is Texture Mapping?

  • Spatially-varying modification of surface appearance at the pixel level
  • Characteristics
    • Color
    • Transparency
    • Shininess

from Hill

19 of 41

Texture mapping: NO specular map

from www.reallusion.com

from www.cgarena.com

20 of 41

Texture mapping: YES specular map

from www.reallusion.com

from www.cgarena.com

21 of 41

What is Texture Mapping?

  • Spatially-varying modification of surface appearance at the pixel level
  • Characteristics
    • Color
    • Transparency
    • Shininess
    • Bumpiness
    • Etc.

from Hill

22 of 41

Bump Mapping

  • So far we’ve been thinking of textures modulating color and transparency only
    • Billboards, decals, lightmaps, etc.
  • But any other per-pixel properties are fair game...
  • Pixel normals usually smoothly varying
    • Computed at vertices for Gouraud shading; color interpolated
    • Interpolated from vertices for Phong shading
  • Textures allow setting per-pixel normal with a bump map

23 of 41

Bump map: Example

from wikipedia.org

Sphere WITH bumps

Sphere

24 of 41

Bump + color map: Example

25 of 41

Texture mapping: Examples

Examples of modulating different characteristics

with identical sphere geometry

26 of 41

Bump mapping: Why?

  • Can get a lot more surface detail without expense of more object vertices to light, transform

courtesy of Nvidia

27 of 41

Bump mapping: How?

  • Idea: Perturb pixel normals n(u, v) derived from object geometry to get additional detail for shading
  • Compute lighting per pixel (like Phong)

from Hill

28 of 41

Bump mapping: Representations

  • 3-D vector m(u, v) added directly to normal n
  • Or: 2-D vector of coefficients (bu, bv) that scale u, v vectors tangent to surface

from Akenine-Moller & Haines

29 of 41

Bump representation: Height map f(u, v)

  • Store just scalar “altitude” at each pixel
  • Get bu, bv from partial derivatives:

    • Approximate with finite differencing

from Akenine-Moller

& Haines

30 of 41

Example: Converting height maps to normal displacements (aka normal maps)

Z coordinate set to some constant scale factor; (X, Y) normalized to [0, 1] range.

Right image is mostly blue because “straight up” vector is (0.5, 0.5, 1)

courtesy of Nvidia

31 of 41

Bump mapping: Example

from MIT CG lecture notes

+

=

32 of 41

Bump mapping: Example

courtesy of A. Awadallah

Height map

Bump texture applied to teapot

33 of 41

Bump mapping: Issues

  • Bumps don’t cast shadows
  • Geometry doesn’t change, so silhouette of object is unaffected
  • Textures can be used to modify underlying geometry with displacement maps
    • Generally in direction of surface normal

courtesy of Nvidia

34 of 41

Displacement Mapping

courtesy of spot3d.com

Bump mapping

Displacement mapping

35 of 41

Displacement Mapping – Height Maps

courtesy of artofillusion.org -- Julian MacDonald

36 of 41

Displacement Mapping the Sphere

courtesy of geeks3d.com

37 of 41

HW #2: "Multitextured Earth" = multiearth

  • Load multiple textures representing different information about surface of Earth
    • Color
    • Clouds
    • Night
    • Water
    • Elevation
  • Number keys to switch display
  • A/D/S/W to rotate/orbit
  • Z/C zoom, X to reset camera

38 of 41

HW #2: "multiearth" details

  • Vertex shader exactly same as tutorial #5
  • Fragment shader part 1

#version 330 core

// Interpolated values from the vertex shaders

in vec2 UV;

// Ouput data

out vec3 color;

// Values that stay constant for the whole mesh.

uniform sampler2D tex_elevation;

uniform sampler2D tex_water;

uniform sampler2D tex_night;

uniform sampler2D tex_clouds;

uniform sampler2D tex_color;

uniform int draw_mode;

39 of 41

HW #2: "multiearth" details

  • Fragment shader part 2

void main() {

if (draw_mode == 0)

color = vec3(1, 0, 0);

else if (draw_mode == 1)

color = texture( tex_elevation, UV ).bgr;

else if (draw_mode == 2)

color = texture( tex_water, UV ).bgr;

else if (draw_mode == 3)

color = texture( tex_night, UV ).bgr;

else if (draw_mode == 4)

color = texture( tex_clouds, UV ).bgr;

else if (draw_mode == 5)

color = texture( tex_color, UV ).bgr;

// simple multi-texturing

else if (draw_mode == 6)

color = mix(texture(tex_clouds,UV).bgr, texture(tex_color,UV).bgr, 0.75);

}

40 of 41

HW #2: "multiearth" details

  • Set up uniform to communicate to shaders the drawing "mode" we want

GLuint drawloc = glGetUniformLocation(programID, "draw_mode");

glUniform1i(drawloc, 0); // 0 is initial mode

  • Load all textures and associate with named uniforms

glGetUniformLocation(programID, texvarname[i].c_str());

teximage[i] = loadBMP_custom(texfilename[i].c_str());

  • In drawing loop, rebind all textures to units

for (int i = 0; i < texfilename.size(); i++) {

glActiveTexture(GL_TEXTURE0 + i);

glBindTexture(GL_TEXTURE_2D, teximage[i]);

glUniform1i(texID[i], i);

}

41 of 41

HW #2: Requirements (frag shader & main.cpp)

  • Draw mode 7: Diffuse lighting on color texture
    • Choose light position/color and diffuse reflectance yourself
    • You can infer sphere normal at each fragment from position
    • See tutorial #8 on shading
  • Draw mode 8: Implement specular lighting with Phong shading ONLY where there is water
  • Draw mode 9: Show the night texture on the dark side and the color texture on the lit side of the Earth relative to the light position
    • Animate the light going around the Earth
    • Make a smooth transition at the border between the dark and light regions (hint: smoothstep)
    • You do not have to do diffuse or specular lighting in this mode
  • Grad students: Bump mapping with elevation texture. Entire surface is specular. dFdx, dFdy might help