1 of 48

Geometry: �Projection

Instructor: Christopher Rasmussen (cer@cis.udel.edu)

February 25, 2021 ❖ Lecture 4

2 of 48

Outline

  • Projections, special views
    • Parallel/Orthographic
    • Perspective
  • GLM
  • HW #1
  • Marschner readings: 7.3, 7.5

3 of 48

Projections in CG

  • Mapping from n-D space down to lower-dimensional subspace
    • E.g., point in 3-D space to point on plane (a 2-D entity) in that space
    • We will be interested in such 3-D to 2-D projections where the plane is the image plane
  • Things to know:
    • Where is the plane? Derived from view matrix V
    • What region of the plane does the image occupy?
    • What kind of projection?

4 of 48

Parallel Projections

3-D points "slide" along direction d until they hit plane, where their locations can be described in 2-D. For example, Q→ q, P→p *

from Hill

*not our usual notation for points

5 of 48

Parallel Projection subtypes

Oblique: d in general orientation

relative to image plane normal n

Orthographic: d parallel to n

from Hill

6 of 48

Oblique Projection: Example

7 of 48

Orthographic Projection

  • Projection direction d is aligned with Z axis
  • Viewing volume is “brick”-shaped region in space
    • Not the same as image size
  • No perspective effects—distant objects look same as near ones, so camera (x, y, z) image (x, y)

from Hill

8 of 48

Camera Orientation under Orthographic Projection (set by lookat)

  • Plan: Projection onto XZ (ground) plane (aka: ichnographic view)
  • Elevations
    • Front: Projection onto XY plane
    • Side: Projection onto YZ plane

courtesy of http://glasnost.itcarlow.ie/~powerk/GeneralGraphicsNotes/projection/orthographicprojection.html

Z

X

Y

Da Vinci's 1502 plan of Imola

Elevations, plan of Pantheon in Rome

9 of 48

Plan/elevation views in games (not necessarily orthographic)

Z

X

Y

10 of 48

Camera Orientation under Orthographic Projection (set by lookat)

  • Isometric: "¾ view" -- axes equally scaled
  • Dimetric: 2 axes the same, 1 different
  • Trimetric: All axes different

By SharkD - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=8497328

These angles are not the same as Euler angles (roll, pitch, yaw) of camera. Camera angles for isometric view are explained here

11 of 48

Camera Orientation under Orthographic Projection (set by lookat)

  • Isometric: "¾ view" -- axes equally scaled
  • Dimetric: 2 axes the same, 1 different
  • Trimetric: All axes different (aka general view)

some images courtesy of http://glasnost.itcarlow.ie/~powerk/GeneralGraphicsNotes/projection/orthographicprojection.html

SimCity 2000 (not quite isometric)

Diablo

Zaxxon

12 of 48

The Geometry Pipeline so far...

M = rotation, scaling, translation that puts model where we want it in the world

V = transformation that places the camera (the result of the lookat() function)

13 of 48

Model vs. World Coordinates

M = rotation, scaling, translation that puts model where we want it in the world

Z

X

Y

Z

X

Y

model frame

world frame

14 of 48

Model vs. World Coordinates

Mi for each pencil i to scale, rotate,

and translate it just so

Z

X

Y

world frame

image courtesy Osman Usta, S2019 student

15 of 48

Orthographic Projection in OpenGL

  • Setting up the viewing volume (VV) with GLM library
    • glm::ortho()
      • left, right, bottom, top: Coordinates of sides of viewing volume (in camera coordinates)
      • znear, zfar: Distances to front, back sides of VV
        • Negative = Behind camera
    • glm::ortho2D(): glm::ortho() with near = -1, far = 1

16 of 48

Projection in OpenGL

  • Applied after model M and view V transformations have put things in camera coordinates
  • Actual matrix scales VV to canonical VV (CVV): Cube extending from -1 to 1 along each dimension
    • This is properly a transformation; the projection is accomplished later by stripping off the Z coordinate
  • All together, called "MVP" transform -- as matrices, P * V * M * x

P = intrinsics of camera; result sometimes called "clip coordinates"

17 of 48

Orthographic Projection Matrix for CVV

Translation to origin followed by scaling to 2 x 2 x 2 cube

(If n & f are not distances then n > f, which is why it is

“-2/(f - n)” below — see formula in Marschner):

18 of 48

Orthographic Projection Matrix for CVV

Translation to origin followed by scaling to 2 x 2 x 2 cube

(If n & f are not distances then n > f, which is why it is

“-2/(f - n)” below — see formula in Marschner):

19 of 48

Perspective with a Pinhole Camera (i.e., no lens), aka "camera obscura"

from Forsyth & Ponce

Instead of single direction d characteristic of parallel projections,

rays emanating from single point c (or eye e) define

perspective projection

c

20 of 48

Stenop.es project: Apartment as pinhole camera

21 of 48

More Stenop.es (note projection is upside-down)

22 of 48

Perspective Projection

from Forsyth & Ponce

c

23 of 48

Contrasting VVs for perspective and orthographic projection

24 of 48

Issues with Real Pinhole Cameras

  • It takes a long time to gather enough light through a small pinhole -- not suitable for scenes with motion
    • Bigger pinhole (aka aperture) for more light → Blurry image
    • Smaller pinhole for greater resolution → Diffraction limit
  • A suitable lens can admit more light while reducing blur

from https://en.wikipedia.org/wiki/Camera_lens

25 of 48

Perspective Projection: Viewing Volume

  • Characteristic shape is a frustum—a truncated pyramid
    • Far plane is arbitrary

26 of 48

Perspective Projection: From Camera Coordinates to Image Coordinates

xcam

xim

27 of 48

Perspective Projection

  • Letting the camera coordinates of the projected point be xcam = (x, y, z)T leads by similar triangles to:

Focal length

(“near” distance)

y / z = v / f and x / z = u / f, so....

v = fy / z and u = fx / z

28 of 48

Perspective Projection

  • Letting the camera coordinates of the projected point be xcam = (x, y, z)T leads by similar triangles to:

Focal length

(“near” distance)

29 of 48

Perspective Projection Matrix

  • Using homogeneous coordinates, we can describe a perspective transformation with the image plane at z = -f (because f > 0 but z < 0) via a 4 x 4 matrix multiplication:

Last step accomplishes distance-dependent scaling by the rule for converting between homogeneous and regular coordinates. This is called the perspective division

30 of 48

Perspective Transformation Matrix

  • Intuitively, what we’re doing is transforming the viewing frustum into a “brick” (aka axis-aligned box), then doing orthographic projection

f does not mean

same thing here

as on previous slide!!

31 of 48

Perspective Transformation: Details

  • Instead of simply projecting all z to near plane n, set matrix such that z = n maps to n and z = f maps to f (this is perspective matrix in Marschner):

  • After applying this, multiply by orthographic transform to scale everything to CVV and project to image coordinates
    • Project only after any steps that require depth information

32 of 48

Perspective Projections in OpenGL

  • glm::frustum() sets transformation to CVV
    • Arguments like glm::ortho() (set left, right, bottom, top, near, far directly) but near, far are distances and must be positive
    • Vertical field of view (FOV): θ = 2 arctan(0.5 height / near), where height = top - bottom

from Woo et al.

33 of 48

glm::perspective()

  • Simplifies call to glm::frustum()
  • Arguments:
    • fovy: Field of view angle (degrees) in Y direction
    • aspect: Ratio of width to height of viewing frustum
    • near, far: Same as glm::frustum()

from Woo et al.

34 of 48

Field of View

  • Controls “strength” of perspective effects

courtesy of S. Marschner

35 of 48

Field of View

  • Controls “strength” of perspective effects

courtesy of S. Marschner

Closer to orthographic projection because rays are more nearly parallel

36 of 48

Viewport Transform

  • A final transform shifts normalized device coordinates (NDC—what you have after perspective divide) to image coordinate origin and scales to fit window width & height
    • This is what makes x = 0, y = 0 go to corner of window instead of center
    • More later in the semester...

37 of 48

Geometry pipeline

Coordinate change rigid transform / view transform V

Perspective transform P

(and/or orthographic scaling) to CVV

2-D scale and shift

Perspective division

Orthographic projection

Camera coordinates

Clip coordinates

Normalized device coordinates

Window coordinates

Screen coordinates

World coordinates

Rotate, scale, translate with model transform M

Model coordinates

38 of 48

Combining Transformations: Order

  • Model-View-Projection matrices combined to make single "MVP" matrix:
    • Model -> World coordinates
    • World -> Camera coordinates
    • Camera -> Screen coordinates
  • But...flip order!
    • P*V*M*x

39 of 48

Transformations with GLM

  • Manual with full API (i.e., how to call functions) is here: http://glm.g-truc.net/glm.pdf

#include <glm/glm.hpp>

#include <glm/gtx/transform.hpp>� � glm::mat4 myMatrix;� glm::vec4 myVector;

// fill these somehow� glm::vec4 transformedVector = myMatrix * myVector;

More next time on OpenGL in general

40 of 48

Important Transformations with GLM

  • Model transformations
    • glm::translate()
    • glm::scale()
    • glm::rotate()

  • View transformations
    • glm::lookat(eye, center, up)

  • Projection transformations
    • glm::perspective() // like gluPerspective()
    • glm::ortho() // like glOrtho()

41 of 48

Model Transformations with GLM

  • glm::translate(t)
  • glm::scale(s)
  • glm::rotate(angle, axis)

  • glm::mat4 Model =

glm::translate(glm::vec3( 3.0f,

0.0f,

0.0f));

42 of 48

Model Transformations with GLM

  • glm::translate(t)
  • glm::scale(s)
  • glm::rotate(angle, axis)

  • glm::mat4 Model =

glm::rotate((float) M_PI,

glm::vec3( 0.0f,

0.0f,

1.0f));

43 of 48

View Transformation with GLM

  • glm::lookat(eye, center, up)

  • glm::mat4 View =

glm::lookat( glm::vec3(4,3,3),

glm::vec3(0,0,0),

glm::vec3(0,1,0));

44 of 48

Projection Transformations with GLM

  • glm::perspective(fovy, aspect, near, far)
  • glm::ortho(left, right, bottom, top, zNear, zFar)

  • glm::mat4 Projection =

glm::perspective( 45.0f,

4.0f / 3.0f,

0.1f,

100.0f);

45 of 48

Homework #1

  • Part 1:
    • Get OpenGL 3.3, GLFW installed using instructions in OpenGL Tutorial #1 regarding installation on your system
    • Run the program, which just pops up an empty (black) window

46 of 48

Homework #1

  • Part 1:
    • Get OpenGL 3.3, GLFW installed using instructions in OpenGL Tutorial #1 regarding installation on your system
    • Run the program, which just pops up an empty (black) window
  • Part 2: Extend modified version of OpenGL Tutorial #3 (Matrices) as follows:
    • Create new 3-D polyhedron from transformed versions of triangle and/or square
    • Make 2-D pattern of different simple shapes/objects on XZ plane
      • Use a few different colors
    • Create plan view and approximately isometric view of model with different parameters to lookat(), submit screenshots with orthographic projection

47 of 48

Homework #1

  • Part 1:
    • Get OpenGL 3.3, GLFW installed using instructions in OpenGL Tutorial #1 regarding installation on your system
    • Run the program, which just pops up an empty (black) window
  • Part 2: Extend modified version of OpenGL Tutorial #3 (Matrices) as follows:
    • Create new 3-D polyhedron from transformed versions of triangle and/or square
    • Make 2-D pattern of different simple shapes/objects on XZ plane
      • Use a few different colors
    • Create plan view and isometric view of model with different parameters to lookat(), submit screenshots with orthographic projection
  • Grad section: Programmatic generation of more complex pattern

48 of 48

Selected HW #1 submissions (spring, 2019)

Scott Benton (440)

Osman Usta (640)