The Vertex Shader CS 418 Computer Graphics John
- Slides: 10
The Vertex Shader CS 418 Computer Graphics John C. Hart
Graphics Pipeline Model Coords Model Xform Viewing Xform World Coords Homogeneous Divide Still Clip Coords. Window Coordinates Window to Viewport Viewing Coords Clipping Perspective Distortion Clip Coords. Viewport Coordinates
Graphics Pipeline Model Coords Model Xform World Coords Homogeneous Divide Viewing Xform Still Clip Coords. Window Coordinates Window to Viewport Viewing Coords Clipping Perspective Distortion Clip Coords. Viewport Coordinates
Graphics Pipeline Model Coords Model Xform World Coords Homogeneous Divide Viewing Xform Still Clip Coords. Window Coordinates Window to Viewport Viewing Coords Clipping Perspective Distortion Clip Coords. Viewport Coordinates
Graphics Pipeline gl. Matrix. Mode(GL_PROJECTION); gl. Frustum(left, right, bottom, top, near, far); gl. Matrix. Mode(GL_MODELVIEW); glu. Look. At(…); …modeling transformations in reverse order…
Vertex Shader Model Coords Model Xform Viewing Xform World Coords Homogeneous Divide Still Clip Coords. Window Coordinates Window to Viewport Viewing Coords Clipping Perspective Distortion Clip Coords. Viewport Coordinates
Vertex Programming Languages Open. GL GLSL • • C-like language with some convenient C++ constructs • Little language devoted to shaders (inspired by Pixar’s Renderman) • Device/OS independent, as opposed to Cg or HLSL • Direct access into Open. GL state including matrices NVIDIA: Cg Microsoft: HLSL Open. GL: GLSL ATI: Render. Monkey Assembly Language Register Combiners Multipass Processing
GLSL Vertex Shader GLchar vertex. Shader. Code = “ void main() { gl. Position = gl_Projection. Matrix*gl_Model. View. Matrix*gl. Vertex; } “; GLuint vertex. Shader. Obj = gl. Create. Shader(GL_VERTEX_SHADER); gl. Shader. Source(vertex. Shader. Obj, 1, vertex. Shader. Code, NULL); gl. Compile. Shader(vertex. Shader. Obj); /* Converts to GPU code */ GLuint program. Obj = gl. Create. Program(); gl. Attach. Object(program. Obj, vertex. Shader. Obj); gl. Link. Program(program. Obj); /* Connects shaders & variables */ gl. Use. Program(program. Obj); /* Open. GL now uses the shader */
GLSL Variables Variable Types Variable Qualifiers • Vectors • Const – vec 4 eye = vec 4(1. 0, 2. 0, 3. 0, 1. 0); – eye. x, eye. y, eye. z, eye. w – also eye. r, eye. g, eye. b, eye. a • Matrices – – mat 4 mv = gl. Model. View. Matrix; elements: mv[1][2] mv[1] is a vec 4 mv * eye gives matrix vector product • Swizzling – eye. xz = eye. zx – Unchanged by the shader • Uniform – Set once per triangle – Set outside begin, end • Attribute – Set once per vertex • Varying – Interpolated across triangle
Passing Variables GLchar vertex. Shader. Code = “ const amp = 0. 1; uniform float phase; attribute float pos; void main() { gl. Vertex. y += amp*sin(pos + phase); gl. Position = gl_Model. View. Projection. Matrix*gl. Vertex; } “; GLint phase. Param = gl. Get. Uniform. Location(program. Obj, ”phase”); GLint pos. Attrib = gl. Get. Attrib. Location(program. Obj, ”pos”); gl. Uniform 1 f(program. Obj, phase. Param, time); gl. Begin(…) gl. Vertex. Attrib 1 f(pos. Attrib, x*z);