Binghamton University. EngiNet. Thomas J. Watson. School of Engineering and Applied Science

Similar documents
Hidden Surface Removal. 3D Graphics with OpenGL. Back-Face Culling

Introduction to 3D Graphics with OpenGL. Z-Buffer Hidden Surface Removal. Binghamton University. EngiNet. Thomas J. Watson

Spring 2013, CS 112 Programming Assignment 2 Submission Due: April 26, 2013

CSC 470 Computer Graphics

CSE 690: GPGPU. Lecture 2: Understanding the Fabric - Intro to Graphics. Klaus Mueller Stony Brook University Computer Science Department

Hierarchical Modeling: Tree of Transformations, Display Lists and Functions, Matrix and Attribute Stacks,

OpenGL Transformations

Surface Graphics. 200 polys 1,000 polys 15,000 polys. an empty foot. - a mesh of spline patches:

Lecture 5: Viewing. CSE Computer Graphics (Fall 2010)

CS 465 Program 4: Modeller

OpenGL: Open Graphics Library. Introduction to OpenGL Part II. How do I render a geometric primitive? What is OpenGL

Modeling Transform. Chapter 4 Geometric Transformations. Overview. Instancing. Specify transformation for objects 李同益

Overview. By end of the week:

Notes on Assignment. Notes on Assignment. Notes on Assignment. Notes on Assignment

Graphics pipeline and transformations. Composition of transformations

CHAPTER 1 Graphics Systems and Models 3

Hierarchical Modeling. University of Texas at Austin CS384G - Computer Graphics Fall 2010 Don Fussell

Describe the Orthographic and Perspective projections. How do we combine together transform matrices?

Computer Science 426 Midterm 3/11/04, 1:30PM-2:50PM

CS354 Computer Graphics Viewing and Modeling

Lecture 4. Viewing, Projection and Viewport Transformations

Computer Graphics. Chapter 10 Three-Dimensional Viewing

Using GLU/GLUT Objects

CS 4204 Computer Graphics

CS475/675: Computer Graphics End-Semester Examination (Solutions), Fall 2016

Lecture 4 of 41. Lab 1a: OpenGL Basics

3D computer graphics: geometric modeling of objects in the computer and rendering them

OpenGL/GLUT Intro. Week 1, Fri Jan 12

C OMPUTER G RAPHICS Thursday

Precept 2 Aleksey Boyko February 18, 2011

The Viewing Pipeline adaptation of Paul Bunn & Kerryn Hugo s notes

CS380: Computer Graphics 2D Imaging and Transformation. Sung-Eui Yoon ( 윤성의 ) Course URL:

To Do. Computer Graphics (Fall 2008) Course Outline. Course Outline. Methodology for Lecture. Demo: Surreal (HW 3)

// double buffering and RGB glutinitdisplaymode(glut_double GLUT_RGBA); // your own initializations

Transformations. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

Midterm Exam Fundamentals of Computer Graphics (COMP 557) Thurs. Feb. 19, 2015 Professor Michael Langer

Transformation Pipeline

Reading. Hierarchical Modeling. Symbols and instances. Required: Angel, sections , 9.8. Optional:

COMP30019 Graphics and Interaction Transformation geometry and homogeneous coordinates

GL_MODELVIEW transformation

Lecture 6 Sections 4.3, 4.6, 4.7. Wed, Sep 9, 2009

OpenGL Tutorial. Ceng 477 Introduction to Computer Graphics

Objectives. transformation. General Transformations. Affine Transformations. Notation. Pipeline Implementation. Introduce standard transformations

COMP 175 COMPUTER GRAPHICS. Lecture 07: Scene Graph. COMP 175: Computer Graphics March 10, Remco Chang 07 Scene Graph

Viewing with Computers (OpenGL)

Computer Graphics Course 2005

API for creating a display window and using keyboard/mouse interations. See RayWindow.cpp to see how these are used for Assignment3

Computer Graphics Geometric Transformations

Graphics and Interaction Transformation geometry and homogeneous coordinates

3D Viewing Episode 2

CS4202: Test. 1. Write the letter corresponding to the library name next to the statement or statements that describe library.

3D Transformation. In 3D, we have x, y, and z. We will continue use column vectors:. Homogenous systems:. x y z. x y z. glvertex3f(x, y,z);

Lecture 3 Sections 2.2, 4.4. Mon, Aug 31, 2009

Graphics and Visualization

CSC Graphics Programming. Budditha Hettige Department of Statistics and Computer Science

UNIT 2 2D TRANSFORMATIONS

CSE528 Computer Graphics: Theory, Algorithms, and Applications

2D and 3D Transformations AUI Course Denbigh Starkey

Computer Graphics. Chapter 7 2D Geometric Transformations

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

CSCI 4620/8626. The 2D Viewing Pipeline

Prof. Feng Liu. Fall /19/2016

Two possible ways to specify transformations. Each part of the object is transformed independently relative to the origin

Illumination and Reflection in OpenGL CS 460/560. Computer Graphics. Shadows. Photo-Realism: Ray Tracing. Binghamton University.

Computer Graphics (CS 4731) Lecture 11: Implementing Transformations. Prof Emmanuel Agu. Computer Science Dept. Worcester Polytechnic Institute (WPI)

6.837 Introduction to Computer Graphics Assignment 5: OpenGL and Solid Textures Due Wednesday October 22, 2003 at 11:59pm

Order of Transformations

Scene Graphs. CS4620/5620: Lecture 7. Announcements. HW 1 out. PA 1 will be out on Wed

Rendering Pipeline and Coordinates Transforms

Computer Graphics CS 543 Lecture 5 (Part 2) Implementing Transformations

Getting Started. Overview (1): Getting Started (1): Getting Started (2): Getting Started (3): COSC 4431/5331 Computer Graphics.

Viewing COMPSCI 464. Image Credits: Encarta and

GRAFIKA KOMPUTER. ~ M. Ali Fauzi

Translation. 3D Transformations. Rotation about z axis. Scaling. CS 4620 Lecture 8. 3 Cornell CS4620 Fall 2009!Lecture 8

Lecture 4 Advanced Computer Graphics (CS & SE )

General Hidden Surface Removal Algorithms. Binghamton University. EngiNet. Thomas J. Watson. School of Engineering and Applied Science CS 460/560

Name: SID: LAB Section: Lab 6 - Part 1: Pixels and Triangle Rasterization

CSE328 Fundamentals of Computer Graphics

Modeling Objects. Modeling. Symbol-Instance Table. Instance Transformation. Each appearance of the object in the model is an instance

Fachhochschule Regensburg, Germany, February 15, 2017

蔡侑庭 (Yu-Ting Tsai) National Chiao Tung University, Taiwan. Prof. Wen-Chieh Lin s CG Slides OpenGL 2.1 Specification

Modeling with Transformations

CS 591B Lecture 9: The OpenGL Rendering Pipeline

Computer Graphics I Lecture 11

Geometry Primitives. Computer Science Department University of Malta. Sandro Spina Computer Graphics and Simulation Group. CGSG Geometry Primitives

Computer graphics MN1

Introduction to OpenGL

Modeling Objects by Polygonal Approximations. Linear and Affine Transformations (Maps)

CSCI 4620/8626. Coordinate Reference Frames

Building Models. CS 537 Interactive Computer Graphics Prof. David E. Breen Department of Computer Science

SFU CMPT 361 Computer Graphics Fall 2017 Assignment 2. Assignment due Thursday, October 19, 11:59pm

Computer Graphics. OpenGL

CS 428: Fall Introduction to. Transformations in OpenGL + hierarchical modeling. Andrew Nealen, Rutgers, /21/2009 1

Graphics Programming

Fundamental Types of Viewing

Computer Graphics 7: Viewing in 3-D

GL_COLOR_BUFFER_BIT, GL_PROJECTION, GL_MODELVIEW

Projection: Mapping 3-D to 2-D. Orthographic Projection. The Canonical Camera Configuration. Perspective Projection

Introduction to OpenGL

CS 4204 Computer Graphics

Transcription:

Binghamton University EngiNet State University of New York EngiNet Thomas J. Watson School of Engineering and Applied Science WARNING All rights reserved. No Part of this video lecture series may be reproduced in any form or by any electronic or mechanical means, including the use of information storage and retrieval systems, without written approval from the copyright owner. 2008 The Research Foundation of the State University of New York CS 460/560 Computer Graphics Professor Richard Eckert Review of 3-D Viewing and Projection Transformations Hidden Surface Removal Back Face Culling 3-D Surfaces: Bezier Patches April 15, 2008 1

3D Viewing/Projection Transformations 3-D points in model must be transformed to viewing coordinate system the Viewing Transformation Then projected onto a projection plane Projection Transformation 3-D Viewing Transformation Converts world coordinates (xw,yw,zy) of a point to viewing coordinates (xv,yv,zv) of the point As seen by a "camera" that is going to "photograph" the scene (xw,yw,zw) ------------------------> (xv,yv,zv) Viewing transformation 4-Parameter Viewing Setup Composite Viewing Transformation Matrix -sin(θ) cos(θ) 0 0 Tv = -cos(φ)*cos(θ) -cos(φ)*sin(θ) sin(φ) 0 -sin(φ)*cos(θ) -sin(φ)*sin(θ) -cos(φ) ρ _ 0 0 0 1_ Projection Transformation Look down xv axis at viewing setup: Triangles OAP' & OBP are similar So set up proportion: yp d ---- = ---- yv zv Solve for yp: yp = (yv*d)/zv Look down yv axis for xp: Result: xp = (xv*d)/zv Plotting Points on Screen Get screen coordinates (xs,ys) from Projection Plane coordinates (xp,yp) Final Transformation: 2D Window-to Viewport Transformation (xs,ys) <--- (xp,yp) See earlier notes Replace xv,yv with xs,ys And xw,yw with xp,yp 2

Skeleton Pyramid Program: Data Structures // Build and display a polygon mesh model of a 4-sided pyramid: struct point3d {float x; float y; float z;}; // a 3d point struct polygon {int n; int *inds;}; // a polygon struct point3d w_pts[5]; // 5 world coordinate vertices struct point3d v_pts[5]; // 5 viewing coordinate vertices POINT s_pts[5]; // 5 screen coordinate vertices struct polygon polys[5]; // 5 polygons define the pyramid // global variables: float v11,v12,v21,v22,v23,v31,v32,v33,v34; // view xform matrix elements int screen_dist; float rho, theta, phi; // viewing parameters int xmax,ymax; // Screen dimensions int num_vertices=5, num_polygons=5; Skeleton Pyramid Program: Function Prototypes void coeff (float r, float t, float p); // calculates viewing transformation // matrix elements, vii void convert (float x, float y, float z, float *xv, float *yv, float *zv, int *xs, int *ys); // converts a 3D world coordinate point to // 3D viewing & 2D screen coordinates // i.e., viewing and projection transformations void build_pyramid (void); // sets up pyramid points and polygons // arrays (see last set of notes) void draw_polygon (int p); // draws polygon p Skeleton Pyramid Program: Function Skeletons // Main Function--Called whenever pyramid is to be displayed void main_ftn ( ) { // Get or set values of rho, theta, phi, and screen_dist here build_pyramid (void); // build polygon model of the pyramid coeff (rho,theta,phi); // compute transformation matrix elements for (int i=0; i<num_vertices; i++) { // Loop to convert polygon vertices from world coordinates // to viewing and screen coordinates; must call convert () each time} for (int f=0; f<num_polygons; f++) { // Loop to draw each polygon face // must call draw_polygon (f) } } void coeff (float r, float t, float p) { // Code to compute non-trivial viewing transformation matrix // elements: v11,v12,v21,v22,v23,v31,v32,v33,v43 } void convert (float x, float y, float z, float *xv, float *yv, float *zv, int *xs, int *ys) { // Code to compute viewing coordinates and screen coordinates of // a point from its 3-D world coordinates. Must implement viewing, // projection, and window-to-viewport transformations described // in class } void build_pyramid (void) { // Code to define the pyramid by setting up w_pts & polys arrays } void draw_polygon (int p) { // Code to draw polygon p by: // obtaining its vertex numbers from the polys array // getting the screen coordinates of each vertex from the s_pts array // making appropriate calls to the system polygon-drawing primitive } Back-Face Culling Define one side of each polygon to be the visible side That side is the outward-facing side Defining each polygon in the polygons array: Systematically number vertices in counterclockwise fashion as seen from outside of the object 3

First: Review of Vector Products Dot (Scalar) Product s = A. B s = A * B * cos(θ) θ is the angle between vectors A and B In terms of components (RH coord system): s = Ax*Bx + Ay*By + Az*Bz Cross (Vector) Product V = A x B, a vector Magnitude: V = A * B * sin(θ) θ is angle between vectors A and B Direction: Given by right-hand rule 1. Align fingers of right hand with first vector 2. Rotate toward second 3. Thumb points in direction of V In the following diagram: V = A x B would point out of the screen toward the observer In terms of components (RH coordinate system): i j k V = Ax Ay Az (a determinant) Bx By Bz i, j, k are unit vectors along x,y,z axes Triple Product A. (B x C) Ax Ay Az A. (B x C) = Bx By Bz Cx Cy Cz (Components in terms of RH coord system) (determinant) Back-Face Culling Consider triangle with vertices 0, 1, 2 Visible side of the triangle: 0,1,2 Vertices numbered in counter-clockwise order Invisible side is: 0,2,1 (clockwise vertex ordering) Define vector N Outward normal to triangle Define Vector V0 Vector from observer to vertex 0 Some Cases: N and V0 nearly parallel (V0. N = 1) Visible side of triangle 0 1 2 invisible to viewer 4

Rotate triangle about side 01 by 90 degrees Now N and V0 are perpendicular (V0. N = 0) Triangle is about to become visible At all other points between these two orientations: V0. N is positive And triangle is invisible to viewer Continue rotation about side 01 Triangle becomes visible to the viewer 90 degrees more, N and V0 are antiparallel V0. N = -1 Triangle facing toward viewer and is visible At all intermediate orientations: Triangle is visible And V0. N is negative Criterion for Invisibility If V0. N > 0, triangle 012 is invisible Now place triangle 012 in an arbitrary position relative to viewer V Outward normal N is vector (cross) product of V01 and V02 V01 is vector from vertex 0 to vertex 1 V02 is vector from vertex 0 to vertex 2 So: N = V01 x V02 Criterion for invisibility: V0. (V01 x V02) > 0 But: V01 = V1 - V0 V02 = V2 - V0 Substituting we get: V0. [(V1 - V0) x (V2 - V0)] > 0, invisibility Expanding: V0. (V1 x V2) - V0. (V1 x V0) - V0. (V0 x V2) + V0. (V0 x V0) > 0 Last Term = 0 (Cross product of any vector with itself = 0) Middle two terms: Quantity inside ( ) is a vector perpendicular to V0 So: V0. (V1 x V2) > 0 For right-handed coordinate system, triple product can be expressed as a determinant X0 Y0 Z0 V0. (V1 x V2) = X1 Y1 Z1 X2 Y2 Z2 (X0,Y0,Z0), (X1,Y1,Z1), (X2,Y2,Z2) are viewing coordinates (xv,yv,zv) of vertices 0, 1, and 2 But viewing coordinate system is left-handed So sign of the determinant must be reversed So dot product of either vector with V0 is 0 5

Final Criterion for Invisibility X0 Y0 Z0 X1 Y1 Z1 < 0 X2 Y2 Z2 Result can be applied to any planar polygon Use viewing coordinates of three consecutive polygon vertices Could implement as a visibility function Computes and returns value of determinant Positive means visible, negative invisible 3-D Surfaces Explicit Representation z = f(x,y) Plotting Fix values of y and vary x Gives a family of curves z0 = f(x,0) z1 = f(x,δ) z2 = f(x,2*δ) z3 = f(x,3*δ) etc. Plotting 3D Surfaces, continued Then fix values of x and vary y Gives another family of curves z0 = f(0,y) z1 = f(δ,y) z2 = f(2*δ,y) z3 = f(3*δ,y) etc. Plotting 3D Surfaces, continued Result is a wireframe that represents the surface Could be broken up into polygons Parametric Representation of 3D Surfaces Need two parameters, say t and s x = x(t,s), y = y(t,s), z = z(t,s) both t and s vary over a range (0 to 1) To plot: Fix values of s and for each vary t over range gives one family of isoparametric curves Fix values of t and for each vary s over range gives another family of isoparametric curves Cubic Bezier Curves (Review) In matrix form, points on curve P [P = x,y] are given in terms of paramter t and four control points P0, P1, P2, P3 Result: P = a*t 3 + b*t 2 + c*t + d, 0 <= t <= 1 Can be written in a more compact form: P = T * Bg * Pc T: row vector of parameter powers [ t 3 t 2 t 1 ] Bg: the constant 4 X 4 Bezier Geometry matrix Pc: column vector of the control points 6

Bicubic Bezier Surface Patches Define 4-vectors S and T: S = _s 3 s 2 s 1_, 0 <= s <= 1 T = _t 3 t 2 t 1_, 0 <= t <= 1 Define points on surface patch Q(s,t) [Q = x,y,z] as: Q(s,t) = S * M B * P0(t) P1(t) P2(t) _P3(t)_ Control points P0, P1, P2, P3 are themselves parameterized by t M B is the Bezier Geometry Matrix we ve seen before So P0(t) = T * M B * P00 P01 P02 _ P03_ Transposing: P0(t) = _P00 P01 P02 P03_ * M BT * T T Do the same for P1(t), P2(t), P3(t) Result: P00 P01 P02 P03 Q(s,t) = S * M B * P10 P11 P12 P13 * M BT * T T P20 P21 P22 P23 _P30 P31 P32 P33_ A Bicubic Bezier Surface Patch Expanding and Rearranging Terms -- x(s,t) Equation Similar equation for y(s,t) Plotting One Set of Isoparametric Curves For (s=0; s<=1; s+=δ) Compute & store x(s,0), y(s,0), z(s,0) Project to screen and store --> xs(s,0), ys(s,0) MoveTo(xs(s,0), ys(s,0)) For (t=0; t<=1; t+=δ) Compute & store x(s,t), y(s,t), z(s,t) Project to screen and store --> xs(s,t), ys(s,t) LineTo(xs(s,t), ys(s,t)) Plotting the Other Set of Isoparametric Curves For (t=0; t<=1; t+=δ) MoveTo(xs(0,t), ys(0,t)) For (s=0; s<=1; s+=δ) LineTo(xs(s,t), ys(s,t)) 7

Introduction to 3D Graphics with OpenGL 3D Graphics Using OpenGL Building Polygon Models ModelView & Projection Transformations Quadric Surfaces User Interaction Hierarchical Modeling Animation OpenGL 3D Coordinate System A Right-handed coordinate system Viewpoint is centered at origin initially Defining 3D Polygons in OpenGL e.g., front face of a cube glbegin(gl_polygon) glvertex3f(-0.5f, 0.5f, 0.5f); glvertex3f(-0.5f, -0.5f, 0.5f); glvertex3f(0.5f, -0.5f, 0.5f); glvertex3f(0.5f, 0.5f, 0.5f); glend(); need to define the other faces Projection Transformation First tell OpenGL you re using the projection matrix glmatrixmode(gl_projection); Then Initialize it to the Identity matrix glloadidentity(); Then define the viewing volume, for example: glfrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 7.0); (left, right, bottom, top, near, far) near & far are positive distances, near < far Viewing volume is the frustum of a pyramid Used for perspective projection or glortho(-1.0, 1.0,-1.0, 1.0, 2.0, 7.0); Viewing volume is a rectangular solid for parallel projection For both the viewpoint (eye) is at (0,0,0) The Viewing Volume Everything outside viewing volume is clipped Think of near plane as being window s client area 8

Modelview Transformation Our cube is not visible It lies in front of near clipping plane Positioning the Camera By default it s at (0,0,0), pointing in z direction, up direction is y-axis Can set the camera point And the lookat point And the up direction glulookat(xc,yc,zc,xa,ya,za,xu,yu,zu); (xc,yc,zc) coordinates of virtual camera (xa,ya,za) coordinates of lookat point (xu,yu,zu) up direction vector Example: glulookat(2.0,2.0,2.0,0.0,0.0,0.0,0.0,0.0,1.0); camera at (2,2,2), looking at origin, z-axis is up Modelview Transformation Used to perform geometric translations, rotations, scalings Also implements the viewing transformation If we don t position the camera, we need to move our cube into the viewing volume glmatrixmode(gl_modelview); glloadidentity(); gltranslate(0.0f, 0.0f, -3.5f); Translates cube down z-axis by 3.5 units OpenGL performs transformations on all vertices First modelview transformation Then projection transformation The two matrices are concatenated Resulting matrix multiplies all points in the model OpenGL Geometric Transformations Modeling Transformations glscalef(2.0f, 2.0f, 2.0f); // twice as big parameters: sx, sy, sz gltranslatef(2.0f, 3.5f, 1.8f); // move object parameters: tx, ty, tz glrotatef(30.0f, 0.0f, 0.0f, 1.0f); // 30 degrees about z-axis parameters: angle (x,y,z) -> coordinates of vector about which to rotate OpenGL Composite Transformations Combine transformation matrices Example: Rotate by 45 degrees about a line parallel to the z axis that goes through the point (xf,yf,zf) the fixed point glmatrixmode(gl_modelview); glloadidentity(); gltranslate(xf,yf,zf); glrotate(45, 0.0,0.0,1.0); gltranslate(-xf,-yf,-zf); Note last transformation specified is first applied Because each transformations in OpenGL is applied to present matrix by postmultiplication 9

Typical code for a polygon mesh model glmatrixmode(gl_projection); glloadidentity(); glfrustum(-1.0, 1.0, -1.0, 1.0, 2.0, 7.0); glmatrixmode(gl_modelview); glloadidentity(); gltranslatef(0.0f, 0.0f, -3.5f); // translate into viewing frustum glrotatef(30.0f, 0.0f, 0.0f, 1.0f); // rotate about z axis by 30 glclearcolor(1.0f, 1.0f, 1.0f, 1.0f); // set background color glclear(gl_color_buffer_bit); // clear window glcolor3f(0.0f, 0.0f, 0.0f); // drawing color glpolygonmode(gl_front_and_back, GL_LINE); glbegin(gl_polygon); //define polygon vertices here glend(); The OpenGL Utility Library (GLU) and Quadric Surfaces Provides many modeling features Quadric surfaces described by quadratic equations in x,y,z spheres, cylinders, disks Polygon Tesselation Approximating curved surfaces with polygon facets Non-Uniform Rational B-Spline Curves & Surfaces (NURBS) Routines to facilitate setting up matrices for specific viewing orientations & projections See 3dxform example program Modeling & Rendering a Quadric with the GLU 1. Get a pointer to a quadric object 2. Make a new quadric object 3. Set the rendering style 4. Draw the object 5. When finished, delete the object OpenGL GLU Code to Render a Sphere GLUquadricObj *mysphere; mysphere=glunewquadric(); gluquadricdrawstyle(mysphere,glu_fill); // some other styles: GLU_POINT, GLU_LINE glusphere(mysphere,1.0,12,12); // radius, # longitude lines, # latitude lines The GLUT and Quadric Surfaces Many predefined quadric surface objects glutwire***() glutsolid***() Some examples: glutwirecube(size); glutsolidcube(size); glutwiresphere(radius,nlongitudes,nlatitudes); glutwirecone(rbase,height,nlongitudes,nlatitudes); glutwireteapot(size); Lots of others See cone_perspective example program Interaction in OpenGL OpenGL GLUT Callback Functions GLUT s version of event/message handling Programmer specifies function to be called by OS in response to different events Specify the function by using glut***func(ftn) We ve already seen glutdisplayfunc(disp_ftn) disp_ftn called when client area needs to be repainted Like Windows response to WM_PAINT messages All GLUT callback functions work like MFC On***() event handler functions 10

Some Other GLUT Callbacks glutreshapefunc(ftn(width,height)) Identifies function ftn() invoked when user changes size of window height & width of new window returned to ftn() glutkeyboardfunc(ftn(key,x,y)) Identifies function ftn() invoked when user presses a keyboard key Character code (key) and position of mouse cursor (x,y) returned to ftn() glutspecialfunction(ftn(key,x,y)) For special keys such as function & arrow keys Mouse Callbacks glutmousefunc(ftn(button, state, x, y)) Identifies function ftn() called when mouse events occur Button presses or releases Position (x,y) of mouse cursor returned Also the state (GLUT_UP or GLUT_DOWN) Also which button GLUT_LEFT_BUTTON, GLUT_RIGHT_BUTTON, or GLUT_MIDDLE_BUTTON Mouse Motion Move event: when mouse moves with a button pressed glutmotionfunctionfunc(ftn(x,y)) ftn(x,y) called when there s a move event Position (x,y) of mouse cursor returned Passive motion event: when mouse moves with no button pressed glutpassivemotionfunctionfunc(ftn(x,y)) ftn(x,y) called when there s a passive motion event Position (x,y) of mouse cursor returned GLUT Menus Can create popup menus and add menu items with: glutcreatewindow (menu-ftn(id)) Menu-ftn(ID) is callback function called when user selects an item from the menu ID identifies which item was chosen glutaddmenuentry(name, ID_value) Adds an entry with name displayed to current menu ID_value returned to menu_ftn() callback glutattachmenu(button) Attaches current menu to specified mouse button When that button is pressed, menu pops up Hierarchical Models In many applications the parts of a model depend on each other Often the parts are arranged in a hierarchy Represent as a tree data structure Transformations applied to parts in parent nodes are also applied to parts in child nodes Simple example: a robot arm Base, lower arm, and upper arm Base rotates lower and upper arm also rotate Lower arm rotates upper arm also rotates Simple Robot Arm Hierarchical Model 11

Use of Matrix Stacks in OpenGL to Implement Hierarchies Matrix stacks store projection & model-view matrices Push and pop matrices with: glpushmatrix(); glpopmatrix(); Can use to position entire object while also preserving it for drawing other objects Use in conjunction with geometrical transformations Example: Robot program OpenGL Hierarchical Models Set up a hierarchical representation of scene (a tree) Each object is specified in its own modeling coordinate system Traverse tree and apply transformations to bring objects into world coordinate system Traversal rule: Every time we go to the left at a node with another unvisited right child, do a push Every time we return to that node, do a pop Do a pop at the end so number of pushes & pops are the same GLUT Animation Simple method is to use an idle callback Called whenever window s event queue is empty Could be used to update display with the next frame of the animation Identify the idle function with: glutidlefunc(idle_ftn()) Simple Example: void idle_ftn() { glutpostredisplay(); } Posts message to event queue that client area needs to be repainted Causes display callback function to be invoked Effectively displays next frame of animation Double Buffering Use two display buffers Front buffer is displayed by display hardware Application draws into back buffer Swap buffers after new frame is drawn into back buffer Implies only one access to display hardware per frame Eliminates flicker In OpenGL, implement by replacing glflush() with glutswapbuffers() in display callback In initialization function, must use: glutinitdisplaymode(glut_double GLUT_RGB); See anim_square & cone_anim examples 12