



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Computer Graphics involves technology to accept, process, transform and present information in a visual form that also concerns with producing images and animations using a computer. This course teach how to make your own design in computer using OpenGl. This lecture includes: Opengl, Programming, Platform, Microsoft, Input, Initialize, Independent, Message, Function, Glut, Create
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




For writing a program, using OpenGL, for any of the operating systems we can use OpenGL Utility Library named, “glut”. “glut” is used to initialize OpenGL on any platform e.g. Microsoft Windows or Linux etc because it is platform independent. “glut” can create window, get keyboard input and run an event handler or message loop in our graphics application.
All those functions that start with the prefix “gl” are the core OpenGL functions and those which start with “glu” or “glut” are the OpenGL utility library functions.
Let’s write a program that uses “glut” and then uses OpenGL function to create graphics.
#include <GL/glut.h> int main() { glutCreateWindow( "first graphics window" ); }
glutCreateWindow glutCreateWindow creates a top-level window.
Usage int glutCreateWindow(char *name); name: ASCII character string for use as window name.
Description: glutCreateWindow creates a top-level window. The name will be provided to the window system as the window's name. The intent is that the window system will label the window with the name.
Implicitly, the current window is set to the newly created window. Each created window has a unique associated OpenGL context. State changes to a window's associated OpenGL context can be done immediately after the window is created. The display state of a window is initially for the window to be shown. But the window's display state is not actually acted upon until glutMainLoop is entered. This means until glutMainLoop is called, rendering to a created window is ineffective because the window cannot yet be displayed.
The value returned is a unique small integer identifier for the window. The range of allocated identifiers starts at one. This window identifier can be used when calling glutSetWindow.
X Implementation Notes The proper X Inter-Client Communication Conventions Manual (ICCCM) top-level properties are established. The WM_COMMAND property that lists the command line used to invoke the GLUT program is only established for the first window created.
This is the simple program that we have written so far. Now we will use more features of “glut” library.
#include <GL/glut.h> int main() { //Set up the OpenGL rendering context. glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
//Create a window and set its width and height. glutCreateWindow( "Deform" ); glutReshapeWindow( 640, 480 );
//The keyboard function gets called whenever we hit a key. glutKeyboardFunc( keyboard );
//The display function gets called whenever the window //requires an update or when we explicitly call glutDisplayFunc( display );
//The reshape function gets called whenever the window changes //shape. glutReshapeFunc( reshape );
// The idle function gets called when there are no window- // events to process. glutIdleFunc( idle );
//Get the ball rolling! glutMainLoop(); }
In the above program we have used “glut” functions. Let us discuss them in detail.
GlutInitDisplayMode
glutInitDisplayMode sets the initial display mode.
Usage void glutInitDisplayMode(unsigned int mode); mode Display mode, normally the bitwise OR -ing of GLUT display mode bit masks. See values below: GLUT_RGBA Bit mask to select an RGBA mode window. This is the default if neither GLUT_RGBA nor GLUT_INDEX are specified. GLUT_RGB An alias for GLUT_RGBA. GLUT_INDEX
height New height of window in pixels.
Description glutReshapeWindow requests a change in the size of the current window. The width and height parameters are size extents in pixels. The width and height must be positive values.
The requests by glutReshapeWindow are not processed immediately. The request is executed after returning to the main event loop. This allows multiple glutReshapeWindow, glutPositionWindow, and glutFullScreen requests to the same window to be coalesced.
In the case of top-level windows, a glutReshapeWindow call is considered only to be a request for sizing the window. The window system is free to apply its own policies to top- level window sizing. The intent is that top-level windows should be reshaped according to glutReshapeWindow's parameters. Whether a reshape actually takes effect and, if so, the reshaped dimensions are reported to the program by a reshape callback.
glutReshapeWindow disables the full screen status of a window if previously enabled.
glutKeyboardFunc glutKeyboardFunc sets the keyboard callback for the current window. Usage void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y)); func The new keyboard callback function.
Description glutKeyboardFunc sets the keyboard callback for the current window. When a user types into the window, each key press generating an ASCII character will generate a keyboard callback. The key callback parameter is the generated ASCII character. The state of modifier keys such as Shift cannot be determined directly; their only effect will be on the returned ASCII data. The x and y callback parameters indicate the mouse location in window relative coordinates when the key was pressed. When a new window is created, no keyboard callback is initially registered, and ASCII key strokes in the window are ignored. Passing NULL to glutKeyboardFunc disables the generation of keyboard callbacks. During a keyboard callback, glutGetModifiers may be called to determine the state of modifier keys when the keystroke generating the callback occurred. We can also see glutSpecialFunc for a means to detect non-ASCII key strokes.
glutDisplayFunc glutDisplayFunc sets the display callback for the current window.
Usage void glutDisplayFunc(void (*func)(void)); func The new display callback function.
Description glutDisplayFunc sets the display callback for the current window. When GLUT determines that the normal plane for the window needs to be redisplayed, the display callback for the window is called. Before the callback, the current window is set to the window needing to be redisplayed and (if no overlay display callback is registered) the layer in use is set to the normal plane. The display callback is called with no parameters. The entire normal plane region should be redisplayed in response to the callback (this includes ancillary buffers if your program depends on their state). GLUT determines when the display callback should be triggered based on the window's redisplay state. The redisplay state for a window can be either set explicitly by calling glutPostRedisplay or implicitly as the result of window damage reported by the window system. Multiple posted redisplays for a window are coalesced by GLUT to minimize the number of display callbacks called. When an overlay is established for a window, but there is no overlay display callback registered, the display callback is used for redisplaying both the overlay and normal plane (that is, it will be called if either the redisplay state or overlay redisplay state is set). In this case, the layer in use is not implicitly changed on entry to the display callback.
See glutOverlayDisplayFunc to understand how distinct callbacks for the overlay and normal plane of a window may be established.
When a window is created, no display callback exists for the window. It is the responsibility of the programmer to install a display callback for the window before the window is shown. A display callback must be registered for any window that is shown. If a window becomes displayed without a display callback being registered, a fatal error occurs. Passing NULL to glutDisplayFunc is illegal as of GLUT 3.0; there is no way to ``deregister'' a display callback (though another callback routine can always be registered). Upon return from the display callback, the normal damaged state of the window (returned by calling glutLayerGet(GLUT_NORMAL_DAMAGED) is cleared. If there is no overlay display callback registered the overlay damaged state of the window (returned by calling glutLayerGet(GLUT_OVERLAY_DAMAGED) is also cleared.
glutReshapeFunc glutReshapeFunc sets the reshape callback for the current window. Usage void glutReshapeFunc(void (*func)(int width, int height)); func The new reshape callback function. Description glutReshapeFunc sets the reshape callback for the current window. The reshape callback is triggered when a window is reshaped. A reshape callback is also triggered immediately before a window's first display callback after a window is created or whenever an overlay for the window is established. The width and height parameters of the callback specify the new window size in pixels. Before the callback, the current window is set to the window that has been reshaped.
If a reshape callback is not registered for a window or NULL is passed to glutReshapeFunc (to deregister a previously registered callback), the default reshape