The OpenGL Pipeline

Source: Internet
Author: User
Tags in degrees
The OpenGL Pipeline

We start with a 3D point VOCs defined in the object coordinate system. This point gets transformed as it goes down the pipeline:

VOCs
| Modeling transform (into world coordinate system)
Vwcs
| Viewing transform (into viewing Coordinate System)
Vvcs
| Projection transform (into clipping Coordinate System)
VCCS
| Perspective Division (into normalized device coordinate system)
Vndcs
| Viewport transform (into device coordinate system)
VDCs

At the end of the pipeline, the point VDCs is a 2D pixel location in the window.

The modeling Transform

The object o on which the point VOCs lives is, by default, positioned at the origin of the world coordinate system (WCS ). to position o somewhere else in the world, we apply a transformation to it. typically, this consists of scaling, then rotating, then translating:

Owcs = (t r s) O

Where T, R, and s are the transformations. Note that scaling (s) is applied first, then rotation (R), and finally translation (t ).

Any point VOCs on O undergoes the same transformations:

Vwcs = (t r s) VOCs

The viewing Transform

Suppose the camera is at location P in the world, and its attached orthogonal coordinate system, <u, v, N>, has U pointing right, V pointing up, and n pointing backward along the direction of view.

To convert vwcs into the viewing coordinate system (VCS) We first find the vector from the camera origin to the point:

V' = vwcs-P

In other words:

  [ 1 0 0 -Px ]
V' = [ 0 1 0 -Py ] Vwcs
  [ 0 0 1 -PZ ]
  [ 0 0 0 1 ]

Second, we convert V' (which is a vector in the WCS) into the VCs. To do so, we project v 'onto each of the three axes of the VCs:

  [ UX Uy Uz 0 ]
Vvcs = [ VX Vy VZ 0 ] V'
  [ NX NY NZ 0 ]
  [ 0 0 0 1 ]

Combining the two transforms above, we get the viewing transform:

  [ UX Uy Uz -P. u ]
Vvcs = [ VX Vy VZ -P. v ] Vwcs
  [ NX NY NZ -P. N ]
  [ 0 0 0 1 ]

Where-P. U denotes the dot product of-P and U.

The projection transform and perspective Division

TheView volume, Shown below, is the volume of space from which objects get rendered. anything outside this volume is clipped. the view volume has left, right, top and bottom clipping planes which correspond to the edges of the window. the view volume also has "near" and "far" clipping planes, which limit the range of depths that visible objects can have.

 

In the dianchabve, the near and far clipping planes are at distances N and F, respectively, but their locations are Z =-N and Z =-f since they're on the Negative Z axis. the L, R, T, and B are the locations of the Left, right, top, and bottom clipping planes where they intersect the near clipping plane. for example, the top clipping plane intersects the near clipping plane at Y = T.

The projection transform takes a point vvcs in the view volume, and transforms it to a point vndcs inCanonical view volume, Shown below. (The strange labeling of the axes will be explained shortly .)

 

The canonical view volume ranges from-1 to + 1 in each of the axes, and the coordinate system (called the normalized device coordinate system, or NDCs) is "left handed ": see that the Z'/W' axis points in the opposite-to-usual ction.

Important Note:The NDCs is in 3D, but we're working with homogeneous points in 4D. So, transforming a point from the VCS to the NDCs involves two steps:

  1. Transform the point vvcs = [x y z 1] t to a homogeneous point VCCS = [x 'y' Z' W'] T.
  2. Transform VCCS to a 3D point vndcs = [x'/w'y'/w'z'/W'] T by dividing the first three coordinates by the last coordinate. this is why the axes in the distriabve are labeled x'/W', y'/W', and Z'/W '.

The projection transform does the first step. it transforms a point vvcs into a point VCCS. the point VCCS is said to be in the clipping coordinate system (CCS ). the projection transform looks like this (we'll fill in the details in a later section ):

  [ E 0 A 0 ]
VCCS = [ 0 F B 0 ] Vvcs
  [ 0 0 C D ]
  [ 0 0 -1 0 ]

The next step in the pipeline, the perspective division, transforms the point VCCS into vndcs:

[VX, NDCs]   [VX, CCS/VW, CCS]
[Vy, NDCs] = [Vy, CCS/VW, CCS]
[VZ, NDCs]   [VZ, CCS/VW, CCS]
The viewport Transform

As a final step, we project the points in the canonical view volume onto the viewport (also called the window ). the viewport is the area on the screen in which you're re drawing. its coordinates are pixel locations in the device coordinate system (DCS ):

 

In the VCs, a line-of-sight from the camera origin corresponds, in the NDCs, to a line parallel to the Z axis: all points on that line project to the same (x, y) location on the image plane.

That means that we need to map (x, y) in the NDCs to (x', y') in the DCS. the Z coordinate in the NDCs corresponds to depth, and is only used when the depth buffer ("Z-buffer") is enabled. for convenience, we map the Z coordinate to the range [0, 1].

The viewport transforms does this:

X' = 0.5 * (x + 1) * (R-l) + L
Y' = 0.5 * (Y + 1) * (t-B) + B
Z' = 0.5 * (Z + 1)

For example, the X values in NDCs are in the range [-1, + 1]. the viewport transform converts them (linearly) into x' values in DCs, which are in the range [L, R].

In OpenGL, this can be set up as follows:

glViewport( x, y, width, height );

Where (X, Y) is the location of the lower-left corner of the viewport, and width and height are its dimensions. Note that (X, Y) isRelative to the origin of the OpenGL windowAnd that everything is measured in pixels. You only useGlviewportIf you want to restrict drawing to a rectangular areaInsideYour window. By default, the viewport is the entire window.

Details of the projection transform

In this section, we'll derive the projection transform, which was given abve as a matrix with unknown elements:

  [ E 0 A 0 ]
VCCS = [ 0 F B 0 ] Vvcs
  [ 0 0 C D ]
  [ 0 0 -1 0 ]

Let VCCS = [x 'y' Z' W'] T and let vvcs = [x y z 1] T.

First, consider how y' is calculated:

From the transformation matrix above, y' = f y + B z. consider a 2D slice at x = 0 through the view volume (shown on the left below) and the corresponding 2D slice at x'/W' = 0 through the canonical view volume (shown on the right below ):

 

Points on the top line of the view volume satisfy y/-z = T/N. that is, there points are of the form (-TZ/N, Z ). these points correspond to points on the top line of the canonical view volume that satisfy y '/W' = 1.

Substitute a point (-TZ/N, Z) into the y' and W' lines of the transformation matrix:

 

Y' = F y + B z
  = F (-TZ/n) + B z
W' =-Z

As stated above, the VCs point (-TZ/N, z) is transformed to a CCS point for which y'/W' = 1. So

 

Y'/W' = (F (-TZ/n) + B Z)/(-Z)
  = T/n f-B
  = 1

So we know that

 

T/n f-B = 1.

By a similar argument (using VCs points (-B/NZ, Z) on the bottom line which map to CCS points for which y '/W' =-1) We can show that

 

B/n f-B =-1.

We can then solve the two equations in F and B to get:

 

F = 2n/(t-B)
B = (T + B)/(t-B)

The mapping for x' is analagous:

Given L and R which are the left and right limits in the X ction of the view volume, we apply the same method as for y' and get

 

E = 2n/(R-l)
A = (R + l)/(R-l)

The mapping for Z' is a bit different:

VCs points on the near plane (Z =-N) map to CCS points on the line Z'/W' =-1, and VCs points on the far plane (Z =-f) map to CCS points on the line Z'/W' = + 1.

From the transformation matrix:

 

Z' = C Z + d
W' =-Z

Substitute for Z =-N and for Z =-F to get two equations in the unknowns C and D:

 

Z'/W' = C (-n) + d)/(n)
  =-C + D/n
  =-1

 

Z'/W' = C (-f) + d)/(f)
  =-C + D/F
  = + 1

Solving the equations yields:

 

C = -(F + N)/(F-N)
D = -2fn/(F-N)

All of the above work gives us the VCs-to-CCS transform matrix:

  [ 2n/(R-l) 0 (R + l)/(R-l) 0 ]
VCCS = [ 0 2n/(t-B) (T + B)/(t-B) 0 ] Vvcs
  [ 0 0 (F + N)/(n-f) 2fn/(n-f) ]
  [ 0 0 -1 0 ]

In OpenGL, this matrix can be set as follows:

glMatrixMode( GL_PROJECTION );glLoadIdentity();glFrustum( l, r, t, b, n, f );

Where l, R, T, B, n, F are as defined above. Alternatively, you can do this:

glMatrixMode( GL_PROJECTION );glLoadIdentity();gluPerspective( fovy, asp, n, f );

Where n and F are as abve, fovy is the angle of the field of view in the Y direction (vertical), in degrees, and ASP is the aspect ratio of the view frustum: (R-l)/(t-B ).

Clipping in the CCS

Points outside the view frustum must be clipped. The clipping cocould be done almost anywhere in the pipeline, but it's best to do clipping in the CCS because:

  1. The canonical view volume in the CCS isIndependent of camera parameters. That means that clipping in the CCS can be implemented in hardware which doesn't have to be parameterized... so it's fast. there's no point earlier in the pipeline where clipping can be done in a camera-independent manner.

     

  2. After the perspective Division (CCS-to-NDCs) some depth information is lost, which can result in improper clipping (as discussed below ). so it doesn' t make sense to clip after the CCs.

An example of improper clipping in the NDCs

In the figure below, the segment PQ In the VCs is transformed to the segment P 'q' in the NDCs. if we clip 'Q', the segment will appear to be exiting the far plane of the canonical view volume. but it shoshould really exit the top plane!

 

This occurs because the Z' and W' coordinates of Q' are both negative after the projection transform. then, when we do the perspective division, the last coordinate (Z'/W') is positive, and Q 'appears on the positive Z axis.

For a better intuition, consider what happens to Q 'as Q is moved along the Z axis of the VCs:

  • As Q approaches the origin, the segment PQ approaches, then passes, the top-front corner of the VCs view volume. at the same time, the transformed Q 'in NDCs will move right (more distant) along the Z'/W' axis.

     

  • When q touches the origin, the transformed Q 'in NDCs has W' = 0 (recall the matrix above, in which W' =-z ), and Q 'lies at infinity on the Z'/W' axis. that is, P 'q' is parallel to the Z'/W' axis.

     

  • As soon as Q passes the origin, Q 'in NDCs has W'> 0 and Z' <0, so Q 'lies on the negative Z'/W' axis, and moves in toward the NDCs origin as Q continues inward.

     

  • After Q passes the center of the VCs view volume, Z 'becomes positive (while W 'remains positive), and Q' moves to the right of the NDCs origin.

Clipping in CCS

If the point were in NDCs, We wowould clip against the six planes that define the faces of the canonical view volume:

 

X'/W' = + 1 X'/W' =-1
Y'/W' = + 1   Y'/W' =-1
Z'/W' = + 1   Z'/W' =-1

The corresponding planes in the CCS are:

 

X'-W' = 0 X' + W' = 0
Y'-W' = 0   Y' + W' = 0
Z'-W' = 0   Z' + W' = 0

Given a segment P 'q' in the CCS, we determine the sixOutcodes(The string of six bits which indicates to which side of each of the six planes the point lies) of the segment endpoints by substituting the [x 'y' Z' W'] T coordinates of the endpoints into the six CCS equations, and testing the signs. then we can clip in exactly the same was we did in 2D: plane by plane with the Sutherland-khoreman algorithm.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.