using graphics libraries lecture 3 mon, sep 1, 2003

21
Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Upload: della-jones

Post on 25-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Using Graphics Libraries

Lecture 3Mon, Sep 1, 2003

Page 2: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Graphics Libraries

Theoretically, with only a functionsetPixel(x, y, red, green, blue),

we could create any graphics image.However, it would be quite tedious.A graphics library provides an abundance of useful functions to simplify the task of creating graphics.

Page 3: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Device Independence

A library is device-independent if it provides a common API, regardless of the hardware on which it is used.The OpenGL API for Windows is identical to the OpenGL API for the Macintosh.Of course, the library must be compiled separately for each hardware system.

Page 4: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Windows-Based Programming

OpenGL consists of three libraries gl – graphics library

Basic functions. glu – graphics library utility

Composites of basic GL functions. glut – graphics library utility toolkit

Functions that interact with the windowing system.

Page 5: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Window-based Programming

int main(int argc, char* argv[]){

glutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(screenWidth, screenHeight);glutInitWindowPosition(100, 150);glutCreateWindow(“My Window Title");

glutDisplayFunc(display);glutReshapeFunc(reshape);glutKeyboardFunc(keyboard);glutMouseFunc(mouse);

init();glutMainLoop();return 0;

}

Page 6: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

The glut functions are used in main() to create and open a graphics window.Functions used to create a graphics window. glutInit(&argc, argv). glutInitDisplayWindow(options). glutInitWindowSize(width, height). glutInitWindowPosition(x, y). glutCreateWindow(name).

Window-Based Programming

Page 7: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Windows-Based Programming

glutInit(&argc, argv). Initializes the glut library. Should be called before any other glut

function. Must receive the command-line

arguments.

glutInitDisplayMode(options). Specifies color mode. Specifies single or double buffering.

Page 8: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Windows-Based Programming

glutInitWindowSize(width, height). Sets the height and width of the

window in pixels.

glutInitWindowPosition(x, y). Sets the position of the upper left

corner of the window.

glutCreateWindow(name). Creates, but does not display, the

window.

Page 9: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Callback Functions

A callback function is a user-specified function that the library will call whenever necessary.Each callback function must be registered with glut.glut provides for over 20 callbacks.

Page 10: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Callback Functions

The glut library contains functions with names of the form

glutXXXFunc(parameter),where XXX stands for some form of windows interaction (mouse, keyboard, etc.).The parameter is a user-defined function xxx().

Page 11: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Callback Functions

In the main function we writeglutXXXFunc(xxx).

Then when XXX is activated by the user (mouse click, keystroke, etc.), the function xxx() is called to handle the event.

Page 12: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

OpenGL Callback Functions

glutDisplayFunc(display); Called whenever the scene needs to be

redrawn. Activated by calls to glutPostRedisplay().

glutReshapeFunc(reshape); Called whenever the window is resized. Activated by resizing the window. Warning: This does not respond to

iconifying the window.

Page 13: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

OpenGL Callback Functions

glutMouseFunc(mouse) Called whenever the mouse is clicked. Activated by mouse clicks. Left or right, up or down.

glutKeyboardFunc(keyboard) Called whenever a key is pressed. Activated by keystrokes (down only)

of an ASCII key (letters, digits, punctuation).

Page 14: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

OpenGL Callback Functions

glutSpecialFunc(special) Called whenever a special key is

pressed. Activated by keystrokes (down only) of

a non-ASCII key (function key, arrow key, etc.).

glutMotionFunc(motion) Called whenever the mouse is moved

while the button is pressed.

Page 15: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

OpenGL Callback Functions

glutPassiveMotionFunc(passiveMotion) Called whenever the mouse is moved

while the button is not pressed.

glutIdleFunc(idle) Called whenever nothing else is

happening.

Page 16: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

The Main Loop

Typically main() ends by calling glutMainLoop()

This function runs forever.It calls the callback functions as necessary.It handles all drawing commands as they are generated.

Page 17: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

The Main Loop

main

display

reshape

keyboard

mouse

Processkeyboard

and mouseevents

glutMainLoop()

display()

mouse()

keyboard()

reshape()

LibraryFunctions

Page 18: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Example: Callback Functions

CallbackTest.cpp

Page 19: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Other Initializations

void init(){

glClearColor(0.8, 0.8, 0.8, 0.0); // light grayglColor3f(0.0f, 0.0f, 0.0f); // blackglMatrixMode(GL_PROJECTION);glLoadIdentity();gluOrtho2D(xmin, xmax, ymin, ymax);glViewport(0, 0, screenWidth, screenHeight);

}

Page 20: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

World Coordinates

The call to gluOrtho2D() establishes the world coordinates of the window.gluOrtho2D(xmin, xmax, ymin, ymax)The x-coordinates go from xmin to xmax from left to right.The y-coordinates go from ymin to ymax from bottom to top.

Page 21: Using Graphics Libraries Lecture 3 Mon, Sep 1, 2003

Example: Draw a 2D Object

DrawTeapot.cpp