30
1 GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

Embed Size (px)

Citation preview

Page 1: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

1GR2-00

GR2Advanced Computer

GraphicsAGR

GR2Advanced Computer

GraphicsAGR

Lecture 4Viewing Pipeline

Getting Started with OpenGL

Page 2: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

2GR2-00

The Story So Far ...Lecture 2

The Story So Far ...Lecture 2

We have seen how we can model objects, by transforming them from their local co-ordinate local co-ordinate representation representation into a world co-world co-ordinate systemordinate system

modellingco-ordinates

worldco-ordinatesMODELLING

TRANSFORMATION

Page 3: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

3GR2-00

The Story So Far...Lecture 3

The Story So Far...Lecture 3

And we have seen how we can transform from a special viewing special viewing co-ordinate system co-ordinate system (camera on z-axis pointing along the axis) into a projection co-ordinate projection co-ordinate systemsystem

viewingco-ordinates

projectionco-ordinatesPROJECTION

TRANSFORMATION

Page 4: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

4GR2-00

Completing the Pipeline - Lecture 4

Completing the Pipeline - Lecture 4

We now need to fill in the missing part

to get

worldco-ordinates

viewingco-ordinatesVIEWING

TRANSFORMATION

mod’gco-ords

worldco-ords

view’gco-ords

proj’nco-ords

Page 5: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

5GR2-00

Viewing Coordinate System - View Reference

Point

Viewing Coordinate System - View Reference

Point

In our world co-ordinate system, we need to specify a view reference view reference point - point - this will become the origin of the view co-ordinate system

This can be any convenient point, along the camera direction from the camera position– indeed one possibility is the

camera position camera position itself

xW

yW

zW

P0

Page 6: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

6GR2-00

Viewing Coordinate System - View Plane

Normal

Viewing Coordinate System - View Plane

Normal

xW

yW

zW

P0

N

xW

yW

zW

P0

Q

Next we need to specify the view plane normal, view plane normal, N N - this will give the camera direction, or z-axis direction

Some graphics systems require you to specify N ...

... others (including OpenGL) allow you to specify a ‘look atlook at’ point, Q, from which N is calculated as direction to the ‘look at’ point from the view reference point

Page 7: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

7GR2-00

Viewing Coordinate System - View Up

Direction

Viewing Coordinate System - View Up

Direction

Finally we need to specify the view-up view-up direction, V direction, V - this will give the y-axis direction

xW

yW

zW

P0

N

V

Page 8: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

8GR2-00

Viewing Co-ordinate System

Viewing Co-ordinate System

This gives us a view reference point P0,and vectors N (corresponding to zV) and V (corresponding to yV)

We can construct a vector U perpendicular to both V and N, and this will correspond to the xV axis

How?

xW

yW

zW

P0

N

V

U

Page 9: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

9GR2-00

Transformation from World to Viewing Co-

ordinates

Transformation from World to Viewing Co-

ordinates

Given an object with positions defined in world co-ordinates, we need to calculate the transformation to viewing co-ordinates

The view reference point must be transformed to the origin, and lines along the U, V, N directions must be transformed to lie along the x, y, z directions

Page 10: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

10GR2-00

Transformation from World to Viewing Co-

ordinates

Transformation from World to Viewing Co-

ordinates

Translate so that P0 lies at the origin

xW

yW

zW

P0

- apply translation by (-x0, -y0, -z0)

(x0, y0, z0)

T = 1 0 0 -x0

0 1 0 -y0

0 0 1 -z0

0 0 0 1

V

U

N

Page 11: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

11GR2-00

Transformation from World to Viewing Co-

ordinates

Transformation from World to Viewing Co-

ordinates

Apply rotations so that the U, V and N axes are aligned with the xW, yW and zW directions

This involves three rotations Rx, then Ry, then Rz– first rotate around xW to bring N into the

xW-zW plane

– second, rotate around yW to align N with zW

– third, rotate around zW to align V with yW

Composite rotation R = Rz. Ry. Rx

Page 12: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

12GR2-00

Rotation MatrixRotation Matrix

Fortunately there is an easy way to calculate R, from U, V and N:

R = u1 u2 u3 0

v1 v2 v3 0

n1 n2 n3 0

0 0 0 1

where U = (u1 u2 u3 )Tetc

Page 13: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

13GR2-00

Viewing TransformationViewing Transformation

Thus the viewing transformation is:M = R . T

This transforms object positions in world co-ordinates to positions in the viewing co-ordinate system..

.. with camera pointing along negative z-axis at a view plane parallel to x-y plane

We can then apply the projection transformation

Page 14: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

14GR2-00

Viewing Pipeline So FarViewing Pipeline So Far

We now should understand this viewing pipeline

mod’gco-ords

worldco-ords

view’gco-ords

proj’nco-ords

Page 15: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

15GR2-00

ClippingClipping

Next we need to understand how the clipping to the view volume is performed

Recall that with perspective projection we defined a view frustum outside of which we wanted to clip points and lines, etc

The next slide is from lecture 3 ...

Page 16: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

16GR2-00

View Frustum - Perspective Projection

View Frustum - Perspective Projection

view window

backplane

frontplane

camera

view frustum

zV

Page 17: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

17GR2-00

Clipping to View FrustumClipping to View Frustum

It is quite easy to clip lines to the front and back planes (just clip in z)..

.. but it is difficult to clip to the sides because they are ‘sloping’ planes

Instead we carry out the projection first which converts the frustum to a rectangular parallelepiped (ie a cuboid)

Page 18: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

18GR2-00

Clipping for Parallel Projection

Clipping for Parallel Projection

In the parallel projection case, the viewing volume is already a rectangular parallelepiped

view window

backplane

frontplane

zV

view volume

Page 19: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

19GR2-00

Normalized Projection Co-ordinates

Normalized Projection Co-ordinates

Final step before clipping is to normalizenormalize the co-ordinates of the rectangular parallelepiped to some standard shape– for example, in some systems, it is

the cube with limits +1 and -1 in each direction

This is just a scalescale transformation Clipping is then carried out

against this standard shape

Page 20: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

20GR2-00

Viewing Pipeline So FarViewing Pipeline So Far

Our pipeline now looks like:

mod’gco-ords

worldco-ords

view’gco-ords

proj’nco-ords

normalizedprojectionco-ordinatesNORMALIZATION

TRANSFORMATION

Page 21: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

21GR2-00

And finally...And finally...

The last step is to position the picture on the display surface

This is done by a viewport viewport transformation transformation where the normalized projection co-ordinates are transformed to display co-ordinates, ie pixels on the screen

Page 22: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

22GR2-00

Viewing Pipeline - The EndViewing Pipeline - The End

A final viewing pipeline is therefore:

mod’gco-ords

worldco-ords

view’gco-ords

proj’nco-ords

normalizedprojectionco-ordinates

deviceco-ordinates

DEVICETRANSFORMATION

Page 23: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

23GR2-00

InterludeInterlude

Why does a mirror reflect left-right and not up-down?

Page 24: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

24GR2-00

OpenGL - Getting Started

Page 25: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

25GR2-00

What is OpenGL?What is OpenGL?

OpenGL provides a set of routines (API) for advanced 3D graphics– derived from Silicon Graphics GL– acknowledged industry standard, even on PCs

(OpenGL graphics cards available)– integrates 3D drawing into X (and other

window systems such as Windows NT)– draws simple primitives (points, lines,

polygons) but NOT complex primitives such as spheres

– provides control over transformations, lighting, etc

– Mesa is publically available equivalent

Page 26: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

26GR2-00

Geometric PrimitivesGeometric Primitives

Defined by a group of vertices - for example to draw a triangle:

glBegin (GL_POLYGON);

glVertex3i (0, 0, 0);

glVertex3i (0, 1, 0);

glVertex3i (1, 0, 1);

glEnd(); See Chapter 2 of the OpenGL

Programming Guide

Page 27: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

27GR2-00

ViewingViewing

OpenGL maintains two matrix transformation modes– MODELVIEW

to specify modelling transformations, and transformations to align camera

– PROJECTION

to specify the type of projection (parallel or perspective) and clipping planes

See Chapter 3 of OpenGL Programming Guide

Page 28: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

28GR2-00

OpenGL Utility Library (GLU)

OpenGL Utility Library (GLU)

Useful set of higher level utility routines to make some tasks easier– written in terms of OpenGL and

provided with the OpenGL implementation

– for example, gluLookAt() is a way of specifying the viewing transformation

Described within the OpenGL Programming Guide– eg gluLookAt() is described in Chap 3,

pp19-21

Page 29: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

29GR2-00

OpenGL Utility Toolkit (GLUT)

OpenGL Utility Toolkit (GLUT)

Set of routines to provide an interface to the underlying windowing system - plus many useful high-level primitives (even a teapot - glutSolidTeapot()!)

See Appendix D of OpenGL Guide Allows you to write ‘event driven’

applications– you specify call back functions which are

executed when an event (eg window resize) occurs

Page 30: 1GR2-00 GR2 Advanced Computer Graphics AGR Lecture 4 Viewing Pipeline Getting Started with OpenGL

30GR2-00

How to Get StartedHow to Get Started

Look at the GR2/AGR resources page:– http://www.scs.leeds.ac.uk/kwb/

GR2/ resources.html Points you to:

– example programs– information about GLUT– information about OpenGL– a simple exercise