View
8
Download
0
Category
Preview:
Citation preview
Lighting and Texturing
Bernhard Bittorf
Bernhard Bittorf 2 / 37
Agenda
• Lighting
• GLSL
• Textures
• References
Lighting Models approximate complex physical processes light-material interaction
Lambert’s cosine law Phong reflection model / Blinn-Phong reflection model
Illumination
indirect illumination approximated by constant term global illumination calculation to complex for real-time rendering
Intensity I = w1 ∙ IA + w2 ∙ ID + w3 ∙ IS
Note: - w = weights- for our purposes the I- terms are vectors of RGBA intensity
Local Illumination
Simplest illumination
IA = Il ka
Il - ambient light source intensity ka - ambient surface reflection coefficient (0 ≤ ka ≤ 1)
Ambient Reflection
matt surfaces reflect light uniform, diffuse in all directions
Intensity depends on the angle between surface and light direction
how to calculate?
Diffuse Reflection
•Lambert’s Cosine Law
ID = IL ∙ kd ∙ cos( θ) ≡ ID = IL ∙ kd ∙ cos (N, L)
for N and L normalized:
ID = IL ∙ kd ∙ max(0 , dot(N , L))
• the diffuse reflection is independent from viewer position
•problem: objects with different distanceto the light source have the same intensity
• solution: adding a distance depend factor(for us negligible or optional)
Diffuse Reflection
for glossy surfaces: specular highlights
total reflection: light is only reflected in direction of R
but not every surface is a mirror
how to calculate?
Specular Reflection
IS = IL ∙ ks ∙ cos n (α) ≡ IS = IL ∙ ks ∙ cos n (V , R)
IS = IL ∙ kS ∙ pow(dot(V , R), n)
with: R = reflect(-L,N)
Phong’s Specular Reflection
calculate average normals from adjacent polygons interpolate vertex normals over polygon
linear interpolation along polygon edges linear interpolation along scan lines
Important: lighting calculation at each raster fragment == per fragment
shading• advantages:
highlights preserved smooth polygon transitions
lower tesselation of objects can be used
Phong Shading
OpenGL Shading Language
C-like, high level programming language for graphical purpose GPU programming
build in vector and matrix types
build in many math functions like dot, cross, pow, max …
look at the spec: http://www.opengl.org/sdk/docs/manglsl/
better: http://www.khronos.org/files/opengl-quick-reference-card.pdf
GLSL
Simple Program vertex shader
#version 330
//varies for each vertexlayout(location=0) in vec3 in_Position;layout(location=1) in vec3 in_Normal;
//output to next shader stageout vec4 normal;
//Uniforms as specified with glUniformMatrix4fvuniform mat4 ModelViewMatrix;uniform mat4 ProjectionMatrix;uniform mat4 NormalMatrix;
void main(void){
gl_Position = (ProjectionMatrix * ModelViewMatrix) * vec4(in_Position,1.0);
normal = NormalMatrix * vec4(normalize(in_Normal), 0.0);}
GLSL
Simple Program Host side
//varies for each vertex //layout(location=1) in vec3 in_PositionglEnableVertexAttribArray(0);glVertexAttribPointer(0,
GLOOST_MESH_NUM_COMPONENTS_VERTEX,GL_FLOAT, GL_FALSE,mesh->getInterleavedInfo().interleavedPackageStride,(GLvoid*)(mesh>getInterleavedInfo().interleavedVertexStride));
//layout(location=1) in vec3 in_Normal;glEnableVertexAttribArray(1);glVertexAttribPointer(1,
GLOOST_MESH_NUM_COMPONENTS_NORMAL,GL_FLOAT, GL_FALSE,mesh->getInterleavedInfo().interleavedPackageStride,(GLvoid*)(mesh->getInterleavedInfo().interleavedNormalStride));
GLSL
Simple Program Host side
//Uniforms as specified with glUniformMatrix4fv//uniform mat4 ModelViewMatrix;
glUniformMatrix4fv(ModelViewMatrixUniformLocation, 1, GL_FALSE, ModelViewMatrixStack.top().data());
//uniform mat4 ProjectionMatrix;glUniformMatrix4fv(ProjectionMatrixUniformLocation,
1, GL_FALSE, ProjectionMatrix.data());
//uniform mat4 NormalMatrix;glUniformMatrix4fv(NormalMatrixUniformLocation,
1, GL_FALSE, normalMatrix.data());
GLSL
Simple Program fragment shader
#version 330
//from vertex shaderin vec4 normal;
//final Pixel Colorout vec4 out_Color;
void main(void){
out_Color = normal; }
GLSL
Example: Appling a 2D Texture
x
y
z
s-coordinate
t-coordinate
s
t
0 1
1
x
y
z
texture space
world space
final image
Texture Mapping
Example: Appling a 2D Texture
so we need texture coordinates
fortunately they come with our sphere model
recall how we got normals to the shaders: glVertexAttribPointer
do same for the texture coordinates note: the stride is now: interleavedTexcoordStridethe number of elements: GLOOST_MESH_NUM_COMPONENTS_TEXCOORD
in the vertex shader: just pass them through to the fragment shader
there access the the texture with: texture2D(sampler2D, texcoord)
sampler2D has to be a uniform create and pass it to the fragment shader like we did with the matrices for
the vertexshader Note: glUniform1i(Uniformlocation, TextureUnitNumber)
Texture Mapping
FreeImage Library for loading images from different typ FrameWork is updated
Documentation
:http://freeimage.sourceforge.net/download.html
Texture Mapping
//load image data with FreeImage to memoryFreeImage_Initialise();unsigned char* imagepath = “myimage.image”;
FIBITMAP *bitmap = FreeImage_Load(FreeImage_GetFileType(imagepath), imagepath);
if(!bitmap)std::cout<<“Image not loaded”<<std::endl;
//Note: data will be BGR orderunsigned char* data = FreeImage_GetBits(bitmap);
Assign Image to OpenGL Texture ObjectglGenTextures(number, texture_ids);
generate unique texture ids
glTexImage2D( target, level, internal_format, w, h,border, format, type, pixels);
load pixel buffer as texture target: GL_TEXTURE_1D, GL_TEXTURE_2D,… internal_format: GL_RGB, GL_RGBA, GL_LUMINANCE… format: input image format (GL_RGB, GL_RGBA…) type: input image pixel component type
(GL_UNSIGNED_BYTE, GL_FLOAT)
glBindTexture( target, texture_id);
bind texture as current active texture
Texture Mapping
• specify texture
//globale variableunsigned tex_id = 0;
// generate texture idglGenTextures(1, &tex_id);if (tex_id == 0) {
// OpenGL was not able to generate additional texturereturn (false);
}
glEnable(GL_TEXTURE_2D);// bind texture objectglBindTexture(GL_TEXTURE_2D, tex_id);// load image data as textureglTexImage2D(GL_TEXTURE_2D,
0, GL_RGB, FreeImage_GetWidth(bitmap), FreeImage_GetHeight(bitmap),0, GL_BGR, GL_UNSIGNED_BYTE, data);
//setting Texture ParametersglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //linear filteringglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); //clamp coordinatesglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
Texture Mapping
• applying texture
//in draw functionglBindTexture(GL_TEXTURE_2D, texid);glUniform1i(SamplerUniformLocation, GL_TEXTURE0+…)
Texture Mapping
• Vertex Shader
.
.
.layout(location=2) in vec2 in_texcoord;..out vec2 out_texcoord;...main(){..out_texcoord = in_texcoord;}
Texture Mapping
• Fragment Shader
.
.
.in vec2 texcoord;uniform sampler2D sampler; ..main(){.vec4 texcolor = texture2D(sampler, texcoord); .}
Texture Mapping
Mipmapping texture is stored as pyramid of over and over reduced dimensions during rendering:
selection of best fitting mipmap levels interpolation between mipmap levels
reduces oversampling artifacts of texture minificationglGenerateMipmap(GL_TEXTURE_2D);
glGenerateMipmap(GL_TEXTURE_2D);XTURE_2D);
only 33% memory overhead
mipmap pyramid
4x4 texture
2x2 texture
1x1 texture
Texture Mapping
Bernhard Bittorf 25 / 37
Online
• www.opengl.orgOfficial home page
• http://www.opengl.org/documentation/specs/
OpenGL specification documents
• http://www.flipcode.com/articles/article_advgltextures.shtmlAd
vanced texturing article
• http://www.arcsynthesis.org/gltut/
• uses its own framework but explains many aspects of OpenGL
• http://openglbook.com
THE OpenGL 3.x / 4.0 tutorial
Bernhard Bittorf 26 / 37
+++ Ende - The end - Finis - Fin - Fine +++ Ende - The end - Finis - Fin - Fine +++
End
Recommended