Title: Projections
1Projections
2Viewports
- Windows can have separate viewports
- void glViewport(GLint x,
- GLint y,
- GLsizei width,
- GLsizei height )
- x, y - Specify the lower left corner of the
viewport rectangle, in pixels - width, height - Specify the width and height of
the viewport
3Viewport Example
- Create two viewports in one window
- wWindow width
- hWindow height
//Left Viewport glViewport(0.0,0.0,w/2.0,h) glMat
rixMode(GL_PROJECTION) glLoadIdentity() glOrtho(
-w/4.0,w/4.0,-h/2.0,h/2.0,-1.0,1.0) glMatrixMode(
GL_MODELVIEW) //Draw the scene drawScene() //Ri
ght Viewport glViewport(w/2.0,0.0,w/2.0,h) glMatr
ixMode(GL_PROJECTION) glLoadIdentity() glOrtho(-
w/4.0,w/4.0,-h/2.0,h/2.0,-1.0,1.0) glMatrixMode(G
L_MODELVIEW) //Draw the scene drawScene()
4Projections
- Flattening a 3D virtual scene by projecting it
onto a plane - The projected geometry is next rasterized into
fragments
5View Frustum
- Frustum
- A portion of a solid, normally a cone or pyramid,
which lies between two parallel planes cutting
the solid. - We are interested in Square frusta
- Used to define the viewing volume
6Near and Far Clipping Planes
- Near and Far clipping planes
- Clip out geometry outside of them
- This Geometry is not rendered
7(No Transcript)
8Orthographic Example
- void glOrtho(GLdouble left,
- GLdouble right,
- GLdouble bottom,
- GLdouble top,
- GLdouble nearVal,
- GLdouble farVal)
//Left Viewport glViewport(0.0,0.0,w,h) glMatrixM
ode(GL_PROJECTION) glLoadIdentity() glOrtho(-w/4
.0,w/4.0,-h/2.0,h/2.0,0.0,1.0) //Draw the
scene glMatrixMode(GL_MODELVIEW) drawScene()
9(No Transcript)
10Perspective Example
- void glFrustum( GLdouble left,
- GLdouble right,
- GLdouble bottom,
- GLdouble top,
- GLdouble nearVal,
- GLdouble farVal)
11Perspective Example
- Defined in GL/glu.h
- void gluPerspective(GLdouble fovy,
- GLdouble aspect,
- GLdouble zNear,
- GLdouble zFar )?
//Left Viewport glViewport(0.0,0.0,w,h) glMatrixM
ode(GL_PROJECTION) glLoadIdentity() gluPerspecti
ve(45.0,w/h,1.0,20.0) //Draw the
scene glMatrixMode(GL_MODELVIEW) drawScene()
12Model -gt View -gt Projection
- Modeling Transformation
- Used to position geometry in the scene
- Viewing Transformation
- Used to position and orient the view of a scene
- Projection Transformation
- Used to project a scene onto a viewing plane
glVertex
Modeling Transformation
View Transformation
Projection Transformation
13Setting up the view
- Used to position and orient the view of a scene
- Viewing transformation can be thought of as a
camera viewing the scene - Order of transformations that setup a camera
- Position
- Orientation
14gluLookAt
- Defined in ltgl/glu.hgt
- void gluLookAt(
- GLdouble eyeX,
- GLdouble eyeY,
- GLdouble eyeZ,
- GLdouble centerX,
- GLdouble centerY,
- GLdouble centerZ,
- GLdouble upX,
- GLdouble upY,
- GLdouble upZ )?
- eyeX, eyeY, eyeZ
- Specifies the position of the eye point.
- centerX, centerY, centerZ
- Specifies the position of the reference point.
- upX, upY, upZ
- Specifies the direction of the up vector.
15gluLookAt continued
- eyeX, eyeY, eyeZ
- Specifies the position of the eye point.
- centerX, centerY, centerZ
- Specifies the position of the reference point.
- upX, upY, upZ
- Specifies the direction of the up vector.
16Depth Buffer
- Additional component of the frame buffer
- Holds the depth value of each fragment
- Needs to be cleared like color buffer
17Setting up Depth Test
- OpenGL disables depth test by default
- glEnable(GL_DEPTH_TEST)
- We have an additional part of the frame buffer
that needs clearing - glClear(GL_COLOR_BUFFER_BIT GL_DEPTH_BUFFER_BIT)
- We need a drawing context that has a depth buffer
- glutInitDisplayMode(GLUT_RGBA GLUT_SINGLE
GLUT_DEPTH)
18Depth Test Example
Display code
GLUT initialization
void display(void)? glEnable(GL_DEPTH_TEST)
glClear(GL_COLOR_BUFFER_BIT
GL_DEPTH_BUFFER_BIT) //Setup state to start
drawing glBegin(GL_TRIANGLES)
glVertex3i(-100, 100, 1) glVertex3i(0,
100, 1) glVertex3i(-50, 50, 1)
glVertex3i(50, 50, 3) glVertex3i(100,
100, 3) glVertex3i(75, 20, 3)
glEnd()
int main(int argc, char argv)?
glutInit(argc, argv) glutInitDisplayMode(GLUT
_RGBA GLUT_SINGLE GLUT_DEPTH)
glutCreateWindow("Depth Example")
glutDisplayFunc(display) glutReshapeFunc(reshap
e) glutMainLoop() return 0