CSCI 420: Computer Graphics
Kyle Olszewski
http://cs420.hao-li.com
Fall 2014
8.2 Programmable Graphics Hardware
1
8.2 Programmable Graphics Hardware Kyle Olszewski - - PowerPoint PPT Presentation
Fall 2014 CSCI 420: Computer Graphics 8.2 Programmable Graphics Hardware Kyle Olszewski http://cs420.hao-li.com 1 Introduction Recent major advance in real time graphics is the programmable pipeline: - First introduced by NVIDIA
CSCI 420: Computer Graphics
http://cs420.hao-li.com
Fall 2014
1
graphics is the programmable pipeline:
programmable pipeline and shaders
2
were added to OpenGL
3
GL_EXT_multisample
GL_ARB_multisample
4
compatibility profile (may not be supported)
5
(for now)
6
that replace a part of the OpenGL pipeline
the fixed OpenGL pipeline
(offline shader)
7
8
Complex materials Shadowing Lighting environments Advanced mapping
9
CPU Vertex Processor Rasterizer Fragment Processor Frame Buffer vertices vertices fragments fragments
10
CPU Vertex Processor Rasterizer Fragment Processor Frame Buffer vertices vertices fragments fragments customizable by a vertex program customizable by a fragment program
(fixed-function pipeline)
as needed
11
12
13
14
/* pass-through vertex shader */
{ gl_Position = gl_ProjectionMatrix * (gl_ModelViewMatrix * gl_Vertex); }
15
/* pass-through vertex shader */
uniform mat4 ModelViewMatrix;
void main() { gl_Position = ProjectionMatrix * (ModelViewMatrix * vec4(Vertex, 1.0)); }
interpolated (by the GPU) to the pixel location !
16
17
/* pass-through fragment shader */
{ gl_FragColor = gl_Color; }
18
/* all-red fragment shader */
{ gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); }
19
/* all-red fragment shader */
{ FragColor = vec4(1.0, 0.0, 0.0, 1.0); }
glBindFragDataLocation(ShaderProgram, 0, "FragColor");
20
21
individual vector components
22
vec4 myVector; myVector.rgba; // is the same as myVector myVector.xy; // is a vec2 myVector.b; // is a float myVector[2]; // is the same as myVector.b myVector.xb; // illegal myVector.xxx; // is a vec3
passed to vertex/fragment shader
vertex/fragment shader
fragment shader
in fragment shader
23
Example: Vertex Color Example: Light Position Eye Position Example: Vertex Color Texture Coords Example: pi, e, 0.480
main()
convention
24
Example function:
in vec3 N,
inout vec3 coord) { if((dot(N, coord)>0) T = vec3(1,0,0); else T = vec3(0,0,0); coord = 2 * T; }
25
transposes)
etc.)
26
gl_Color, gl_Normal, gl_Vertex, etc.
gl_Position, gl_PointSize, etc.
gl_FragCoord, gl_FrontFacing, etc.
gl_FragColor, gl_FragDepth, etc.
27
28
29
30
varying vec3 n; varying vec3 vtx; void main(void) { // transform vertex position to eye coordinates: vtx = vec3(gl_ModelViewMatrix * gl_Vertex); // transform normal: n = normalize(gl_NormalMatrix * gl_Normal); // transform vertex position to clip coordinates: gl_Position = gl_ModelViewProjectionMatrix *
}
31
these will be passed to fragment program (interpolated by hardware)
32
varying vec3 n; varying vec3 vtx; void main (void) { // we are in eye coordinates, so eye pos is (0,0,0) vec3 l = normalize(gl_LightSource[0].position.xyz - vtx); vec3 v = normalize(-vtx); vec3 r = normalize(-reflect(l,n)); //calculate ambient, diffuse, specular terms: vec4 Iamb = gl_FrontLightProduct[0].ambient; vec4 Idiff = gl_FrontLightProduct[0].diffuse * max(dot(n,l), 0.0); vec4 Ispec = gl_FrontLightProduct[0].specular
// write total color: gl_FragColor = gl_FrontLightModelProduct.sceneColor + Iamb + Idiff + Ispec; }
interpolated from vertex program
n l r v φ
θ
variables
functions like:
GLenum whatToCheck, GLfloat * statusVals)
33
34
http://cs420.hao-li.com
35