the vertex shader
play

The Vertex Shader CS418 Computer Graphics John C. Hart Graphics - PowerPoint PPT Presentation

The Vertex Shader CS418 Computer Graphics John C. Hart Graphics Pipeline Model Model World Viewing Viewing Perspective Coords Xform Coords Xform Coords Distortion Still Homogeneous Clip Clip Clipping Divide Coords. Coords.


  1. The Vertex Shader CS418 Computer Graphics John C. Hart

  2. Graphics Pipeline Model Model World Viewing Viewing Perspective Coords Xform Coords Xform Coords Distortion Still Homogeneous Clip Clip Clipping Divide Coords. Coords. Window Window Viewport to Coordinates Coordinates Viewport

  3. Graphics Pipeline Model Model World Viewing Viewing Perspective Coords Xform Coords Xform Coords Distortion Still Homogeneous Clip Clip Clipping Divide Coords. Coords. Window Window Viewport to Coordinates Coordinates Viewport             x x s m             y y             s  m W2V Persp View Model             0 z m                         1 1

  4. Graphics Pipeline Model Model World Viewing Viewing Perspective Coords Xform Coords Xform Coords Distortion Still Homogeneous Clip Clip Clipping Divide Coords. Coords. Window Window Viewport to Coordinates Coordinates Viewport           x x s m           y y           s  m W2V Persp ModelView           0 z m                     1 1

  5. Graphics Pipeline           x x s m           y y           s  m W2V Persp ModelView           0 z m                     1 1 glMatrixMode(GL_PROJECTION); glFrustum(left,right,bottom,top,near,far); glMatrixMode(GL_MODELVIEW); gluLookAt(…); …modeling transformations in reverse order…

  6. Vertex Shader Model Model World Viewing Viewing Perspective Coords Xform Coords Xform Coords Distortion Still Homogeneous Clip Clip Clipping Divide Coords. Coords. Window Window Viewport to Coordinates Coordinates Viewport

  7. Vertex Programming Languages OpenGL GLSL • NVIDIA: Cg • C-like language with some convenient C++ constructs • Microsoft: HLSL • Little language devoted to • OpenGL: GLSL shaders (inspired by Pixar’s • ATI: RenderMonkey Renderman) • Assembly Language • Device/OS independent, as • Register Combiners opposed to Cg or HLSL • Multipass Processing • Direct access into OpenGL state including matrices

  8. GLSL Vertex Shader GLchar vertexShaderCode = “ void main() { glPosition = gl_ProjectionMatrix*gl_ModelViewMatrix*glVertex; } “; GLuint vertexShaderObj = glCreateShader (GL_VERTEX_SHADER); glShaderSource (vertexShaderObj, 1, vertexShaderCode, NULL); glCompileShader (vertexShaderObj); /* Converts to GPU code */ GLuint programObj = glCreateProgram (); glAttachObject (programObj,vertexShaderObj); glLinkProgram (programObj); /* Connects shaders & variables */ glUseProgram (programObj); /* OpenGL now uses the shader */

  9. GLSL Variables Variable Types Variable Qualifiers • Vectors • Const – vec4 eye = vec4(1.0,2.0,3.0,1.0); – Unchanged by the shader – eye.x, eye.y, eye.z, eye.w • Uniform – also eye.r, eye.g, eye.b, eye.a – Set once per triangle • Matrices – Set outside begin, end – mat4 mv = glModelViewMatrix; • Attribute – elements: mv[1][2] – Set once per vertex – mv[1] is a vec4 • Varying – mv * eye gives matrix vector – Interpolated across triangle product • Swizzling – eye.xz = eye.zx

  10. Passing Variables GLchar vertexShaderCode = “ const amp = 0.1; uniform float phase; attribute float pos; void main() { glVertex.y += amp*sin(pos + phase); glPosition = gl_ModelViewProjectionMatrix*glVertex; } “; GLint phaseParam = glGetUniformLocation(programObj,”phase”); GLint posAttrib = glGetAttribLocation(programObj,”pos”); glUniform1f(programObj,phaseParam,time); glBegin(…) glVertexAttrib1f(posAttrib,x*z);

Recommend


More recommend