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

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

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

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

OpenGL Transformations

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 李同益

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

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

CHAPTER 1 Graphics Systems and Models 3

CS 465 Program 4: Modeller

CS354 Computer Graphics Viewing and Modeling

Using GLU/GLUT Objects

Overview. By end of the week:

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

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

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

Computer Graphics Geometric Transformations

CS 4204 Computer Graphics

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

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

GL_MODELVIEW transformation

Computer Graphics. Chapter 10 Three-Dimensional Viewing

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

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

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

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

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

Lecture 4. Viewing, Projection and Viewport Transformations

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

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

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

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

C OMPUTER G RAPHICS Thursday

Precept 2 Aleksey Boyko February 18, 2011

3D Viewing Episode 2

Graphics and Visualization

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

Prof. Feng Liu. Fall /19/2016

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

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);

Computer Graphics Course 2005

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

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

OpenGL/GLUT Intro. Week 1, Fri Jan 12

Lecture 4 of 41. Lab 1a: OpenGL Basics

Viewing COMPSCI 464. Image Credits: Encarta and

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

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

OpenGL Tutorial. Ceng 477 Introduction to Computer Graphics

Graphics pipeline and transformations. Composition of transformations

Chapter 3: Modeling Transformation

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

GRAFIKA KOMPUTER. ~ M. Ali Fauzi

Transformation Pipeline

Viewing with Computers (OpenGL)

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

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

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

Rendering Pipeline and Coordinates Transforms

Transformations IV. Week 3, Wed Jan 20

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

Computer Graphics I Lecture 11

Order of Transformations

Computer Graphics. Chapter 7 2D Geometric Transformations

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

COMP30019 Graphics and Interaction Transformation geometry and homogeneous coordinates

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

UNIT 2 2D TRANSFORMATIONS

CSCI 4620/8626. Coordinate Reference Frames

Fundamental Types of Viewing

Computer graphics MN1

Interactive Computer Graphics A TOP-DOWN APPROACH WITH SHADER-BASED OPENGL

Graphics and Interaction Transformation geometry and homogeneous coordinates

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

COMP3421. Introduction to 3D Graphics

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

CITSTUDENTS.IN VIEWING. Computer Graphics and Visualization. Classical and computer viewing. Viewing with a computer. Positioning of the camera

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

Transformations (Rotations with Quaternions) October 24, 2005

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

CS770/870 Spring 2017 Curve Generation

Introduction to OpenGL

Computer Graphics. Chapter 5 Geometric Transformations. Somsak Walairacht, Computer Engineering, KMITL

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

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

COMP3421. Introduction to 3D Graphics

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

2D and 3D Transformations AUI Course Denbigh Starkey

Modeling with Transformations

Computer Viewing Computer Graphics I, Fall 2008

Introduction to OpenGL

3D Mathematics. Co-ordinate systems, 3D primitives and affine transformations

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

3D Graphics for Game Programming (J. Han) Chapter II Vertex Processing

Project Sketchpad. Ivan Sutherland (MIT 1963) established the basic interactive paradigm that characterizes interactive computer graphics:

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

Curves D.A. Forsyth, with slides from John Hart

CIS 636 Interactive Computer Graphics CIS 736 Computer Graphics Spring 2011

Transcription:

Hidden Surface Removal Back Face Culling 3D Surfaces Bicubic Parametric Bezier Surface Patches 3D Graphics with OpenGL 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

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 (determinant) (Components in terms of RH coord system)

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

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 dot product of either vector with V0 is 0 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

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

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))

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

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

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(); See 3dxform example program 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

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

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: glutcreatemenu (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

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