CS 445 / 645 Introduction to Computer Graphics Lecture 4 OpenGL Intro Assignment 1 Lecture 4 OpenGL Intro Assignment 1.

Slides:



Advertisements
Similar presentations
Computer Graphics - Viewing -
Advertisements

SI23 Introduction to Computer Graphics
02/17/05CISC640/440 OpenGL Tutorial1 OpenGL Tutorial CISC 640/440 Computer Graphics TA: Qi Li/Mani Thomas
OpenGL: Simple Use Open a window and attach OpenGL to it Set projection parameters (e.g., field of view) Setup lighting, if any Main rendering loop –Set.
MAT 594CM S2010Fundamentals of Spatial ComputingAngus Forbes Overview Goals of the course: 1. to introduce real-time 3D graphics programming with openGL.
Graphics Pipeline.
Computer Graphic Creator: Mohsen Asghari Session 2 Fall 2014.
David Luebke5/16/2015 Administrivia l Back on track: canceling OpenGL lecture 2 l Assignment 1 –Greg Yukl found an alternate XForms site:
Based on slides created by Edward Angel
Viewing and Transformation
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Computer Viewing Ed Angel Professor of Computer Science, Electrical and Computer Engineering,
OpenGL and Projections
Objectives Learn to build arbitrary transformation matrices from simple transformations Learn to build arbitrary transformation matrices from simple transformations.
Further Programming for 3D applications CE Introduction to Further Programming for 3D application Bob Hobbs Faculty of Computing, Engineering and.
Sep 30, I3I3DG&VR CE00539-m Interactive 3D Graphics and Virtual Reality Bob Hobbs Faculty of Computing, Engineering and Technology Staffordshire.
OpenGL (II). How to Draw a 3-D object on Screen?
CS 4731: Computer Graphics Lecture 11: 3D Viewing Emmanuel Agu.
Rendering Pipeline Aaron Bloomfield CS 445: Introduction to Graphics Fall 2006 (Slide set originally by Greg Humphreys)
3D Rendering with JOGL Introduction to Java OpenGL Graphic Library By Ricardo Veguilla
3D coordinate systems X Y Z Right-Hand Coordinate System X Y Z Left-Hand Coordinate System OpenGL uses this! Direct3D uses this!
Introduction to Computer Graphics CS 445 / 645 Lecture 4 OpenGL OpenGL OpenGL Programming Guide.
1 OpenGL Basics A Graphics Standard ©Mel Slater, Anthony Steed
INTRO TO COMPUTER GRAPHICS TEXT EDWARD ANGEL: EDITION 5 CS770/870
More OpenGL CS 551/645 - Introduction to Computer Graphics.
OpenGL Shading Language (Advanced Computer Graphics) Ernest Tatum.
Week 2 - Wednesday CS361.
Image Synthesis Rabie A. Ramadan, PhD 2. 2 Java OpenGL Using JOGL: Using JOGL: Wiki: You can download JOGL from.
Geometric transformations The Pipeline
UBI 516 Advanced Computer Graphics Introduction to OpenGL OpenGL Programming Guide.
OpenGL and Parametric Curves Advanced Multimedia Technology: Computer Graphics Yung-Yu Chuang 2005/12/21 with slides by Brian Curless, Zoran Popovic, Robin.
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
Homogeneous Form, Introduction to 3-D Graphics Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 20,
Introduction to OpenGL 1. 2 OpenGL A Graphics rendering API introduced in 1992 by Silicon Graphics Inc Provide the low-level functions to access graphics.
Lecture 6: 3D graphics Concepts 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 718,  ex 2271 
Computer Graphics Bing-Yu Chen National Taiwan University.
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.
OpenGL: Introduction Yanci Zhang Game Programming Practice.
Intro to OpenGL Transformations CS 445/645 Introduction to Computer Graphics David Luebke, Spring 2003.
OpenGL The Viewing Pipeline: Definition: a series of operations that are applied to the OpenGL matrices, in order to create a 2D representation from 3D.
Image Synthesis Rabie A. Ramadan, PhD 4. 2 Review Questions Q1: What are the two principal tasks required to create an image of a three-dimensional scene?
OpenGL Viewing and Modeling Transformation Geb Thomas Adapted from the OpenGL Programming Guidethe OpenGL Programming Guide.
2 COEN Computer Graphics I Evening’s Goals n Discuss viewing and modeling transformations n Describe matrix stacks and their uses n Show basic geometric.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 OpenGL Transformations.
Viewing and Transformation. Pixel pipeline Vertex pipeline Course Map Transformation & Lighting Primitive assembly Viewport culling & clipping Texture.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
The Camera Analogy ► Set up your tripod and point the camera at the scene (viewing transformation) ► Arrange the scene to be photographed into the desired.
Chapters 5 2 March Classical & Computer Viewing Same elements –objects –viewer –projectors –projection plane.
OpenGL: The Open Graphics Language Introduction By Ricardo Veguilla.
David Luebke1/5/2016 CS 551 / 645: Introductory Computer Graphics David Luebke
Chap 3 Viewing and Transformation
Lecture 7 Midterm Review. OpenGL Libraries gl: Basic OpenGL library, e.g. primitives. glu: OpenGL Utility library, a set of functions to create texture.
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
Learning Objectives Affine transformations Affine transformations Translation Translation Rotation Rotation Scaling Scaling Reflection Reflection Shear.
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
1 Geometric Transformations-II Modelling Transforms By Dr.Ureerat Suksawatchon.
Introduction to Graphics Programming. Graphics API.
Geometric Transformations Ceng 477 Introduction to Computer Graphics Computer Engineering METU.
CSC Graphics Programming Budditha Hettige Department of Statistics and Computer Science.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL LAB III.
OpenGL: The Open Graphics Language Technology and Historical Overview By Ricardo Veguilla.
Introduction to 3-D Viewing Glenn G. Chappell U. of Alaska Fairbanks CS 381 Lecture Notes Monday, October 27, 2003.
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Administrivia Back on track: canceling OpenGL lecture 2 Assignment 1
Rendering Pipeline Aaron Bloomfield CS 445: Introduction to Graphics
Modeling 101 For the moment assume that all geometry consists of points, lines and faces Line: A segment between two endpoints Face: A planar area bounded.
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Projection in 3-D Glenn G. Chappell
Presentation transcript:

CS 445 / 645 Introduction to Computer Graphics Lecture 4 OpenGL Intro Assignment 1 Lecture 4 OpenGL Intro Assignment 1

X-Axis Shear Shear along x axis (What is the matrix for y axis shear?) x y x y

X-Axis Shear Shear along x axis (What is the matrix for y axis shear?) x y x y

Reflect About X Axis x x

What is the matrix for reflect about Y axis? x x

Reading Assignment Read Chapters 1 – 4 of the Red Book (OpenGL Programming Guide)

OpenGL Design Goals SGI’s design goals for OpenGL: High-performance (hardware-accelerated) graphics APIHigh-performance (hardware-accelerated) graphics API Some hardware independenceSome hardware independence Natural, terse API with some built-in extensibilityNatural, terse API with some built-in extensibility OpenGL has become a standard because: It doesn’t try to do too muchIt doesn’t try to do too much –Only renders the image, doesn’t manage windows, etc. –No high-level animation, modeling, sound (!), etc. It does enoughIt does enough –Useful rendering effects + high performance It is promoted by SGI (& Microsoft, half-heartedly)It is promoted by SGI (& Microsoft, half-heartedly) SGI’s design goals for OpenGL: High-performance (hardware-accelerated) graphics APIHigh-performance (hardware-accelerated) graphics API Some hardware independenceSome hardware independence Natural, terse API with some built-in extensibilityNatural, terse API with some built-in extensibility OpenGL has become a standard because: It doesn’t try to do too muchIt doesn’t try to do too much –Only renders the image, doesn’t manage windows, etc. –No high-level animation, modeling, sound (!), etc. It does enoughIt does enough –Useful rendering effects + high performance It is promoted by SGI (& Microsoft, half-heartedly)It is promoted by SGI (& Microsoft, half-heartedly)

OpenGL is Not Alone GLUT The OpenGL Utility ToolkitThe OpenGL Utility Toolkit –Interface to windowing system and OS –Provides handy shape primitives (torus, teapot, cube) FLTK Fast Light ToolkitFast Light Toolkit –Graphical User Interface (GUI) builder Boost Additional libraries that work with STLAdditional libraries that work with STLGLUT The OpenGL Utility ToolkitThe OpenGL Utility Toolkit –Interface to windowing system and OS –Provides handy shape primitives (torus, teapot, cube) FLTK Fast Light ToolkitFast Light Toolkit –Graphical User Interface (GUI) builder Boost Additional libraries that work with STLAdditional libraries that work with STL

The Big Picture Who gets control of the main control loop? FLTK – the code that waits for user input and processes itFLTK – the code that waits for user input and processes it –Must be responsive to user… do as I say GLUT – the code that controls the window and refreshGLUT – the code that controls the window and refresh –Must be responsive to windowing system and OS OpenGL – the code that controls what is drawnOpenGL – the code that controls what is drawn –Must be responsive to the program that specifies where objects are located. If something moves, I want to see it. Who gets control of the main control loop? FLTK – the code that waits for user input and processes itFLTK – the code that waits for user input and processes it –Must be responsive to user… do as I say GLUT – the code that controls the window and refreshGLUT – the code that controls the window and refresh –Must be responsive to windowing system and OS OpenGL – the code that controls what is drawnOpenGL – the code that controls what is drawn –Must be responsive to the program that specifies where objects are located. If something moves, I want to see it.

The Big Picture Who gets control of the main control loop? Answer: FLTKAnswer: FLTK –We’ll try to hide the details from you for now –But be aware of the conflict that exists FLTK must be aware of GLUT and OpenGL state at all timesFLTK must be aware of GLUT and OpenGL state at all times –Must give code compute cycles when needed We’ll discuss OpenGL as if it were standaloneWe’ll discuss OpenGL as if it were standalone Who gets control of the main control loop? Answer: FLTKAnswer: FLTK –We’ll try to hide the details from you for now –But be aware of the conflict that exists FLTK must be aware of GLUT and OpenGL state at all timesFLTK must be aware of GLUT and OpenGL state at all times –Must give code compute cycles when needed We’ll discuss OpenGL as if it were standaloneWe’ll discuss OpenGL as if it were standalone

OpenGL Simple Example – no color or lights or textures… An array of 100 vertices (x,y,z)An array of 100 vertices (x,y,z) –double verts[100][3] An array of 30 triangles (made of three verts)An array of 30 triangles (made of three verts) –int tri[30][3] An array of 10 matrix transformationsAn array of 10 matrix transformations –double trans[10][16] Simple Example – no color or lights or textures… An array of 100 vertices (x,y,z)An array of 100 vertices (x,y,z) –double verts[100][3] An array of 30 triangles (made of three verts)An array of 30 triangles (made of three verts) –int tri[30][3] An array of 10 matrix transformationsAn array of 10 matrix transformations –double trans[10][16]

OpenGL Main Loop While (1) do…While (1) do… –Determine the 10 matrix transformations –Multiply transformations together (composite them) –Apply resulting transformation to each triangle –Render each triangle Main Loop While (1) do…While (1) do… –Determine the 10 matrix transformations –Multiply transformations together (composite them) –Apply resulting transformation to each triangle –Render each triangle

Determine the 10 matrix transformations We know how to build the matrices by hand or… Use the GL implementationsUse the GL implementations –glScale (theta degrees about (u, v, w) axis) –glRotate (x, y, and z amounts) –glTranslate (x, y, and z amounts) We know how to build the matrices by hand or… Use the GL implementationsUse the GL implementations –glScale (theta degrees about (u, v, w) axis) –glRotate (x, y, and z amounts) –glTranslate (x, y, and z amounts)

Multiply transformations together (composite them) We know how to do matrix multiplication or… Use the GL implementationsUse the GL implementations –Each call to glTranslate, glRotate, glScale…. automatically multiplies new transformation by product of previous transformations, MODELVIEW matrix –Remember order of matrix multiplication and why this is a good way to do things We know how to do matrix multiplication or… Use the GL implementationsUse the GL implementations –Each call to glTranslate, glRotate, glScale…. automatically multiplies new transformation by product of previous transformations, MODELVIEW matrix –Remember order of matrix multiplication and why this is a good way to do things

Apply resulting transformation by each triangle For each triangle, access its three pointsFor each triangle, access its three points –glBegin (GL_TRIANGLES) –glVertex (point1); glVertex(point2); glVertex(point3) –glEnd(); Multiply each point by MODELVIEW matrixMultiply each point by MODELVIEW matrix Feed the three transformed points to a GL function that projects 3-D triangles to 2-D screen spaceFeed the three transformed points to a GL function that projects 3-D triangles to 2-D screen space –PROJECTION matrix defines virtual camera  Aspect ratio, clipping planes, camera type For each triangle, access its three pointsFor each triangle, access its three points –glBegin (GL_TRIANGLES) –glVertex (point1); glVertex(point2); glVertex(point3) –glEnd(); Multiply each point by MODELVIEW matrixMultiply each point by MODELVIEW matrix Feed the three transformed points to a GL function that projects 3-D triangles to 2-D screen spaceFeed the three transformed points to a GL function that projects 3-D triangles to 2-D screen space –PROJECTION matrix defines virtual camera  Aspect ratio, clipping planes, camera type

Render GL renderer Given the 2-D coordinates of triangle verticesGiven the 2-D coordinates of triangle vertices –OpenGL determines which pixels to illuminate It repeats this for all trianglesIt repeats this for all triangles GL renderer Given the 2-D coordinates of triangle verticesGiven the 2-D coordinates of triangle vertices –OpenGL determines which pixels to illuminate It repeats this for all trianglesIt repeats this for all triangles

OpenGL Main Loop Rendering is a pipelined process Object Coordinates Eye Coordinates Clip Coordinates Device Coordinates Window Coordinates MODELVIEW matrix PROJECTION matrix Perspective division Viewport transformation

Immediate Mode This example demonstrates GL’s Immediate Mode Every triangle is transformed immediately to screen spaceEvery triangle is transformed immediately to screen space It is then thrown awayIt is then thrown away If you repeat the same triangles each frame, use a Display ListIf you repeat the same triangles each frame, use a Display List –display list cache’s vertices to optimize pipeline This example demonstrates GL’s Immediate Mode Every triangle is transformed immediately to screen spaceEvery triangle is transformed immediately to screen space It is then thrown awayIt is then thrown away If you repeat the same triangles each frame, use a Display ListIf you repeat the same triangles each frame, use a Display List –display list cache’s vertices to optimize pipeline

Adding Extra Features GL is a state machine There are many variables stored as ‘globals’There are many variables stored as ‘globals’ –What color to use – glColor()  All subsequent vertices will be assigned that color –What rendering mode (wireframe or solid)  All subsequent polygons will be rendered that way –What matrix is active (MODELVIEW or PROJECTION) – glMatrixMode()  All subsequent matrix commands will affect that matrix GL is a state machine There are many variables stored as ‘globals’There are many variables stored as ‘globals’ –What color to use – glColor()  All subsequent vertices will be assigned that color –What rendering mode (wireframe or solid)  All subsequent polygons will be rendered that way –What matrix is active (MODELVIEW or PROJECTION) – glMatrixMode()  All subsequent matrix commands will affect that matrix

Adding Extra Features Setting different transformations to different triangles Not all triangles should be affected by same transformation matrixNot all triangles should be affected by same transformation matrix –think of limbs of an arm You can change a matrix at willYou can change a matrix at will –glMatrixMode (GL_MODELVIEW) –glLoadIdentity (); Or you can store previous results for future useOr you can store previous results for future use –glPushMatrix() and glPopMatrix() –Puts a copy of current matrix on top of stack (or deletes top matrix) Setting different transformations to different triangles Not all triangles should be affected by same transformation matrixNot all triangles should be affected by same transformation matrix –think of limbs of an arm You can change a matrix at willYou can change a matrix at will –glMatrixMode (GL_MODELVIEW) –glLoadIdentity (); Or you can store previous results for future useOr you can store previous results for future use –glPushMatrix() and glPopMatrix() –Puts a copy of current matrix on top of stack (or deletes top matrix)

Wireframe Wireframe with depth-cueing

Wireframe with antialiasing Flat-shaded polygons

Smooth-shaded polygons Texture maps and shadows

Close-up With Fog

Modeling Transformations glTranslate (x, y, z) Post-multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z-valuesPost-multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z-values glRotate (theta, x, y, z) Post-multiplies the current matrix by a matrix that rotates the object in a counterclockwise direction about the ray from the origin through the point (x, y, z)Post-multiplies the current matrix by a matrix that rotates the object in a counterclockwise direction about the ray from the origin through the point (x, y, z) glTranslate (x, y, z) Post-multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z-valuesPost-multiplies the current matrix by a matrix that moves the object by the given x-, y-, and z-values glRotate (theta, x, y, z) Post-multiplies the current matrix by a matrix that rotates the object in a counterclockwise direction about the ray from the origin through the point (x, y, z)Post-multiplies the current matrix by a matrix that rotates the object in a counterclockwise direction about the ray from the origin through the point (x, y, z)

Modeling Transformations glScale (x, y, z) Post-multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axes.Post-multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axes. glScale (x, y, z) Post-multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axes.Post-multiplies the current matrix by a matrix that stretches, shrinks, or reflects an object along the axes.

Matrix Multiplcations Certain commands affect the current matrix in OpenGL glMatrixMode() sets the current matrixglMatrixMode() sets the current matrix glLoadIdentity() replaces the current matrix with an identity matrixglLoadIdentity() replaces the current matrix with an identity matrix glTranslate() postmultiplies the current matrix with a translation matrixglTranslate() postmultiplies the current matrix with a translation matrix gluPerspective() postmultiplies the current matrix with a perspective projection matrixgluPerspective() postmultiplies the current matrix with a perspective projection matrix It is important that you understand the order in which OpenGL concatenates matrices Certain commands affect the current matrix in OpenGL glMatrixMode() sets the current matrixglMatrixMode() sets the current matrix glLoadIdentity() replaces the current matrix with an identity matrixglLoadIdentity() replaces the current matrix with an identity matrix glTranslate() postmultiplies the current matrix with a translation matrixglTranslate() postmultiplies the current matrix with a translation matrix gluPerspective() postmultiplies the current matrix with a perspective projection matrixgluPerspective() postmultiplies the current matrix with a perspective projection matrix It is important that you understand the order in which OpenGL concatenates matrices

Matrix Operations In OpenGL In OpenGL: Vertices are multiplied by the MODELVIEW matrixVertices are multiplied by the MODELVIEW matrix The resulting vertices are multiplied by the projection matrixThe resulting vertices are multiplied by the projection matrixExample: Suppose you want to scale an object, translate it, apply a lookat transformation, and view it under perspective projection. What order should you make calls?Suppose you want to scale an object, translate it, apply a lookat transformation, and view it under perspective projection. What order should you make calls? In OpenGL: Vertices are multiplied by the MODELVIEW matrixVertices are multiplied by the MODELVIEW matrix The resulting vertices are multiplied by the projection matrixThe resulting vertices are multiplied by the projection matrixExample: Suppose you want to scale an object, translate it, apply a lookat transformation, and view it under perspective projection. What order should you make calls?Suppose you want to scale an object, translate it, apply a lookat transformation, and view it under perspective projection. What order should you make calls?

Matrix Operations in OpenGL Problem: scale an object, translate it, apply a lookat transformation, and view it under perspective A correct code fragment: glMatrixMode(GL_PERSPECTIVE);glLoadIdentity();gluPerspective(…);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(…);glTranslate(…);glScale(…); /* Draw the object...*/ Problem: scale an object, translate it, apply a lookat transformation, and view it under perspective A correct code fragment: glMatrixMode(GL_PERSPECTIVE);glLoadIdentity();gluPerspective(…);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(…);glTranslate(…);glScale(…); /* Draw the object...*/

Matrix Operations in OpenGL Problem: scale an object, translate it, apply a lookat transformation, and view it under perspective An incorrect code fragment: glMatrixMode(GL_PERSPECTIVE);glLoadIdentity();glTranslate(…);glScale(…);gluPerspective(…);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(…); /* Draw the object...*/ Problem: scale an object, translate it, apply a lookat transformation, and view it under perspective An incorrect code fragment: glMatrixMode(GL_PERSPECTIVE);glLoadIdentity();glTranslate(…);glScale(…);gluPerspective(…);glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(…); /* Draw the object...*/

Multiplication Order glMatrixMode (MODELVIEW); glLoadIdentity();glMultMatrix(N);glMultMatrix(M);glMultMatrix(L);glBegin(POINTS);glVertex3f(v);glEnd(); glLoadIdentity();glMultMatrix(N);glMultMatrix(M);glMultMatrix(L);glBegin(POINTS);glVertex3f(v);glEnd(); Modelview matrix successively contains: I(dentity), N, NM, NML The transformed vertex is: NMLv = N(M(Lv))

Manipulating Matrix Stacks Observation: Certain model transformations are shared among many models We want to avoid continuously reloading the same sequence of transformations glPushMatrix ( ) push all matrices in current stack down one level and copy topmost matrix of stackpush all matrices in current stack down one level and copy topmost matrix of stack glPopMatrix ( ) pop the top matrix off the stackpop the top matrix off the stack Observation: Certain model transformations are shared among many models We want to avoid continuously reloading the same sequence of transformations glPushMatrix ( ) push all matrices in current stack down one level and copy topmost matrix of stackpush all matrices in current stack down one level and copy topmost matrix of stack glPopMatrix ( ) pop the top matrix off the stackpop the top matrix off the stack

Matrix Manipulation - Example Drawing a car with wheels and lugnuts draw_wheel( ); for (j=0; j<5; j++) { glPushMatrix (); glRotatef(72.0*j, 0.0, 0.0, 1.0); glTranslatef (3.0, 0.0, 0.0); draw_bolt ( ); glPopMatrix ( ); }

Matrix Manipulation - Example draw_wheel( ); for (j=0; j<5; j++) { glPushMatrix (); glRotatef(72.0*j, 0.0, 0.0, 1.0); glTranslatef (3.0, 0.0, 0.0); draw_bolt ( ); glPopMatrix ( ); Global – Bottom Up Start Rot Trans

Matrix Manipulation - Example draw_wheel( ); for (j=0; j<5; j++) { glPushMatrix (); glRotatef(72.0*j, 0.0, 0.0, 1.0); glTranslatef (3.0, 0.0, 0.0); draw_bolt ( ); glPopMatrix ( ); Local – Top Down Start Rot Trans

OpenGL: Conventions Functions in OpenGL start with gl Most functions just gl (e.g., glColor() )Most functions just gl (e.g., glColor() ) Functions starting with glu are utility functions (e.g., gluLookAt() )Functions starting with glu are utility functions (e.g., gluLookAt() ) Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c)Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c) Functions in OpenGL start with gl Most functions just gl (e.g., glColor() )Most functions just gl (e.g., glColor() ) Functions starting with glu are utility functions (e.g., gluLookAt() )Functions starting with glu are utility functions (e.g., gluLookAt() ) Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c)Functions starting with glx are for interfacing with the X Windows system (e.g., in gfx.c)

OpenGL: Conventions Function names indicate argument type and number Functions ending with f take floatsFunctions ending with f take floats Functions ending with i take intsFunctions ending with i take ints Functions ending with b take bytesFunctions ending with b take bytes Functions ending with ub take unsigned bytesFunctions ending with ub take unsigned bytes Functions that end with v take an array.Functions that end with v take an array.Examples glColor3f() takes 3 floatsglColor3f() takes 3 floats glColor4fv() takes an array of 4 floatsglColor4fv() takes an array of 4 floats Function names indicate argument type and number Functions ending with f take floatsFunctions ending with f take floats Functions ending with i take intsFunctions ending with i take ints Functions ending with b take bytesFunctions ending with b take bytes Functions ending with ub take unsigned bytesFunctions ending with ub take unsigned bytes Functions that end with v take an array.Functions that end with v take an array.Examples glColor3f() takes 3 floatsglColor3f() takes 3 floats glColor4fv() takes an array of 4 floatsglColor4fv() takes an array of 4 floats

OpenGL: Conventions Variables written in CAPITAL letters Example: GLUT_SINGLE, GLUT_RGBExample: GLUT_SINGLE, GLUT_RGB usually constantsusually constants use the bitwise or command (x | y) to combine constantsuse the bitwise or command (x | y) to combine constants Variables written in CAPITAL letters Example: GLUT_SINGLE, GLUT_RGBExample: GLUT_SINGLE, GLUT_RGB usually constantsusually constants use the bitwise or command (x | y) to combine constantsuse the bitwise or command (x | y) to combine constants

OpenGL: Simple Use Open a window and attach OpenGL to it Set projection parameters (e.g., field of view) Setup lighting, if any Main rendering loop Set camera pose with gluLookAt()Set camera pose with gluLookAt() –Camera position specified in world coordinates Render polygons of modelRender polygons of model –Simplest case: vertices of polygons in world coordinates Open a window and attach OpenGL to it Set projection parameters (e.g., field of view) Setup lighting, if any Main rendering loop Set camera pose with gluLookAt()Set camera pose with gluLookAt() –Camera position specified in world coordinates Render polygons of modelRender polygons of model –Simplest case: vertices of polygons in world coordinates

OpenGL: Simple Use Open a window and attach OpenGL to it glutCreateWindow() or FLTK window methodglutCreateWindow() or FLTK window method Open a window and attach OpenGL to it glutCreateWindow() or FLTK window methodglutCreateWindow() or FLTK window method

OpenGL: Perspective Projection Set projection parameters (e.g., field of view) Typically, we use a perspective projection Distant objects appear smaller than near objectsDistant objects appear smaller than near objects Vanishing point at center of screenVanishing point at center of screen Defined by a view frustum (draw it)Defined by a view frustum (draw it) Other projections: orthographic, isometric Set projection parameters (e.g., field of view) Typically, we use a perspective projection Distant objects appear smaller than near objectsDistant objects appear smaller than near objects Vanishing point at center of screenVanishing point at center of screen Defined by a view frustum (draw it)Defined by a view frustum (draw it) Other projections: orthographic, isometric

Setting up Camera glMatrixMode(GL_MODELVIEW);glLoadIdentity(); gluLookAt(eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ); eye[XYZ]: camera position in world coordinateseye[XYZ]: camera position in world coordinates look[XYZ]: a point centered in camera’s viewlook[XYZ]: a point centered in camera’s view up[XYZ]: a vector defining the camera’s verticalup[XYZ]: a vector defining the camera’s vertical Creates a matrix that transforms points in world coordinates to camera coordinates Camera at originCamera at origin Looking down -Z axisLooking down -Z axis Up vector aligned with Y axisUp vector aligned with Y axisglMatrixMode(GL_MODELVIEW);glLoadIdentity(); gluLookAt(eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ); eye[XYZ]: camera position in world coordinateseye[XYZ]: camera position in world coordinates look[XYZ]: a point centered in camera’s viewlook[XYZ]: a point centered in camera’s view up[XYZ]: a vector defining the camera’s verticalup[XYZ]: a vector defining the camera’s vertical Creates a matrix that transforms points in world coordinates to camera coordinates Camera at originCamera at origin Looking down -Z axisLooking down -Z axis Up vector aligned with Y axisUp vector aligned with Y axis

OpenGL: Perspective Projection In OpenGL: Projections implemented by projection matrixProjections implemented by projection matrix gluPerspective() creates a perspective projection matrix:gluPerspective() creates a perspective projection matrix:glSetMatrix(GL_PROJECTION); glLoadIdentity(); //load an identity matrix gluPerspective(vfov, aspect, near, far); Parameters to gluPerspective() : vfov : vertical field of viewvfov : vertical field of view aspect : window width/heightaspect : window width/height near, far : distance to near & far clipping planesnear, far : distance to near & far clipping planes In OpenGL: Projections implemented by projection matrixProjections implemented by projection matrix gluPerspective() creates a perspective projection matrix:gluPerspective() creates a perspective projection matrix:glSetMatrix(GL_PROJECTION); glLoadIdentity(); //load an identity matrix gluPerspective(vfov, aspect, near, far); Parameters to gluPerspective() : vfov : vertical field of viewvfov : vertical field of view aspect : window width/heightaspect : window width/height near, far : distance to near & far clipping planesnear, far : distance to near & far clipping planes

OpenGL: Lighting Setup lighting, if any Simplest option: change the current color between polygons or vertices glColor() sets the current colorglColor() sets the current color Or OpenGL provides a simple lighting model: Set parameters for light(s)Set parameters for light(s) –Intensity, position, direction & falloff (if applicable) Set material parameters to describe how light reflects from the surfaceSet material parameters to describe how light reflects from the surface Won’t go into details now; check the red book if interested Setup lighting, if any Simplest option: change the current color between polygons or vertices glColor() sets the current colorglColor() sets the current color Or OpenGL provides a simple lighting model: Set parameters for light(s)Set parameters for light(s) –Intensity, position, direction & falloff (if applicable) Set material parameters to describe how light reflects from the surfaceSet material parameters to describe how light reflects from the surface Won’t go into details now; check the red book if interested

OpenGL: Specifying Geometry Geometry in OpenGL consists of a list of vertices in between calls to glBegin() and glEnd() A simple example: telling GL to render a triangleA simple example: telling GL to render a triangleglBegin(GL_POLYGON); glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3); glEnd(); Usage: glBegin( geomtype ) where geomtype is:Usage: glBegin( geomtype ) where geomtype is: –Points, lines, polygons, triangles, quadrilaterals, etc... Geometry in OpenGL consists of a list of vertices in between calls to glBegin() and glEnd() A simple example: telling GL to render a triangleA simple example: telling GL to render a triangleglBegin(GL_POLYGON); glVertex3f(x1, y1, z1); glVertex3f(x2, y2, z2); glVertex3f(x3, y3, z3); glEnd(); Usage: glBegin( geomtype ) where geomtype is:Usage: glBegin( geomtype ) where geomtype is: –Points, lines, polygons, triangles, quadrilaterals, etc...

Primitive Types GL_POINTSGL_LINE {S | _STRIP | _LOOP}{S | _STRIP | _LOOP}GL_TRIANGLE {S | _STRIP | _FAN}{S | _STRIP | _FAN}GL_QUAD {S | _STRIP}{S | _STRIP}GL_POLYGONGL_POINTSGL_LINE {S | _STRIP | _LOOP}{S | _STRIP | _LOOP}GL_TRIANGLE {S | _STRIP | _FAN}{S | _STRIP | _FAN}GL_QUAD {S | _STRIP}{S | _STRIP}GL_POLYGON

GL_POLYGON List of vertices defines polygon edges Polygon must be convex List of vertices defines polygon edges Polygon must be convex

Non-planar Polygons Imagine polygon with non-planar vertices Some perspectives will be rendered as concave polygons These concave polygons may not rasterize correctly Imagine polygon with non-planar vertices Some perspectives will be rendered as concave polygons These concave polygons may not rasterize correctly

OpenGL: More Examples Example: GL supports quadrilaterals: glBegin(GL_QUADS); glVertex3f(-1, 1, 0); glVertex3f(-1, -1, 0); glVertex3f(1, -1, 0); glVertex3f(1, 1, 0); glEnd(); This type of operation is called immediate-mode rendering; each command happens immediatelyThis type of operation is called immediate-mode rendering; each command happens immediately Example: GL supports quadrilaterals: glBegin(GL_QUADS); glVertex3f(-1, 1, 0); glVertex3f(-1, -1, 0); glVertex3f(1, -1, 0); glVertex3f(1, 1, 0); glEnd(); This type of operation is called immediate-mode rendering; each command happens immediatelyThis type of operation is called immediate-mode rendering; each command happens immediately

OpenGL: Drawing Triangles You can draw multiple triangles between glBegin(GL_TRIANGLES) and glEnd() : float v1[3], v2[3], v3[3], v4[3];...glBegin(GL_TRIANGLES); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v1); glVertex3fv(v3); glVertex3fv(v4); glEnd(); Each set of 3 vertices forms a triangle What do the triangles drawn above look like?What do the triangles drawn above look like? How much redundant computation is happening?How much redundant computation is happening? You can draw multiple triangles between glBegin(GL_TRIANGLES) and glEnd() : float v1[3], v2[3], v3[3], v4[3];...glBegin(GL_TRIANGLES); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v1); glVertex3fv(v3); glVertex3fv(v4); glEnd(); Each set of 3 vertices forms a triangle What do the triangles drawn above look like?What do the triangles drawn above look like? How much redundant computation is happening?How much redundant computation is happening?

OpenGL: Triangle Strips An OpenGL triangle strip primitive reduces this redundancy by sharing vertices: glBegin(GL_TRIANGLE_STRIP);glVertex3fv(v0);glVertex3fv(v1);glVertex3fv(v2);glVertex3fv(v3);glVertex3fv(v4);glVertex3fv(v5);glEnd(); triangle 0 is v0, v1, v2triangle 0 is v0, v1, v2 triangle 1 is v2, v1, v3 (why not v1, v2, v3?)triangle 1 is v2, v1, v3 (why not v1, v2, v3?) triangle 2 is v2, v3, v4triangle 2 is v2, v3, v4 triangle 3 is v4, v3, v5 (again, not v3, v4, v5)triangle 3 is v4, v3, v5 (again, not v3, v4, v5) An OpenGL triangle strip primitive reduces this redundancy by sharing vertices: glBegin(GL_TRIANGLE_STRIP);glVertex3fv(v0);glVertex3fv(v1);glVertex3fv(v2);glVertex3fv(v3);glVertex3fv(v4);glVertex3fv(v5);glEnd(); triangle 0 is v0, v1, v2triangle 0 is v0, v1, v2 triangle 1 is v2, v1, v3 (why not v1, v2, v3?)triangle 1 is v2, v1, v3 (why not v1, v2, v3?) triangle 2 is v2, v3, v4triangle 2 is v2, v3, v4 triangle 3 is v4, v3, v5 (again, not v3, v4, v5)triangle 3 is v4, v3, v5 (again, not v3, v4, v5) v0v0 v2v2 v1v1 v3v3 v4v4 v5v5

OpenGL: More Examples Example: GL supports quadrilaterals: glBegin(GL_QUADS); glVertex3f(-1, 1, 0); glVertex3f(-1, -1, 0); glVertex3f(1, -1, 0); glVertex3f(1, 1, 0); glEnd(); This type of operation is called immediate-mode rendering; each command happens immediatelyThis type of operation is called immediate-mode rendering; each command happens immediately Example: GL supports quadrilaterals: glBegin(GL_QUADS); glVertex3f(-1, 1, 0); glVertex3f(-1, -1, 0); glVertex3f(1, -1, 0); glVertex3f(1, 1, 0); glEnd(); This type of operation is called immediate-mode rendering; each command happens immediatelyThis type of operation is called immediate-mode rendering; each command happens immediately

OpenGL: Front/Back Rendering Each polygon has two sides, front and back OpenGL can render the two differently The ordering of vertices in the list determines which is the front side: When looking at the front side, the vertices go counterclockwiseWhen looking at the front side, the vertices go counterclockwise –This is basically the right-hand rule –Note that this still holds after perspective projection Each polygon has two sides, front and back OpenGL can render the two differently The ordering of vertices in the list determines which is the front side: When looking at the front side, the vertices go counterclockwiseWhen looking at the front side, the vertices go counterclockwise –This is basically the right-hand rule –Note that this still holds after perspective projection

OpenGL: Drawing Triangles You can draw multiple triangles between glBegin(GL_TRIANGLES) and glEnd() : float v1[3], v2[3], v3[3], v4[3];...glBegin(GL_TRIANGLES); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v1); glVertex3fv(v3); glVertex3fv(v4); glEnd(); Each set of 3 vertices forms a triangle What do the triangles drawn above look like?What do the triangles drawn above look like? How much redundant computation is happening?How much redundant computation is happening? You can draw multiple triangles between glBegin(GL_TRIANGLES) and glEnd() : float v1[3], v2[3], v3[3], v4[3];...glBegin(GL_TRIANGLES); glVertex3fv(v1); glVertex3fv(v2); glVertex3fv(v3); glVertex3fv(v1); glVertex3fv(v3); glVertex3fv(v4); glEnd(); Each set of 3 vertices forms a triangle What do the triangles drawn above look like?What do the triangles drawn above look like? How much redundant computation is happening?How much redundant computation is happening?

OpenGL: Triangle Strips An OpenGL triangle strip primitive reduces this redundancy by sharing vertices: glBegin(GL_TRIANGLE_STRIP);glVertex3fv(v0);glVertex3fv(v1);glVertex3fv(v2);glVertex3fv(v3);glVertex3fv(v4);glVertex3fv(v5);glEnd(); triangle 0 is v0, v1, v2triangle 0 is v0, v1, v2 triangle 1 is v2, v1, v3 (why not v1, v2, v3?)triangle 1 is v2, v1, v3 (why not v1, v2, v3?) triangle 2 is v2, v3, v4triangle 2 is v2, v3, v4 triangle 3 is v4, v3, v5 (again, not v3, v4, v5)triangle 3 is v4, v3, v5 (again, not v3, v4, v5) An OpenGL triangle strip primitive reduces this redundancy by sharing vertices: glBegin(GL_TRIANGLE_STRIP);glVertex3fv(v0);glVertex3fv(v1);glVertex3fv(v2);glVertex3fv(v3);glVertex3fv(v4);glVertex3fv(v5);glEnd(); triangle 0 is v0, v1, v2triangle 0 is v0, v1, v2 triangle 1 is v2, v1, v3 (why not v1, v2, v3?)triangle 1 is v2, v1, v3 (why not v1, v2, v3?) triangle 2 is v2, v3, v4triangle 2 is v2, v3, v4 triangle 3 is v4, v3, v5 (again, not v3, v4, v5)triangle 3 is v4, v3, v5 (again, not v3, v4, v5) v0v0 v2v2 v1v1 v3v3 v4v4 v5v5

Double Buffering Avoids displaying partially rendered frame buffer OpenGL generates one raster image while another raster image is displayed on monitor glxSwapBuffers (Display *dpy, Window, w) glutSwapBuffers (void) Avoids displaying partially rendered frame buffer OpenGL generates one raster image while another raster image is displayed on monitor glxSwapBuffers (Display *dpy, Window, w) glutSwapBuffers (void)