more opengl cs 551/645 - introduction to computer graphics

34
More OpenGL CS 551/645 - Introduction to Computer Graphics

Upload: jared-melton

Post on 29-Dec-2015

254 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: More OpenGL CS 551/645 - Introduction to Computer Graphics

More OpenGL

CS 551/645 - Introduction to Computer Graphics

Page 2: More OpenGL CS 551/645 - Introduction to Computer Graphics

2

What’s New?

Everyone getting class email? SGI accounts at Small Hall working

– sgi-5.unixlab is misconfigured

Program 1 deadline extended until Monday, September 18th, 2000 at midnight

TA Office Hours: Tues (3:30 - 5:00) W (3:00 - 5:00) in Small Hall

Page 3: More OpenGL CS 551/645 - Introduction to Computer Graphics

3

Where are we in the book?

Read OpenGL Guide, Chapters 1 - 3 Read Foley (CGP&P) Chapter 3

Page 4: More OpenGL CS 551/645 - Introduction to Computer Graphics

4

Unix Commands - Basics

mkdir foo - make a directory rmdir foo - remove a directory cp foo bar - create a new file named bar

that is a copy of foo mv foo bar - create a new file named bar

that is a copy of foo and delete foo

Page 5: More OpenGL CS 551/645 - Introduction to Computer Graphics

5

Unix Commands - Basics

ls - display the contents of the directory ls -l - display the ‘l’ong version of the

directory cd foo - move from the current directory to

the directory named foo cd .. - move up a directory cd ../foo - move up and over to the foo

directory

Page 6: More OpenGL CS 551/645 - Introduction to Computer Graphics

6

Unix Commands - Basics

foo | bar - called ‘pipe’ a way to sequence commands. The output from foo is used as input by bar

foo > bar - called a redirect. The output from foo is redirected to a file named bar. Useful for capturing output streams to a file

foo < bar - the contents of file bar are used as input to foo

Page 7: More OpenGL CS 551/645 - Introduction to Computer Graphics

7

Unix Commands - Basics

man foo - print out the manual pages for the command, foo

tar - a way to convert a directory structure into a flat file

tar - a way to unpack a flat file into a directory structure

tar | tar - a way to copy a directory structure from one place to another

Page 8: More OpenGL CS 551/645 - Introduction to Computer Graphics

8

Unix Commands - Basics

gzip - a way to compress foo. A new file named foo.gz will be created and foo will be deleted

gunzip foo.gz - a way to uncompress foo.

Page 9: More OpenGL CS 551/645 - Introduction to Computer Graphics

9

Setup Hints

Things complicated because of unification between blue.unix and sgi.unixlab– .xinitrc, .profile, .login, .kshrc may all be incorrectly

configured for another kind of computer– Unfortunately there isn’t a quick fix– One solution is to create a subdir and move all the

dot files to the subdir (so they won’t be executed when you log on the SGI. This will screw up other logins, though.

Page 10: More OpenGL CS 551/645 - Introduction to Computer Graphics

10

Makefile

-I refers to the include path. Compiler looks in all these directories for .h files

-L refers to the library path. Compiler looks in all these directories for the required libraries

-l refers to the required libraries

Page 11: More OpenGL CS 551/645 - Introduction to Computer Graphics

11

Creating Geometry

All geometry composed of vertices (points) ‘Primitive type’ defines the shape they describe

Example vertices: glVertex (x, y, z, w)– glVertex2d - z coordinate is set to 0.0– glVertex3d - w coordinate is set to 1.0– glVertex4d - all coordinates specified (rarely done)

Page 12: More OpenGL CS 551/645 - Introduction to Computer Graphics

12

Primitive Types

GL_POINTS GL_LINE{S | _STRIP | _LOOP} GL_TRIANGLE{S | _STRIP | _FAN} GL_QUAD{S | _STRIP} GL_POLYGON

OpenGL Programming Guide, page 45

Page 13: More OpenGL CS 551/645 - Introduction to Computer Graphics

13

GL_POLYGON

List of vertices defines polygon edges Polygon must be convex

Page 14: More OpenGL CS 551/645 - Introduction to Computer Graphics

14

Non-planar Polygons

Imagine polygon with non-planar vertices Some perspectives will be rendered as

concave polygons These concave polygons may not rasterize

correctly

Page 15: More OpenGL CS 551/645 - Introduction to Computer Graphics

15

Generating Primitives

Primitive defined within glBegin() and glEnd() Very few GL commands can be executed

within these two GL calls Any amount of computation can be performed

glBegin (GL_LINE_LOOP);for (j=0; j<10; j++) {

angle = 2*M_PI*j/10;glVertex2f (cos(angle), sin(angle);

}glEnd();

Page 16: More OpenGL CS 551/645 - Introduction to Computer Graphics

16

glColor()

Color is a GL state variable Vertices are colored according to current value

glBegin (GL_POINTS)glColor3f (0.0, 1.0, 0.0);glColor3f (1.0, 0.0, 0.0);glVertex ();glColor3f (1.0, 1.0, 0.0);glColor3f (0.0, 0.0, 1.0);glVertex ();glVertex ();

glEnd();

Page 17: More OpenGL CS 551/645 - Introduction to Computer Graphics

17

Polygon Rendering Options

Rendered as points, lines, or filled Front and back faces can be rendered separately

using glPolygonMode( ) glPolygonStipple( ) overlays a MacPaint-style

overlay on the polygon glEdgeFlag specifies polygon edges that can be

drawn in line mode Normal vectors: normalized is better, but glEnable

(GL_NORMALIZE) will guarantee it

Page 18: More OpenGL CS 551/645 - Introduction to Computer Graphics

18

Polygonalization Hints

Keep orientations (windings) consistent Best to use triangles (guaranteed planar) Keep polygon number to minimum Put more polygons on silhouettes Avoid T-intersections to avoid cracks

Use exact say coordinates for closing loops

AA B B

CC

BAD OK

Page 19: More OpenGL CS 551/645 - Introduction to Computer Graphics

19

Viewing with GL

Camera Analogy– Set up your tripod and point the camera

Viewing Transformation

– Move objects from world into camera scene Modeling Transformation

– Choose a camera lens and adjust zoom Projection Transformation

– Determine size of photograph Viewport Transformation

Page 20: More OpenGL CS 551/645 - Introduction to Computer Graphics

20

Useful Transformation Commands

glMatrixMode (GL_MODELVIEW, GL_PROJECTION, or GL_TEXTURE)

– Specify which matrix subsequent transformation commands will effect

glLoadIdentity ( )– Sets the currently modified matrix to identity

Page 21: More OpenGL CS 551/645 - Introduction to Computer Graphics

21

Useful Transformation Commands

glLoadMatrix ( )– explicitly load the parameter into the currently

modified matrix glMultMatrix ( )

– explicitly multiply the currently modified matrix by the parameter matrix and store the results in the modified matrix

Argument is vector of 16 values. Vector specifies matrix column by column, left to right.

Page 22: More OpenGL CS 551/645 - Introduction to Computer Graphics

22

Useful Transformation Commands

Warning about declaring matrices in C– matrix m[4][4] can be accesses as m[ i ] [ j ], but this

accesses the ith column and the jth row of the OpenGL transformation matrix

– Because C convention is opposite the OpenGL convention, m[16] is recommended declaration

Page 23: More OpenGL CS 551/645 - Introduction to Computer Graphics

23

Modeling Transformations

glTranslate (x, y, z)– Multiplies the current matrix by a matrix that moves

the object by the given x-, y-, and z-values

glRotate (theta, x, y, z)– Multiplies the current matrix by a matrix that rotates

the object in a counterclockwise direction about the ray from the origin through the point (x, y, z)

Page 24: More OpenGL CS 551/645 - Introduction to Computer Graphics

24

Modeling Transformations

glScale (x, y, z)– Multiplies the current matrix by a matrix that

stretches, shrinks, or reflects an object along the axes.

Page 25: More OpenGL CS 551/645 - Introduction to Computer Graphics

25

Viewing Transformations

Default camera: y is up, look down -z axis glTranslate and glRotate change view gluLookAt ( ) uses these two commands to

change the view

Page 26: More OpenGL CS 551/645 - Introduction to Computer Graphics

26

Projection Transformations

First, execute– glMatrixMode (GL_PROJECTION);– glLoadIdentity ( );

glFrustum (…)– defines position of viewing frustum and near and far

clipping planes– note, frustum need not be symmetric

gluPerspective (…)– specifies frustum, but limited to symmetric ones

Page 27: More OpenGL CS 551/645 - Introduction to Computer Graphics

27

Viewing Transformations

First, execute– glLoadMatrixMode (GL_ORTHO);– glLoadIdentity( );

glOrtho (…)– defines position of viewing frustum and near and far

clipping planes

gluOrtho2D (…)– projects a 2D image to a 2D screen

Page 28: More OpenGL CS 551/645 - Introduction to Computer Graphics

28

Viewport Transformation

glViewport (x, y, width, height)– defines the lower-left corner of the viewport and the

size of the viewport

glDepthRange ( )– depth, distance from camera, is stored with the (x,

y) position of each rendered vertex– this function clamps the range of depth values

stored in the depth buffer

Page 29: More OpenGL CS 551/645 - Introduction to Computer Graphics

29

Troubleshooting the ‘Black Screen’

Object color different from background? Object within the (near, far) clipping planes?

– Try (0.001, 100000.0) to check

Don’t make near clipping plane too small– depth buffer accuracy will degrade

Camera pointing towards the object? Double-check origins for rotations

Page 30: More OpenGL CS 551/645 - Introduction to Computer Graphics

30

Manipulating Matrix Stacks

Observation: Certain model transformations are shared among many models

We want to avoid continuously reloading the same sequence of transformations

glPushMatrix ( ) – push all matrices in current stack down one level and

copy topmost matrix of stack glPopMatrix ( )

– pop the top matrix off the stack

Page 31: More OpenGL CS 551/645 - Introduction to Computer Graphics

31

Matrix Manipulation - Example

Drawing a car with wheels and lugnuts

draw_wheel( );for (j=0; j<5; j++) {

glPushMatrix ();glRotatef(72.0*j, 0.0, 0.0, 1.0);glTranslatef (3.0, 0.0, 0.0);draw_bolt ( );

glPopMatrix ( );

Page 32: More OpenGL CS 551/645 - Introduction to Computer Graphics

32

Add Matrices to Projection Stack

Additional Clipping Planes– glClipPlane (ID, eqn)

Six additional clipping planes can be added ID (0…5) corresponds to each plane eqn points to 4 parameters of plane: Ax + By + Cz + D = 0

– glEnable ( GL_CLIP_PLANEi ) Enables clipping plane

– glDisable ( GL_CLIP_PLANEi) Disables clipping plane

Page 33: More OpenGL CS 551/645 - Introduction to Computer Graphics

33

Reversing Transformations

Where in 3-space is the user’s mouse point?– We’ve used transformations to transform 3D to 2D– How about 2D to 3D?

gluUnProject (winx, winy, winz, modelMatrix, projMatrix, viewport, worldx, worldy, worldz);– You provide everything but world coordinates

Page 34: More OpenGL CS 551/645 - Introduction to Computer Graphics

34

gluUnproject ( )

Computes the line from center of projection to mouse point

Then computes the x-, y-, z-value of the line at some ‘depth’ in the world

If depth is set to near clipping plane, the returned (x, y, z) is a point on the clipping plane