102
8/9/2019 lezione33 http://slidepdf.com/reader/full/lezione33 1/102 Grafica Computazionale lezione33 Informatica e Automazione, "Roma Tre" May 25, 2010

lezione33

Embed Size (px)

Citation preview

Page 1: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 1/102

Grafica Computazionalelezione33

Informatica e Automazione, "Roma Tre"

May 25, 2010

Page 2: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 2/102

Page 3: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 3/102

Contents

OpenGL SuperBible

Vertex transformation pipeline2D projectionModeling transformationsViewing transformationProjection abuse

Write a generic source environment

Page 4: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 4/102

OpenGL SuperBible

Main reference:

Page 5: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 5/102

OpenGL SuperBible

Main reference:

  OpenGL SuperBible

Page 6: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 6/102

OpenGL SuperBible

Main reference:

  OpenGL SuperBible

  book code

Page 7: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 7/102

Hello to Glut and OpenGL World

Hello World

First example with window creation and display callback

Page 8: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 8/102

Contents

OpenGL SuperBible

Vertex transformation pipeline2D projectionModeling transformationsViewing transformationProjection abuse

Write a generic source environment

Page 9: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 9/102

The vertex transformation pipeline

Remember:

Page 10: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 10/102

The vertex transformation pipeline

Remember:

 Original vertex data  ≡  MC (Modeling coordinates)

Page 11: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 11/102

The vertex transformation pipeline

Remember:

 Original vertex data  ≡  MC (Modeling coordinates)  Transformed eye coords  ≡  VRC (View Reference Coords)

Page 12: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 12/102

The vertex transformation pipeline

Remember:

 Original vertex data  ≡  MC (Modeling coordinates)  Transformed eye coords  ≡  VRC (View Reference Coords)  Normalized device coords  ≡  NPC (Norm. Projection coords)

Page 13: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 13/102

The vertex transformation pipeline

Remember:

 Original vertex data  ≡  MC (Modeling coordinates)  Transformed eye coords  ≡  VRC (View Reference Coords)  Normalized device coords  ≡  NPC (Norm. Projection coords)   Window coordinates  ≡  DC (device coordinates)

Page 14: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 14/102

Object Coordinates

Object Coordinates are transformed by the ModelView matrix toproduce Eye Coordinates

Page 15: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 15/102

Object Coordinates

Object Coordinates are transformed by the ModelView matrix toproduce Eye Coordinates

  Eye Coordinates are transformed by the Projection matrix to

produce Clip Coordinates

Page 16: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 16/102

Object Coordinates

Object Coordinates are transformed by the ModelView matrix toproduce Eye Coordinates

  Eye Coordinates are transformed by the Projection matrix to

produce Clip Coordinates

 Clip Coordinate X, Y, and Z are divided by Clip Coordinate Wto produce Normalized Device Coordinates

Page 17: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 17/102

Object Coordinates

Object Coordinates are transformed by the ModelView matrix toproduce Eye Coordinates

  Eye Coordinates are transformed by the Projection matrix to

produce Clip Coordinates

 Clip Coordinate X, Y, and Z are divided by Clip Coordinate Wto produce Normalized Device Coordinates

 Normalized Device Coordinates are scaled and translated bythe viewport parameters to produce Window Coordinates

Ob d

Page 18: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 18/102

Object Coordinates

Obj C di

Page 19: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 19/102

Object Coordinates

 Object coordinates are the raw coordinates you submit to

OpenGL with a call to glVertex*() or glVertexPointer()

Obj C di

Page 20: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 20/102

Object Coordinates

 Object coordinates are the raw coordinates you submit to

OpenGL with a call to glVertex*() or glVertexPointer()

 They represent the coordinates of your object or other

geometry you want to render

W ld C di ?

Page 21: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 21/102

World Coordinate system ?

Objects are often modeled in one coordinate system, then scaled,translated, and rotated into the world you’re constructing

W ld C di t t ?

Page 22: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 22/102

World Coordinate system ?

Objects are often modeled in one coordinate system, then scaled,translated, and rotated into the world you’re constructing

  World Coordinates result from transforming ObjectCoordinates by the modelling transforms stored in theModelView matrix

W ld C di t t ?

Page 23: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 23/102

World Coordinate system ?

Objects are often modeled in one coordinate system, then scaled,translated, and rotated into the world you’re constructing

  World Coordinates result from transforming ObjectCoordinates by the modelling transforms stored in theModelView matrix

  However, OpenGL has no concept of World Coordinates

World Coordinate system ?

Page 24: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 24/102

World Coordinate system ?

Objects are often modeled in one coordinate system, then scaled,translated, and rotated into the world you’re constructing

  World Coordinates result from transforming ObjectCoordinates by the modelling transforms stored in theModelView matrix

  However, OpenGL has no concept of World Coordinates

 World Coordinates are purely an application construct

Eye Coordinates

Page 25: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 25/102

Eye Coordinates

Eye Coordinates result from transforming Object Coordinates bythe ModelView matrix

Eye Coordinates

Page 26: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 26/102

Eye Coordinates

Eye Coordinates result from transforming Object Coordinates bythe ModelView matrix

 The ModelView matrix contains both modelling and viewingtransformations that place the viewer at the origin with theview direction aligned with the negative Z axis

Eye Coordinates

Page 27: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 27/102

Eye Coordinates

Eye Coordinates result from transforming Object Coordinates bythe ModelView matrix

 The ModelView matrix contains both modelling and viewingtransformations that place the viewer at the origin with theview direction aligned with the negative Z axis

 Clip Coordinates result from transforming Eye Coordinates bythe Projection matrix

Eye Coordinates

Page 28: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 28/102

Eye Coordinates

Eye Coordinates result from transforming Object Coordinates bythe ModelView matrix

 The ModelView matrix contains both modelling and viewingtransformations that place the viewer at the origin with theview direction aligned with the negative Z axis

 Clip Coordinates result from transforming Eye Coordinates bythe Projection matrix

 Clip Coordinate space ranges from -Wc to Wc in all threeaxes, where Wc is the Clip Coordinate W value

Eye Coordinates

Page 29: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 29/102

Eye Coordinates

Eye Coordinates result from transforming Object Coordinates bythe ModelView matrix

 The ModelView matrix contains both modelling and viewingtransformations that place the viewer at the origin with theview direction aligned with the negative Z axis

 Clip Coordinates result from transforming Eye Coordinates bythe Projection matrix

 Clip Coordinate space ranges from -Wc to Wc in all threeaxes, where Wc is the Clip Coordinate W value

 OpenGL clips all coordinates outside this range

Eye Coordinates

Page 30: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 30/102

Eye Coordinates

Eye Coordinates result from transforming Object Coordinates bythe ModelView matrix

 The ModelView matrix contains both modelling and viewingtransformations that place the viewer at the origin with theview direction aligned with the negative Z axis

 Clip Coordinates result from transforming Eye Coordinates bythe Projection matrix

 Clip Coordinate space ranges from -Wc to Wc in all threeaxes, where Wc is the Clip Coordinate W value

 OpenGL clips all coordinates outside this range

 Perspective division performed on the Clip Coordinatesproduces Normalized Device Coordinates, ranging from -1 to 1in all three axes

Window Coordinates

Page 31: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 31/102

Window Coordinates

Window Coordinates result from scaling and translating NormalizedDevice Coordinates by the viewport

Window Coordinates

Page 32: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 32/102

Window Coordinates

Window Coordinates result from scaling and translating NormalizedDevice Coordinates by the viewport

 The parameters to glViewport() and glDepthRange() controlthis transformation

Window Coordinates

Page 33: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 33/102

C

Window Coordinates result from scaling and translating NormalizedDevice Coordinates by the viewport

 The parameters to glViewport() and glDepthRange() controlthis transformation

 With the viewport, you can map the Normalized DeviceCoordinate cube to any location in your window and depthbuffer

HOWTO set up a 2D projection

Page 34: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 34/102

p p j

To set up a 2D projection, you need to change the  Projectionmatrix.

HOWTO set up a 2D projection

Page 35: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 35/102

p p j

To set up a 2D projection, you need to change the  Projectionmatrix.

 Normally, it’s convenient to set up the projection so one worldcoordinate unit is equal to one screen pixel, as follows:

HOWTO set up a 2D projection

Page 36: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 36/102

p p j

To set up a 2D projection, you need to change the  Projectionmatrix.

 Normally, it’s convenient to set up the projection so one worldcoordinate unit is equal to one screen pixel, as follows:

HOWTO set up a 2D projection

Page 37: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 37/102

To set up a 2D projection, you need to change the  Projectionmatrix.

 Normally, it’s convenient to set up the projection so one worldcoordinate unit is equal to one screen pixel, as follows:

  gluOrtho2D()  sets up a Z range of -1 to 1, so you need touse one of the  glVertex2*()  functions to ensure yourgeometry isn’t clipped by the zNear or zFar clipping planes.

Look carefully !! (from GLRect.py)

Page 38: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 38/102

What I did ?!?

Page 39: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 39/102

Execute (GLbounce.py) again and look !!

Page 40: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 40/102

Howto give each object its own transform?

Page 41: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 41/102

Use the ModelView matrix stack.

Howto give each object its own transform?

Page 42: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 42/102

Use the ModelView matrix stack.

 A typical OpenGL application first sets the matrix mode with acall to  glMatrixMode(GL_MODELVIEW)  and loads a viewingtransform, perhaps with a call to  gluLookAt().

Howto give each object its own transform?

Page 43: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 43/102

Use the ModelView matrix stack.

 A typical OpenGL application first sets the matrix mode with acall to  glMatrixMode(GL_MODELVIEW)  and loads a viewingtransform, perhaps with a call to  gluLookAt().

 Then the code renders each object in the scene with its owntransformation by wrapping the rendering with calls toglPushMatrix()  and  glPopMatrix()

Howto give each object its own transform?

Page 44: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 44/102

For example:

The above code renders a cylinder rotated 90 degrees around theX-axis.

The ModelView matrix is restored to its previous value after theglPopMatrix()  call.

Similar call sequences can render subsequent objects in the scene.

Howto give each object its own transform? (Atom.py)

Page 45: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 45/102

Of course, we need to massage the ModelView matrix:

Howto give each object its own transform?

Page 46: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 46/102

consider the above equivalent with:

Absolute versus relative coordinates

Page 47: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 47/102

If you need to render the same object in multiple locations

Absolute versus relative coordinates

Page 48: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 48/102

If you need to render the same object in multiple locations

 OpenGL lets you do this two ways:

Absolute versus relative coordinates

Page 49: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 49/102

If you need to render the same object in multiple locations

 OpenGL lets you do this two ways:

Absolute versus relative coordinates

Page 50: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 50/102

If you need to render the same object in multiple locations

 OpenGL lets you do this two ways:

Use absolute coordinates

Absolute versus relative coordinates

Page 51: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 51/102

If you need to render the same object in multiple locations

 OpenGL lets you do this two ways:

Use absolute coordinates

  Maintain multiple copies of each object, each with its ownunique set of vertices

Absolute versus relative coordinates

Page 52: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 52/102

If you need to render the same object in multiple locations

 OpenGL lets you do this two ways:

Use absolute coordinates

  Maintain multiple copies of each object, each with its ownunique set of vertices

  You don’t need to change the ModelView matrix to render theobject at the desired location

Absolute versus relative coordinates

f d d h b l l l

Page 53: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 53/102

If you need to render the same object in multiple locations

 OpenGL lets you do this two ways:

Use absolute coordinates

  Maintain multiple copies of each object, each with its ownunique set of vertices

  You don’t need to change the ModelView matrix to render theobject at the desired location

Absolute versus relative coordinates

If d d h bj i l i l l i

Page 54: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 54/102

If you need to render the same object in multiple locations

 OpenGL lets you do this two ways:

Use absolute coordinates

  Maintain multiple copies of each object, each with its ownunique set of vertices

  You don’t need to change the ModelView matrix to render theobject at the desired location

Use relative coordinates

Absolute versus relative coordinates

If d t d th bj t i lti l l ti

Page 55: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 55/102

If you need to render the same object in multiple locations

 OpenGL lets you do this two ways:

Use absolute coordinates

  Maintain multiple copies of each object, each with its ownunique set of vertices

  You don’t need to change the ModelView matrix to render theobject at the desired location

Use relative coordinates

  Keep only one copy of the object, and render multiple times by

Absolute versus relative coordinates

If d t d th bj t i lti l l ti

Page 56: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 56/102

If you need to render the same object in multiple locations

 OpenGL lets you do this two ways:

Use absolute coordinates

  Maintain multiple copies of each object, each with its ownunique set of vertices

  You don’t need to change the ModelView matrix to render theobject at the desired location

Use relative coordinates

  Keep only one copy of the object, and render multiple times by

Absolute versus relative coordinates

If you need to render the same object in multiple locations

Page 57: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 57/102

If you need to render the same object in multiple locations

 OpenGL lets you do this two ways:

Use absolute coordinates

  Maintain multiple copies of each object, each with its ownunique set of vertices

  You don’t need to change the ModelView matrix to render theobject at the desired location

Use relative coordinates

  Keep only one copy of the object, and render multiple times by

1.   pushing the ModelView matrix stack,

Absolute versus relative coordinates

If you need to render the same object in multiple locations

Page 58: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 58/102

If you need to render the same object in multiple locations

 OpenGL lets you do this two ways:

Use absolute coordinates

  Maintain multiple copies of each object, each with its ownunique set of vertices

  You don’t need to change the ModelView matrix to render theobject at the desired location

Use relative coordinates

  Keep only one copy of the object, and render multiple times by

1.   pushing the ModelView matrix stack,

2.  setting the desired transform, sending the geometry, and

Absolute versus relative coordinates

If you need to render the same object in multiple locations

Page 59: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 59/102

If you need to render the same object in multiple locations

 OpenGL lets you do this two ways:

Use absolute coordinates

  Maintain multiple copies of each object, each with its ownunique set of vertices

  You don’t need to change the ModelView matrix to render theobject at the desired location

Use relative coordinates

  Keep only one copy of the object, and render multiple times by

1.   pushing the ModelView matrix stack,

2.  setting the desired transform, sending the geometry, and

3.   popping the stack

Absolute versus relative coordinates

If you need to render the same object in multiple locations

Page 60: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 60/102

If you need to render the same object in multiple locations

 OpenGL lets you do this two ways:

Use absolute coordinates

  Maintain multiple copies of each object, each with its ownunique set of vertices

  You don’t need to change the ModelView matrix to render theobject at the desired location

Use relative coordinates

  Keep only one copy of the object, and render multiple times by

1.   pushing the ModelView matrix stack,

2.  setting the desired transform, sending the geometry, and

3.   popping the stack

 Repeat these steps for each object

Page 61: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 61/102

How does the camera work in OpenGL?

Page 62: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 62/102

As far as OpenGL is concerned, there is no camera

How does the camera work in OpenGL?

Page 63: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 63/102

As far as OpenGL is concerned, there is no camera

  More specifically, the camera is always located at the eyespace coordinate (0., 0., 0.)

How does the camera work in OpenGL?

Page 64: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 64/102

As far as OpenGL is concerned, there is no camera

  More specifically, the camera is always located at the eyespace coordinate (0., 0., 0.)

 To give the appearance of moving the camera, your OpenGLapplication must move the scene with the inverse of thecamera transformation.

How can I move my eye, or camera, in my scene?

Page 65: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 65/102

OpenGL doesn’t provide an interface to do this using a camera

model

How can I move my eye, or camera, in my scene?

Page 66: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 66/102

OpenGL doesn’t provide an interface to do this using a camera

model

 However, the GLU library provides the  gluLookAt()  function,which takes

How can I move my eye, or camera, in my scene?

Page 67: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 67/102

OpenGL doesn’t provide an interface to do this using a camera

model

 However, the GLU library provides the  gluLookAt()  function,which takes

How can I move my eye, or camera, in my scene?

Page 68: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 68/102

OpenGL doesn’t provide an interface to do this using a camera

model

 However, the GLU library provides the  gluLookAt()  function,which takes

1.   an eye position,

How can I move my eye, or camera, in my scene?

Page 69: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 69/102

OpenGL doesn’t provide an interface to do this using a camera

model

 However, the GLU library provides the  gluLookAt()  function,which takes

1.   an eye position,

2.   a position to look at,

How can I move my eye, or camera, in my scene?

Page 70: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 70/102

OpenGL doesn’t provide an interface to do this using a camera

model

 However, the GLU library provides the  gluLookAt()  function,which takes

1.   an eye position,

2.   a position to look at,

3.  and an up vector, all in object space coordinates

How can I move my eye, or camera, in my scene?

Page 71: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 71/102

OpenGL doesn’t provide an interface to do this using a camera

model

 However, the GLU library provides the  gluLookAt()  function,which takes

1.   an eye position,

2.   a position to look at,

3.  and an up vector, all in object space coordinates

 This function computes the inverse camera transformaccording to its parameters and multiplies it onto the currentmatrix stack.

My camera should go to the ModelView or Projectionmatrix?

Page 72: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 72/102

The GL_PROJECTION matrix should contain only the projection

transformation calls it needs to transform eye space coordinatesinto clip coordinates.

The GL_MODELVIEW matrix, as its name implies, should containmodeling and viewing transformations, which transform objectspace coordinates into eye space coordinates

My camera should go to the ModelView or Projectionmatrix?

Page 73: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 73/102

The GL_PROJECTION matrix should contain only the projection

transformation calls it needs to transform eye space coordinatesinto clip coordinates.

The GL_MODELVIEW matrix, as its name implies, should containmodeling and viewing transformations, which transform objectspace coordinates into eye space coordinates

 Remember to place the camera transformations on theGL_MODELVIEW matrix and never on theGL_PROJECTION matrix.

My camera should go to the ModelView or Projectionmatrix?

Page 74: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 74/102

The GL_PROJECTION matrix should contain only the projection

transformation calls it needs to transform eye space coordinatesinto clip coordinates.

The GL_MODELVIEW matrix, as its name implies, should containmodeling and viewing transformations, which transform objectspace coordinates into eye space coordinates

 Remember to place the camera transformations on theGL_MODELVIEW matrix and never on theGL_PROJECTION matrix.

 Think of the projection matrix as describing the attributes of your camera, such as field of view, focal length, fish eye lens,etc

My camera should go to the ModelView or Projectionmatrix?

Page 75: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 75/102

The GL_PROJECTION matrix should contain only the projection

transformation calls it needs to transform eye space coordinatesinto clip coordinates.

The GL_MODELVIEW matrix, as its name implies, should containmodeling and viewing transformations, which transform objectspace coordinates into eye space coordinates

 Remember to place the camera transformations on theGL_MODELVIEW matrix and never on theGL_PROJECTION matrix.

 Think of the projection matrix as describing the attributes of your camera, such as field of view, focal length, fish eye lens,etc

 Think of the ModelView matrix as where you stand with thecamera and the direction you point it.

From "GL_PROJECTION  abuse (By Steve Baker)"

Page 76: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 76/102

One of the most common OpenGL programming errors is misuse of 

the  GL_PROJECTION  matrix stack

From "GL_PROJECTION  abuse (By Steve Baker)"

Page 77: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 77/102

One of the most common OpenGL programming errors is misuse of 

the  GL_PROJECTION  matrix stack

From "GL_PROJECTION  abuse (By Steve Baker)"

Page 78: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 78/102

One of the most common OpenGL programming errors is misuse of 

the  GL_PROJECTION  matrix stack

 This matrix stack is often perceived as being the correct placeto put the position and orientation of the  camera  - but this is

not in fact the case.

From "GL_PROJECTION  abuse (By Steve Baker)"

Page 79: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 79/102

One of the most common OpenGL programming errors is misuse of 

the  GL_PROJECTION  matrix stack

 This matrix stack is often perceived as being the correct placeto put the position and orientation of the  camera  - but this is

not in fact the case.

 Unfortunately, when someone does go and put their cameratransform into the  GL_PROJECTION  matrix (instead of into

GL_MODELVIEW where it belongs)

From "GL_PROJECTION  abuse (By Steve Baker)"

Page 80: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 80/102

One of the most common OpenGL programming errors is misuse of 

the  GL_PROJECTION  matrix stack

 This matrix stack is often perceived as being the correct placeto put the position and orientation of the  camera  - but this is

not in fact the case.

 Unfortunately, when someone does go and put their cameratransform into the  GL_PROJECTION  matrix (instead of into

GL_MODELVIEW where it belongs)

 The consequences are rather subtle:

Lighting

OpenGL has to transform vertex normals into world coordinate space that is to say WITHOUT the effects of

Page 81: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 81/102

coordinate space - that is to say WITHOUT the effects of perspective - but WITH the effects of the camera position

Lighting

OpenGL has to transform vertex normals into world coordinate space that is to say WITHOUT the effects of

Page 82: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 82/102

coordinate space - that is to say WITHOUT the effects of perspective - but WITH the effects of the camera position

Lighting

OpenGL has to transform vertex normals into world coordinate space that is to say WITHOUT the effects of

Page 83: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 83/102

coordinate space - that is to say WITHOUT the effects of perspective - but WITH the effects of the camera position

 Hence, only the  GL_MODELVIEW  matrix is applied to the

normals for lighting

Lighting

OpenGL has to transform vertex normals into world coordinate space - that is to say WITHOUT the effects of

Page 84: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 84/102

coordinate space - that is to say WITHOUT the effects of perspective - but WITH the effects of the camera position

 Hence, only the  GL_MODELVIEW  matrix is applied to the

normals for lighting

 If you put the camera transform into the  GL_PROJECTIONmatrix then your lighting will be wrong

Lighting

OpenGL has to transform vertex normals into world coordinate space - that is to say WITHOUT the effects of

Page 85: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 85/102

coordinate space that is to say WITHOUT the effects of perspective - but WITH the effects of the camera position

 Hence, only the  GL_MODELVIEW  matrix is applied to the

normals for lighting

 If you put the camera transform into the  GL_PROJECTIONmatrix then your lighting will be wrong

 However, some people do their own lighting - and in any case,it can be a subtle error that you might not have noticedpreviously.

Fog

OpenGL has to figure out how far each vertex is from the 

Page 86: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 86/102

camera

Fog

OpenGL has to figure out how far each vertex is from the 

Page 87: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 87/102

camera

Fog

OpenGL has to figure out how far each vertex is from the 

Page 88: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 88/102

camera

 Once again, perspective effects are not relevent to thiscalculation - so the  GL_PROJECTION  matrix is not used

Fog

OpenGL has to figure out how far each vertex is from the camera

Page 89: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 89/102

camera

 Once again, perspective effects are not relevent to thiscalculation - so the  GL_PROJECTION  matrix is not used

 If you put the camera transform into the  GL_PROJECTIONmatrix then your fogging will be wrong

Fog

OpenGL has to figure out how far each vertex is from the camera

Page 90: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 90/102

camera

 Once again, perspective effects are not relevent to thiscalculation - so the  GL_PROJECTION  matrix is not used

 If you put the camera transform into the  GL_PROJECTIONmatrix then your fogging will be wrong

 Since few people use fog, many people have the error anddon’t know it.

TexGen

Since OpenGL uses the eyepoint to figure out some of the 

Page 91: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 91/102

p y p gTexGen’ed texture coordinates,

TexGen

Since OpenGL uses the eyepoint to figure out some of the 

Page 92: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 92/102

TexGen’ed texture coordinates,

TexGen

Since OpenGL uses the eyepoint to figure out some of the 

Page 93: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 93/102

TexGen’ed texture coordinates,

  if the camera position is all mixed up with the projectioninformation then you’ll end up with some pretty strangetexture coordinates

TexGen

Since OpenGL uses the eyepoint to figure out some of the 

Page 94: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 94/102

TexGen’ed texture coordinates,

  if the camera position is all mixed up with the projectioninformation then you’ll end up with some pretty strangetexture coordinates

 (Thanks to Brian Sharp at 3Dfx for pointing this one out)

Z-buffer

I b li (b t h f) th t d

Page 95: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 95/102

I believe (but have no proof) that a screwed up 

GL_PROJECTION  matrix could cause your Z values to be computed strangely 

Z-buffer

I believe (but have no proof) that a screwed up

Page 96: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 96/102

I believe (but have no proof) that a screwed up 

GL_PROJECTION  matrix could cause your Z values to be computed strangely 

Z-buffer

I believe (but have no proof) that a screwed up

Page 97: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 97/102

I believe (but have no proof) that a screwed up 

GL_PROJECTION  matrix could cause your Z values to be computed strangely 

 This would likely only manifest itself as a lack of Z precisionunder some circumstances - so you might not notice - and itmight not matter.

The Golden Rule ( "GL_PROJECTION abuse")

The rule is:

Page 98: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 98/102

The rule is:

The only functions that should be called when the  glMatrixModeis set to  ‘GL_PROJECTION‘ are:

glLoadIdentity

to initialise the stack.

gluPerspective/glFrustum/glOrtho/gluOrtho2

to set the appropriate projection onto the stack.

You could use glLoadMatrix to set up your own projection matrix(only if you understand the restrictions and consequences)

Menu management GLUT callbacks

Page 99: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 99/102

Menu management GLUT callbacks

Page 100: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 100/102

 download the file (triangle.py)

Menu management GLUT callbacks

Page 101: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 101/102

 download the file (triangle.py)

  study it

Menu management GLUT callbacks

Page 102: lezione33

8/9/2019 lezione33

http://slidepdf.com/reader/full/lezione33 102/102

 download the file (triangle.py)

  study it

 produce a generic framework where the keybord keys are usedto either ROTATE on two axes about the origin, or to MOVEthe viewer far or close to the origin.