Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation &...

26
Shader

Transcript of Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation &...

Page 1: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Shader

Page 2: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Original Rendering Pipeline

Page 3: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Vertex Shader Vertex transformation Normal

transformation & normalization

Texture coordinate generation & transformation

Per-vertex processing

Page 4: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Fragment (pixel) Shader Operations on

interpolated values

Texture access Texture

application Color sum

Page 5: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Shading languages

DirectX’s High Level Shading Language

NVidia’s Cg ATI’s RenderMonkey OpenGL Shading Language (GLSL)

Page 6: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Introduction of GLSL

Data Type Vertex Shader Fragment Shader

Page 7: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

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.

Page 8: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

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);

Page 9: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

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.

Page 10: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Addition data type

struct, array Similar to C. No union, enum, class

Page 11: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

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

Page 12: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Qualifiers

Vertex Shader

Fragment Shader

OpenGL Application

uniform

attribute

varying

Page 13: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Vertex Shader

Page 14: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Fragment Shader

Page 15: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

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();

}

Page 16: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Fragment Shader Code Example

void main(void){

gl_FragColor = gl_Color; }

Page 17: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Result

OpenGL Gouraud Shading GLSL Phong Shading

Page 18: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

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

Page 19: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

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);

Page 20: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

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);

}

Page 21: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

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;

}

Page 22: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Texture

Page 23: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

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 .

Page 24: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & 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 參數

Page 25: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

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

Page 26: Shader. Original Rendering Pipeline Vertex Shader Vertex transformation Normal transformation & normalization Texture coordinate generation & transformation.

Phong ShadingGouraud Shading

Bump Mapping