lecture 3 opengl. review rasterization opengl libraries: gl, glu, glut buffers: buffer types ...

24
Lecture 3 Lecture 3 OpenGL OpenGL

Upload: mitchell-cox

Post on 01-Jan-2016

292 views

Category:

Documents


2 download

TRANSCRIPT

Lecture 3Lecture 3OpenGLOpenGL

Lecture 3Lecture 3

ReviewReview RasterizationRasterization OpenGL Libraries: GL, GLU, OpenGL Libraries: GL, GLU, GLUTGLUT Buffers: Buffers:

Buffer TypesBuffer Types Clearing BuffersClearing Buffers

PrimitivesPrimitives ColorsColors Sample CodeSample Code

Lecture ThreeLecture Three

Color ModesColor Modes RGB vs. Color-Index modeRGB vs. Color-Index mode

Event Driven ProgrammingEvent Driven Programming Call back RegistrationCall back Registration

Mouse, Keyboard call backsMouse, Keyboard call backs Reshape, Display, Idle Reshape, Display, Idle

Double bufferingDouble buffering AnimationAnimation

Color ModesColor Modes

RGB Mode: A color is specified by:RGB Mode: A color is specified by: RedRed GreenGreen BlueBlue AlphaAlpha

Number of colors simultaneously displayed:Number of colors simultaneously displayed: n bits represents 2^n colors.n bits represents 2^n colors. 8 to 16 bits represent 256 to 64K colors8 to 16 bits represent 256 to 64K colors

Color ModesColor Modes

Color Index Mode:Color Index Mode: Uses Uses Color Index TableColor Index Table We can still use We can still use only only 2^n color 2^n color simultaneouslysimultaneously But the set of 2^n colors are picked from a table, But the set of 2^n colors are picked from a table, Color Color

Index TableIndex Table

Void glutInitDisplayMode (GLUT_RGBA or GLUT_INDEXVoid glutInitDisplayMode (GLUT_RGBA or GLUT_INDEX))

Choosing Color ModelChoosing Color Model

In order to choose a color Model use:In order to choose a color Model use: Void glutInitDisplayMode (GLUT_RGBA or GLUT_INDEXVoid glutInitDisplayMode (GLUT_RGBA or GLUT_INDEX))

Need more details?Need more details? Visit ‘Online Resources’ in course website, or click following linkVisit ‘Online Resources’ in course website, or click following link http://www.cse.yorku.ca/~alireza/courses/540/rgba.pdfhttp://www.cse.yorku.ca/~alireza/courses/540/rgba.pdf

Event Driven ProgrammingEvent Driven Programming

Definition. Definition. A programming where the primary activity is A programming where the primary activity is reaction to the receipt of event.reaction to the receipt of event.

Applications: GUI, HCIApplications: GUI, HCI

Ingredients:Ingredients: Events: Events: Can be from any source, human input, e.g. mouse or Can be from any source, human input, e.g. mouse or

other sensors.other sensors.

Event Dispatcher: Event Dispatcher: Assigns events to event handlers.Assigns events to event handlers.

Event HandlerEvent Handler

Event Processing in GLUTEvent Processing in GLUT

Initial Setup:Initial Setup: Initializing Display Modes, e.g. Color mode, Initializing Display Modes, e.g. Color mode,

Single/Double Buffer, etc.Single/Double Buffer, etc.

Entering GLUT event processing loopEntering GLUT event processing loop void glutMainLoop (void)void glutMainLoop (void)

Call Back RegistrationCall Back Registration

Definitions:Definitions: current windowcurrent window Normal PlaneNormal Plane

Assigning events to event-handler.Assigning events to event-handler. glutReshapeFunctionglutReshapeFunction glutDisplayFunctionglutDisplayFunction glutKeyboardFunctionglutKeyboardFunction glutMouseFunction glutMouseFunction

Display FunctionDisplay Function

The display function of The display function of current windowcurrent window When is it called?When is it called?

If the If the normal planenormal plane of a window needs to be redisplayed as a of a window needs to be redisplayed as a result of window damage. GLUT determines this based on result of window damage. GLUT determines this based on redisplay state of a window.redisplay state of a window.

When a user requests.When a user requests.

How is it called?How is it called? Implicitly:Implicitly: By GLUT, e.g. when the window system reports a By GLUT, e.g. when the window system reports a

region of the window's normal plane is undefined (for example, region of the window's normal plane is undefined (for example, damaged by another window moving or being initially shown) damaged by another window moving or being initially shown)

Explicitly:Explicitly: Using glutPostRedisplay function. Using glutPostRedisplay function.

Reshape FunctionReshape Function

Takes care of Window Resize When is it called?

Whenever current window is reshaped

How is it called? By GLUT Width and height of new window is passed void reshape (int w, int h)

main methodmain method

GLUT initializationGLUT initialization glutInit (&argc, argv)glutInit (&argc, argv) glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB)glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB) glutInitWindowSize (width, height) glutInitWindowSize (width, height) glutInitWindowPosition (X, Y)glutInitWindowPosition (X, Y)

Registering Event Handler functionsRegistering Event Handler functions Display Function, Reshape Function, etc.Display Function, Reshape Function, etc.

Entering GLUT Event ProcessingEntering GLUT Event Processing glutMainLoop functionglutMainLoop function

Sample Code in main methodSample Code in main method glutInit(&argc, argv);glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (250, 250); glutInitWindowSize (250, 250); glutInitWindowPosition (100, 100);glutInitWindowPosition (100, 100); glutCreateWindow (argv[0]);glutCreateWindow (argv[0]); glClearColor (0.0, 0.0, 0.0, 0.0);glClearColor (0.0, 0.0, 0.0, 0.0); glShadeModel (GL_FLAT);glShadeModel (GL_FLAT); glutDisplayFunc(display); glutDisplayFunc(display); glutReshapeFunc(reshape); glutReshapeFunc(reshape); glutMouseFunc(mouse);glutMouseFunc(mouse); glutKeyboardFunc(keyboard);glutKeyboardFunc(keyboard); glutMainLoop();glutMainLoop(); return 0;return 0;

Event Handling: KeyboardEvent Handling: Keyboard

RegistrationRegistration void glutKeyboardFunc (void (*func) (unsigned char key, int x, int y))

X,Y are the coordinate of mouse when key is pressed

Refer to online resources GLUT API for details about key callbacks.

For non-ASCII key strokes F1~F12, Direction, Del, Ins, PgUp, etc. glutSpecialFunc

Event Handling: MouseEvent Handling: Mouse

RegistrationRegistration void glutMouseFunc (void (*func) (int button, int state, int x, int y))

Button value: GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON.

State Value: GLUT_UP or GLUT_DOWN: Represents that the event happened because of press or release.

void glutMotionFunc (void (*func) (int x, int y)) Is called whenever the mouse moves within the window while one or more

mouse buttons are pressed.

void glutPassiveMotionFunc (void (*func) (int x, int y)) Is called whenever the mouse moves within the window while no mouse

buttons are pressed.

Idle FunctionIdle Function

Even sitting Idle is an eventEven sitting Idle is an event

Handling Idle eventHandling Idle event glutIdleFunc (void (*func) (void))glutIdleFunc (void (*func) (void))

Most useful for creating animationsMost useful for creating animations

ExampleExample

Download the “Keyboard Event” sample Download the “Keyboard Event” sample code from the course website.code from the course website.

Let’s explain the codeLet’s explain the code

Add an arbitrary mouse event to the codeAdd an arbitrary mouse event to the code

Double BufferingDouble Buffering

Using Two buffersUsing Two buffers Front BufferFront Buffer Back BufferBack Buffer

Is specified in initialization stepIs specified in initialization step glutInitDisplayMode (GLUT_DOUBLE)glutInitDisplayMode (GLUT_DOUBLE)

Replace glFlush() with glSwapBuffers()Replace glFlush() with glSwapBuffers()

Increases the performanceIncreases the performance It’s NOT free, you have to pay: It’s NOT free, you have to pay: memorymemory

ExampleExample

Download the “Double Buffer Example” Download the “Double Buffer Example” sample code from the course website.sample code from the course website.

Compare the performance of Single buffer Compare the performance of Single buffer and Double bufferand Double buffer

Try to add events to the program.Try to add events to the program.

AnimationAnimation

AlgorithmAlgorithm Initialize GLUT windowing systemInitialize GLUT windowing system Initialize Event HandlersInitialize Event Handlers

DisplayDisplay ReshapeReshape Mouse, Keyboard, etcMouse, Keyboard, etc IdleIdle

Single/Double BufferingSingle/Double Buffering glFlush or glSwapBuffersglFlush or glSwapBuffers

ConclusionConclusion

What we covered today:What we covered today: Color ModesColor Modes Event Driven ProgrammingEvent Driven Programming Registering Callback FunctionsRegistering Callback Functions Double BufferingDouble Buffering AnimationAnimation

QUESTIONS?QUESTIONS?

Next WeekNext Week

Basic 3D GraphicsBasic 3D Graphics Basic Camera Setup and ControlBasic Camera Setup and Control Orthographic ProjectionOrthographic Projection Perspective ProjectionPerspective Projection Drawing 3D ObjectsDrawing 3D Objects GLUT solid and wireframe objectsGLUT solid and wireframe objects

ENDEND