程序代写代做代考 algorithm cse3431-lecture9

cse3431-lecture9

Matrix Order

Normally projection has to apply to all
objects (i.e. the entire scene) thus it must
pre-multiply the modelview matrix
• M = MprojMmodelview or
• M = MprojMviewMmodel

However, with shaders you have absolute control of the matrices

and the way they are multiplied

Important
Projection parameters are given in CAMERA
Coordinate system (Viewing).

So if camera is at z = 50, is aligned with the
world CS, and you give near = 10 where is
the near plane with respect to the world?

Important
Projection parameters are given in CAMERA
Coordinate system (Viewing).

So if the camera is at z = 50, is aligned with
the world CS, and you give |near| = 10 where
is the near plane with respect to the world?
• Transformed by inverse(Mvcs)
• i.e. (0,0,40)

Nonlinearity of perspective
transformation

Tracks:
Left: x= -1, y = -1
Right: x = 1, y = -1
Z = -inf, inf

View volume:
Left = -1, right = 1
Bot = -1, top = 1
Near = 1, far = 4

Reminder: ZVCS:=-near –> ZNDCS = -1
-far –> 1
Notice that the curve flattens as

-ZVCS–> far

On systems with limited numerical precision for the z-buffer (e.g. 8 bits)
a large difference in near and far can result in multiple ZVCS values to
map on the same value in ZNDCS. As a result the graphics system cannot
resolve visibility correctly!

Rule of thumb: Limit the z-range as much as you can

Z in NDCS vs -Z in VCS

ZNDCS = 5/3 +8/(3 (-ZVCS))

near far

Viewport transformation

Modeling
transformation

Viewing
transformation

Projection
transformation

Perspective
division

Viewport
transformation

OCS WCS VCS CCS

NDCS
DCS

M-1cam

Viewport

(left,bottom)

(right,top)

(1,1,-1)

NDCS Screen Space
(1,1,-1) –> (top,bottom, 0)
(-1,-1,-1) –> (left,bottom, 1)

• Transforms the canonical coordinates to a viewport of
size W x H from (0,0) at lower left; thus,

viewport is [ 0, W ] x [ 0, H ]

• Scales and translates z to be in [0,1]

• How does a partial coverage matrix look like?

Example: Full window coverage

MFull
V P

=

2

66
4

1 0 0 W/2
0 1 0 H/2
0 0 1 0
0 0 0 1

3

77
5

2

66
4

W

2
0 0 0

0 H
2

0 0
0 0 1 0
0 0 0 1

3

77
5

2

66
4

1 0 0 0
0 1 0 0
0 0 0.5 0.5
0 0 0 1

3

77
5

So..Pixel Centers?
• Pixel size: 1×1
• Therefore pixel centers


at fractional locations 

in screen space


pij = (i.5, j.5)

• In OpenGL the bottom left corner of the window is at (0,0)
• In some windowing systems the top left is at (0,0)
• When do you care about this?…When needing the location

of the mouse from the windowing system

(0,0)

(3,3)

(1.5,0)

Viewport in WebGL
• gl.viewport( x, y, width, height );

– (x,y): lower left corner of viewport rectangle in pixels.
– width, height: width and height of viewport in pixels.
– Generally put the code in a reshape callback.

– Example: the whole window
• gl.viewport(0,0,canvas.width, canvas.height);

Why viewports?
Undo the distortion of the projection
transformation

Stereo views
Render the scene twice from different points
of view

Example: Two viewports
void render()
{
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

// Set the first viewport
gl.viewport(0,0,canvas.width/2,canvas.height/2);
// Set an orthrographic projection matrix
projectionMatrix = ortho(-3,3,-3,3,1,100) ;

modelViewMatrix = mat4() ;

var eye = vec3(0,0,10) ;
modelViewMatrix = mult(modelViewMatrix,lookAt(eye, at , up));

drawObjects() ;

// Set the second viewport
gl.viewport(canvas.width/2,canvas.height/2,canvas.width/2,canvas.height/2);
// Set an orthographicprojection matrix
projectionMatrix = ortho(-3,3,-3,3,1,100) ;
modelViewMatrix = mat4() ;
eye = vec3(10,10,0) ;
modelViewMatrix = mult(modelViewMatrix,lookAt(eye, at , up));

drawObj() ;
}

Example: Two viewports
• Viewport one: lower left quadrant
• Viewport one: top right quadrant Width: 500 pixels

H
ei

gh
t:

50
0

pi
xe

ls

Transformations in the pipeline

Modeling
transformation

Viewing
transformation

Projection
transformation

Perspective
division

Viewport
transformation

OCS WCS VCS CCS

NDCS
DCS

(e.g. pixels)

LookAt()

Ortho()

Frustrum() 

Perspective

gl.viewport()

Translate()…

ModelView Matrix

Vertex Shader

Matrices in the Pipeline

Modeling
transformation

Viewing
transformation

Projection
transformation

Perspective
division

Viewport
transformation

OCS WCS VCS CCS

NDCS
DCS

M-1camMmod Mproj

Mvp

Vertex Shader

Vertex Shader
attribute vec4 vPosition;
attribute vec3 vNormal;

varying vec4 fColor;

void
main()
{

gl_Position = projectionMatrix * modelViewMatrix * vPosition;
fColor = vec4(1.0f, 0.0f, 0.0f, 1.0f) ;

}

// Notice that perspective division happens later. 

// gl_Position is in homogeneous coordinates

Line Rendering Algorithm
Compute Mmod
Compute M-1cam
Compute Mmodelview = M-1cam Mmod
Compute MO
Compute MP // disregard MP here and below for orthographic-only case
Compute Mproj = MOMP
Compute MVP // Viewport transformation
Compute M = MVP Mproj Mmodelview
for each line segment i between vertices Pi and Qi do 

P = MPi; Q = MQi 

drawline(Px/hP, Py/hP, Qx/hQ, Qy/hQ) // hP,hQ are the 4th coordinates of P,Q
end for

Leave a Reply

Your email address will not be published. Required fields are marked *