#4: OpenGL Implementation & Project 2 CSE167: Computer Graphics TAs: Alex Kozlowski & Cameron Chrisman UCSD, Winter 2006.

Slides:



Advertisements
Similar presentations
OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
Advertisements

CS 4731 Lecture 2: Intro to 2D, 3D, OpenGL and GLUT (Part I) Emmanuel Agu.
Lab 1: OpenGL Tutorial CS 282. What’s the plan for today? Go over our framework code. Learn some basic OpenGL! Reveal Lab 1 Answer questions.
What is OpenGL? Low level 2D and 3D Graphics Library Competitor to Direct3D (the rendering part of DirectX) Used in: CAD, virtual reality, scientific.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
CSC 461: Lecture 51 CSC461 Lecture 5: Simple OpenGL Program Objectives: Discuss a simple program Discuss a simple program Introduce the OpenGL program.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
OpenGL and Projections
InteractionHofstra University1 Graphics Programming Input and Interaction.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2008 Tamara Munzner OpenGL, GLUT, Transformations.
CENG477 Introduction to Computer Graphics Introduction to OpenGL, GLUT and GLUI.
Computer Graphics CS 385 February 7, Fundamentals of OpenGl and Glut Today we will go through the basics of a minimal OpenGl Glut project, explaining.
Linear Algebra Review CSE169: Computer Animation Instructor: Steve Rotenberg UCSD, Winter 2005.
OpenGL A Brief Overview. What is OpenGL?  It is NOT a programming language.  It is a Graphics Rendering API consisting of a set of function with a well.
Programming with OpenGL and GLUT
Reference1. [OpenGL course slides by Rasmus Stenholt]
CS380 LAB I OpenGL Donghyuk Kim Reference1. [OpenGL course slides by Rasmus Stenholt] Reference2. [
1 GLUT Callback functions Event-driven: Programs that use windows  Input/Output  Wait until an event happens and then execute some pre-defined functions.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2010 Tamara Munzner GLUT, Transformations.
CSE 470: Computer Graphics. 10/15/ Defining a Vertex A 2D vertex: glVertex2f(GLfloat x, GLfloat y); 2D vertexfloating pointopenGL parameter type.
Lecture 3 OpenGL.
 “OpenGL (Open Graphics Library) is a standard specification defining a cross- language cross-platform API for writing applications that produce 2D and.
1. OpenGL/GLU/GLUT  OpenGL v4.0 (latest) is the “core” library that is platform independent  GLUT v3.7 is an auxiliary library that handles window creation,
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
Introduction to GL Geb Thomas. Example Code int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
Interactive Computer Graphics CS 418 MP1: Dancing I TA: Zhicheng Yan Sushma S Kini Mary Pietrowicz Slides Taken from: “An Interactive Introduction to OpenGL.
Modeling with OpenGL Practice with OpenGL transformations.
Computing & Information Sciences Kansas State University CIS 536/636 Introduction to Computer Graphics Lecture 4 of 41 William H. Hsu Department of Computing.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
CGGM Lab. Tan-Chi Ho 2001 Viewing and Transformation.
GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window.
Chap 3 Viewing and Transformation
CSCE 441: Computer Graphics
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
CS559: Computer Graphics Lecture 12: OpenGL - Transformation Li Zhang Spring 2008.
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
CIS 681 Review: OpenGL. CIS 681 Command Syntax OpenGL commands start with a gl. This is followed by the base command such as Color. Followed by the number.
Introduction to Graphics Programming. Graphics API.
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. [
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Computer Graphics -practical- Lecture 6. (visual c++) open gl library To use open GL with VC++ we add these files:- 1)Glut.h C:\program files\ Microsoft.
Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran
The Human Visual System vs The Pinhole camera
CS380 Lab Spring Myungbae Son.
Reference1. [OpenGL course slides by Rasmus Stenholt]
“Computer Science is no more about computers than astronomy is about telescopes.” Professor Edsger Dijkstra.
Programming with OpenGL Part 2: Complete Programs
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
OpenGL A Brief Overview.
Lab 3 Geometric Drawing Lab 3 Geometric Drawing.
Starting to draw dealing with Windows which libraries? clipping
גרפיקה ממוחשבת: מבוא ל-OpenGL
Rendering Pipeline, OpenGL/GLUT
Drawing in the plane 455.
OpenGL program.
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Starting to draw dealing with Windows which libraries? clipping
OpenGL, GLUT, Transformations I Week 2, Wed Jan 16
Programming with OpenGL Part 2: Complete Programs
OpenGL A Brief Overview.
Presentation transcript:

#4: OpenGL Implementation & Project 2 CSE167: Computer Graphics TAs: Alex Kozlowski & Cameron Chrisman UCSD, Winter 2006

1 How was Project 1? Too Hard? Too Easy? Not enough time? Not enough explanation? Goal of Project 1: Get comfortable with Matrices, Vectors, Points, and Order of Operations

2 Outline for Today 1. What is OpenGL? 2. Program Layout and Common Commands 3. Stack Operations 4. C++, Vectors, Matrices 5. Project 2

3 What is OpenGL? A low-level graphics library specification Provides a small set of geometric primitives: points, lines, polygons, images, and bitmaps. Support for 2 and 3 dimensions Includes commands that control how these objects are rendered into the frame buffer

4 What is GLUT? Like OpenGL, it’s a library A windowing API for OpenGL Easy for the user to handle window events When to redraw the window Mouse Clicks Keyboard Commands Resizing the window Cross platform

5 Outline for Today 1. What is OpenGL? 2. Program Layout and Common Commands 3. Stack Operations 4. C++, Vectors, Matrices 5. Project 2

6 Typical Graphics Program int main( int argc, char** argv ) { // Initialize glut glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH ); // Window position (from top corner), and size (width and height) glutInitWindowPosition( 20, 60 ); glutInitWindowSize( 360, 360 ); glutCreateWindow( "CSE167 Matrix Project" ); // Setup the OpenGL features we'd like to use initRendering(); // Set up callback functions for key presses glutKeyboardFunc( myKeyboardFunc ); // Handles "normal" ascii symbols // Set up the callback function for resizing windows glutReshapeFunc( resizeWindow ); // call this whenever window needs redrawing glutDisplayFunc( drawScene ); // Start the main loop. glutMainLoop never returns. glutMainLoop(); return(0); // This line is never reached. }

7 Typical Graphics Program void drawScene(void) { // This command clears the screen to the glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Setup our projection matrix glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60,g_Aspect,.1,80); // Switch to ModelView glMatrixMode(GL_MODELVIEW); // Camera transformations would typically go here // Draw stuff… // Swap the buffers glutSwapBuffers(); // Tell glut to immediately redraw the scene for the next frame glutPostRedisplay(); }

8 Drawing Primitives in OpenGL Starts with glBegin(primitive_constant) and ends with glEnd() 10 Primitive Types GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, and GL_POLYGON We’ll use GL_TRIANGLES most often in this class

9 Drawing Primitives in OpenGL // 2 Triangles glBegin(GL_TRIANGLES); glColor3f(1,0,0); glVertex3f( 1, 1,0); glVertex3f(-1, 1,0); glVertex3f(-1,-1,0); glVertex3f( 1,-1,0); glVertex3f( 1, 1,0); glEnd(); // 1 Quad glBegin(GL_QUADS); glColor3f(1,0,0); glVertex3f( 1, 1,0); glVertex3f(-1, 1,0); glVertex3f(-1,-1,0); glVertex3f( 1,-1,0); glEnd();

10 Outline for Today 1. What is OpenGL? 2. Program Layout and Common Commands 3. Stack Operations 4. C++, Vectors, Matrices 5. Project 2

11 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix()

12 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix() I

13 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix() I I I I

14 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix() I*T(2,1) I

15 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix() I*T(2,1) I

16 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix() I*T(2,1) I

17 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix() I*T(2,1)* T(1,1) I*T(2,1) I

18 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix() I*T(2,1)* T(1,1)*R(60) I*T(2,1) I

19 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix() I*T(2,1)* T(1,1)*R(60) I*T(2,1) I

20 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix() I*T(2,1)* T(1,1)*R(60) I*T(2,1) I

21 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix() I*T(2,1) I

22 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix() I*T(2,1)* T(-1,0)*R(45) I*T(2,1) I

23 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix() I*T(2,1)* T(-1,0)*R(45) I*T(2,1) I

24 Books on a Table LoadIdentity() PushMatrix() Translate(2,1) drawTable() PushMatrix() Translate(1,1) RotateZ(60) drawBook() PopMatrix() PushMatrix() Translate(-1,0) RotateZ(-45) drawBook() PopMatrix() I*T(2,1)* T(-1,0)*R(45) I*T(2,1) I

25 Outline for Today 1. What is OpenGL? 2. Program Layout and Common Commands 3. Stack Operations 4. C++, Vectors, Matrices 5. Project 2

26 C++ What’s wrong with the following code? int *Differences(const int aSamples[], int arraySize) { int tempArray[arraySize]; for(int i = 0; i < arraySize-1; i++){ tempArray[i] = aSamples[i]-aSamples[i+1]; } return tempArray; }

27 C++ What’s wrong with the following code? Fixed. int *Differences(const int aSamples[], int arraySize) { int tempArray = new int[arraySize]; for(int i = 0; i < arraySize-1; i++){ tempArray[i] = aSamples[i]-aSamples[i+1]; } return tempArray; }

28 C++ Code runs fine at first, but after a while the computer seems to slow. Why? int main() { while(true) { int array = new int[100]; array[0] = 0; array[1] = 1; for(int j = 2; j < 100; j++) { array[j] = array[j-1] + array[j-2]; }

29 C++ Code runs fine at first, but after a while the computer seems to slow. Why? Fixed. int main() { int array = new int[100]; while(true) { array[0] = 0; array[1] = 1; for(int j = 2; j < 100; j++) { array[j] = array[j-1] + array[j-2]; }

30 LocalToWorld Matrix Assume a spaceship was modeled in local space pointing down the –z axis. It’s placed in the world using the following matrix: What world space direction is the spaceship facing? What is the world space position of the spaceship? The pilot of the ship looks up and sees an enemy ship approaching. What world space direction is he looking?

31 Angle Between Vectors How do you find the angle θ between vectors a and b? a b θ

32 Angle Between Vectors a b θ

33 Target ‘Lock On’ For an airplane to get a missile locked on, the target must be within a 10 degree cone in front of the plane. If the plane’s matrix is M and the target position is t, find an expression that determines if the plane can get a lock on. M t Slide taken from Steve Rotenberg, CSE167 F2005

34 Target ‘Lock On’ For an airplane to get a missile locked on, the target must be within a 10 degree cone in front of the plane. If the plane’s matrix is M and the target position is t, find an expression that determines if the plane can get a lock on. a b c d t Slide taken from Steve Rotenberg, CSE167 F2005

35 Target ‘Lock On’ We want to check the angle between the heading vector (-c) and the vector from d to t: We can speed that up by comparing the cosine instead ( cos(10°)=.985 ) Slide taken from Steve Rotenberg, CSE167 F2005

36 Example: Target ‘Lock On’ We can even speed that up further by removing the division and the square root in the magnitude computation: All together, this requires 8 multiplications and 8 adds Slide taken from Steve Rotenberg, CSE167 F2005

37 Alignment to Target An object is at position p with a unit length heading of h. We want to rotate it so that the heading is facing some target t. Find a unit axis a and an angle θ to rotate around. p h t Slide taken from Steve Rotenberg, CSE167 F2005

38 Alignment to Target p h t t-p θ a Slide taken from Steve Rotenberg, CSE167 F2005

39 Outline for Today 1. What is OpenGL? 2. Program Layout and Common Commands 3. Stack Operations 4. C++, Vectors, Matrices 5. Project 2

40 LocalToWorld Matrix Implement a stack Redo your solar system to use the stack Position the Hubble so that it always faces the earth Find out whether the sun’s center is within X degrees of the telescope lens