Upload
amos-johnston
View
264
Download
0
Tags:
Embed Size (px)
Citation preview
Shader
Original Rendering Pipeline
Vertex Shader Vertex transformation Normal
transformation & normalization
Texture coordinate generation & transformation
Per-vertex processing
Fragment (pixel) Shader Operations on
interpolated values
Texture access Texture
application Color sum
Shading languages
DirectX’s High Level Shading Language
NVidia’s Cg ATI’s RenderMonkey OpenGL Shading Language (GLSL)
Introduction of GLSL
Data Type Vertex Shader Fragment Shader
GLSL Language Definition Data Type Description
int Integer float Floating-point bool Boolean (true or false). vec2 Vector with two floats. vec3 Vector with three floats. vec4 Vector with four floats. mat22x2 floating-point matrix. mat33x3 floating-point matrix. mat44x4 floating-point matrix.
Vector Vector is like a structure You can use following to access
.r .g .b .a .x .y .z .w .s .t .p .q
Example:vec4 color;
color.rgb = vec3(1.0 , 1.0 , 0.0 ); color.a = 0.5or color = vec4(1.0 , 1.0 , 0.0 , 0.5); or color.xy = vec2(1.0 , 1.0); color.zw =vec2(0.0 , 0.5);
Addition data type : Texture Sampler
sampler{1,2,3}D sampler{1,2}DShadow samplerCube
sampler{1,2,3}D Texture unit to access the content of
texture. sampler*DShadow
The depth texture for shadow map. samplerCube
The cube map.
Addition data type
struct, array Similar to C. No union, enum, class
Qualifiers Used to management the input and
output of shaders. attribute
Communicate changing variables from the application to a vertex shader.
uniform Communicate changing variables from the
application to any shader. varying
Communicate interpolated variables from a vertex shader to a fragment shader
Qualifiers
Vertex Shader
Fragment Shader
OpenGL Application
uniform
attribute
varying
Vertex Shader
Fragment Shader
Vertex Shader Code Examplevoid main(void){
vec3 v3Normal;float fAngle;float fShininessFactor;v3Normal = gl_NormalMatrix * gl_Normal;v3Normal = normalize(v3Normal);fAngle = max(0.0, dot(v3Normal, vec3(gl_LightSource[0].halfVector)));fShininessFactor = pow(fAngle, gl_FrontMaterial.shininess);gl_FrontColor = gl_LightSource[0].ambient * gl_FrontMaterial.ambient +
gl_LightSource[0].diffuse * gl_FrontMaterial.diffuse * fAngle +
gl_LightSource[0].specular * gl_FrontMaterial.specular * fShininessFactor;gl_Position = ftransform();
}
Fragment Shader Code Example
void main(void){
gl_FragColor = gl_Color; }
Result
OpenGL Gouraud Shading GLSL Phong Shading
Use GLSL in OpenGL
You need those head and library files glew.h wglew.h glew32.lib glew32s.lib glew32.dll
texture glaux.h glaux.lib glaux.dll
Texture#define TEX_NUM 1 //the number of textures you use.Gluint texObject[TEX_NUM]; //texture objectglEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D, texObject[0]);GLint location = glGetUniformLocationARB( MyShader, "colorTexture");
glMultiTexCoord2fv(GL_TEXTURE0_ARB, object->tList[object->faceList[i][j].t].ptr);
LoadTexturevoid LoadTexture(int textureIndex,char* filename){ AUX_RGBImageRec* img;img = auxDIBImageLoadA(filename);glBindTexture(GL_TEXTURE_2D, texobject[textureIndex]);gluBuild2DMipmaps(GL_TEXTURE_2D, 4, img->sizeX, img->sizeY,GL_RGB,
GL_UNSIGNED_BYTE, img->data);glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_
MIPMAP_LINEAR);glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MI
PMAP_LINEAR);
}
TextureVertex shadervoid main(){
gl_TexCoord[0].xy = gl_MultiTexCoord0.xy;gl_Position = ftransform();
}Fragment shaderuniform sampler2D colorTexture;void main (void){
gl_FragColor = texture2D(colorTexture, gl_TexCoord[0].xy).rgba;
}
Texture
vec3 v3Normal = gl_NormalMatrix * gl_Normal; vec3 vVertex = vec3(gl_ModelViewMatrix * gl_Vertex); lightDir = vec3(gl_LightSource[0].position.xyz -
vVertex); eyeDir = -vVertex;
gl_ModelViewMatrix : 4x4 Matrix representing the m-v matrix. gl_NormalMatrix : 3x3 Matrix representing the inverse transpose
m-v matrix.This matrix is used for normal transformation .
uniform sampler2D colorTexture;vec2 i = vec2 ( gl_TexCoord[1].x,gl_TexCoord[1].y ); // 將 texture 座標點存入 ivec4 i_color = texture2D(colorTexture,i);// 將 i 座標點的顏色存入 i_colorgl_FrontMaterial.ambient gl_LightSource[0].ambientgl_FrontMaterial.diffuse gl_LightSource[0].diffuse gl_FrontMaterial.specular gl_LightSource[0].specular // 對應程式中的各種 light 參數
HW2 : Shader Programming
Write a shader program with Phong Shading. (50%)
Write a shader program with Bump Mapping. (50%)
Due day : 5/21( 四 ) 5/25 HW3
Phong ShadingGouraud Shading
Bump Mapping