Texturing
March 8, 2018 ❖ Lecture 10
Outline
Lighting details
from Hill
Shading methods: Notes
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
);
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);
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;
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;
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 );
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);
}
What is Texture Mapping?
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube
What is Texture Mapping?
from Hill
Texture mapping applications: Text!
http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-11-2d-text
Texture mapping applications: Billboards
from Akenine-Moller & Haines
Also called ”impostors”: Image-aligned polygons in 3-D
from www.massal.net/projects
Why use texture mapping?
Why use texture mapping?
Why use texture mapping?
What is Texture Mapping?
from Hill
Texture mapping: NO specular map
from www.reallusion.com
from www.cgarena.com
Texture mapping: YES specular map
from www.reallusion.com
from www.cgarena.com
What is Texture Mapping?
from Hill
Bump Mapping
Bump map: Example
from wikipedia.org
Sphere WITH bumps
Sphere
Bump + color map: Example
Courtesy of http://www.alejandrosegovia.net
Texture mapping: Examples
Examples of modulating different characteristics
with identical sphere geometry
Bump mapping: Why?
courtesy of Nvidia
Bump mapping: How?
from Hill
Bump mapping: Representations
from Akenine-Moller & Haines
Bump representation: Height map f(u, v)
from Akenine-Moller
& Haines
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
Bump mapping: Example
from MIT CG lecture notes
+
=
Bump mapping: Example
courtesy of A. Awadallah
Height map
Bump texture applied to teapot
Bump mapping: Issues
courtesy of Nvidia
Displacement Mapping
courtesy of spot3d.com
Bump mapping
Displacement mapping
Displacement Mapping – Height Maps
courtesy of artofillusion.org -- Julian MacDonald
Displacement Mapping the Sphere
courtesy of geeks3d.com
HW #2: "Multitextured Earth" = multiearth
HW #2: "multiearth" details
#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;
HW #2: "multiearth" details
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);
}
HW #2: "multiearth" details
GLuint drawloc = glGetUniformLocation(programID, "draw_mode");
glUniform1i(drawloc, 0); // 0 is initial mode
glGetUniformLocation(programID, texvarname[i].c_str());
teximage[i] = loadBMP_custom(texfilename[i].c_str());
for (int i = 0; i < texfilename.size(); i++) {
glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(GL_TEXTURE_2D, teximage[i]);
glUniform1i(texID[i], i);
}
HW #2: Requirements (frag shader & main.cpp)