Viewing in OpenGL - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Viewing in OpenGL

Description:

Choose a lens and adjust zoom (projection transform) ... Camera Analogy. This one is brittle when moving to stereoscopic displays ... – PowerPoint PPT presentation

Number of Views:88
Avg rating:3.0/5.0
Slides: 25
Provided by: david1453
Category:
Tags: opengl | viewing

less

Transcript and Presenter's Notes

Title: Viewing in OpenGL


1
Viewing in OpenGL
  • CS4451A

2
Camera Analogy
  • Set up the tripod and point the camera (view
    transform)
  • Arrange the objects in the scene (model
    transform)
  • Choose a lens and adjust zoom (projection
    transform)
  • Choose photo print size (viewport transform)

3
Camera Analogy
  • Caveat! Beware!
  • This is an analogy
  • Analogies break

4
Camera Analogy
  • This one is brittle when moving to stereoscopic
    displays
  • You specify transforms in this order, but
    mathematical operations do not follow this order.

5
Vertex Transformation
6
Modelview Matrix
  • Modelview matrix converts object coordinates into
    eye coordinates
  • Model transformations move the object, view
    transformations move the eyepoint,
    interchangeable!

7
Projection Matrix
  • Selecting the lens, telephoto or wide angle
  • Mathematically, selecting the volume to be mapped
    to viewport (mapping, homology, projective
    transform)

8
Perspective Division
  • The divide by w step x y z z/d
  • xprojected dx/z x/(z/d)
  • yprojected dy/z y/(z/d)

9
Vertex Transformation
  • Mapping window on projection plane to viewport
  • Use glViewport
  • Done in reshape function so you know what size
    the viewport has become

10
Cube.c
  • void reshape (int w, int h)
  • glViewport (0, 0, (GLsizei) w, (GLsizei) h)
  • glMatrixMode (GL_PROJECTION)
  • glLoadIdentity ()
  • glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
  • glMatrixMode (GL_MODELVIEW)
  • void display(void)
  • glClear (GL_COLOR_BUFFER_BIT)
  • glColor3f (1.0, 1.0, 1.0)
  • glLoadIdentity () / clear the matrix /
  • / viewing transformation /
  • gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0,
    1.0, 0.0)
  • glScalef (1.0, 2.0, 1.0) / modeling
    transformation /
  • glutWireCube (1.0)
  • glFlush ()
  • void init(void)
  • glClearColor (0.0, 0.0, 0.0, 0.0)
  • glShadeModel (GL_FLAT)
  • int main(int argc, char argv)
  • glutInit(argc, argv)
  • glutInitDisplayMode (GLUT_SINGLE GLUT_RGB)
  • glutInitWindowSize (500, 500)
  • glutInitWindowPosition (100, 100)
  • glutCreateWindow (argv0)
  • init ()
  • glutDisplayFunc(display)
  • glutReshapeFunc(reshape)
  • glutMainLoop()
  • return 0

11
Cube.c
  • void reshape (int w, int h)
  • glViewport (0, 0, (GLsizei) w, (GLsizei) h)
  • glMatrixMode (GL_PROJECTION)
  • glLoadIdentity ()
  • glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0)
  • glMatrixMode (GL_MODELVIEW)
  • void display(void)
  • glClear (GL_COLOR_BUFFER_BIT)
  • glColor3f (1.0, 1.0, 1.0)
  • glLoadIdentity () / clear the matrix /
  • / viewing transformation /
  • gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0,
    1.0, 0.0)
  • glScalef (1.0, 2.0, 1.0) / modeling
    transformation /
  • glutWireCube (1.0)
  • glFlush ()

12
Viewing
  • Use rotations and translations or
  • void gluLookAt(
  • GLdouble eyex, GLdouble eyey, GLdouble eyez,
  • GLdouble centerx, GLdouble centery, GLdouble
    centerz,
  • GLdouble upx, GLdouble upy, GLdouble upz)
  • Eye point
  • Center of field of view (aim point)
  • Up vector
  • Default
  • gluLookat (0.0, 0.0, 0.0, 0.0, 0.0, -100.0, 0.0,
    1.0, 0.0)

13
Modeling
  • void glTranslatefd(TYPE x, TYPE y, TYPE z)
  • void glRotatefd(TYPE angle, TYPE x, TYPE y,
    TYPE z)
  • void glScalefd(TYPE x, TYPE y, TYPE z)

14
Projection
  • Parallel
  • glOrtho
  • gluOrtho2D
  • Perspective
  • glFrustum
  • glPerspective

15
Orthogonal View Volumes
  • void glOrtho(GLdouble left, GLdouble right,
  • GLdouble bottom, GLdouble top,
  • GLdouble near, GLdouble far)

16
Orthogonal View Volumes
  • void gluOrtho2D(
  • GLdouble left, GLdouble right,
  • GLdouble bottom, GLdouble top)
  • Assumes z within 1.0,1.0

17
Perspective View Volumes
  • void glFrustum(
  • GLdouble left, GLdouble right,
  • GLdouble bottom, GLdouble top,
  • GLdouble near, GLdouble far)

18
Perspective View Volumes
  • void gluPerspective(GLdouble fovy, GLdouble
    aspect, GLdouble near, GLdouble far)
  • Easier to use, but limited to symmetric, (not off
    axis)

19
Viewport
  • void glViewport(GLint x, GLint y,
  • GLsizei width, GLsizei height)
  • You may need to preserve aspect ratio

20
Correcting distortion
  • Left
  • gluPerspective(fovy, 1.0, near, far)
  • glViewport(0, 0, 400, 400)
  • Right
  • gluPerspective(fovy, 1.0, near, far)
  • glViewport (0, 0, 400, 200)
  • Corrected
  • gluPerspective(fovy, 2.0, near, far)
  • glViewport(0, 0, 400, 200)

21
Pushing and Popping Matrices
  • Draw the car body.
  • Remember where you are, and translate to the
    right front wheel.
  • Draw the wheel and throw away the last
    translation so your current position is back at
    the origin of the car body.
  • Remember where you are, and translate to the left
    front wheel....

22
Pushing and Popping Matrices
23
Pushing and Popping Matrices
  • draw_wheel_and_bolts()
  • long i
  • draw_wheel()
  • for(i0ilt5i)
  • glPushMatrix()
  • glRotatef(72.0i,0.0,0.0,1.0)
  • glTranslatef(3.0,0.0,0.0)
  • draw_bolt()
  • glPopMatrix()
  • draw_body_and_wheel_and_bolts()
  • draw_car_body()
  • glPushMatrix()
  • glTranslatef(40,0,30) /move to first
    wheel position/
  • draw_wheel_and_bolts()
  • glPopMatrix()
  • glPushMatrix()
  • glTranslatef(40,0,-30) /move to 2nd
    wheel position/
  • draw_wheel_and_bolts()
  • glPopMatrix()
  • ... /draw last two wheels
    similarly/

24
Unproject
  • Reverses the projection process. Instead of
    mapping points in object space to the screen,
    maps screen points to object space.
  • int gluUnProject(
  • GLdouble winx, GLdouble winy, GLdouble winz,
  • const GLdouble modelMatrix16,
  • const GLdouble projMatrix16,
  • const GLint viewport4,
  • GLdouble objx, GLdouble objy, GLdouble objz)
  • Maps from viewport space into object space
  • winz0 gives near clipping plane point
  • winz1 gives far clipping plane point
Write a Comment
User Comments (0)
About PowerShow.com