3D Programming and Viewing Week 3 David Breen Department of Computer Science Drexel University Based on material from Ed Angel, University of New Mexico.

Slides:



Advertisements
Similar presentations
Computer Graphics - Viewing -
Advertisements

Better Interactive Programs
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Building Models modified by Ray Wisman Ed Angel Professor of Computer Science,
1 Building Models. 2 Objectives Introduce simple data structures for building polygonal models ­Vertex lists ­Edge lists OpenGL vertex arrays.
1 View Shandong University Software College Instructor: Zhou Yuanfeng
 The success of GL lead to OpenGL (1992), a platform-independent API that was  Easy to use  Close enough to the hardware to get excellent performance.
Based on slides created by Edward Angel
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Computer Viewing Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
OpenGL and Projections
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Better Interactive Programs Ed Angel Professor of Computer Science, Electrical and Computer.
Classical Viewing CS4395: Computer Graphics 1 Mohan Sridharan Based on slides created by Edward Angel.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Models and Architectures Ed Angel Professor of Computer Science, Electrical and Computer.
1 Chapter 5 Viewing. 2 Perspective Projection 3 Parallel Projection.
CS 470 Introduction to Computer Graphics Basic 3D in OpenGL.
3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!
Computer Graphics (fall 2009)
CSC461 Lecture 11: Interactive Programs Contents and Objectives Picking Writing modes – XOR/Copy Rubberbanding Display list.
Transformation & Projection Feng Yu Proseminar Computer Graphics :
Computer Graphics I, Fall 2010 Classical Viewing.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
Viewing Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
1 E. Angel and D. Shreiner : Interactive Computer Graphics 6E © Addison-Wesley 2012 Classical Viewing Sai-Keung Wong ( 黃世強 ) Computer Science National.
Chapter 5 Viewing.
1 Classical Viewing. 2 Objectives Introduce the classical views Compare and contrast image formation by computer with how images have been formed by architects,
C O M P U T E R G R A P H I C S Guoying Zhao 1 / 67 C O M P U T E R G R A P H I C S Guoying Zhao 1 / 67 Computer Graphics Three-Dimensional Graphics III.
Computer Graphics Bing-Yu Chen National Taiwan University.
Computing & Information Sciences Kansas State University Lecture 20 of 42CIS 636/736: (Introduction to) Computer Graphics Lecture 21 of 42 William H. Hsu.
1 Introduction to Computer Graphics with WebGL Ed Angel Professor Emeritus of Computer Science Founding Director, Arts, Research, Technology and Science.
CAP 4703 Computer Graphic Methods Prof. Roy Levow Chapter 5.
Demetriou/Loizidou – ACSC330 – Chapter 5 Viewing Dr. Giorgos A. Demetriou Computer Science Frederick Institute of Technology.
1 Better Interactive Programs. 2 Objectives Learn to build more sophisticated interactive programs using ­Picking Select objects from the display Three.
Computer Graphics I, Fall 2010 Computer Viewing.
Representation. Objectives Introduce concepts such as dimension and basis Introduce coordinate systems for representing vectors spaces and frames for.
Viewing and Projection. 2 3 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009  Introduce the classical views  Compare and contrast image.
CS5500 Computer Graphics March 12, 2007.
Graphics Graphics Korea University kucg.korea.ac.kr Viewing 고려대학교 컴퓨터 그래픽스 연구실.
Chapters 5 2 March Classical & Computer Viewing Same elements –objects –viewer –projectors –projection plane.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Classical Viewing Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
Classical Viewing Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
Computer Graphics I, Fall 2010 Building Models.
Classical Viewing Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico.
Viewing Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
CS5500 Computer Graphics March 20, Computer Viewing Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts.
1 E. Angel and D. Shreiner: Interactive Computer Graphics 6E © Addison-Wesley 2012 Computer Viewing Isaac Gang University of Mary Hardin-Baylor.
CSC Graphics Programming Budditha Hettige Department of Statistics and Computer Science.
Viewing. Classical Viewing Viewing requires three basic elements - One or more objects - A viewer with a projection surface - Projectors that go from.
Viewing 고려대학교 컴퓨터 그래픽스 연구실 kucg.korea.ac.kr.
Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009
Viewing.
Computer Viewing.
Isaac Gang University of Mary Hardin-Baylor
Better Interactive Programs
CSC461: Lecture 18 Classical Viewing
CSC461: Lecture 19 Computer Viewing
Classical Viewing Ed Angel
Building Models Ed Angel
Isaac Gang University of Mary Hardin-Baylor
Introduction to Computer Graphics with WebGL
Introduction to Computer Graphics with WebGL
Better Interactive Programs
Isaac Gang University of Mary Hardin-Baylor
Angel: Interactive Computer Graphics5E © Addison-Wesley 2009
University of New Mexico
Computer Graphics Computer Viewing
Computer Viewing Ed Angel Professor Emeritus of Computer Science
University of New Mexico
Isaac Gang University of Mary Hardin-Baylor
Viewing and Projection
Viewing and Projection
Presentation transcript:

3D Programming and Viewing Week 3 David Breen Department of Computer Science Drexel University Based on material from Ed Angel, University of New Mexico CS 432/637 INTERACTIVE COMPUTER GRAPHICS

3D Objects

Angel: Interactive Computer Graphics 3E © Addison-Wesley D Quadrilateral glBegin(GL_POLYGON) glVertex3f(-1.0, -1.0, -1.0); glVertex3f(-1.0, 1.0, -1.0); glVertex3f(-1.0, 1.0, 1.0); glVertex3f(-1.0, -1.0, 1.0); glEnd( ); type of object location of vertex end of object definition Vertices should be coplanar!

Angel: Interactive Computer Graphics 3E © Addison-Wesley Modeling a Cube GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0}, {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}}; GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}}; Model a color cube for rotating cube program Define global arrays for vertices and colors

Angel: Interactive Computer Graphics 3E © Addison-Wesley Drawing a polygon from a list of indices Draw a quadrilateral from a list of indices into the array vertices and use color corresponding to first index void polygon(int a, int b, int c, int d) { glBegin(GL_POLYGON); glColor3fv(colors[a]); glVertex3fv(vertices[a]); glVertex3fv(vertices[b]); glVertex3fv(vertices[c]); glVertex3fv(vertices[d]); glEnd(); }

Angel: Interactive Computer Graphics 3E © Addison-Wesley Draw cube from faces void colorcube( ) { polygon(0,3,2,1); polygon(2,3,7,6); polygon(0,4,7,3); polygon(1,2,6,5); polygon(4,5,6,7); polygon(0,1,5,4); } Note that vertices are ordered so that we obtain correct outward facing normals

Angel: Interactive Computer Graphics 3E © Addison-Wesley Efficiency The weakness of our approach is that we are building the model in the application and must do many function calls to draw the cube Drawing a cube by its faces in the most straight forward way requires –6 glBegin, 6 glEnd –6 glColor –24 glVertex –More if we use texture and lighting

Angel: Interactive Computer Graphics 3E © Addison-Wesley Vertex Arrays OpenGL provides a facility called vertex arrays that allow us to store array data in the implementation Six types of arrays supported –Vertices –Colors –Color indices –Normals –Texture coordinates –Edge flags We will need only colors and vertices

Angel: Interactive Computer Graphics 3E © Addison-Wesley Initialization Using the same color and vertex data, first we enable glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); Identify location of arrays glVertexPointer(3, GL_FLOAT, 0, vertices); glColorPointer(3, GL_FLOAT, 0, colors); glShadeModel(GL_FLAT); /* 1 color/face */ 3d arraysstored as floats data contiguous data array

Angel: Interactive Computer Graphics 3E © Addison-Wesley Mapping indices to faces Form an array of face indices Each successive four indices describe a face of the cube Draw through glDrawElements which replaces all glVertex and glColor calls in the display callback GLubyte cubeIndices[24] = {0,3,2,1,2,3,7,6 0,4,7,3,1,2,6,5,4,5,6,7,0,1,5,4};

Angel: Interactive Computer Graphics 3E © Addison-Wesley Vertex Arrays Glfloat vertices [][3] = {{-1.0,-1.0,1.0}, {-1.0,1.0,1.0}, {1.0,1.0,1.0}, {1.0,-1.0,1.0}, {-1.0,-1.0,-1.0}, {-1.0,1.0,-1.0}, {1.0,1.0,-1.0}, {1.0,-1.0,-1.0}}; Glfloat colors [][3] = {{1.0,0.0,0.0}, {0.0,1.0,1.0}, {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0}, {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,0.0,0.0}}; Glubyte cubeIndices [] = {0, 3, 2, 1, 2, 3, 7, 6, 0, 4, 7, 3, 1, 2, 6, 5, 4, 5, 6, 7, 0, 1, 5, 4};

Angel: Interactive Computer Graphics 3E © Addison-Wesley Drawing the cube Method 1: Method 2: for(i=0; i<6; i++) glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE, &cubeIndices[4*i]); format of index data start of index data what to draw number of indices glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices); Draws cube with 1 function call!!

Angel: Interactive Computer Graphics 3E © Addison-Wesley Drawing a Colored Cube glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); glColorPointer(3, GL_FLOAT, 0, colors); glVertexPointer(3, GL_FLOAT, 0, vertices); glShadeModel(GL_FLAT) /* 1 color/face */ for (i = 0; i < 6; i++) glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_BYTE,cubeIndice s+(4*i)); glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, cubeIndices);

Display Lists

Angel: Interactive Computer Graphics 3E © Addison-Wesley Immediate and Retained Modes Recall that in a standard OpenGL program, once an object is rendered there is no memory of it and to redisplay it, we must re-execute the code for it –Known as immediate mode graphics –Can be especially slow if the objects are complex and must be sent over a network Alternative is define objects and keep them in some form that can be redisplayed easily –Retained mode graphics –Accomplished in OpenGL via display lists

Angel: Interactive Computer Graphics 3E © Addison-Wesley Display Lists Conceptually similar to a graphics file –Must define (name, create) –Add contents –Close In client-server environment, display list is placed on server –Can be redisplayed without sending primitives over network each time

Angel: Interactive Computer Graphics 3E © Addison-Wesley Display List Functions Creating a display list GLuint id; void init( void ) { id = glGenLists( 1 ); glNewList( id, GL_COMPILE ); /* or use GL_COMPILE_AND_EXECUTE */ /* other OpenGL routines */ glEndList(); } Call a created list void display( void ) { glCallList( id ); }

Angel: Interactive Computer Graphics 3E © Addison-Wesley Display Lists and State Most OpenGL functions can be put in display lists State changes made inside a display list persist after the display list is executed Can avoid unexpected results by using glPushAttrib and glPushMatrix upon entering a display list and glPopAttrib and glPopMatrix before exiting

Angel: Interactive Computer Graphics 3E © Addison-Wesley Hierarchy and Display Lists Consider model of a car ­Create display list for chassis ­Create display list for wheel glNewList( CAR, GL_COMPILE ); glCallList( CHASSIS ); glTranslatef( … ); glCallList( WHEEL ); glTranslatef( … ); glCallList( WHEEL ); … glEndList(); Consider model of a car ­Create display list for chassis ­Create display list for wheel glNewList( CAR, GL_COMPILE ); glCallList( CHASSIS ); glTranslatef( … ); glCallList( WHEEL ); glTranslatef( … ); glCallList( WHEEL ); … glEndList();

Angel: Interactive Computer Graphics 3E © Addison-Wesley Vertex Buffer Objects An extension added in OGL 1.5 Regions of memory (buffers) accessible through identifiers Made active through binding, like display lists and textures Provides control over the mappings and unmappings of buffer objects Defines the usage type of the buffers –Allows graphics drivers to optimize internal memory management of the buffers Possible to share VBO data among various clients Clients are able to bind common buffers in the same way as textures or display lists.

Angel: Interactive Computer Graphics 3E © Addison-Wesley Vertex Buffer Objects glBindBuffer –Allows client-state functions to use binding buffers instead of working in absolute memory on the client side glBufferData, glBufferSubData, and glGetBufferSubData –Control size of the buffer data, provide usage hints, and allow copying to a buffer glMapBuffer and glUnmapBuffer –Lock and unlock buffers, allowing data to be loaded into them or relinquishing control to the server

GLU and GLUT Objects

Angel: Interactive Computer Graphics 3E © Addison-Wesley GLU Quadrics GLUquadricObj *mySphere; mySphere = gluNewQuadric(); gluQuadricDrawStyle(mySphere, GLU_FILL); gluQuadricNormals(mySphere, GLU_FLAT); gluQuadricTexture(mySphere, GL_TRUE); gluSphere(mySphere, 1.0, 12, 12); … gluDeleteQuadric(mySphere); gluCylinder gluDisk gluPartialDisk radius slicesstacks

Angel: Interactive Computer Graphics 3E © Addison-Wesley GLU Quadrics GLUquadricObj *myCyl; myCyl = gluNewQuadric(); gluQuadricDrawStyle(myCyl, GLU_FILL); gluQuadricNormals(myCyl, GLU_SMOOTH); gluQuadricTexture(myCyl, GL_FALSE); gluCylinder(myCyl, 3.0, 2.0, 2.0, 2, 20); … gluDeleteQuadric(myCyl); gluQuadricDrawStyle(myDisk, GLU_LINE); gluDisk(1.0, 5.0, 6, 16); gluQuadricDrawStyle(myPartDisk, GLU_POINT); gluPartialDisk (1.0, 5.0, 6, 16, 25.0, 210.0); base radiustop radius heightslicesstacks

Angel: Interactive Computer Graphics 3E © Addison-Wesley GLU Quadrics gluSphere gluCylinder gluDisk gluPartialDisk

Angel: Interactive Computer Graphics 3E © Addison-Wesley GLUT Objects glutWireCube, glutSolidCube centered around origin with length size glutWireSphere, glutSolidSphere centered around origin with radius length glutWireCone, glutSolidCone base on z=0 specify base radius and height glutWireTorus, glutSolidTorus aligned with z axis specify inner and outer radii Specify # of sides, rings, slices and stacks

Angel: Interactive Computer Graphics 3E © Addison-Wesley GLUT Objects glutWireTetrahedron, glutSolidTetrahedron glutWireOctahedron, glutSolidOctahedron glutWireDodecahedron, glutSolidDodecahedron glutWireIcosahedron, glutSolidIcosahedron Generate Platonic solids with all vertices on unit sphere glutWireTeapot, glutSolidTeapot size may be adjusted

Angel: Interactive Computer Graphics 3E © Addison-Wesley GLUT Objects

Viewing Overview

Angel: Interactive Computer Graphics 3E © Addison-Wesley Classical Viewing Viewing requires three basic elements –One or more objects –A viewer with a projection surface –Projectors that go from the object(s) to the projection surface Classical views are based on the relationship among these elements –The viewer picks up the object and orients it how she would like to see it –Or the object is stationary and the viewer moves around it Each object is assumed to be constructed from flat principal faces –Buildings, polyhedra, manufactured objects

Angel: Interactive Computer Graphics 3E © Addison-Wesley Planar Geometric Projections Standard projections project onto a plane Projectors are lines that either –converge at a center of projection –are parallel Such projections preserve lines –but not necessarily angles Nonplanar projections are needed for applications such as map construction

Angel: Interactive Computer Graphics 3E © Addison-Wesley Perspective vs Parallel Computer graphics treats all projections the same and implements them with a single pipeline Classical viewing developed different techniques for drawing each type of projection Fundamental distinction is between parallel and perspective viewing even though mathematically parallel viewing is the limit of perspective viewing

Angel: Interactive Computer Graphics 3E © Addison-Wesley Taxonomy of Planar Geometric Projections parallel perspective axonometric multiview orthographic oblique isometricdimetrictrimetric 2 point1 point3 point planar geometric projections

Angel: Interactive Computer Graphics 3E © Addison-Wesley Perspective Projection

Angel: Interactive Computer Graphics 3E © Addison-Wesley Parallel Projection

Angel: Interactive Computer Graphics 3E © Addison-Wesley Orthographic Projection Projectors are orthogonal to projection surface

Angel: Interactive Computer Graphics 3E © Addison-Wesley Advantages and Disadvantages Preserves both distances and angles –Shapes preserved –Can be used for measurements Building plans Manuals Cannot see what object really looks like because many surfaces hidden from view –Often we add the isometric

Angel: Interactive Computer Graphics 3E © Addison-Wesley Oblique Projection Arbitrary relationship between projectors and projection plane

Angel: Interactive Computer Graphics 3E © Addison-Wesley Perspective Projection Projectors coverge at center of projection

Angel: Interactive Computer Graphics 3E © Addison-Wesley Vanishing Points Parallel lines (not parallel to the projection plan) on the object converge at a single point in the projection (the vanishing point) Drawing simple perspectives by hand uses these vanishing point(s) vanishing point

Angel: Interactive Computer Graphics 3E © Addison-Wesley Advantages and Disadvantages Objects further from viewer are projected smaller than the same sized objects closer to the viewer (diminution) –Looks realistic Equal distances along a line are not projected into equal distances (nonuniform foreshortening) Angles preserved only in planes parallel to the projection plane More difficult to construct by hand than parallel projections (but not more difficult by computer)

Computer Viewing

Angel: Interactive Computer Graphics 3E © Addison-Wesley Objectives Introduce OpenGL viewing functions Look at alternate viewing APIs

Angel: Interactive Computer Graphics 3E © Addison-Wesley Computer Viewing There are three aspects of the viewing process, all of which are implemented in the pipeline, –Positioning the camera Setting the model-view matrix –Selecting a lens Setting the projection matrix –Clipping Setting the view volume

Angel: Interactive Computer Graphics 3E © Addison-Wesley The OpenGL Camera In OpenGL, initially the world and camera frames are the same –Default model-view matrix is an identity The camera is located at origin and points in the negative z direction OpenGL also specifies a default view volume that is a cube with sides of length 2 centered at the origin –Default projection matrix is an identity

Angel: Interactive Computer Graphics 3E © Addison-Wesley Default Projection Default projection is orthographic clipped out z=0 2

Angel: Interactive Computer Graphics 3E © Addison-Wesley Moving the Camera Frame If we want to visualize an object with both positive and negative z values we can either –Move the camera in the positive z direction Translate the camera frame –Move the objects in the negative z direction Translate the world frame Both of these views are equivalent and are determined by the model-view matrix –Want a translation ( glTranslatef(0.0,0.0,-d); ) –d > 0

Angel: Interactive Computer Graphics 3E © Addison-Wesley Moving Camera back from Origin default frames frames after translation by –d d > 0

Angel: Interactive Computer Graphics 3E © Addison-Wesley Moving the Camera We can move the camera to any desired position by a sequence of rotations and translations Example: side view –Move it away from origin –Rotate the camera –Model-view matrix C = TR

Angel: Interactive Computer Graphics 3E © Addison-Wesley The LookAt Function The GLU library contains the function gluLookAt to form the required modelview matrix through a simple interface Note the need for setting an up direction Still need to initialize –Can concatenate with modeling transformations Example: isometric view of cube aligned with axes glMatrixMode(GL_MODELVIEW): glLoadIdentity(); gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0., );

Angel: Interactive Computer Graphics 3E © Addison-Wesley The LookAt Function gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz)

Angel: Interactive Computer Graphics 3E © Addison-Wesley LookAt Example void display() { glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(1.0,1.0,1.0, 0.0,0.0,0.0, 0.0,1.0,0.0); glutWireCube(0.5); glutSwapBuffers(); }

Angel: Interactive Computer Graphics 3E © Addison-Wesley Other Viewing APIs The LookAt function is only one possible API for positioning the camera Others include –View reference point, view plane normal, view up (PHIGS, GKS-3D) –Yaw, pitch, roll –Elevation, azimuth, twist –Direction angles

Angel: Interactive Computer Graphics 3E © Addison-Wesley Projections and Normalization The default projection in the eye (camera) frame is orthographic For points within the default view volume Most graphics systems use view normalization –All other views are converted to the default view by transformations that determine the projection matrix –Allows use of the same pipeline for all views x p = x y p = y z p = 0

Angel: Interactive Computer Graphics 3E © Addison-Wesley OpenGL Orthographic Viewing glOrtho(xmin,xmax,ymin,ymax,near,far) all values measured from camera near and far can be positive and negative

Angel: Interactive Computer Graphics 3E © Addison-Wesley OpenGL Perspective glFrustum(xmin,xmax,ymin,ymax,near,far) -near) all values measured from camera near and far should be positive

Angel: Interactive Computer Graphics 3E © Addison-Wesley Using Field of View With glFrustum it is often difficult to get the desired view gluPerpective(fovy, aspect, near, far) often provides a better interface aspect = w/h fovy given in degrees front plane

Angel: Interactive Computer Graphics 3E © Addison-Wesley OpenGL Perspective -z max -z min ) glFrustum allows for an unsymmetric viewing frustum gluPerspective does not

Angel: Interactive Computer Graphics 3E © Addison-Wesley Another LookAt Example void reshape(int w, int h) { glViewport(0, 0, (Glsizei) w, (Glsizei) h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerpective(45.0,(Glfloat)w/(Glfloat)h, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(1.0,1.0,1.0, 0.0,0.0,0.0, 0.0,1.0,0.0); }

Angel: Interactive Computer Graphics 3E © Addison-Wesley Hidden-Surface Removal /* back-face culling for convex objects */ glEnable(GL_CULL_FACE); glCullFace(GL_BACK); /* More general Z-buffer algorithm */ glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ); glEnable(GL_DEPTH_TEST); /* Must clear depth buffer too! */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );