ch 2 graphics programming page 1 csc 367 coordinate systems (2.1.2) device coordinates, or screen...

15
Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and the code is not portable generally expressed in integers World coordinates an effort was made to hide the actual device coordinates from the programmer and allow ANY coordinate to be used generally expressed in floats programmer defines world space Mapping the mathematical transformation from one coordinate system to another involves relatively simple algebra Example Device coordinates of 640 x 480 World coordinates of 5000 x 4000 Map (2500, 2000) to ___________ Map (1000, 1000) to ___________

Upload: jordan-robinson

Post on 02-Jan-2016

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 1CSC 367

Coordinate Systems (2.1.2)

• Device coordinates, or screen coordinates (pixels)

put limitations on programmers and the code is not portable

generally expressed in integers

• World coordinates

an effort was made to hide the actual device coordinates from the programmer and allow ANY coordinate to be used

generally expressed in floats

programmer defines world space

• Mapping

the mathematical transformation from one coordinate system to another

involves relatively simple algebra

• Example

Device coordinates of 640 x 480

World coordinates of 5000 x 4000

Map (2500, 2000) to ___________

Map (1000, 1000) to ___________

Page 2: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 2CSC 367

OpenGL

• Originally SGIs GL

modified to be more portable

• Collection of functions that interface to graphics hardware

• Works well on a network

• Protocol (format of instructions) is the same regardless of the computer. This makes the code portable or system independent

• It does NOT do

windows

input

modeling

• It does do sophisticated rendering of 3D scenes

Page 3: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 3CSC 367

uisGL (3D object library)

• The book explains that OpenGL is not OO

• This API helps to hide some ugly details

• #include <uisGL.h>

• Vertex

uisVertex P1, P2, P3(10,10,0);

P1.set(20,20,0);

P2.set(40,40,0);

P3 = P1 + P2;

P3 = P1 / 2;

P3 = P2 * 2.0;

cout << P3;

Page 4: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 4CSC 367

Typical API Functions (2.2.1)

• Primitives

draw points, lines, polygons (maybe curves)

• Attributes

colors, fill patterns, fonts

• Viewing

position, orientation and lens for the virtual camera

• Input

handle events from keyboard and mouse

• Control

initialize programs, create windows

• Transformation

rotate, translate, and scale objects

Page 5: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 5CSC 367

Drawing Primitives (2.1.1)

• We will consider 2D to be a special case (x,y,0)

• Point or Vertex define geometric objects

glVertex2f (x, y);

glVertex3f (x,y,z);

• Array or Vector format

float vertex[3];

glVertex3fv(vertex);

• Drawing individual points

glBegin(GL_POINTS);

glVertex3f (x,y,z);

glVertex3f (x,y,z);

glEnd( ); <--- warning!

• Drawing a line

glBegin(GL_LINES);

glVertex3f(x,y,z);

glVertex3f(x,y,z);

glEnd( );

Page 6: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 6CSC 367

Polygons (2.3.1)

• a series of connected lines

• simple polygon edges do not cross

• the interior can be filled with a color

• convex polygons can be tested by connecting all vertices with each other. If all of the lines remain inside the polygon then it is covex.

• concave polygons are not convex

• Drawing a polygon

float P1[3] = {10, 10, 0};

glBegin(GL_POLYGON);

glVertex3fv (P1);

glVertex3fv (P2);

glVertex3fv (P3);

glVertex3fv (P4);

glEnd();

• Interior can be filled with a color

• Edges can be turned on or off

Page 7: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 7CSC 367

Attributes (2.3.5)

• the name for any property that determines how an object appears

• a solid red line is different that a dashed green line

• color, line thickness, fill pattern

• The general approach is to set current attributes and then display the object.

• Each geometric type has a set of attributes. A line has color, width and style.

• glPointSize (2.0) // 2 pixels wide

Page 8: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 8CSC 367

Color (2.4)

• Ignore color theory and mathematics

• Three colored spot lights analogy

• Different than mixing paints

• RGB color or true color

C = T1R + T2G + T3B

T is the intensity that ranges from 0 - 1

• glColor3f (0, 0, 0); // balck

• glColor3f (1, 0, 0); // red

• glClearColor (1, 1, 1, 0); // white background

• glClear (); // clear the screen

• The number of colors that can be displayed are determined by the number of bits available for each pixel

true color requires 24 bits - 8 bits for each

Page 9: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 9CSC 367

Indexed Color

• not often needed any longer

• when the number of bits per pixel is limited

• rather than limit the possible range of colors we limit the possible number of colors

• painter example that can mix an unlimited number of paints but can only fit so many on the cardboard plate

• Color-lookup Table

the 8-bit representation is used as an index into the color lookup table

contains a limited number of entries but each entry can represent any color

set current color by specifying an index

common configuration is 256 indices

Page 10: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 10CSC 367

Viewing 2.5

• Viewing rectangle or window

items within the window will be seen and others will be clipped

• 3D Viewing Volume

glOrtho(left,right,bottom,top,near,far)

• Be aware of right handed coordinate system

positive Z comes out from screen

• Viewport

a rectangular region within the window

glViewport(x,y,width,height)

• Aspect Ratio

the ratio of the viewing rectangle should be the same as the viewport

Page 11: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 11CSC 367

Matrix Mode (2.5.3)

• Primitives are multiplied by matrices before being displayed.

• OpenGL has two important matrices

MODELVIEW - for viewing parameters

PROJECTION - for display parameters

• obj’ = obj x MV

• obj’’ = obj’ x P

• Typical initialization

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(0,10,0,10,0,10);

glMatrixMode(GL_MODELVIEW);

• Leave in MODELVIEW mode

Page 12: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 12CSC 367

GLUT (GL Untility Toolkit) 2.6

• Windowing system independent API for OpenGL applications

• Allows one to

window management

simple menus

event processing loop

input

• We will use an X11 implementation of GLUT. There could be a Windows version or Macintosh version.

• X11 is a protocal that allows device independent display of graphics over a network. Using real X11 commands would be a pain. Somewhat equivalent to programming in assembler.

• Client Server model. The client is the application and the computer that does the display is the server.

Page 13: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 13CSC 367

Event Processing 2.6

• What stops the image from disappearing after it has been drawn?

• Event Processing Loop

glutMainLoop()

this causes an infinite loop until closing the window by hand

• Callback Mechanism

a programmer defined function that glutMainLoop will call when GLUT determines an event has occurred.

this is called “registering a call back”

• Display function

glutDisplayFunc(*func(void))

• Additional Events

mouse button presses

keyboard

window movement

Page 14: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 14CSC 367

Sample Code

• look a source

• discuss Makefile

Page 15: Ch 2 Graphics Programming page 1 CSC 367 Coordinate Systems (2.1.2) Device coordinates, or screen coordinates (pixels) put limitations on programmers and

Ch 2 Graphics Programming page 15CSC 367

Sierpinski Gasket

• interesting example from the book

• Choose a random point within the triangle

• Loop

Find midpoint between current point and a random vertex

Make a mark

Update current point

• Extended to 3D requires a tetrahedron