1 of 68

Computer Graphics Seminar

MTAT.03.305

Fall 2021

Raimond Tunnel

2 of 68

Computer Graphics

  • Graphical illusion via the computer
  • Displaying something meaningful (incl art)

3 of 68

4 of 68

Math

  • Computers are good at… computing.
  • To do computer graphics, we need math for the computer to compute.
  • Geometry, algebra, calculus.

5 of 68

Math

  • For creating and manipulating 3D objects we use:
    • Analytic geometry – math about coordinate systems
    • Linear algebra – math about vectors and spaces

6 of 68

Skills for Computer Graphics

  • Mathematical understanding

  • Geometrical (spatial) thinking

  • Programming

  • Visual creativity & aesthetics

GLuint vaoHandle;

glGenVertexArrays(1, &vaoHandle);

glBindVertexArray(vaoHandle);

7 of 68

The Standard Graphics Pipeline

Data

Vertex Shader

Culling & Clipping

Rasterization

Fragment Shader

Visibility tests & Blending

Define geometry and transformations

Apply transformations on the geometry

8 of 68

Point

  • Simplest geometry primitive
  • In homogeneous coordinates:

(x, y, z, w), w ≠ 0

  • Represents a point (x/w, y/w, z/w)
  • Usually you can put w = 1 for points
  • Actual division will be done by GPU later

9 of 68

Line (segment)

  • Consists of:
    • 2 endpoints
    • Infinite number of points between
  • Defined by the endpoints
  • Interpolated and rasterized in the GPU

10 of 68

Line (segment)

  • Consists of:
    • 2 endpoints
    • Infinite number of points between
  • Defined by the endpoints
  • Interpolated and rasterized in the GPU

11 of 68

Line (segment)

  • Consists of:
    • 2 endpoints
    • Infinite number of points between
  • Defined by the endpoints
  • Interpolated and rasterized in the GPU

B

A

B

A

12 of 68

Triangle

  • Consists of:
    • 3 points called vertices
    • 3 lines called edges
    • 1 face
  • Defined by 3 vertices
  • Face interpolated and rasterized in the GPU
  • Counter-clockwise order defines the front face

13 of 68

Triangle

  • Consists of:
    • 3 points called vertices
    • 3 lines called edges
    • 1 face
  • Defined by 3 vertices
  • Face interpolated and rasterized in the GPU
  • Counter-clockwise order defines the front face

14 of 68

Why Triangles?

  • They are in many ways the simplest polygons
    • 3 different points always form a plane
    • Easy to rasterize (fill the face with pixels)
    • Every other polygon can be converted to triangles

15 of 68

Why Triangles?

  • They are in many ways the simplest polygons
    • 3 different points always form a plane
    • Easy to rasterize (fill the face with pixels)
    • Every other polygon can be converted to triangles
  • OpenGL used to support other polygons that were:
    • Simple – No edges intersect each other
    • Convex – All points between any two inner points are inner points

16 of 68

Examples of Polygons

A

B

C

D

E

F

G

I

J

K

L

C

C

B

B

C

A

A

B

D

D

E

F

H

A

17 of 68

OpenGL <3.1 primitives

OpenGL Programming Guide 7th edition, p49

18 of 68

After OpenGL 3.1

OpenGL Programming Guide 8th edition, p89-90

19 of 68

It’s all just points, lines and triangles!

  • We can now define our geometric objects.

20 of 68

It’s all just points, lines and triangles!

  • We can now define our geometric objects.
  • But we also want to move our objects around...

World’s Origin (0, 0, 0)

21 of 68

Transformations

  • Linear transformations
    • Scaling, reflection
    • Rotation
    • Shearing
  • Affine transformations
    • Translation (moving / shifting)
  • Projection transformations
    • Perspective
    • Orthographic

Homogeneous coordinates are needed here...

...and for the perspective projection.

22 of 68

Transformations

  • Every transformation is a function
  • As you have learned from algebra:
    • All linear functions can be represented as matrices

Column-major format

23 of 68

Transformations

  • Every transformation is a function
  • As you have learned from algebra:
    • All linear functions can be represented as matrices

Column-major format

Linear function, which increases the first coordinate two times.

24 of 68

Transformations

  • Every transformation is a function
  • As you have learned from algebra:
    • All linear functions can be represented as matrices

Column-major format

Same function as a matrix

25 of 68

Transformations

  • GPU-s are built for doing transformations with matrices on points (vertices).

DRAM

ALU

ALU

ALU

...

Control

Cache

ALU

ALU

ALU

ALU

...

Control

Cache

ALU

ALU

ALU

ALU

...

Control

Cache

ALU

ALU

ALU

ALU

...

Control

Cache

ALU

ALU

ALU

ALU

...

Control

Cache

ALU

ALU

ALU

ALU

...

Control

Cache

ALU

ALU

ALU

ALU

...

Control

Cache

ALU

Vertex shader code

26 of 68

Transformations

  • GPU-s are built for doing transformations with matrices on points (vertices).��
  • Linear transformations satisfy:

We will not use homogeneous coordinates at the moment, but they will be back...

27 of 68

Linear Transformation

Scale

28 of 68

Scaling

  • Multiplies the coordinates by a scalar factor.

29 of 68

Scaling

  • Multiplies the coordinates by a scalar factor.
  • Scales the standard basis vectors / axis

30 of 68

Scaling

  • In general we can scale each axis������
  • If some factor is negative, this matrix will reflect the points from that axis. Thus we get reflection.

– x-axis scale factor

– y-axis scale factor

– z-axis scale factor

What happens to out triangles when an odd number of factors are negative?

31 of 68

Linear Transformation

Shear

32 of 68

Shearing

  • Remember it for translations later.
  • Tilts only one axis.
  • Squares become parallelograms.

33 of 68

Shearing

  • Shear-y, we tilt parallel to y-axis by angle φ counter-clockwise���
  • Shear-x, we tilt parallel to x-axis by angle φ clockwise

34 of 68

Linear Transformation

Rotation

35 of 68

Rotation

  • Shearing moved only one axis
  • Also changed the size of the basis vector
  • Can we do better?

Did you notice that the columns of the transformation matrix show the coordinates of the new basis vectors?

36 of 68

Rotation

37 of 68

Rotation

  • So if we rotate by α in counter-clockwise order in 2D, the transformation matrix is:�������
  • In 3D we can do rotations in each plane (xy, xz, yz), so there can be 3 different matrices.

38 of 68

Rotation II

  • Rodrigues’ Rotation Formula – Rotate P around axis a.

39 of 68

Rotation II

  • Rodrigues’ Rotation Formula – Rotate P around axis a.
  • Derivation:
    1. Separate P’s location vector �into parallel and perpendicular �components:

This is easily done via scalar projection.

40 of 68

Rotation II

  • Rodrigues’ Rotation Formula – Rotate P around axis a.
  • Derivation:
    • Separate P’s location vector �into parallel and perpendicular �components:
    • Create a 2D basis from and�a perpendicular vector .

41 of 68

Rotation II

  • Rodrigues’ Rotation Formula – Rotate P around axis a.
  • Derivation:
    • Separate P’s location vector �into parallel and perpendicular �components:
    • Create a 2D basis from and�a perpendicular vector .
    • Rotate on the 2D basis by �angle to get .

Formula is simpler as we only need to rotate the first basis vector.

42 of 68

Rotation II

  • Rodrigues’ Rotation Formula – Rotate P around axis a.
  • Derivation:
    • Separate P’s location vector �into parallel and perpendicular �components:
    • Create a 2D basis from and�a perpendicular vector .
    • Rotate on the 2D basis by �angle to get .
    • Recombine the result with the �parallel component:

43 of 68

Rotation II

  • Rodrigues’ Rotation Formula – Rotate P around axis a.
  • Will result in a formula, which is identical to quaternion rotation formula:

44 of 68

Rotation III

  • Quaternion rotation – Rotate P around axis a.

45 of 68

Rotation III

  • Quaternion rotation – Rotate P around axis a.
  • Quaternion:

46 of 68

Rotation III

  • Quaternion rotation – Rotate P around axis a.
  • Quaternion: ��
  • Conjugate quaternion:

47 of 68

Rotation III

  • Quaternion rotation – Rotate P around axis a.
  • Quaternion: ��
  • Conjugate quaternion:
  • Let be the location vector of P.�Quaternion representing will be .

48 of 68

Rotation III

  • Quaternion rotation – Rotate P around axis a.
  • Quaternion: ��
  • Conjugate quaternion:
  • Let be the location vector of P.�Quaternion representing will be .
  • Let be the normalized rotation axis vector. The “rotation �by “ quaternion will be .

49 of 68

Rotation III

  • Quaternion rotation – Rotate P around axis a.

50 of 68

Back to the Main Track

51 of 68

Do we have everything now?

  • We can scale, shear, and rotate around the origin...

What if we have an object not centered in the origin?

52 of 68

Affine Transformation

Translation

53 of 68

Translation

  • Imagine that a 1D world is at the y=1 line in 2D space.
  • Notice that all the points are in the form: (x, 1)

Objects

The 1D World

54 of 68

Translation

  • Do a shear-x(45°) operation on the 2D world!

55 of 68

Translation

  • Do a shear-x(45°) operation on the 2D world!

56 of 68

Translation

  • Do a shear-x(63.4°)...

57 of 68

Translation

  • We can do translation (movement)!

tan(45°) = 1

tan(63.4°) = 2

58 of 68

Translation

  • When we represent our points in one dimension higher space, where the extra coordinate is 1, we get to the homogeneous space.

1D

2D

3D

59 of 68

Transformations

  • This together gives us a very good toolset to transform our geometry as we wish.

Augmented transformation matrix

Linear transformations

Translation column

Used for perspective projection

60 of 68

Multiple Transformations

  • Everything starts from the origin!
  • To apply multiple transformations, just multiply matrices.

61 of 68

Multiple Transformations

Our initial geometry defined by vertices: (-1, -1), (1, -1), (1, 1), (-1, 1)

62 of 68

Multiple Transformations

63 of 68

Multiple Transformations

64 of 68

Multiple Transformations

  • Combine the transformations to a single matrix.

65 of 68

Multiple Transformations

  • Combine the transformations to a single matrix.

1. We first rotated.

2. Then translated.

66 of 68

Multiple Transformations

  • Combine the transformations to a single matrix.
  • The order of transformations is important!

67 of 68

Now You Know

Data

Vertex Shader

Culling & Clipping

Rasterization

Fragment Shader

Visibility tests & Blending

vs

68 of 68

Next Time...

  • What is color in computer graphics?
  • How to color our rasterized pixels?
  • Light calculations.

Data

Vertex Shader

Culling & Clipping

Rasterization

Fragment Shader

Visibility tests & Blending

vs