370 likes | 494 Views
CGMB214: Intro. To Computer Graphics. Chapter 2 Overview of Graphics System Part II. Image from http://www.desktopwallpapers.in. What you need to know. To be able to write a simple openGL program in C++. Introduction. A basic library of functions is provided in OpenGL
E N D
CGMB214: Intro. To Computer Graphics Chapter 2 Overview of Graphics System Part II Image from http://www.desktopwallpapers.in
What you need to know • To be able 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
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 • Prefixed with “gl” • Each component word within a function name has its first letter capitalized • E.g. glBegin, glClear, glEnd
Basic OpenGL Syntax (cont…) • Certain function require that one or more of their arguments be assigned a symbolic constant specifying i.e. • A parameter name • A value for parameter • Particular mode • All such constant begin with uppercase letters GL follows by ( _ ) then the constant name • E.g. GL_2D, GL_RGB, GL_POLYGON
Basic OpenGL Syntax (cont…) • OpenGL functions also expect specific data type • i.e. OpenGL function parameter might expect a value that is specified as a 32-bit integer but the size of an integer specification can be different on different machine • To indicate a specific data type OpenGL uses special built-in data-type names • E.g. GLbyte, GLshort, GLint, GLdouble, etc
Related Libraries • Associated libraries for handling special operations • OpenGL Utility (GLU) – function names start with glu prefix • Open Inventor – object oriented toolkit written in C++ • OpenGL Extension to the X Window System (GLX) – provides a set of routines that are prefixed with the letter glx • 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 (prefix 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 <windows.h> # include <GL/gl.h> # include <GL/glu.h>
Header Files (cont…) • 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 <GL/glut.h>
Header Files (cont…) • Other than that we also need to include the header files required by C/C++ • E.g. # include <stdio.h> # include <stdlib.h> # include <math.h>
Display-Window Management Using GLUT • We begin with the basic elements of how to create a window. OpenGL was intentionally designed to be independent of any specific window system. As a result, a number of the basic window operations are not provided in OpenGL. • Therefore, a separate library called GLUT or OpenGL Utility Toolkit was created to provide these functions. • Basically, GLUT provides the necessary tools for requesting windows to be created and providing interaction with I/O devices
The main() Function int main(int argc, char** argv) // program arguments { glutInit(&argc, argv); // initialize glut and gl // double buffering and RGB glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize(400, 300); // initial window size glutInitWindowPosition(0, 0); // initial window position glutCreateWindow(argv[0]); // create window // ...initialize callbacks here (described below)... myInit(); // your own initializations glutMainLoop(); // turn control over to glut return 0; // (make the compiler happy) }
Explanations • glutInit() • The arguments given to the main program (argc and argv) are the command-line arguments supplied to the program.This assumes a typical Unix environment, in which the program is invoked from a command line. These info will be passed into the main initialization procedure, glutInit(). • This procedure must be called before any others. It processes (and removes) command-line arguments that may be of interest to GLUT and the window system and does general initialization of GLUT and OpenGL.
Explanations (cont…) • glutInitDisplayMode() • This function performs initializations informing OpenGL how to set up its frame buffer. • 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. • For example, this include depth information for hidden surface removal. • 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, which are given in Table 1.
Explanations (cont…) • Color • In OpenGL, we need to specify the color representation that we want to use. • From the table, there are three methods, of which two are fairly commonly used: GLUT_RGB or GLUT_RGBA • The first uses standard RGB colors (24-bit color, consisting of 8 bits of red, green and blue) and is the default.
Explanations (cont…) • The second method request RGBA coloring. • The forth component indicates the opaqueness of the color. • (1 = fully opaque, 0 = fully transparent)
Explanations (cont…) • Single or Double Buffering • GLUT_SINGLE and GLUT_DOUBLE specify whether we want to use single or double buffering respectively • 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.
Explanations (cont…) • 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 • Double buffering is a method to eliminate this flicker. • Double buffering: • two separates frame buffers front buffer and back buffer. • front buffer for displaying back buffer for drawing • swapping to update the image
Explanations (cont…) • 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. • In OpenGL, we use GLUT_DEPTH for this purpose
Explanations (cont…) • glutInitWindowSize(): • This command specifies the desired width and height of the graphics window. The general form is: glutInitWindowSize(int width, int height) • The values are given in numbers of pixels.
Explanations (cont…) • glutInitPosition(): • This command specifies the location of the upper left corner of the graphics window. The form is glutInitWindowPosition(int x, int y) • where the (x, y) coordinates are given relative to the upper left corner of the display. • Thus, the arguments (0,0) places the window in the upper left corner of the display.
Explanations (cont…) • glutCreateWindow(): • This command actually creates the graphics window. The general form of the command is glutCreateWindowchar(*title) • where title is a character string. Each window has a title, and the argument is a string which specifies the window’s title.
Event-Driven Programming & Callbacks 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 Table: Common callbacks and the associated registration functions
Callback Setup int main(intargc, 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 } }