30
Mini project on Glut Bitmap Font using openGL CHAPTER 1 INTRODUCTION 1.1 Introduction to the OpenGL OpenGL is an application program interface that is used to define 2D and 3D computer graphics.OpenGL has become the industry's most widely used and supported 2D and 3D graphics application programming interface (API), bringing thousands of applications to a wide variety of computer platforms. OpenGL fosters innovation and speeds application development by incorporating a broad set of rendering, texture mapping, special effects, and other powerful visualization functions. Developers can leverage the power of OpenGL across all popular desktop and workstation platforms, ensuring wide application deployment. OpenGL is independent of the hardware, operating, and windowing systems in use. The fact that it is windowing-system independent, makes it portable. OpenGL program must interface with the windowing system of the platform where the graphics are to be displayed. Therefore, a number of windowing toolkits have been developed for use with OpenGL. OpenGL functions are designed to provide 2D and 3D graphics with the emphasis on 3D. The program is fully functional, including everything that users usually want out of 3D graphics. This includes: 3D modeling transformations color 1

Project Report on Glut Bitmap Font

Embed Size (px)

Citation preview

Page 1: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

CHAPTER 1 INTRODUCTION

1.1 Introduction to the OpenGL

OpenGL is an application program interface that is used to define 2D and

3D computer graphics.OpenGL has become the industry's most widely used and supported 2D

and 3D graphics application programming interface (API), bringing thousands of applications to

a wide variety of computer platforms. OpenGL fosters innovation and speeds application

development by incorporating a broad set of rendering, texture mapping, special effects, and

other powerful visualization functions. Developers can leverage the power of OpenGL across all

popular desktop and workstation platforms, ensuring wide application deployment.

OpenGL is independent of the hardware, operating, and windowing systems in use. The fact

that it is windowing-system independent, makes it portable. OpenGL program must interface

with the windowing system of the platform where the graphics are to be displayed. Therefore, a

number of windowing toolkits have been developed for use with OpenGL. OpenGL functions are

designed to provide 2D and 3D graphics with the emphasis on 3D. The program is fully

functional, including everything that users usually want out of 3D graphics. This includes:

3D modeling

transformations

color

lighting

OpenGL functions in a client/server environment. That is, the application program producing

nthe graphics may run on a machine other than the one on which the graphics are displayed. The

server part of OpenGL, which runs on the workstation where the graphics are displayed, can

access whatever physical graphics device or frame buffer is available on that machine.

OpenGL is designed as a streamlined, hardware-independent interface to be implemented on

many different hardware platforms. To achieve these qualities, no commands for performing

windowing tasks or obtaining user input are included in OpenGL; instead, you must work

through whatever windowing system controls the particular hardware you're using.

1

Page 2: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

Similarly, OpenGL doesn't provide high-level commands for describing models of three-

dimensional objects. Such commands might allow you to specify relatively complicated shapes

such as automobiles, parts of the body, airplanes, or molecules.

1.2 Introduction to Glut

The OpenGL UtilityToolkit (GLUT) is a programming interface with ANSI C and FORTRAN

bindings for writing window system independent OpenGL programs. The toolkit supports the

following functionality:

Multiplewindows for OpenGL rendering.

Callback driven event processing.

Sophisticated input devices.

An “idle” routine and timers.

A simple, cascading pop-up menu facility.

Utility routines to generate various solid and wire frame objects.

Support for bitmap and stroke fonts.I

Miscellaneous window management functions, including managing overlays.

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.

Within the OpenGL programming Guide (ISBN 0-201-60458-2, also known as " the red book ")

one says the following about this subject: The OpenGL Utility Toolkit (GLUT) is a window-

system-independent toolkit written by Mark Kilgard, to hide the complexities of differing

window system APIs. So basically GLUT provides you with a utility that creates a window for

you, in that window you can show the output of your OpenGL program. But glut is replacable,

you can use the GtkGLArea or GtkGLExt for GTK, you can also use OpenGL within Perl

without explicitly using GLUT.

2

Page 3: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

CHAPTER 2 FUNCTIONS USED

2.1 Built in functions

1. glBegin()

2. glColor3fv()

3. glEnd()

4. glPushMatrix()

5. glTranslatef()

6. glClear()

7. glPopMatrix()

8. glFlush()

9. glScalef()

10. gluLookAt()

11. glRotatef()

12. glutSwapBuffers()

13. glutPostRedisplay()

14. glViewport()

15. glMatrixMode()

16. glLoadIdentity()

17. glutInit()

18. glutInitWindowSize()

19. glutInitWindowPosition()

20. glutCreateWindow()

21. glutDisplayFunc()

22. glutReshapeFunc()

23. glutReshapeFunc()

24. glutCreateMenu()

25. glutAddMenuEntry()

26. glutAddSubMenu()

27. glutAttachMenu()

28. glEnable()

29. glutMainLoop()

3

Page 4: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

2.2 USER DEFINED FUNCTIONS

1. display()

2. reshape()

3. selectFont(int newfont)

4. selectMessage(int msg)

5. selectCursorImage(int msg)

6. selectColor(int color)

7. output(int x, int y, char *string)

8. spin()

#include<GL/glut.h>

Includes the OpenGL utility Toolkit header file. This statement automatically includes gl.h, glu.h,

glx.h. And with Microsoft Windows, it includes the appropriate header file to access WGL.

2.3 Window Management

Five routines perform tasks to initialize a window

glutInit(int *argcp, char **argv) 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. Examples of this

situation include the failure to connect to the window system, the lack of window system support

for OpenGL, and invalid command line options. glutInit also processes command line options,

but the specific options parse are window system dependent.

glutInitDisplayMode(unsigned int mode) The initial display mode is used when creating top-

level windows, subwindows, and overlays to determine the OpenGL display mode for the to-be-

created window or overlay.

4

Page 5: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

GLUT_RGBA selects the RGBA color model, but it does not request any bits of alpha

(sometimes called an alpha buffer or destination alpha) be allocated. To request alpha,

specify GLUT_ALPHA. The same applies toGLUT_LUMINANCE.

glutInitWindowSize(int width, int height) specifies the size, in pixels, of your window.

glutInitWindowPosition(int x, int y) specifies the screen location for the upper-left corner of

your window.

glutCreateWindow(char *name) 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.

2.4 Menu Management

int glutCreateMenu(void (*func)(int value)): func The callback function for the menu that is

called when a menu entry from the menu is selected. The value passed to the callback is

determined by the value for the selected menu entry.

void glutDestroyMenu(int menu): glutDestroyMenu destroys the specified menu by menu. If

menu was the current menu, the current menu becomes invalid and glutGetMenu will return

zero. When a menu is destroyed, this has no effect on any sub-menus for which the destroyed

menu has triggers.Sub-menu triggers are by name, not reference.

void glutAddMenuEntry(char *name, int value): glutAddMenuEntryadds amenu entry to the

bottomof the current menu. The stringnamewill be displayed for the newly added menu entry. If

the menu entry is selected by the user, the menu’s callback will be called passing value as the

callback’s parameter.

5

Page 6: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

void glutAddSubMenu(char *name, int menu): glutAddSubMenu adds a sub-menu trigger to

the bottom of the current menu. The string name will be displayed for the newly added sub-menu

trigger. If the sub-menu trigger is entered, the sub-menu numbered menu will be cascaded,

allowing sub-menu menu items to be selected.

void glutAttachMenu(int button): glutAttachMenu attaches a mouse button for the current

window to the identifier of the current menu;

void glutSetCursor(int cursor): glutSetCursor changes the cursor image of the current window.

Each call requests the window system change the cursor appropriately. The cursor image when a

window is created is GLUT CURSOR INHERIT. The exact cursor images used are

implementation dependent. The intent is for the image to convey the meaning of the cursor

name. For a top-levelwindow, GLUT CURSOR INHERIT uses the default window system

cursor

2.5 Font Rendering

void glutBitmapCharacter(void *font, int character): Rendering a nonexistent character has

no effect. glutBitmapCharacter automatically sets the OpenGL unpack pixel storage modes it

needs appropriately and saves and restores the previous modes before returning. The generated

call to glBitmap will adjust the current raster position based on the width of the character.

2.6 The Display Callback

glutDisplayFunc(void (*func)(void)) 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, thecurrent 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

glutPostRedisplay(void) Mark the normal plane of current window as needing to be

redisplayed. The next iteration through glutMainLoop, the window's display callback will be

6

Page 7: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

called to redisplay the window's normal plane. Multiple calls toglutPostRedisplay before the next

display callback opportunity generates only a single redisplay callback. glutPostRedisplay may

be called within a window's display or overlay display callback to re-mark that window for

redisplay.

2.6 Running the Program

The vary last thing you must do is call glutMainLoop(void).All windows that have been created

are shown, and rendering to those windows is now effective. Event processing begins, and the

registered display callback is triggered. Once this loop is entered, it is never exited!

2.7 Handling Input Events

You can use these routines to register callback commands that are invoked when specified events

occur.

glutReshapeFunc(void (*func)(int width, int height)) 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.

Display Callback

void

display(void)

{

glClear(GL_COLOR_BUFFER_BIT);

output(100, 54, "This is written in a GLUT bitmap font.");

output(90, 400, "(positioned in pixels with upper-left origin)");

glPushMatrix();

glTranslatef(1,1,1);

glRotatef(theta,0,0,0);

output(400, 54, "This is written in a GLUT bitmap font.");

7

Page 8: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

output(390, 400, "(positioned in pixels with upper-left origin)");

output(500, 250, message);

glPopMatrix();

glutSwapBuffers();

}

2.8 Important Functions Used In Project:

gluLookAt(eye_x,eye_y,eye_z,at_X,at_y,at_z,up_x,up_y,up_z): post multiplies the current

matrix by a matrix determined by a viewer at the eye point looking at the at point with specified

up direction.

8

Page 9: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

CHAPTER 3 REQUIREMENT SPECIFICATION

3.1 Hardware Requirements

Processor : Intel processor

Main memory : 1 GB RAM

Hard Disk : 80 GB

3.2 Software Requirements

Operating system : Windows XP

Toolkit : BLOODSHED DEV-C++ (4.9.9.2version)

OPENGL/GLUT : 3.7.6 version

OPENGL Library

3.3 User Interface:

The interface for the openGL requires for the user to have a mouse connected, and the

corresponding drivers installed. This is because most of the implementation details require and

presume the presence of a mouse.

3.4 Hardware Interface:

The standard output device, as mentioned earlier has been assumed to be a color monitor. The

mouse, the main input device, has to be functional. Apart from these hardware requirements,

there should be sufficient hard disk space and primary memory available for proper working of

the package.

3.5 Software Interface:

The openGL has been implemented on the dev C/C++ platform and mainly requires an

appropriate version of the compiler to be installed and functional. Though it has been

implemented on dev C/C++, it is pretty much platform independent with the restriction that there

is support for the execution of C++ files. Text mode is recommended.

9

Page 10: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

3.6 FEATURES

Mouse interaction in generating Menus and submenus.

GLUT font rendering function have been used.

GLUT Cursor Image Functions have been used.

Use of GLUT Window Management Functions.

Use of GLUT Menu Management Functions.

CHAPTER4 IMPLEMENTATION

10

Page 11: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

The Main function flow is as follows :

First it initializes a default font by using a ‘for’ loop at the starting of the display

function.

Then as going on a sequence of submenus in main menu are created by assigning

the respective call back functions to them.

Call Back Functions Are

SELECT COLOR

SELECT FONT

SELECT MESSAGE

SELECT CURSOR

And finally these submenus are attached to the main menu in appropriate way.

Actually all these functions and activates are done in the reverse way as execution

starts.

DISPLAY FUNCTION:

This function is responsible for the displaying the BITMAP FONT on the window

with starting pixel position at origin.

The parameters are sent from the display function to the output function

(parameters are font and message string).

And also rotation and translation attributes and actions are also included in the

function.

The display is like ZOOM IN and ZOOM OUT type which constantly goes on

displaying in that fashion.

4.1 Source Code

11

Page 12: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

#include <string.h>

#include <GL/glut.h>

void *font = GLUT_BITMAP_TIMES_ROMAN_24;

void *fonts[] =

{

GLUT_BITMAP_9_BY_15,

GLUT_BITMAP_TIMES_ROMAN_10,

GLUT_BITMAP_TIMES_ROMAN_24,

GLUT_BITMAP_HELVETICA_10,

GLUT_BITMAP_HELVETICA_12,

GLUT_BITMAP_HELVETICA_18,

GLUT_BITMAP_8_BY_13

};

char defaultMessage[] = "GLUT means OpenGL.";

char *message = defaultMessage;

void

selectFont(int newfont)

{

if(newfont==7)

{

glutFullScreen();

}

else

if(newfont==8)

{

exit(0);

}

else

{

font = fonts[newfont];

glutPostRedisplay();

}

12

Page 13: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

}

void

selectMessage(int msg)

{

switch (msg) {

case 1:

message = "Welcome to Our Project";

break;

case 2:

message = "Our Project is on using BITMAP Text in Opengl";

break;

case 3:

message = "Thanks for visiting Our project Hope you like it!!!....";

break;

}

}

void

selectCursorImage(int msg)

{

switch (msg) {

case 1:

glutSetCursor(GLUT_CURSOR_HELP);

break;

case 2:

glutSetCursor(GLUT_CURSOR_CYCLE);

break;

case 3:

glutSetCursor(GLUT_CURSOR_WAIT);

break;

case 4:

glutSetCursor(GLUT_CURSOR_CROSSHAIR);

break;

13

Page 14: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

case 5:

glutSetCursor(GLUT_CURSOR_NONE);

break;

case 6:

glutSetCursor(GLUT_CURSOR_DESTROY);

break;

case 7:

glutSetCursor(GLUT_CURSOR_SPRAY);

break;

}

}

void

selectColor(int color)

{

switch (color) {

case 1:

glColor3f(0.0, 1.0, 0.0);

break;

case 2:

glColor3f(1.0, 0.0, 0.0);

break;

case 3:

glColor3f(1.0, 1.0, 1.0);

break;

case 4:

glColor3f(0.0, 0.0, 1.0);

break;

}

glutPostRedisplay();

}

void

tick(void)

14

Page 15: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

{

glutPostRedisplay();

}

void

output(int x, int y, char *string)

{

int len, i;

glRasterPos2f(x, y);

len = (int) strlen(string);

for (i = 0; i < len; i++) {

glutBitmapCharacter(font, string[i]);

}

}

GLfloat theta=0;

void spin()

{

theta+=0.2;

if(theta>360.0)theta-=360.0;

glutPostRedisplay();

}

void

display(void)

{

glClear(GL_COLOR_BUFFER_BIT);

output(100, 54, "This is written in a GLUT bitmap font.");

output(90, 400, "(positioned in pixels with upper-left origin)");

glPushMatrix();

glTranslatef(1,1,1);

glRotatef(theta,0,0,0);

output(400, 54, "This is written in a GLUT bitmap font.");

15

Page 16: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

output(390, 400, "(positioned in pixels with upper-left origin)");

output(500, 250, message);

glPopMatrix();

glutSwapBuffers();

}

void

reshape(int w, int h)

{

glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

//glFrustum(w, h,0, 0, 2, 10);

gluOrtho2D(0,w,h,0);

glMatrixMode(GL_MODELVIEW);

gluLookAt(0, 0, 1, 0, 0, 0, 0, 1, 0);

}

int

main(int argc, char **argv)

{

int i, msg_submenu, color_submenu,cursor_submenu;

glutInit(&argc, argv);

for (i = 1; i < argc; i++) {

if (!strcmp(argv[i], "-mono")) {

font = GLUT_BITMAP_9_BY_15;

}

}

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

glutInitWindowSize(600, 500);

glutCreateWindow("GLUT bitmap font example");

glClearColor(0.0, 0.0, 0.0, 1.0);

glutDisplayFunc(display);

16

Page 17: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

glutReshapeFunc(reshape);

glutSetWindowTitle("Pranoy");

glutSetIconTitle("Pranoy");

glutIdleFunc(tick);

glutIdleFunc(spin);

msg_submenu = glutCreateMenu(selectMessage);

glutAddMenuEntry("Welcome", 1);

glutAddMenuEntry("Info", 2);

glutAddMenuEntry("End", 3);

cursor_submenu = glutCreateMenu(selectCursorImage);

glutAddMenuEntry("Help", 1);

glutAddMenuEntry("Cycle", 2);

glutAddMenuEntry("Wait", 3);

glutAddMenuEntry("Cross Hair", 4);

glutAddMenuEntry("None", 5);

glutAddMenuEntry("Destroy", 6);

glutAddMenuEntry("Spray", 7);

color_submenu = glutCreateMenu(selectColor);

glutAddMenuEntry("Green", 1);

glutAddMenuEntry("Red", 2);

glutAddMenuEntry("White", 3);

glutAddMenuEntry("Blue", 4);

glutCreateMenu(selectFont);

glutAddMenuEntry("9 by 15", 0);

glutAddMenuEntry("Times Roman 10", 1);

glutAddMenuEntry("Times Roman 24", 2);

glutAddMenuEntry("Helvetica 10", 3);

glutAddMenuEntry("Helvetica 12", 4);

glutAddMenuEntry("Helvetica 18", 5);

glutAddMenuEntry("8 BY 13", 6);

glutAddMenuEntry("Full screen", 7);

glutAddMenuEntry("Exit Full Screen", 8);

glutAddSubMenu("Messages", msg_submenu);

glutAddSubMenu("Color", color_submenu);

17

Page 18: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

glutAddSubMenu("Change Cursor Image", cursor_submenu);

glutAttachMenu(GLUT_RIGHT_BUTTON);

glutAttachMenu(GLUT_LEFT_BUTTON);

glutAttachMenu(GLUT_MIDDLE_BUTTON);

glutMainLoop();

return 0;

}

CHAPTER 5 OUTPUTS

18

Page 19: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

Fig(5.1): Translating Bitmap font from the top left of Window

19

Page 20: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

Fig(5.2): BITMAP FONT message changed and also color to red and moving towards

corner.

20

Page 21: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

21

Page 22: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

CHAPTER 6 CONCLUSION

We acquired knowledge about terms, concepts and questions related to the subject what make us

capable of working with the commands of the OpenGL language, GLUT LIBRARY

FUNCTIONS and consequently we are prepared to elaborate 3D TEXT based graphical

projects of the more diverse types.

This project gave us a satisfactory view of Text mapping, text pixel positioning, text rotations

and Font rendering operations.

22

Page 23: Project Report on Glut Bitmap Font

Mini project on Glut Bitmap Font using openGL

CHAPTER7 BIBLIOGRAPHY

INTERACTIVE COMPUTER GRAPHICS-5th EDITION

-EDWARD ANGEL

www.opengl.org/ documentation/books/

COMPUTER GRAPHICS USING OPENGL-2nd EDITION

-F.S.HILL

THE OPENGL UTILITY TOOLKIT (GLUT) PROGRAMMING INTERFACE.

-MARK J. KILGARD , SILICON GRAPHICS, INC.

23