Sei sulla pagina 1di 29

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

OpenGL Handbook

For Beginners

This Handbook provides basics of OpenGL and describes different functions of OpenGL in detail with the help of pseudo-codes. It also provides few sample OpenGL programs.

Computer Graphics (CS354)

Chapter 1 The Introduction


1.1 What is OpenGL?
OpenGL (Open Graphics Library) is a standard specification, defining a crosslanguage, cross-platform API for writing applications that produce 2D and 3D computer graphics. OpenGL was developed by Silicon Graphics Inc. (SGI) in 1992 based on their previous proprietary Iris GL, and became industry standard several years ago. OpenGL is widely used in CAD, virtual reality, scientific visualization, information visualization, and flight simulation. It is also used in video games, where it competes with Direct3D on Microsoft Windows platforms. Graphics cards usually have an OpenGL implementation. Because the OpenGL specification is not platform-specific, it is possible to write an application that will be possible to use against many different types of graphics cards. It also increases the chance that the application will continue to work when new hardware will become available. OpenGL is an immediate mode graphics programming API. OpenGL is defined and maintained by the Architectural Revision Board (ARB), an organization that includes members as SGI, IBM, and DEC, and Microsoft. OpenGL also provides a complete feature set for 2D and 3D graphics operations in a pipelined hardware accelerated architecture for triangle and polygon rendering. OpenGL is a powerful and generic toolset for hardware assisted computer graphics.

1.2 Why OpenGL?


OpenGL hides the complexities of interfacing with different 3D accelerators by presenting a single, uniform interface. OpenGL hides differing capabilities of hardware platforms by requiring support of full OpenGL feature set for all implementations.

SDMCET/CSE/OpenGL Handbook/v3.0

Page

Computer Graphics (CS354)

1.3 What is GLUT?


GLUT is the OpenGL Utility Toolkit. GLUT is used in the development of OpenGL applications. OpenGL is a graphics library, which means that it can do all sorts of things with geometric forms, lightings, transformations etc. But OpenGL was designed to be portable so it can be used within various environments/operating systems (Linux, UNIX, Solaris, Windows, Amiga...) without changing the OpenGL code. There is a little extra advantage when using GLUT. If you are developing an OpenGL application, then you should include the header files gl.h and glu.h (glu is the OpenGL Utility Library); but when you include glut.h, it guarantees the inclusion of gl.h and glu.h. The following diagram depicts the library organization of OpenGL:

SDMCET/CSE/OpenGL Handbook/v3.0

Page

Computer Graphics (CS354)

Chapter 2 The Installation


2.1 Installation of OpenGL in Microsoft Visual Studio 2003
This section describes the procedure for setting OpenGL in Visual Studio development environment. This section assumes that the user has installed Visual Studio .NET 2003 (and/or 2002) and is familiar with its use. The following are the steps: 1. Install Microsoft Visual C++. 2. Download the glut libraries from the following link. GLUT Libraries 3. Installation of GLUT Package (Needs to be done only once) 4. Windows OS usually comes with OpenGL and Visual Studio comes with the OpenGL libraries, but neither of them comes with GLUT. From the downloaded zip file, extract and put the following files in the following locations:

File glut32.dll glut32.lib glut.h

Location C:\WINDOWS\system\ C:\Program Files\Microsoft 2003\Vc7\PlatformSDK\Lib C:\Program Files\Microsoft 2003\Vc7\PlatformSDK\Include\gl

Visual Visual

Studio Studio

NET NET

Note:
If you plan on giving your OpenGL program to your friends for running on Windows platform, you must also include the glut32.dll file with your program. If they don't have this file in the same directory as your application or in their C:\WINDOWS\system folder, then the program will not run.

2.2 Creating Visual Studio Project:


Because GLUT was designed to be system independent it makes its own windows it is better to let GLUT run as a console application rather than as a native Windows application (which would require #include <windows.h>). To create an empty console project in Visual Studio, do the following:
Page

SDMCET/CSE/OpenGL Handbook/v3.0

Computer Graphics (CS354)

1. Goto File New Project. Then the New Project Dialog will appear.

2. In the Project Types pane, select Visual C++ Projects. Then select Win32 Console Project in the Templates pane. Name your project and click OK. The Win32 Application Wizard dialog will appear.

SDMCET/CSE/OpenGL Handbook/v3.0

Page

Computer Graphics (CS354)

3. Click the Application Settings tab on the left, and check the Empty Project box. Then click Finish.

Your empty console project will be created.


Page

SDMCET/CSE/OpenGL Handbook/v3.0

Computer Graphics (CS354)

4.

Add Source Code: Adding source files to the project should be familiar to you. There are two facts you should know, however. When you include GLUT in a program, it automatically includes the rest of the OpenGL header files. So explicitly having: #include <GL/gl.h> #include <GL/glu.h> In either Linux or Windows isn't necessary, provided you include GLUT. In short, you only need to do this: #include <GL/glut.h> Do not use a backslash in the preprocessor #include. #include <GL\glut.h> No!

5.

Modify Project Properties: Before compiling your project, you need to set up Visual Studio's linker so it knows where to find GLUT. To do this, you must open the Property Pages dialog for your project. There are two ways to do this: a. Use Visual Studio's menu option (Project Properties)

SDMCET/CSE/OpenGL Handbook/v3.0

Page

Computer Graphics (CS354)

b. OR: Use the Solution Explorer located in the upper right corner. Right-click your project's name as shown and select Properties.

6. Using either of the above options, the Property Pages dialog will open. Once it appears, do the following: a. From the Configuration: list box, select All Configurations:

b. In the left pane, select the Linker sub-tree and then the Input option. Add the following code to the Additional Dependencies text box in the right pane:

SDMCET/CSE/OpenGL Handbook/v3.0

Page

Copy & Paste: opengl32.lib glu32.lib glut32.lib

Computer Graphics (CS354)

Now Visual Studio knows where to find GLUT. c. This step is optional. If you want to prevent your program from opening a console window in addition to your GLUT window when it is run, select the Command Line option in the left pane. Then add the following code to the Additional Options: text box: Copy & Paste: /SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup

SDMCET/CSE/OpenGL Handbook/v3.0

Page

Computer Graphics (CS354)

Visual Studio thinks it's still building a normal console application, which is why a console window will appear if you run your program from inside Visual Studio or by double-clicking its icon. The /SUBSYSTEM: WINDOWS command tells Visual Studio this is a windowed application, which means a console window isn't necessary. However, when Visual Studio makes windowed applications, it wants to start program execution from WinMain() or wWinMain(), which you haven't defined. Setting the /ENTRY:mainCRTStartup flag tells Visual Studio to start execution at the usual main() instead.

Note:
If you choose to disable the console window, remember that you won't be able to see any console output from your program using printf(), cout, cerr, etc. So, if you plan on having users run your program from a DOS prompt and console output needs to be seen, do not disable the console window. Now your program is ready for development in an excellent IDE while still being portable.

2.3 Installation in Dev C++:


Dev C++ Page

SDMCET/CSE/OpenGL Handbook/v3.0

10

1. Download the latest version of Dev C++ from the following link:

Computer Graphics (CS354)

2. Download the Glut libraries from the following link:


GLUT Package

3. Install & Run Dev C++, Click on FileNew Project

4. After Clicking on Project a window will pop up as shown below:

SDMCET/CSE/OpenGL Handbook/v3.0

Page

11

Computer Graphics (CS354)

Select Empty Project. You can specify any useful name to your project in Name: Text field. For example, here the sample project is named as CG LAB Click Ok. 5. Now Go to ProjectProject Options. Click Ok. You will get a window as below:

Click on Parameters Tab and add the following line in the Linker Text Area: -lglut32 -lglu32 -lopengl32 -lwinmm -lgdi32

SDMCET/CSE/OpenGL Handbook/v3.0

Page

12

Computer Graphics (CS354)

Now you have successfully configured your project. 6. In the solution explorer window, which is situated on left of your screen, named as CG LAB (your project name), right click and click on New File.

This step will create a space for you to write the OpenGL Programs. 7. After typing the program, compile the program using Ctrl +F9. 8. Run the program using Ctrl + F10.

SDMCET/CSE/OpenGL Handbook/v3.0

Page

13

Computer Graphics (CS354)

Chapter 3 List of OpenGL Functions


Before we start writing OpenGL applications, we need to remember some of the basic OpenGL functions that interact with the environment. Few of such functions are given here. In the following sections, different types of OpenGL functions with their descriptions are explained in detail:

3.1 Initialization:
The first thing we need to do is to call the glutInit() procedure. It should be called before any other GLUT routine because it initializes the GLUT library. The parameters to glutInit() should be the same as those of main(), specifically main(int argcp, char** argv) and glutInit(&argc, argv) , where argc is a pointer to the program's unmodified argcp variable from main. Upon return, the value pointed to by argcp will be updated, and argv is the program's unmodified argv variable from main. Like argcp, the data for argv will be updated. The next thing we need to do is call the glutInitDisplayMode() procedure to specify the display mode for a window. You must first decide whether you want to use an RGBA (GLUT_RGBA) or color-index (GLUT_INDEX) color model. The RGBA mode stores its color buffers as red, green, blue, and alpha color components. The fourth color component, alpha, corresponds to the notion of opacity. An alpha value of 1.0 implies complete opacity, and an alpha value of 0.0 implies complete transparency. Color-index mode, in contrast, stores color buffers in indices. Your decision on color mode should be based on hardware availability and what your application requires. More colors can usually be simultaneously represented with RGBA mode than with color-index mode. And for special effects, such as shading, lighting, and fog, RGBA mode provides more flexibility. In general, use RGBA mode whenever possible. If not specified by the programmer, RGBA mode is the default mode in OpenGL. Another decision you need to make when setting up the display mode is whether you want to use single buffering (GLUT_SINGLE) or double buffering (GLUT_DOUBLE) scheme. Applications that use both front and back color buffers are double-buffered. Smooth animation is accomplished by rendering into only the back buffer (which isn't displayed), then causing the front and back buffers to be swapped. If you aren't using animation, stick with single buffering, which is the default buffering scheme in OpenGL. Finally, you must decide if you want to use a depth buffer (GLUT_DEPTH), a stencil buffer (GLUT_STENCIL) and/or an accumulation buffer (GLUT_ACCUM). The depth buffer stores a depth value for each pixel. By using a "depth test", the depth buffer can be used to display objects with a smaller depth value in front of objects with a larger
SDMCET/CSE/OpenGL Handbook/v3.0

Page

14

Computer Graphics (CS354)

depth value. The second buffer, the stencil buffer is used to restrict drawing to certain portions of the screen, just as a cardboard stencil can be used with a can of spray paint to make a printed image. Finally, the accumulation buffer is used for accumulating a series of images into a final composed image. None of these are default buffers. We need to create the characteristics of our window. A call to glutInitWindowSize() will be used to specify the size, in pixels, of your initial window. The arguments indicate t he height and width (in pixels) of the requested window. Similarly, glutInitWindowPosition() is used to specify the screen location for the upper-left corner of your initial window. The arguments- x and y, indicate the location of the window relative to the entire display.

3.2 Creating a Window:


To create a window with the previously set characteristics (display mode, size, location, etc), the programmer uses the glutCreateWindow() function. The function takes a string as a parameter which may appear in the title bar if the window system you are using supports it. The window is not actually displayed until the glutMainLoop() is entered.

3.3 Display Function:


The glutDisplayFunc() procedure is the first and most important event callback function you will see. A callback function is one where a programmer-specified routine can be registered to be called in response to a specific type of event. For example, the argument of glutDisplayFunc() is the function that is called whenever GLUT determines that the contents of the window needs to be redisplayed. Therefore, you should put all the routines that you need to draw a scene in this display callback function.

3.4 Reshape Function:


The glutReshapeFunc() is a callback function that specifies the function that is called whenever the window is resized or moved. Typically, the function that is called when needed by the reshape function displays the window to the new size and redefines the viewing characteristics as desired. If glutReshapeFunc() is not called, a default reshape function is called which sets the view to minimize distortion and sets the display to the new height and width.

SDMCET/CSE/OpenGL Handbook/v3.0

Page

15

Computer Graphics (CS354)

3.5 Main Loop:


The very last thing you must do is call glutMainLoop(). All windows that have been created can now be shown, and rendering those windows is now effective. The program will now be able to handle events as they occur (mouse clicks, window resizing, etc). In addition, the registered display callback (from our glutDisplayFunc() ) is triggered. Once this loop is entered, it is never exited!

3.6 Functions for drawing Points, Lines and Polygons:


Each geometric object is described by a set of vertices (points) and the type of primitive to be drawn. A vertex is no more than a point defined in three dimensional space. Whether and how these vertices are connected is determined by the primitive type. Every geometric object is ultimately described as an ordered set of vertices. We use glVertex*() function to specify a vertex. The '*' is used to indicate that there are variations to the base function glVertex(). Some OpenGL functions names have one, two, or three letters at the end to denote the number and type of parameters to the function. The first character indicates the number of values of the indicated type that must be presented to the function. The second character indicates the specific type of the arguments. The final character, if present, is 'v', indicating that the function takes a pointer to an array (a vector) of values rather than a series of individual arguments. For example, in the function glVertex3fv(), '3' is used to indicate three arguments, 'f' is used to indicate the arguments are floating point, and 'v' indicates that the arguments are in vector format.

3.6.1 Points:
A point is represented by a single vertex. Vertices specified by the user as twodimensional (only x- and y-coordinates) are assigned a z-coordinate equal to zero. To control the size of a rendered point, use glPointSize() and supply the desired size in pixels as the argument. The default is as 1 pixel by 1 pixel point. If the width specified is 2.0, the point will be a square of 2 by 2 pixels. glVertex*() is used to describe a point, but it is only effective between a glBegin() and a glEnd() pair. The argument passed to glBegin() determines what sort of geometric primitive is constructed from the vertices.

SDMCET/CSE/OpenGL Handbook/v3.0

Page

16

Computer Graphics (CS354)

Eg :

glBegin(GL_POINTS); glVertex2f(0.0, glVertex2f(0.0, glVertex2f(4.0, glVertex2f(6.0, glVertex2f(4.0, glEnd();

0.0); 3.0); 3.0); 1.5); 0.0);

3.6.2 Lines:
In OpenGL, the term line refers to a line segment, not the mathematician's version that extends to infinity in both directions. The easiest way to specify a line is in terms of the vertices at the endpoints. As with the points above, the argument passed to glBegin() tells it what to do with the vertices. The options for lines include: a. GL_LINES: Draws a series of unconnected line segments drawn between each set of vertices. An extraneous vertex is ignored. b. GL_LINE_STRIP: Draws a line segment from the first vertex to the last. Lines can intersect arbitrarily. c. GL_LINE_LOOP : Same as GL_STRIP, except that a final line segment is drawn from the last vertex back to the first. With OpenGL, the description of the shape of an object being drawn is independent of the description of its color. When a particular geometric object is drawn, it's drawn using the currently specified coloring scheme. In general, an OpenGL programmer first sets the color, using glColor*() and then draws the objects. Until the color is changed, all objects are drawn in that color or using that color scheme. Example:
glBegin(GL_LINES); glColor3f(1.0, 1.0, 0.0); // yellow glVertex2(-1.0, 1,0); glVertex2f(2.0, 2.0); glColor3f(1.0, 0.0, 0.0); // red glVertex2f(0.0, 0.0); glVertex2f(1.0, -1.0); glVertex2f(-2.0, -2.0); glEnd();

SDMCET/CSE/OpenGL Handbook/v3.0

Page

17

Computer Graphics (CS354)

3.6.3 Polygons:
Polygons are the areas enclosed by single closed loops of line segments, where the line segments are specified by the vertices at their endpoints. Polygons are typically drawn with the pixels in the interior filled in, but you can also draw them as outlines or a set of points. In OpenGL, there are a few restrictions on what constitutes a primitive polygon. For example, the edges of a polygon cannot intersect and they must be convex (no indentations). There are special functions for a three-sided (triangle) and four-sided (quadrilateral) polygons, glBegin(GL_TRIANGLES) and glBegin(GL_QUADS) , respectively. However, the general case of a polygon can be defined using glBegin(GL_POLYGON). Example: glBegin(GL_POLYGON); glColor3f(1.0, 1.0, 0.0); // yellow glVertex2f(0.0, 0.0); glVertex2f(0.0, 3.0); glVertex2f(4.0, 3.0); glVertex2f(6.0,1.5); glVertex2f(4.0, 0.0) ;
glEnd();

3.7 Drawing 3D Objects:


GLUT has a series of drawing routines that are used to create three-dimensional models. This means we don't have to reproduce the code necessary to draw these models in each program. These routines render all their graphics in immediate mode. Each object comes in two flavors, wire or solid. Available objects are: glutWireSphere() or glutSolidSphere() glutWireCube() or glutSolidCube() glutWireTorus() or glutSolidTorus() glutWireIcosahedron() or glutSolidIconsahedron() glutWireOctahedron() or glutSolidOctahedron() glutWireTetrahedron() or glutSolidTetrahedron() glutWireDodecahedron() or glutSolidDodecahedron() glutWireCone() or glutSolidCone() glutWireTeapot() or glutSolidTeapot()

SDMCET/CSE/OpenGL Handbook/v3.0

Page

18

Computer Graphics (CS354)

3.8 Display Lists:


A display list is a group of OpenGL functions that have been stored for later execution. When a display list is invoked, the functions in it are executed in the order in which they were issued. Most OpenGL functions can be either stored in a display list or issued in immediate mode, which causes them to be executed immediately. A display list must be bracketed with the glNewList() and glEndList() functions. The argument for glNewList() is the unique name that identifies the list. A display list contains only OpenGL functions. The values in the display list can't be changed and it is not possible to either add or remove functions from the list once it has been stored. You can delete the entire display list and create a new one, but you can't edit it. The function glNewList() specifies the start of a display list. The first parameter is a nonzero positive integer that uniquely identifies the display list. The second parameter specifies whether the OpenGL function should be executed immediately as well as placed in the display list (GL_COMPILE_AND_EXECUTE) or just added to the display list without executing them (GL_COMPILE). The function glEndList(), obviously, specifies the end of a display list. After you have created a display list, you can execute it by calling glCallList(). With glCallList() , you can execute the same display list many times, call a display list from another routine, or mix calls to execute display lists with calls to perform immediate-mode graphics. Any changes in the current color and current matrix made during the execution of the display list remain in effect after it has been called. Therefore, manipulating the matrix stack may be required to change the state of the variables before executing a display list and then restoring these values after the list has been executed. Example: glNewList(1,GL_COMPILE); glColor3f(1.0, 0.0, 0.0); // red glBegin(GL_TRIANGLES); glVertex2f(0.0, 0.0); glVertex2f(1.0, 0.0); glVertex2f(0.0, 1.0); glEnd(); glTranslate(1.5, 0.0, 0.0); // move object glEndList();
Page

glNewList(2,GL_COMPILE); glColor3f(0.0, 1.0, 0.0); // blue


SDMCET/CSE/OpenGL Handbook/v3.0

19

Computer Graphics (CS354)

glBegin(GL_QUAD); glVertex2f(0.0, 0.0); glVertex2f(1.0, 0.0); glVertex2f(1.0, 1.0); glVertex2f(0.0, 1.0); glEnd(); glTranslate(-1.5, 0.0, 0.0); // move object glEndList(); It is important to note that OpenGL functions are not necessarily executed as soon as they are issued. It is necessary to call the function glFlush() to ensure that all previously issued functions are executed. glFlush() is generally called at the end of a sequence of drawing functions to ensure all objects in the scene are drawn before beginning to accept user input.

3.9 Color:
Drawing on a computer screen is different from drawing o paper in that the paper starts out white, and all you have to do is draw the picture. On the computer, the memory holding the picture is usually filled with the last picture you drew, so you typically need to clear it to some background color before you start to draw the new scene. To change the background color, we call glClearColor() and specify the color we have chosen for the background. The default clearing color is (0,0,0,0) which is black. A subsequent call to glClear() will clear the specified buffers to their current clearing values. To clear the color buffer use the argument GL_COLOR_BUFFER_BIT. To set a color, use the function glColor3f(). It takes three floating point parameters which are between 0.0 and 1.0. The parameters are, in order, the red, green, and blue components of the color. You can think of these as specifying a "mix" of colors, where 0.0 means don't use any of this color and 1.0 means use that entire component. For example, glColor3f(1.0, 0.0, 0.0) makes the brightest red that the system can draw, with no green or blue components. All zeros make black (an absence of colored light) and all ones makes white (a presence of all colored light). Eight common colors and their functions are: a. b. c. d. e. f. g. glColor3f(0.0, glColor3f(1.0, glColor3f(0.0, glColor3f(1.0, glColor3f(0.0, glColor3f(1.0, glColor3f(0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0); 0.0); 0.0); 0.0); 1.0); 1.0); 1.0); //black //red //green //yellow //blue //magenta //cyan

SDMCET/CSE/OpenGL Handbook/v3.0

Page

20

Computer Graphics (CS354)

h. glColor3f(1.0, 1.0, 1.0);

//white

3.10 Menu:
GLUT supports simple cascading pop-up menus. They are designed to let a user select various modes within a program. The functionality is simple and minimalistic and is meant to be that way. Do not mistake GLUT's pop-up menu facility with an attempt to create a full-featured user interface. It is illegal to create or destroy menus, or change, add, or remove menu items while a menu (and any cascaded sub-menus) is in use (that is, popped up). The popup menus can be arranged in a hierarchy of menus, sub-menus, sub-sub-menus, and so on. Since a sub-menu identifier must be passed as an argument to the GLUT functions that create the parent menu, the menu must be created from the bottom up. For example, assume we want to create a main menu with entries Option A or Submenu1, where the sub-menu has entries of Options B, C, or Submenu2, where the sub-sub-menu has Options D and E. Submenu identifiers must be declared for each of our menu entries. This is a unique integer value will be assigned as the menus are called by glutCreateMenu() . Start with the lowest level menu and pass as an argument the callback function, menuCB, which defines menu selection options. To create the lowest level menu and its entries: sub2 = glutCreateMenu(menuCB); glutAddMenuEntry("Option D", 4); glutAddMenuEntry("Option E", 5); Then go up one more level and do the same thing for Submenu1: sub1 = glutCreateMenu(menuCB); glutAddMenuEntry("Option B", 2); glutAddMenuEntry("Option C", 3); glutAddSubMenu("SubMenu2", sub2); Finally, we need to go the same thing for the highest level menu: glutCreateMenu(menuCB); glutAddMenuEntry("Option A", 1); glutAddSubMenu("SubMenu1", sub1); glutAddMenuEntry("Quit", 6);

SDMCET/CSE/OpenGL Handbook/v3.0

Page

21

These menu options can now be used in the callback function menuCB to process the users menu choice.

Computer Graphics (CS354)

void menuCB(int item) { switch (item) { case 1: /* do whatever option A does */ break; case 2: /* do whatever option B does */ break; . . . } }

3.11 Mouse:
GLUT supports interaction with the computer mouse that is triggered when one of the three typical buttons is presses. A mouse callback function can be initiated when a given mouse button is pressed or released. The function glutMouseFunc() is used to specify the callback function to use when a specified button is given state at a certain location. The buttons are defined as GL_LEFT_BUTTON, GL_RIGHT_BUTTON, or GL_MIDDLE_BUTTON and the states for that button are either GLUT_DOWN (when pressed) or GLUT_UP (when released). Finally, x and y callback parameters indicate the location (in window-relative coordinates) of the mouse at the time of the event. Just as with the menu options above, the callback function can use a switch statement with multiple variations of mouse buttons, actions and locations to do some pretty nifty things. For example, we could use the mouse to cause an object within certain x and y ranges to rotate 90 degrees to the right with a click of the right button, to rotate 90 degrees to the left with a click of the left button, and to flip upside down with a click of the middle button.

3.12 Keyboard:
GLUT interaction using keyboard inputs is handled very similarly to those with the mouse. The function glutKeyboardFunc() is used to run the callback function specified and pass as parameters, the ASCII code of the pressed key, and the x and y coordinates of the mouse cursor at the time of the event. Special keys can also be used as triggers. The key passed to the callback function, in this case, takes one of the following values (defined in glut.h). a. b. c. d. GLUT_KEY_UP GLUT_KEY_RIGHT GLUT_KEY_DOWN GLUT_KEY_PAGE_UP Up Arrow Key Right Arrow Key Down Arrow Key Page Up Key

SDMCET/CSE/OpenGL Handbook/v3.0

Page

22

Computer Graphics (CS354)

e. f. g. h.

GLUT_KEY_PAGE_DOWN GLUT_KEY_HOME GLUT_KEY_END GLUT_KEY_INSERT

Page Down Key Home Key End Key Insert Key

3.13 Name Stacks:


The name stack holds the information that is returned to the program after the user selects a series of objects. To create a name stack, first initialize it with glInitNames(), which simply clears the stack, and then adds integer names to it while issuing corresponding drawing functions. To add a name to the top of the stack, you use the glPushName() function and to remove a name from the top of the stack, you use the glPopName() function. To replace the name at the top of the stack with a different one, you use the glLoadName() function. For example to draw a scene with three objects: glInitNames(); glPushName(0); glPushMatrix(); /* create your glLoadName(1); drawObject1(); glLoadName(2); drawObject2(); glLoadName(3); drawObject3(); glPopMatrix();

// save the current transformation state desired viewing volume here */ // call the procedure to draw object 1 // call the procedure to draw object 2 // call the procedure to draw object 3 // restore to previous transformation state

Calls to glPushName(), glPopName(), and glLoadName() are ignored if you're not in selection mode. So you might want to simplify your code by using these calls throughout your drawing code, and then use the same drawing code for both selection and normal rendering modes.

3.14 Picking:
You can use the selection process to select specific objects for further manipulation. To do this, you use a special picking matrix in conjunction with the projection matrix to restrict drawing to a small region of the viewport, typically near the cursor. Then you allow for some form of input, such as clicking a mouse button to initiate selection mode. With selection mode established and with the special picking matrix used, objects that are drawn near the cursor causes selection hits. Thus, during picking you're typically determining which objects are drawn near the cursor.
SDMCET/CSE/OpenGL Handbook/v3.0

Page

23

Computer Graphics (CS354)

Before calling the standard projection matrix (such as glOrtho() or glFrustum()), we need to use the utility routine gluPickMatrix() to multiply the specified projection matrix by a special picking matrix. The center of the picking region is passed as x and y values (in window coordinates), typically the cursor location. A width and height are also specified which define the picking region in screen coordinates. You can think of the width and height values as the sensitivity of the picking device. You should also specify a viewport. Use glGetIntegerv(GL_VIEWPORT, viewport) to get the current values for the viewport array. For example, to create a 5x5 pixel picking region near the cursor location, use gluPickMatrix((GLdouble) x, (GLdouble) y, 5.0, 5.0, viewport); You will probably want to save the contents of the current projection matrix before any manipulation, so the sequence of operations may look like this: glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPickMatrix( . . .); glFrustum(); //... draw scene for picking; perform picking; process picking glPopMatrix();

3.15 Hits:
In selection mode, we can use the name stack and mouse interaction to keep track of which objects are selected and process them accordingly. When you enter selection mode, OpenGL initializes a pointer to the beginning of the selection array. We can then use mouse input values, such as GL_DOWN and GL_LEFT_BUTTON, to determine if a hit should be recorded and update the hit record with each mouse input. Each time a hit record is written into the array, the pointer is updated accordingly. The hit records are accumulated in the array until glRendermode(GL_RENDER) is called. As noted above, the return value for this function is the number of hits in the array. This can then be passed to another procedure, along with the array itself to act on the selected pieces.

SDMCET/CSE/OpenGL Handbook/v3.0

Page

24

Computer Graphics (CS354)

Chapter 4 Sample OpenGL Programs


Problem-1: To draw a line on a Window using OpenGL Source Code:
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. #include<stdio.h> #include<stdlib.h> #include <GL/glut.h> /* Header for including Glut libraries */ void myinit() { glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */ glColor3f(1.0, 0.0, 0.0); /* draw in red */ /* set up viewing */ /* 300 x 300 window with origin lower left */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 50.0, 0.0, 50.0); glMatrixMode(GL_MODELVIEW); } void display( void ) { glClear(GL_COLOR_BUFFER_BIT); /*clear the window */ glPointSize(4.0); /* Set point size to 4px */ /* Draw Two Lines onto the Window */ glBegin(GL_LINES); glVertex2f(10.0, 10.0); glVertex2f(30.0, 30.0); glEnd(); glFlush(); /* clear buffers */ }

23. void main(int argc, char** argv) 24. { /* Standard GLUT initialization */ 25. glutInit(&argc,argv); 26. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); /* default, not needed */ 27. glutInitWindowSize(300,300); /* 300 x 300 pixel window */ 28. 29. 30. glutInitWindowPosition(0,0); /* place window top left on display */ glutCreateWindow("Line Draw"); /* window title */ glutDisplayFunc(display); /* display callback invoked
SDMCET/CSE/OpenGL Handbook/v3.0

Page

25

Computer Graphics (CS354)

31. 32. 33. }

myinit(); glutMainLoop();

when window opened */ /* set attributes */ /* enter event loop */

Output :

Program-2: To Generate a Sierpinski Gasket using Randomly Generating Points. Source Code:
/* Two-Dimensional Sierpinski Gasket */ /* Generated Using Randomly Selected Vertices */ #include<stdio.h> #include<stdlib.h> #include <GL/glut.h> void myinit() { /* attributes */ glClearColor(1.0, 1.0, 1.0, 1.0); /* white background */ glColor3f(1.0, 0.0, 0.0); /* draw in red */ /* set up viewing */ /* 500 x 500 window with origin lower left */ glMatrixMode(GL_PROJECTION); glLoadIdentity();
SDMCET/CSE/OpenGL Handbook/v3.0

Page

26

Computer Graphics (CS354)

gluOrtho2D(0.0, 50.0, 0.0, 50.0); glMatrixMode(GL_MODELVIEW); } void display( void ) { GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}}; /* A triangle */ int j, k; int rand(); /* standard random number generator */ GLfloat p[2] ={7.5,5.0}; /* An arbitrary initial point inside traingle */ glClear(GL_COLOR_BUFFER_BIT); /*clear the window */ /* compute and plots 5000 new points */ glBegin(GL_POINTS); for( k=0; k<50000; k++) { j=rand()%3; /* pick a vertex at random */ /* Compute point halfway between selected vertex and old point */ p[0] = (p[0]+vertices[j][0])/2.0; p[1] = (p[1]+vertices[j][1])/2.0; /* plot new point */ glVertex2fv(p); } glEnd(); glFlush(); /* clear buffers */ } void main(int argc, char** argv) { /* Standard GLUT initialization */ glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);/* default, not needed */ glutInitWindowSize(500,500); /* 500 x 500 pixel window */ glutInitWindowPosition(0,0); /* place window top left on display */ glutCreateWindow("Sierpinski Gasket"); /* window title */ glutDisplayFunc(display); /* display callback invoked when window opened */
SDMCET/CSE/OpenGL Handbook/v3.0

Page

27

Computer Graphics (CS354)

myinit(); /* set attributes */ glutMainLoop(); /* enter event loop */ }

Output:

SDMCET/CSE/OpenGL Handbook/v3.0

Page

28

Computer Graphics (CS354)

Chapter 5 References

1. 2. 3. 4. 5. 6.

http://www.opengl.org/ http://user.xmission.com/~nate/glut/glut-3.7.6-bin.zip http://www.bloodshed.net/dev/devcpp.html http://www.nigels.com/glt/devpak/ http://www.cs.uccs.edu/~semwal/man.html "Interactive Computer Graphics - A Top Down Approach Using OpenGL", 5th Edition, Edward Angel, Addison-Wesley 2009 "OpenGL - A Primer, 3rd Edition, Edward Angel http://en.wikipedia.org/wiki/OpenGL

7. 8.

SDMCET/CSE/OpenGL Handbook/v3.0

Page

29

Potrebbero piacerti anche