lecture 6 introduction to open gl and glut

Post on 20-Mar-2017

79 Views

Category:

Education

7 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Computer GraphicsShahid Mahmood

Introduction to OpenGL Supporting Libraries

Lecture 6 - Content

Initially, Silicon Graphics Inc. Developed a system called “Graphics Library, GL”

In 1992, a variation of GL, called OpenGL was announced

OpenGL was designed to be platform-independent – to run across a wide range of computer hardware, not just on Silicon Graphics machines

OpenGL - Origins

Soon, OpenGL was accepted as a standard computer graphics programming , due to its power and portability

As we know, OpenGL is not a programming language, it’s the specification of an Application Programming Interface (API)

It defines a set functions for creating computer graphics

OpenGL – Success

OpenGL provides 3D geometric objects, such as lines, polygons, triangle meshes, spheres, cubes, quadric surfaces, and curves

It provides 3D modeling transformations and viewing functions to create views of 3D scenes, using the idea of virtual camera

OpenGL – Capabilities

OpenGL supports high-quality rendering of scenes, including hidden-surface removal, multiple light sources, transparency, textures, blending, fog

It provides display lists for creating graphics caches and hierarchical models. It also supports the interactive picking of objects

It supports the manipulation of images as pixels, enabling frame-buffer effects such as anti-aliasing, motion blur, etc.

OpenGL – Capabilities, cont.

OpenGL is itself is only concerned with graphics rendering – OpenGL functions always start with “gl”

To extend OpenGL’s functionality, two libraries have been developed, OpenGL Utility Library (GLU), and the OpenGL Utility Toolkit (GLUT)

OpenGL – The support libraries

GLU provides functions to draw more complex primitives, such as curves and surfaces, it also help specify 3D views of scenes

All GLU functions names start with “glu”

GLU

GLUT provides the facilities for interaction that OpenGL lacks

It provides windows management functions, handling input events from the mouse and keyboard

It also provides basic functions for creating Graphical User Interfaces (GUIs).

All GLUT functions names start with “glut”

GLUT

Since OpenGL has been designed to be device-independent, it is not concerned with the exact makes and model of the graphics display and interaction hardware it uses, instead its functions refer to windows and events

An OpenGL window is a rectangular area on a physical display screen into which OpenGL draws graphics

An OpenGL event occurs when the operates an input device

To respond to the input event, the application must provide a function known as a callback function to handle the event

OpenGL automatically calls the application’s function, passing it with event data

OpenGL – Platform & device independence

OpenGL does not draw its graphics directly to the window

Instead draws into a data structure (an array of pixels) inside OpenGL called the frame-buffer, or simply buffer

Periodically, OpenGL copies the pixels in the frame buffer into the window.

OpenGL – frame-buffer

OpenGL – Program structureGLUT, GLEW Includes

Create GLUT Window

Register callback functions

User Initializations

Initialize GLUT

GLUT main loop

Open window◦ Configure display mode,Window position/size

Register input callbackFunction (GLUT)

◦ Render, resize, input: keyboard,mouse, etc

User Initialization◦ Set background color, clear Color, etc◦ Generate points to be drawn◦ Initialize shader stuff◦ Intitalize GLEW, Register GLUT callbacks, glutMainLoop()

OpenGL/GLUT – Program structure

GLUT, GLEW Includes

Create GLUT Window

Register callback functions

User Initializations

Initialize GLUT

GLUT main loop

GLUT is used to create and open window◦ glutInit(&argc, argv); // Initializes GLUT◦ glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

Sets display mode (e.ge single frame-buffer with RGB colors)

◦ glutInitWindowsSize(640, 480); Sets window size (Width x Height) in pixels

◦ glutInitPosition (100, 150); Sets location of upper left corner of window

glutCreateWindow(“Window title”);open window with title “Window title”

GLUT: Window management

Void main(int argc, char** argv) { // initialize toolkit, set display mode and create window

glutInit(&argc, argv); // initialize toolkit

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize(640, 480);

glutInitWindowPosition(100, 150);

glutCreateWindow(“Window title”);…

}

OpenGL Skeleton

Register callback functions,

Do user initialization

Wait in glutMainLoop for events …

}

OpenGL Skeleton

OpenGL programs are event-driven

Sequential program◦ Start program at main()◦ Perform actions 1, 2, 3 … N◦ End

Event-driven program◦ Start at main()◦ Initialize ◦ Wait in infinite loop

Wait till defined event occurs Event occurs => take defined actions

}

Sequential VS Event-driven

Program only responds to events Do nothing until event occurs

◦ Mouse clicks◦ Keyboard stroke◦ Window resize

Programmer defines◦ Events that program should respond to◦ Actions to be taken when event occurs◦ Wait in infinite loop

System (window)◦ Receives event, maintains event queue

}

OpenGL: Event-driven

Programmer registers callback functions (event handler)

Callback function called when event occurs

Example: Programmer◦ Declare function onMouseClick, to be called on mouse click◦ Register the function glutMouseFunc(onMouseClick);

When OS receives mouse click, calls callback function onMouseClick

}

OpenGL: Event-driven

Register callbacks for all events your program will react to

No registered callback = no action

If no registered keyboard callback function, hitting keyboard keys generates no response.

}

GLUT Callback Functions

GLUT callback functions in skeleton

glutDisplayFunc(myDisplay) // image to be drawn initially

glutReshapeFunc(myReshape) // called when window is reshaped

glutMouseFunc(myReshape) // called when mouse button is pressed

}

GLUT Callback Functions

top related