lecture 6 introduction to open gl and glut

21
Computer Graphics Shahid Mahmood

Upload: simpleok

Post on 20-Mar-2017

79 views

Category:

Education


7 download

TRANSCRIPT

Page 1: Lecture 6   introduction to open gl and glut

Computer GraphicsShahid Mahmood

Page 2: Lecture 6   introduction to open gl and glut

Introduction to OpenGL Supporting Libraries

Lecture 6 - Content

Page 3: Lecture 6   introduction to open gl and glut

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

Page 4: Lecture 6   introduction to open gl and glut

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

Page 5: Lecture 6   introduction to open gl and glut

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

Page 6: Lecture 6   introduction to open gl and glut

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.

Page 7: Lecture 6   introduction to open gl and glut

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

Page 8: Lecture 6   introduction to open gl and glut

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

Page 9: Lecture 6   introduction to open gl and glut

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

Page 10: Lecture 6   introduction to open gl and 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

Page 11: Lecture 6   introduction to open gl and glut

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

Page 12: Lecture 6   introduction to open gl and glut

OpenGL – Program structureGLUT, GLEW Includes

Create GLUT Window

Register callback functions

User Initializations

Initialize GLUT

GLUT main loop

Page 13: Lecture 6   introduction to open gl and glut

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

Page 14: Lecture 6   introduction to open gl and glut

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

Page 15: Lecture 6   introduction to open gl and glut

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

Page 16: Lecture 6   introduction to open gl and glut

Register callback functions,

Do user initialization

Wait in glutMainLoop for events …

}

OpenGL Skeleton

Page 17: Lecture 6   introduction to open gl and glut

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

Page 18: Lecture 6   introduction to open gl and glut

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

Page 19: Lecture 6   introduction to open gl and glut

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

Page 20: Lecture 6   introduction to open gl and glut

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

Page 21: Lecture 6   introduction to open gl and glut

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