Binghamton University

EngiNet™

State University of New York

EngiNet™

Thomas J. Watson WARNING All rights reserved. No Part of this video lecture School of Engineering series may be reproduced in any form or by any electronic or mechanical means, including the use and Applied Science of information storage and retrieval systems, without written approval from the copyright owner.

©2001 The Research Foundation of the State University of New York

CS 560

Computer Graphics

Professor Richard Eckert

Lecture # 6

February 7, 2001

1 Lecture 5 1. Lowest Level (earliest)-- Assembly/machine Wednesday, 2-7-01 language

l Programs drive hardware directly Computer Graphics Software –Fast, but non-portable –Difficult to program –Prone to errors

2. Medium Level (General Programming Packages) l B. Standard Graphics Packages –Sets of specifications l A. Extensions to high level languages-- – Supposedly language/platform graphics libraries independent – e.g., Borland's BGI, Windows' GDI, Silicon – Usually with bindings for many high level Graphics GL, Microsoft's DirectX languages – Still have platform dependencies – Syntax for accessing graphics functions – Easier to program – Examples: GKS, PHIGS, OpenGL – Usually slower, but with optimized compilers, not so bad

3. Special-purpose We'll be working at level 2 for Application packages most of this course l e.g., Corel Draw, 3D Studio, Harvard Graphics, Photoshop l Good for what they do, but specific uses

2 Describing positions of 2-D Cartesian System objects – Measure distance along two mutually perpendicular axes l Need coordinate systems – Position given by 2 numbers (x,y) l 2-D and 3-D Cartesian systems used universally

3-D Cartesian System Other commonly-used – Measure distance along three mutually systems perpendicular axes – Position given by three numbers (x,y,z) l Depend on symmetry – Spherical (rho, theta, phi) – Cylindrical (r, theta, z) l Conversion formulas used

Types of coordinate 1. Modeling coordinate systems system (MCS) l System used by programmer (modeler) to describe a single object l Can be 2-D or 3-D l Depends on object being modeled l Origin, scale picked according to object l Varies from object to object

3 2. World Coordinate System 3. Device Coordinate (WCS) System (DCS) l Reference system used to position l Coordinate system used by output device objects in a scene l Units usually pixels l Can be 2-D or 3-D l Always 2-D l Origin, scale, units specific to scene l Varies according to HW platform l Coordinate transformations map from l Graphics SW maps from WCS to DCS an MCS to the WCS – Called the “Viewing Transformation” – Effectively positions objects in scene l If WCS is 3-D, a projection transformation is – Called the “Modeling Transformation” also involved

4. Normalized Device Graphics Transformation Coordinates (NDCS) Pipeline – 2-D system l MCS ------> WCS ------> NDCS ------> DCS – 0 <= x <= 1; 0<= y <= 1 modeling xform viewing xform HW-depend. xform – Intermediate between WCS and DCS l The first two transformations are device independent – Hardware-independent

A Window A Viewport

l A rectangular area (2-D) l A rectangular portion of the screen or Rectangular parallelepiped (3-D) l Expressed in device coordinates l Expressed in WC – e.g., xvmin, yvmin, xvmax, yvmax – e.g., l A window is mapped to a viewport xwmin,xwmax,ywmin,ywmin,zwmin,zwmax l Specifies a part of the scene of interest

4 Window to Viewport Clipping Transformation

– mapping from a window to a viewport l Removal of parts of scene outside a window or viewport l Graphics system may perform clipping with respect to a window or a viewport

Animation Techniques using BASIC COMPONENTS OF A windows/viewports/clipping GRAPHICS SOFTWARE l Zooming SYSTEM – Change window size from frame to frame, clip, and transform l Examples from: Windows & OpenGL • scene appears to approach or recede from viewer l Panning – Translate window from frame to frame, clip, and transform • scene appears to move across the screen

1. Output Primitives l Lines l Building blocks for drawing pictures Windows: l Plotting a pixel--most primitive MoveTo(x,y); // Set Curr. Pos. SetPixel(x,y,colref); // Windows--plots pixel colref = GetPixel(x,y); // returns pixel color LineTo(x,y); // line from CP to (x,y)

glBegin (GL_POINTS); // OpenGL glBegin (GL_LINES); //OpenGL glVertex2f (x, y); // 2==>2D, f==>floating pt glVertex2f(x1,y1); //endpoint vertices glEnd(); // current drawing color used glVertex2f(x2,y2); //appear in pairs glEnd()

5 l l Polylines and Polygons Other primitives

Polyline(lppts,num_pts); // Windows--use pts –Windows // array, number of points Polygon(lppts,num_pts); • Lots of other primitives • See prior notes on Windows glBegin (GL_POLYGON); // OpenGL programming glVertex2f(x1,y1); // polygon vertex … // more vertices –OpenGL glEnd(); • GL_TRIANGLES, GL_LINE_STRIP, GL_QUADS, etc. -- lots more

l Text 3-D primitives

Windows: l Windows has nothing TextOut(x,y,lpszStr,cStrLngth); l OpenGL – GLU graphics library OpenGL-- Use display lists and/or • sphere, cube, cone, etc. GLUT library functions

2. Attributes (State Variables) 3. Transformations l Done with matrix math l Properties of primitives – how they appear l Setting windows/viewports – e.g., color, line style, text style, fill patterns – Window-to-viewport transformation l Usually modal l Moving objects – values retained until changed – Geometric Transformations l Windows -- see prior notes – e.g., translation, rotation, scaling l Changing coordinate system l OpenGL-- glProperty(); – ‘Property’ is state variable to set l Changing viewpoint • e.g., glColor3f (R,G,B); l Different types of projections

6 4. Segmentation l Windows – window-to-viewport transformation l Dividing scene into component parts • done with Mapping Modes for (later) manipulation – programmer must implement others l Windows--strictly immediate mode l OpenGL is very rich – (DirectX has support for retained mode) – glLoadMatrix(), glRotatef(), glTranslatef(), l OpenGL has Display lists glScalef(), glViewport (), glFrustum(), – Groups of OpenGL commands that have glOrtho(), gluPerspective(), etc. been stored for later execution – Can be hierarchical l PHIGS uses hierarchical segments

5. Input/Interaction Input/Interaction in OpenGL l Obtain data from input devices or graphics system l Auxiliary libraries (aux, GLX, GLUT) – – So user can manipulate scene interactively Use underlying windowing system – Provide input-handling callback ftns l Windows: Built into event-driven, • e.g., auxMouseFunc () message-based paradigm • glutMouseFunc() – take pointer to callback function – programmer must write these

7. Storing/retrieving/ 6. Control/Housekeeping manipulating bitmapped Images l BitBlT--Bit Block Transfer l Initialize graphics system, put window up, etc. l Windows-- – Device Dependent Bitmaps l Windows--Extensive support • BitBlt(), StretchBlt(), etc. – RegisterClass(), CreateWindow(), etc. – But very slow l OpenGL – Device Independent Bitmaps--faster – Use of auxiliary libraries – DirectX-- flipping surfaces--fastest! • e.g., auxInitWindow(), auxInitDisplayMode (), auxMainLoop(ptr to ftn. that draws scene) l OpenGL-- – glReadPixels(); glDrawPixels(); glCopyPixels();

7 8. Photorealism Intro. to OpenGL for Windows

l Industry standard for high-quality 3-D l Hidden surfaces, lighting, shading, graphics applications reflection properties, etc. l Available on many HW and OS l Windows--Very little support platforms – DirectX ()--Quite a bit of support l “Thin” software interface to underlying graphics HW l OpenGL--A lot of support! – e.g., light sources, lighting models, l Implementing on Windows brings material properties, blending, workstation-class graphics to PC antialiasing, fog, depth buffer, etc. l Real 3-D graphics to Windows

Steps in using OpenGL in Rendering Context (RC) Windows Applications l OpenGL equivalent of Windows GDI DC l Get a DC for rendering location (window) l Mechanism by which OpenGL calls are l Choose & select a “pixel format” for DC rendered to the device l Create a Rendering Context (RC) for DC l Links OpenGL calls to a Windows l Associate (bind) the RC with the DC window l Draw using OpenGL function calls l Must be compatible with window’s DC l Release the RC & DC l Keeps track of current values of OpenGL state variables

Pixel Format PIXELFORMATDESCRIPTOR l Data structure used to set Pixel Format l Holds attributes for device drawing surface l Some fields: – dwFlags: “OR” of properties constants, e.g. l Describes things like: • doublebuffered, stereo, window or bitmap, etc. – Using single or double buffering – iPixelType – Direct or indirect color • color type (RGBA or indexed) – Drawing to a window or a bitmap – cColorBits: # of bitplanes – Color depth (# of bit planes) – cRedBits: # of red – ZBuffer depth – cRedShift: where red bits are – Others – etc.

8 Choosing and setting the Creating and using an RC Pixel Format

l Set up a PIXELFORMATDESCRIPTOR l Use “wgl” function to create an RC: variable (e.g., pfd) – wglCreateContext(hDC) l ChoosePixelFormat(hDC,&pfd) – Returns a handle to an OpenGL Rendering – gets DC’s pixel format closest to one Context (HGLRC) desired l Make the RC “Current” (bind RC to DC) – returns an integer pf_index – wglMakeCurrent(hDC, hRC) l SetPixelFormat(hDC, pf_index, &pfd) l Now we can draw with OpenGL calls – Set that pixel format in the DC

Cleanup l Make RC noncurrent (Unbind RC from DC) – wglMakeCurrent(hDC, NULL) l Get rid of the DC – ReleaseDC() or EndPaint() in API app. – Done automatically in MFC when OnDraw() returns l Get rid of the RC – wglDeleteContext(hRC)

Building a MINOGL Example Program Windows/OpenGL App l Displays a rectangle in different l Includes in .h file: shades of red – // OpenGL interface l See online listing of view class of – // OpenGL utility library interface minogl example OpenGL program l Must add opengl32.lib & glu32.lib to linker's 'Object/library modules’ – Look on CS-460/560 “Sample Programs” Page – 'Project'|'Settings'|'Link tab' – Link: • MINOGL: A Simple OpenGL Example Program for Windows MFC (minoglView.cpp)

9