University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2008 Tamara Munzner OpenGL, GLUT, Transformations.

Slides:



Advertisements
Similar presentations
Department of nskinfo i-education
Advertisements

OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
Computer Graphics CSCE 441
Geometric Transformations
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2007 Tamara Munzner Transformations I Week.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2005 Tamara Munzner Transformations Week 2,
Transformations II Week 2, Wed Jan 17
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2008 Tamara Munzner Math Review Rendering.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2005 Tamara Munzner Transformations II Week.
Image Warping : Computational Photography Alexei Efros, CMU, Fall 2006 Some slides from Steve Seitz
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
Image Warping : Computational Photography Alexei Efros, CMU, Fall 2008 Some slides from Steve Seitz
Image Warping : Computational Photography Alexei Efros, CMU, Fall 2005 Some slides from Steve Seitz
#4: OpenGL Implementation & Project 2 CSE167: Computer Graphics TAs: Alex Kozlowski & Cameron Chrisman UCSD, Winter 2006.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
University of British Columbia © MIchiel van de Panne Geometric Transformations and quick OpenGL intro.
OpenGL and Projections
Computer Graphics (Fall 2005) COMS 4160, Lecture 10: OpenGL 1
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2005 Tamara Munzner Rendering Pipeline OpenGL/GLUT.
Introduction to OpenGL Jian Huang This set of slides are extracted from the Interactive OpenGL Programming course given by Dave Shreine, Ed Angel and Vicki.
Computer Graphics Bing-Yu Chen National Taiwan University.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
OpenGL Matrices and Transformations Angel, Chapter 3 slides from AW, Red Book, etc. CSCI 6360.
Using OpenGL in Visual C++ Opengl32.dll and glu32.dll should be in the system folder Opengl32.dll and glu32.dll should be in the system folder Opengl32.lib.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2010 Tamara Munzner GLUT, Transformations.
Lecture 3 OpenGL.
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
Modeling with OpenGL Practice with OpenGL transformations.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
Transformations
GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2007 Tamara Munzner OpenGL/GLUT Intro Week.
CS552: Computer Graphics Lecture 6: Viewing in 2D.
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
Recap from Monday DCT and JPEG Point Processing Histogram Normalization Questions: JPEG compression levels Gamma correction.
Introduction to Graphics Programming. Graphics API.
Introduction to Graphics Programming. Graphics: Conceptual Model Real Object Human Eye Display Device Graphics System Synthetic Model Synthetic Camera.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
OpenGL CS418 Computer Graphics John C. Hart. OpenGL: Event-driven How in OpenGL? Programmer registers callback functions Callback function called when.
CS380 LAB II OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
Image Warping 2D Geometric Transformations
Transformations. Modeling Transformations  Specify transformations for objects  Allows definitions of objects in own coordinate systems  Allows use.
INTRODUCTION TO OPENGL
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2008 Tamara Munzner Viewing/Projections I.
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Transformations University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2016 Tamara Munzner.
Final Review I University of British Columbia
The Human Visual System vs The Pinhole camera
Computer Graphics Lecture 33
Introduction to Computer Graphics CS 445 / 645
Reference1. [OpenGL course slides by Rasmus Stenholt]
Programming with OpenGL Part 2: Complete Programs
Image Warping (Szeliski Sec 2.1.2)
Image Warping (Szeliski Sec 2.1.2)
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
Image Warping : Computational Photography
Transformations 3 University of British Columbia
Rendering Pipeline, OpenGL/GLUT
Image Warping : Computational Photography
Rendering Pipeline Week 2, Mon Jan 11
Image Warping (Szeliski Sec 2.1.2)
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
OpenGL, GLUT, Transformations I Week 2, Wed Jan 16
Transformations 2 University of British Columbia
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2008 Tamara Munzner OpenGL, GLUT, Transformations I Week 2, Wed Jan 16

2 Review: Rendering Pipeline Geometry Database Geometry Database Model/View Transform. Model/View Transform. Lighting Perspective Transform. Perspective Transform. Clipping Scan Conversion Scan Conversion Depth Test Depth Test Texturing Blending Frame- buffer Frame- buffer

3 OpenGL (briefly)

4 OpenGL API to graphics hardware based on IRIS_GL by SGI designed to exploit hardware optimized for display and manipulation of 3D graphics implemented on many different platforms low level, powerful flexible pipeline processing set state as needed

5 Graphics State set the state once, remains until overwritten glColor3f(1.0, 1.0, 0.0)  set color to yellow glSetClearColor(0.0, 0.0, 0.2)  dark blue bg glEnable(LIGHT0)  turn on light glEnable(GL_DEPTH_TEST)  hidden surf.

6 Geometry Pipeline tell it how to interpret geometry glBegin( ) mode = GL_TRIANGLE, GL_POLYGON, etc. feed it vertices glVertex3f(-1.0, 0.0, -1.0) glVertex3f(1.0, 0.0, -1.0) glVertex3f(0.0, 1.0, -1.0) tell it you’re done glEnd()

7 Open GL: Geometric Primitives glPointSize( float size); glLineWidth( float width); glColor3f( float r, float g, float b);....

8 Code Sample void display() { glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(0.0, 1.0, 0.0); glBegin(GL_POLYGON); glVertex3f(0.25, 0.25, -0.5); glVertex3f(0.75, 0.25, -0.5); glVertex3f(0.75, 0.75, -0.5); glVertex3f(0.25, 0.75, -0.5); glEnd(); glFlush(); } more OpenGL as course continues

9 GLUT

10 GLUT: OpenGL Utility Toolkit developed by Mark Kilgard (also from SGI) simple, portable window manager opening windows handling graphics contexts handling input with callbacks keyboard, mouse, window reshape events timing idle processing, idle events designed for small/medium size applications distributed as binaries free, but not open source

11 Event-Driven Programming main loop not under your control vs. batch mode where you control the flow control flow through event callbacks redraw the window now key was pressed mouse moved callback functions called from main loop when events occur mouse/keyboard state setting vs. redrawing

12 GLUT Callback Functions // you supply these kind of functions // you supply these kind of functions void reshape(int w, int h); void keyboard(unsigned char key, int x, int y); void mouse(int but, int state, int x, int y); void idle(); void display(); // register them with glut // register them with glut glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMouseFunc(mouse); glutIdleFunc(idle); glutDisplayFunc(display); void glutDisplayFunc (void (*func)(void)); void glutKeyboardFunc (void (*func)(unsigned char key, int x, int y)); void glutIdleFunc (void (*func)()); void glutReshapeFunc (void (*func)(int width, int height));

13 GLUT Example 1 #include void display() { glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); glColor4f(0,1,0,1); glBegin(GL_POLYGON); glVertex3f(0.25, 0.25, -0.5); glVertex3f(0.75, 0.25, -0.5); glVertex3f(0.75, 0.75, -0.5); glVertex3f(0.25, 0.75, -0.5); glEnd(); glutSwapBuffers(); } int main(int argc,char**argv) { glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(640,480); glutCreateWindow("glut1"); glutDisplayFunc( display ); glutMainLoop(); return 0; // never reached }

14 GLUT Example 2 #include void display() { glRotatef(0.1, 0,0,1); glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); glColor4f(0,1,0,1); glBegin(GL_POLYGON); glVertex3f(0.25, 0.25, -0.5); glVertex3f(0.75, 0.25, -0.5); glVertex3f(0.75, 0.75, -0.5); glVertex3f(0.25, 0.75, -0.5); glEnd(); glutSwapBuffers(); } int main(int argc,char**argv) { glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(640,480); glutCreateWindow("glut2"); glutDisplayFunc( display ); glutMainLoop(); return 0; // never reached }

15 Redrawing Display display only redrawn by explicit request glutPostRedisplay() function default window resize callback does this idle called from main loop when no user input good place to request redraw will call display next time through event loop should return control to main loop quickly continues to rotate even when no user action

16 GLUT Example 3 #include void display() { glRotatef(0.1, 0,0,1); glClearColor(0,0,0,1); glClear(GL_COLOR_BUFFER_BIT); glColor4f(0,1,0,1); glBegin(GL_POLYGON); glVertex3f(0.25, 0.25, -0.5); glVertex3f(0.75, 0.25, -0.5); glVertex3f(0.75, 0.75, -0.5); glVertex3f(0.25, 0.75, -0.5); glEnd(); glutSwapBuffers(); } void idle() { glutPostRedisplay(); } int main(int argc,char**argv) { glutInit( &argc, argv ); glutInitDisplayMode( GLUT_RGB|GLUT_DOUBLE); glutInitWindowSize(640,480); glutCreateWindow("glut1"); glutDisplayFunc( display ); glutIdleFunc( idle ); glutMainLoop(); return 0; // never reached }

17 Keyboard/Mouse Callbacks again, do minimal work consider keypress that triggers animation do not have loop calling display in callback! what if user hits another key during animation? instead, use shared/global variables to keep track of state yes, OK to use globals for this! then display function just uses current variable value

18 GLUT Example 4 #include bool animToggle = true; float angle = 0.1; void display() { glRotatef(angle, 0,0,1);... } void idle() { glutPostRedisplay(); } int main(int argc,char**argv) {... glutKeyboardFunc( doKey );... } void doKey(unsigned char key, int x, int y) { if ('t' == key) { animToggle = !animToggle; if (!animToggle) glutIdleFunc(NULL); else glutIdleFunc(idle); } else if ('r' == key) { angle = -angle; } glutPostRedisplay(); }

19 Transformations

20 Transformations transforming an object = transforming all its points transforming a polygon = transforming its vertices

21 Matrix Representation represent 2D transformation with matrix multiply matrix by column vector apply transformation to point transformations combined by multiplication matrices are efficient, convenient way to represent sequence of transformations!

22 Scaling scaling a coordinate means multiplying each of its components by a scalar uniform scaling means this scalar is the same for all components:  2 2

23 non-uniform scaling: different scalars per component: how can we represent this in matrix form? Scaling X  2, Y  0.5

24 Scaling scaling operation: or, in matrix form: scaling matrix

25 2D Rotation  (x, y) (x', y') x ' = x cos(  ) - y sin(  ) y ' = x sin(  ) + y cos(  ) counterclockwise RHS

26 2D Rotation From Trig Identities x = r cos (  ) y = r sin (  ) x' = r cos (  +  ) y' = r sin (  +  ) Trig Identity… x' = r cos(  ) cos(  ) – r sin(  ) sin(  ) y' = r sin(  ) cos(  ) + r cos(  ) sin(  ) Substitute… x' = x cos(  ) - y sin(  ) y' = x sin(  ) + y cos(  )  (x, y) (x', y') 

27 2D Rotation Matrix easy to capture in matrix form: even though sin(q) and cos(q) are nonlinear functions of q, x ' is a linear combination of x and y y ' is a linear combination of x and y

28 2D Rotation: Another Derivation (x ',y ' )  (x,y)

29 2D Rotation: Another Derivation (x ',y ' ) (x,y)  x y

30 2D Rotation: Another Derivation (x ',y ' ) (x,y)  x y  x y

31 2D Rotation: Another Derivation x ' B A (x ',y ' ) (x,y)

32 2D Rotation: Another Derivation x x ' B A (x ',y ' ) (x,y)  x

33 2D Rotation: Another Derivation x B A y y   (x ',y ' ) (x,y)  x ' 

34 Shear shear along x axis push points to right in proportion to height x y x y

35 Shear shear along x axis push points to right in proportion to height x y x y

36 Reflection x x reflect across x axis mirror

37 Reflection reflect across x axis mirror x x

38 2D Translation (x,y) (x’,y’)

39 2D Translation (x,y) (x’,y’) scaling matrixrotation matrix

40 2D Translation (x,y) (x’,y’) scaling matrixrotation matrix vector addition matrix multiplication

41 2D Translation (x,y) (x’,y’) scaling matrixrotation matrix translation multiplication matrix?? vector addition matrix multiplication

42 Linear Transformations linear transformations are combinations of shear scale rotate reflect properties of linear transformations satisifes T(sx+ty) = s T(x) + t T(y) origin maps to origin lines map to lines parallel lines remain parallel ratios are preserved closed under composition

43 Challenge matrix multiplication for everything except translation how to do everything with multiplication? then just do composition, no special cases homogeneous coordinates trick represent 2D coordinates (x,y) with 3-vector (x,y,1)

44 Homogeneous Coordinates our 2D transformation matrices are now 3x3: use rightmost column

45 Homogeneous Coordinates Geometrically point in 2D cartesian x y

46 Homogeneous Coordinates Geometrically point in 2D cartesian + weight w = point P in 3D homog. coords multiples of (x,y,w) form a line L in 3D all homogeneous points on L represent same 2D cartesian point example: (2,2,1) = (4,4,2) = (1,1,0.5) homogeneouscartesian / w x y w w=1

47 Homogeneous Coordinates Geometrically homogenize to convert homog. 3D point to cartesian 2D point: divide by w to get (x/w, y/w, 1) projects line to point onto w=1 plane like normalizing, one dimension up when w=0, consider it as direction points at infinity these points cannot be homogenized lies on x-y plane (0,0,0) is undefined homogeneouscartesian / w x y w w=1

48 Affine Transformations affine transforms are combinations of linear transformations translations properties of affine transformations origin does not necessarily map to origin lines map to lines parallel lines remain parallel ratios are preserved closed under composition

49 Homogeneous Coordinates Summary may seem unintuitive, but they make graphics operations much easier allow all affine transformations to be expressed through matrix multiplication we’ll see even more later... use 3x3 matrices for 2D transformations use 4x4 matrices for 3D transformations