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

the vertex shader
SMART_READER_LITE
LIVE PREVIEW

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.


slide-1
SLIDE 1

The Vertex Shader

CS418 Computer Graphics John C. Hart

slide-2
SLIDE 2

Graphics Pipeline

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

slide-3
SLIDE 3

Graphics Pipeline

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

W2V Persp View Model 1 1

s m s m m

x x y y z                                                                         

slide-4
SLIDE 4

Graphics Pipeline

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

W2V Persp ModelView 1 1

s m s m m

x x y y z                                                             

slide-5
SLIDE 5

Graphics Pipeline

W2V Persp ModelView 1 1

s m s m m

x x y y z                                                             

glMatrixMode(GL_PROJECTION); glFrustum(left,right,bottom,top,near,far); glMatrixMode(GL_MODELVIEW); gluLookAt(…); …modeling transformations in reverse order…

slide-6
SLIDE 6

Vertex Shader

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

slide-7
SLIDE 7

Vertex Programming

Languages

  • NVIDIA: Cg
  • Microsoft: HLSL
  • OpenGL: GLSL
  • ATI: RenderMonkey
  • Assembly Language
  • Register Combiners
  • Multipass Processing

OpenGL GLSL

  • C-like language with some

convenient C++ constructs

  • Little language devoted to

shaders (inspired by Pixar’s Renderman)

  • Device/OS independent, as
  • pposed to Cg or HLSL
  • Direct access into OpenGL

state including matrices

slide-8
SLIDE 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 */

slide-9
SLIDE 9

GLSL Variables

Variable Types

  • Vectors

– vec4 eye = vec4(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

– mat4 mv = glModelViewMatrix; – elements: mv[1][2] – mv[1] is a vec4 – mv * eye gives matrix vector product

  • Swizzling

– eye.xz = eye.zx

Variable Qualifiers

  • Const

– Unchanged by the shader

  • Uniform

– Set once per triangle – Set outside begin, end

  • Attribute

– Set once per vertex

  • Varying

– Interpolated across triangle

slide-10
SLIDE 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);