TOPIC 3 INTRODUCTION TO OPENGL CGMB214: Introduction to Computer Graphics.

Slides:



Advertisements
Similar presentations
OpenGL Open a Win32 Console Application in Microsoft Visual C++.
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…
Chapter 2: Graphics Programming
Computer Graphics CSCE 441
Pemrograman OpenGL Dasar
1 Computer Graphics Chapter 2 Input Devices. RM[2]-2 Input Devices Logical Input Devices  Categorized based on functional characteristics.  Each device.
CS 4731 Lecture 2: Intro to 2D, 3D, OpenGL and GLUT (Part I) Emmanuel Agu.
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
OpenGL (I). What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D 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,
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 GLUT Callback Functions.
Based on slides created by Edward Angel
CSC461 Lecture 9: GLUT Callbacks Objectives Introduce double buffering for smooth animations Programming event input with GLUT.
Informationsteknologi Tuesday, November 6, 2007Computer Graphics - Class 41 Today’s class Input and interaction.
Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003.
Chi-Cheng Lin, Winona State University CS430 Computer Graphics Graphics Programming and OpenGL.
Introduction to OpenGL and GLUT GLUT. What is OpenGL? An application programming interface (API) A (low-level) Graphics rendering API Generate high-quality.
1 Working with Callbacks Yuanfeng Zhou Shandong University.
WORKING WITH CALLBACKS Ed Angel Professor of Computer Science, Electrical and Computer Engineering, and Media Arts University of New Mexico Angel: Interactive.
Lecture 5: Interaction 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
1Computer Graphics Input and Interaction Lecture 8 John Shearer Culture Lab – space 2
Lecture 3 OpenGL.
1 Input and Interaction. 2 Input Devices Physical input devices Keyboard devices and pointing devices Logical input devices.
Computer Graphics I, Fall 2010 Input and Interaction.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 1: Background Ed Angel Professor of Computer Science, Electrical.
 “OpenGL (Open Graphics Library) is a standard specification defining a cross- language cross-platform API for writing applications that produce 2D and.
1 Figures are extracted from Angel's book (ISBN x) The Human Visual System vs The Pinhole camera Human Visual System Visible Spectrum Pinhole.
CS 480/680 Computer Graphics Programming with Open GL Part 7: Input and Interaction Dr. Frederick C Harris, Jr. Fall 2011.
Interactive Computer Graphics CS 418 MP1: Dancing I TA: Zhicheng Yan Sushma S Kini Mary Pietrowicz Slides Taken from: “An Interactive Introduction to OpenGL.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Programming with OpenGL Review.
Introduction to OpenGL and GLUT. What’s OpenGL? An Application Programming Interface (API) A low-level graphics programming API – Contains over 250 functions.
Program 2 due 02/01  Be sure to document your program  program level doc  your name  what the program does  each function  describe the arguments.
1 Input and Interaction. 2 Objectives Introduce the basic input devices ­Physical Devices ­Logical Devices ­Input Modes Event-driven input Introduce double.
Intro to OpenGL (Version 2) Geb Thomas. Setting Up GLUT You will need GLUT for opening windows We can use the version made by Nate Robins: –
Affine Geometry Jehee Lee Seoul National University.
Computer Graphics: Programming, Problem Solving, and Visual Communication Steve Cunningham California State University Stanislaus and Grinnell College.
Computer Graphics I, Fall 2010 Working with Callbacks.
CA 302 Computer Graphics and Visual Programming Lecture 2: Introduction to OpenGL Aydın Öztürk
University of New Mexico
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
NoufNaief.net TA: Nouf Al-harbi.
GLUT functions glutInit allows application to get command line arguments and initializes system gluInitDisplayMode requests properties for the window.
Introduction to OpenGL Programming
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
31/1/2006Based on: Angel (4th Edition) & Akeine-Möller & Haines (2nd Edition)1 CSC345: Advanced Graphics & Virtual Environments Lecture 2: Introduction.
Introduction to OpenGL Muhammad Aamir Khan Lecturer, DCS, UOP.
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.
1 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Working with Callbacks.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Basic Program with OpenGL and GLUT
Introduction to OpenGL (IDE: Eclipse)
Computer Graphics Lecture 33
CSC461 Lecture 8: Input Devices
Programming with OpenGL Part 2: Complete Programs
The User Interface Lecture 2 Mon, Aug 27, 2007.
Programming with OpenGL Part 2: Complete Programs
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Introduction to OpenGL
Input and Interaction Ed Angel
University of New Mexico
Input and Interaction Ed Angel
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

TOPIC 3 INTRODUCTION TO OPENGL CGMB214: Introduction to Computer Graphics

Objectives To demonstrate the basic usage of OpenGL To write a simple OpenGL program in C

Introduction A basic library of functions is provided in OpenGL  It helps to specify  Graphics primitives  Attributes  Geometric transformation  Viewing transformation  Etc OpenGL is designed to be hardware independent, therefore many operations, such as input and output routines are not included in the basic library

Basic OpenGL Syntax

Function names in the OpenGL basic library are prefixed with gl Each component word within a function name has its first letter capitalised. E.g. glBegin, glClear, glCopyPixels, glPolygonMode

Basic OpenGL Syntax Arguments, for example parameter name, a value for a parameter or a particular mode begin with uppercase letter GL. The component name comprise of uppercase letter and underscore. E.g. GL_2D, GL_RGB, GL_CCW, GL_POLYGON, GL_AMBIENT_AND_DIFFUSE

Basic OpenGL Syntax Data type in OpenGL starts with capital letters and the remainder is written in lower case. E.g. GLbyte, GLshort, GLint, GLfloat, GLdouble, GLboolean

Related Libraries

OpenGL Utility (GLU)  Provide routines for  Setting up viewing and projection matrices  Describing complex object with lines and polygon approximations  Displaying quadrics and B-splines using linear approximations  Processing the surface rendering operations  Function names start with glu prefix Open Inventor  Object oriented toolkit written in C++ for interactive 3D applications OpenGL Extension to the X Window System (GLX)  provides a set of routines that are prefixed with the letter glX

Related Libraries Apple GL (AGL)  Interface for window-management operations in Apple systems (prefix agl) Windows-to-OpenGL (WGL)  Prefixed with letter wgl Presentation Manager to OpenGL (PGL)  An interface for IBM OS/2 OpenGL Utility Toolkit (GLUT)  Provides library of functions for interacting with any screen- windowing system  Prefixed with letter glut

Header Files

In our graphic program we will need to include the header file for the OpenGL core library For most application we will also need GLU We will also need to include the header file for window system  The header file that accesses WGL routines is windows.h  This header file must be listed before the OpenGL and GLU header files because it contains macros needed by the Microsoft Windows version of OpenGL libraries  So the code should look like below # include

Header Files However if we use GLUT to handle the window- managing operation we don’t need the gl.h and glu.h This is because GLUT will ensure that these header files will be included correctly So the code should look something like below # include

Header Files Other than that we also need to include the header files required by C/C++ E.g. # include

Display-Window Management Using GLUT

Initialise GLUT glutInit(&argc, argv); Initialising function Can be substitute with command line argument, but as for now, just leave it as it is glutInit will initialize the GLUT library and negotiate a session with the window system. During this process, glutInit may cause the termination of the GLUT program with an error message to the user if GLUT cannot be properly initialized. glutInit must be called before others.

Display-Window Management Using GLUT Initialise OpenGL on how to set up its frame buffer and colour glutInitDisplayMode(GL_SINGLE|GL_RGB); Initialising display mode function Built in constant which are OR-ed together. Can be changed based on table below  Frame buffer is a special 2-dimensional array in main memory where the graphical image is stored. OpenGL maintains an enhanced version of the frame buffer with additional information  The system also needs to know how we are representing colors of our general needs in order to determine the depth (number of bits) to assign for each pixel in the frame buffer  The argument to glutInitDisplayMode() is a logical-or (using the operator “|”) of a number of possible options

Display-Window Management Using GLUT Display ModeMeaning Colour Representation GLUT_RGBUse RGB colour (default). 24-bit colour. GLUT_RGBA Use RGB colour including α (for transparency) The α component indicates the opaqueness of the color (1 = fully opaque, 0 = fully transparent) GLUT_INDEXFor colourmapped colours (not recommended)

Display-Window Management Using GLUT Display ModeMeaning Buffering GLUT_SINGLE Use single buffering(default) GLUT_DOUBLE Use double buffering two separates frame buffers front buffer and back buffer. front buffer for displaying back buffer for drawing swapping to update the image GLUT_DEPTH Use depth buffer Is needed in 3-dimensional graphics for hidden surface removal. Use a special array called depth buffer. It is a 2-dimensional array that stores the distance (or depth) of each pixel from the viewer. This makes it possible to determine which surfaces are closest, thus visible, which are farther and thus hidden To understand the difference between these two options, we need to know how frame buffer works. In raster graphics systems, whatever is written to the frame buffer is immediately transferred to the display. This process is repeated frequently (30 – 60 times a second). To do this a typical approach is to first erase the old contents by setting all the pixels to some background color, i.e. black. After this, the new contents are drawn However, even though these processes might happen very fast, the process of setting the image to black and then redrawing everything produces a noticeable flicker in the image

Display-Window Management Using GLUT Set the initial location of the window glutInitWindowPosition(int x, int y); Initialising Window Position function Setting the position of the window from upper left corner of the screen

Display-Window Management Using GLUT Initialise the size of the window glutInitWindowSize(int width, int height); Initialising window size function Setting the size of the window

Display-Window Management Using GLUT Open and display a window glutCreateWindow(“string”); Function that open and display a window A character string that will appear on the title bar

Display-Window Management Using GLUT Specify what the display window should contain glutDisplayFunc(function name); Function to specify what to be displayed on the window The name of the function that contains the lines to be executed

Display-Window Management Using GLUT Make sure the window continue running. Should be at the last line glutMainLoop(); Function to specify what to be displayed on the window This should be the last function in the program. It will display the initial graphics and puts the program into an infinite loop that checks for input from devices such as mouse. If the program is not an interactive program, this function will make sure the window is being displayed all the time until the user close the window.

Display-Window Management Using GLUT 300 px 400 px

Display-Window Management Using GLUT To set the background color of the window glClearColor(Red, Green, Blue, Opacity); Function to specify the background color of the window The first 3 parameters determine the value of Red, Green and Blue, respectively. The last parameter is for opaqueness

Display-Window Management Using GLUT To set the color of the primitives glColor3i(Red, Green, Blue); Function to specify the background color of the window The 3 parameters determine the value of Red, Green and Blue, respectively. The number 3 is for the number of color component, Red, Green and Blue. The letter ‘i’ is for the data type. ‘i’ is for integer

Display-Window Management Using GLUT To display 2D picture (because 2D is a special case of 3D) glMatrixMode(Glenum mode); Function to specify the current matrix Can be set to either to define camera, perspective, measuring system etc. glOrtho2D(start x, end x, start y, end y); Function to defines the coordinate reference frame Specify the range of x and y

Event-Driven Programming & Callbacks (Function Call) Virtually all interactive graphics programs are event driven. Therefore, a graphics program must be prepared at any time for input from any number of sources, including the mouse, or keyboard, or other graphics devices such as trackballs and joysticks. In OpenGL, this is done through the use of callbacks.

Callbacks The callbacks are used when the graphics program instructs the system to invoke a particular procedure or functions whenever an event of interest occurs, say, the mouse button is clicked. The graphics program indicates its interest, or registers, for various events. This involves telling the window system which event type we are interested in, and passing it the name of a procedure we have written to handle the event.

Types of callback Callbacks are used for two purposes: User input events System events User input events: include things such as mouse clicks, the motion of the mouse (without clicking) that is also called passive motion and keyboard hits. Note: The program is only signaled about events that happen to its window. For example, entering text into another window’s dialogue box will not generate a keyboard event for the program.

Types of callback System event: There are a number of different events that are generated by the system such as display event, reshape event, idle event and timer event. display event : a special event that every OpenGL program must handle. A display event is invoked when the system senses that the contents of the window need to be redisplayed, either because:  the graphics window has completed its initial creation  an obscuring window has moved away, thus revealing all or part of the graphics window  the program explicitly requests redrawing, by calling  glutPostRedisplay() procedure.

Types of callback Reshape event: Happens whenever the window shape is altered.  The callback provides information on the new size of the window. Idle event: Happens every time the system has nothing to do Timer event: Happens when after the waiting period is over

Callbacks Input Event Callback RequestUser callback function prototype (return void) Mouse button glutMouseFuncmyMouse(int a, int s, int x, int y) Mouse motion glutPassiveMotionFuncmyMotion(int x, int y) Keyboard keyglutKeyboardFuncmyKeyboard(unsigned char c, int x, int y) System EventCallback RequestUser callback function prototype (return void) (Re)displayglutDisplayFuncmyDisplay() (Re)size window glutReshapeFuncmyReshape(int w, int h) Timer eventglutTimerFuncmyTimer(int id) Idle eventglutIdleFuncmyIdle() Table: Common callbacks and the associated registration functions

Callback Setup int main(int argc, char** argv) {... glutDisplayFunc(myDraw); // set up the callbacks glutReshapeFunc(myReshape); glutMouseFunc(myMouse); glutKeyboardFunc(myKeyboard); glutTimerFunc(20, myTimeOut, 0);... }

Callback Functions Callback functions depend on the function definition that we create.

Examples of Callback Functions System Event void myDraw() { // called to display window //...insert your drawing code here... } void myReshape(int w, int h) { // called if reshaped windowWidth = w; // save new window size windowHeight = h; //...may need to update the projection... glutPostRedisplay(); // request window redisplay } void myTimeOut(int id) { // called if timer event //...advance the state of animation incrementally... glutPostRedisplay(); // request redisplay glutTimerFunc(20, myTimeOut, 0); // request next timer event }

Callback Function for System Event From the example, both the timer and reshape callback invoke the function glutPostRedisplay(). This function informs OpenGL that the state of the scene has changed and should be redrawn

Example of Callback Functions for User Input Events void myMouse(int b, int s, int x, int y) { switch (b) { // b indicates the button case GLUT_LEFT_BUTTON: if (s == GLUT_DOWN) // button pressed //... else if (s == GLUT_UP) // button released //... break; //... // other button events }

GLUT Parameters GLUT parameter names associated with mouse events

Example of Callback Functions for User Input Events // called if keyboard key hit void myKeyboard(unsigned char c, int x, int y) { switch (c) { // c is the key that is hit case ’q’: // ’q’ means quit exit(0); break; //... // other keyboard events }