160 likes | 289 Views
Cg, GLSL Report 01. Programmable GPU Language. What Programmable GPU Language. What? This language make it possible for you to control something drawn using programmable GPU(Graphic Processing Unit). Cg, GLSL (openGL) VS HLSL (DirectX). Cg (C for graphics)? Developed by NVIDIA.
E N D
Cg, GLSLReport 01 Programmable GPU Language
What Programmable GPU Language • What? • This language make it possible for you to control something drawn using programmable GPU(Graphic Processing Unit)
Cg, GLSL (openGL)VS HLSL(DirectX) • Cg (C for graphics)? • Developed by NVIDIA. • GLSL (openGL Shader Language)? • Developed by ARB. • HLSL(High Level Shader Language)? • Developed by Microsoft and NVIDIA. • Cg, GLSL, HLSL • These languages based on C.
3D Primitives Local Space World Transformation Transform into 3D world coordinate system World Space View Space Transformation Transform into 3D viewing coordinate system View Space Backface Culling Remove back facing primitives Lighting Illustrate according to lighting and reflectance Clipping Clip primitives outside window’s view Projection Transformation Transform into 2D projection coordinate system Projection Space Viewport Transformation Transform into viewport Viewport Space Rasterization Draw pixels 2D Image Fixed Function Path
Programmable Vertex Path Vertex program Model Space Homogeneous Clip Space World & View Space Transformation Lighting Projection Transformation Fixed Function Vertex Path Vertex and Fragment (or Pixel) Program • What? • Vertex program? • Run for model, view space transformation, lighting, projection transformation
3D Application CPU-GPU Boundary 3D API GPU Command & Data Stream Assembled Polygons, Lines & Points Pixel Location Stream Pixel Updates Vertex Index Stream GPU Front End Primitive Assembly Rasterization & Interpolation Raster Operations Frame Buffer Pretransformed Vertices Rasterized Pretransformed Fragments Transformed Fragments Transformed Vertices Programmable Vertex Processor Programmable Fragment Processor Vertex and Fragment (or Pixel) Program • What? • Fragment (or Pixel) program? • This program run for rasterization in GPU • Programmable Graphics Pipeline
Example for HLSL,Cg,GLSL • Phong reflection Model • Depending on angle of incident light and angle to viewer LIGHT N R=L+2S L+S=Ncosθ S=N(N×L)-L RESULT:R=2N(N×L)-L S R VIEWER L θ θ α V ( ) n = × R I K V I L S S
Example for HLSL(DirectX9.0) • Vertex, Pixel Shader code struct VS_INPUT { vector position : POSITION; vector normal : NORMAL; }; VS_OUTPUT VS_Main(VS_INPUT input) { ` VS_OUTPUT output = (VS_OUTPUT)0; output.position = mul(input.position, ViewProjMatrix); LightDirection.w = 0.1f; input.normal.w = 0.0f; output.light = mul(LightDirection, ViewMatrix); output.normal = mul(input.normal, ViewMatrix); output.view = normalize(mul(-input.position, ViewMatrix)); return output; } struct VS_OUTPUT { vector position : POSITION; float3 light : TEXCOORD0; float3 normal : TEXCOORD1; float3 view : TEXCOORD2; }; struct PS_INPUT { float3 light : TEXCOORD0; float3 normal : TEXCOORD1; float3 view : TEXCOORD2; }; PS_OUTPUT PS_Main(PS_INPUT input) { PS_OUTPUT output = (PS_OUTPUT)0; float s = saturate( dot(input.normal , input.light) ); // r = 2 * (n * l ) * n-1 float3 reflect = normalize(2*s*input.normal - input.light); float r = pow( saturate(dot(input.view, reflect)), 6 ); output.diffuse = (AmbientMtrl = AmbientLightIntensity) + (s * (DiffuseLightIntensity * DiffuseMtrl)) + (r * (SpecularLightIntensity * SpecularMtrl)); return output; } struct PS_OUTPUT { vector diffuse : COLOR; };
Example for GLSL(openGL2.0) • Vertex, Fragment Shader Code varying vec3 L; varying vec3 N; varying vec3 P; void main(void) { P = vec3(gl_ModelViewMatrix * gl_Vertex); L = normalize(gl_LightSource[0].position.xyz-P); N = normalize(gl_NormalMatrix * gl_Normal); gl_Position = gl_ModelViewProjectionMatrix *gl_Vertex; } varying vec3 L; varying vec3 N; varying vec3 P; void main (void) { vec3 E = normalize(-P); vec3 R = normalize(-reflect(L,N)); vec4 Iamb = gl_FrontLightProduct[0].ambient; vec4 Idiff = gl_FrontLightProduct[0].diffuse *dot(N,L), 0.0); vec4 Ispec = gl_FrontLightProduct[0].specular *pow(max(dot(R,E),0.0),0.3 *gl_FrontMaterial.shininess); gl_FragColor=gl_FrontLightModelProduct.sceneColor + Iamb + Idiff + Ispec; }
Example for Cg (openGL) • Vertex, Fragment Cg Code struct V2FI { float4 HPosition : POSITION; float3 Normal : TEXCOORD0; float3 ViewVector : TEXCOORD1; float3 LightVector : TEXCOORD2; }; V2FI main(appin IN, uniform float4x4 Proj : register(c0), uniform float4x4 View : register(c4), uniform float4x4 ViewI : register(c8), uniform float4x4 ViewIT : register(c12), uniform float4 LightPos ) { V2FI OUT; // compute view vector float4 pos = mul(View, IN.Position); OUT.ViewVector = -pos.xyz; // compute light vector float4 lightpos = mul(View, LightPos); OUT.LightVector = (pos-lightpos).xyz; // project point onto screen OUT.HPosition = mul(Proj, pos); // transform normal - should use inverse transpose ! OUT.Normal = mul(ViewIT, float4(IN.Normal,0.0)).xyz; return OUT; struct V2FI { float3 Normal : TEXCOORD0; float3 ViewVector : TEXCOORD1; float3 LightVector : TEXCOORD2; }; struct PixelOut { float4 COL : COLOR; }; PixelOut main(V2FI IN, uniform float4 Ambient, uniform float4 Diffuse, uniform float4 Specular) { float3 n=normalize(IN.Normal.xyz); float3 v=normalize(IN.ViewVector.xyz); float3 l=normalize(IN.LightVector.xyz); float3 r=reflect(-v,n); float spec=pow(clamp(dot(l,r),0.0,1.0),30.0); float diff=clamp(dot(n,l),0.3,1.0); float4 color=Ambient+Diffuse*diff+Specular*spec; PixelOut OUT; OUT.COL = color; return OUT; }
Blinn-Phong Model • Popular variation of Phong model. • What are the advantage? • Faster to compute than reflection vector. • Still view-dependent since H depends on V. • Uses the halfway vector, H. Φ+ Ψ=(Φ- Ψ)+ θ 2 Ψ= θ H=L+V/|L+V| N H R Φ VIEWER L Ψ θ ( ) n = × H I K N I V L S S
GLSLExample for Blinn-Phong Model • Fragment GLSLcode varying vec3 L; varying vec3 N; varying vec3 P; void main (void) { vec3 E = normalize(-P); // vec3 R = normalize(-reflect(L,N)); vec3 H=(L+E)/length(L+E); vec4 Iamb = gl_FrontLightProduct[0].ambient; vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(N,L), 0.0); // vec4 Ispec = gl_FrontLightProduct[0].specular * pow(max(dot(R,E),0.0),0.3 * gl_FrontMaterial.shininess); vec4 Ispec = gl_FrontLightProduct[0].specular *pow(max(dot(N,H),0.0),0.3 * gl_FrontMaterial.shininess); gl_FragColor = gl_FrontLightModelProduct.sceneColor + Iamb + Idiff + Ispec; }
Phong vs Blinn-Phong • True reflection vector VS Half vector approximation.