12/22/2016 21:38 1 comp 175c - computer graphics dan hebert computer graphics comp 175 chapter 3...

98
1 06/17/22 16:21 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

Upload: ezra-fletcher

Post on 18-Jan-2018

222 views

Category:

Documents


0 download

DESCRIPTION

32/22/ :38 3 Comp 175C - Computer Graphics Dan Hebert Interaction 0 Interaction is an important component of graphics applications 0 Most modern APIs, such as OpenGL, doesn’t support interaction directly (why?) 0 We need additional libraries, such as GLUT, for device interaction

TRANSCRIPT

Page 1: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

105/05/23 16:50

1Comp 175C - Computer Graphics Dan Hebert

Computer Graphics

Comp 175Chapter 3

Instructor: Dan Hebert

Page 2: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

205/05/23 16:50

2Comp 175C - Computer Graphics Dan Hebert

Outline

0 Input Device Types0 Interactive Graphics Programming0 Example Program

Page 3: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

305/05/23 16:50

3Comp 175C - Computer Graphics Dan Hebert

Interaction

0 Interaction is an important component of graphics applications

0 Most modern APIs, such as OpenGL, doesn’t support interaction directly (why?)

0 We need additional libraries, such as GLUT, for device interaction

Page 4: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

405/05/23 16:50

4Comp 175C - Computer Graphics Dan Hebert

Interaction with Graphics System

ChangeImage

Reactto

Change

Graphics System User

InputDevice

Display

Page 5: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

505/05/23 16:50

5Comp 175C - Computer Graphics Dan Hebert

Computer GraphicsConceptual Model

ApplicationModel

ApplicationProgram Graphics

System

OutputDevices

InputDevices

API

Function Callsor Protocol

Data

Page 6: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

605/05/23 16:50

6Comp 175C - Computer Graphics Dan Hebert

GL Library Organization (under X Windows)

OpenGlapplication

program

GLU

GL

GLUT

GLX

Xlib, Xtk

Framebuffer

Page 7: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

705/05/23 16:50

7Comp 175C - Computer Graphics Dan Hebert

GL Library Organization

Window OS

Application

GL GLU GLUT

Page 8: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

805/05/23 16:50

8Comp 175C - Computer Graphics Dan Hebert

Input Devices

We can think about input devices in two ways:

• Physical device – that can be described by their real-world physical properties. (mouse, keyboard, joystick…)

• Logical devices – that is characterized by its high-level interface with the user program. It is an abstraction of device data. (functions, think of the windows device driver)

Page 9: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

905/05/23 16:50

9Comp 175C - Computer Graphics Dan Hebert

Physical Device Types

0 Keyboard0 Choice0 Locators

Page 10: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

1005/05/23 16:50

10Comp 175C - Computer Graphics Dan Hebert

Keyboard

0 Returns character codes with specific meanings

Page 11: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

1105/05/23 16:50

11Comp 175C - Computer Graphics Dan Hebert

Choice

0 Returns a choice that has been selected from a number of discrete options

- Button box- Function keypad

Page 12: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

1205/05/23 16:50

12Comp 175C - Computer Graphics Dan Hebert

Locator Devices

0 Return a position and/or orientation

– Mouse– Trackball– Tablet– Joystick – Touch screen

Page 13: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

1305/05/23 16:50

13Comp 175C - Computer Graphics Dan Hebert

3D Input Devices

0 Return 3D position and /or orientation- Digitizer- 3D Spaceball- Glove- Tracker

Page 14: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

1405/05/23 16:50

14Comp 175C - Computer Graphics Dan Hebert

3D Input Devices

Digitizer

- 3D model

Page 15: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

1505/05/23 16:50

15Comp 175C - Computer Graphics Dan Hebert

3D Input Devices

3D Spaceball

Page 16: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

1605/05/23 16:50

16Comp 175C - Computer Graphics Dan Hebert

3D Input Devices

Haptic Glove

- Force feedbackTracking hand and finger position/orientation

Page 17: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

1705/05/23 16:50

17Comp 175C - Computer Graphics Dan Hebert

3D Input Devices

Motion Tracker

- Magnetic

- Acoustic

- Inertial

- Optical

- GPS

Page 18: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

1805/05/23 16:50

18Comp 175C - Computer Graphics Dan Hebert

Logical Device

0Characterized from the perspective of user application program- High-level interface with the user program - An abstraction of device data

Page 19: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

1905/05/23 16:50

19Comp 175C - Computer Graphics Dan Hebert

Logical Device Types

0 String- Returns ASCII strings

0 Locator- Returns position and or orientation

0 Pick- Returns the identifier of object

0 Choice- Returns a choice that has been selected from a

number of options0 Dial

- Return analog input (continuous control)0 Stroke

- Returns an array of locations

Page 20: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

2005/05/23 16:50

20Comp 175C - Computer Graphics Dan Hebert

Describe Input Device Behavior

0 Ways to describe input device behavior

- Measure: what the device returns to user program

- Trigger: when the device returns those measurements

Page 21: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

2105/05/23 16:50

21Comp 175C - Computer Graphics Dan Hebert

Ways to Read Input Device

0Sample mode0Request mode0Event mode

Page 22: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

2205/05/23 16:50

22Comp 175C - Computer Graphics Dan Hebert

Sample Mode

Hi, What is its input right now? Give me the data immediately!

Measure Process

ProgramSample

(1)

Measure(2)

- No trigger needed

- Return immediately

- Prepare the data before function call

Page 23: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

2305/05/23 16:50

23Comp 175C - Computer Graphics Dan Hebert

Request Mode

Do not return the measure until the device is triggered

- Wait for triggering

- Think of the C function call “ scanf() ”

Measure Process

ProgramRequest

(1)

Measure(4)

Trigger Process

Trigger(3)

(2)

Page 24: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

2405/05/23 16:50

24Comp 175C - Computer Graphics Dan Hebert

Event Mode

Wait until a device is triggered and user does something

• Two asynchronous processes

- Event generation (device trigger)

- User request (event query)

• Event generation and process are independent

• Multiple input devices

Page 25: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

2505/05/23 16:50

25Comp 175C - Computer Graphics Dan Hebert

Event Mode

Let’s think of the Windows event mechanism

- Generate a event by a device trigger

- Place the event in the event queue

- Examine the event queue and process

Event process

Mouse

Keyboard

… Process

Event 1

Event 2

Event 3

Event queue

Event sources

Page 26: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

2605/05/23 16:50

26Comp 175C - Computer Graphics Dan Hebert

Event Mode

Two Asynchronous Processes

Measure Process

ProgramAwait(1)

Event (2)

Trigger Process

Event Queue

Trigger(1)

Measure (2)

Page 27: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

2705/05/23 16:50

27Comp 175C - Computer Graphics Dan Hebert

Demonstration (code)

Page 28: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

2805/05/23 16:50

28Comp 175C - Computer Graphics Dan Hebert

Interactive Graphics Programming

We will focus on0 Event-driven Input mode0 Callback mechanism to do event process0 GLUT programming

Page 29: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

2905/05/23 16:50

29Comp 175C - Computer Graphics Dan Hebert

Callback Functions

0 Callback Function Routine to call when some event happens.e.g. mouse/keyboard, window changes, etc.

0 GLUT uses a callback mechanism to do its event processing.

Page 30: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

3005/05/23 16:50

30Comp 175C - Computer Graphics Dan Hebert

Event-driven Input Programming

0 Decide input events- mouse, keyboard, window……

0 Write callback functions- respond to each input event, ……

0 Register callback functions- tell the system to handle the event, ……

0 Event processing loop- Wait and process

Page 31: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

3105/05/23 16:50

31Comp 175C - Computer Graphics Dan Hebert

GLUT Event Callback Functions

GLUT can handle on the most common used input events

Ex:0Key action0Mouse action0 Idle (called when nothing else is going on)0Window events (contents, resized or moved)

Page 32: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

3205/05/23 16:50

32Comp 175C - Computer Graphics Dan Hebert

Keyboard Event

void keyboard_callback_func ( unsigned char key, int x, int y )

{

switch (key)

{

case ‘q’ :

exit (0);

break;

……

}

It’s called when a key is struck on the keyboardglutKeyboardFunc ( keyboard_callback_func );

Page 33: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

3305/05/23 16:50

33Comp 175C - Computer Graphics Dan Hebert

Special Keyboard Event

void special_callback_func ( unsigned char key, int x, int y )

{

switch (key)

{

….

It’s called when a function or direction key is struck on the keyboard

glutSpecialFunc ( special_callback_func );

Page 34: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

3405/05/23 16:50

34Comp 175C - Computer Graphics Dan Hebert

Mouse Event

void mouse_callback_func ( int button, int state, int x, int y )

- button: which button is depressed/released.

GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON

- state: action state.

GLUT_DOWN, GLUT_UP

- x, y: position

It’s called when a mouse button is depressed/released

glutMouseFunc ( mouse_callback_func );

Page 35: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

3505/05/23 16:50

35Comp 175C - Computer Graphics Dan Hebert

Mouse Event

void mouse_callback_func ( int button, int state, int x, int y )

{

if ( button == GLUT_LEFT_BUTTON && state == GLUT_DOWN )

exit (0);

……

}

Page 36: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

3605/05/23 16:50

36Comp 175C - Computer Graphics Dan Hebert

Mouse Passive Motion Event

void mouse_passivemotion_func (int x, int y )

- x, y: position

It’s called when a mouse is moved in a window and no buttons are pressed

glutPassiveMotionFunc ( mouse_passivemotion_func );

Page 37: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

3705/05/23 16:50

37Comp 175C - Computer Graphics Dan Hebert

Mouse Motion Event

void mouse_motion_func (int x, int y )

- x, y: position

It’s called when a mouse is moved in a window and one or more buttons are pressed

glutMotionFunc ( mouse_motion_func );

Page 38: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

3805/05/23 16:50

38Comp 175C - Computer Graphics Dan Hebert

Mouse Entry Event

void mouse_motion_func (int state )

- GLUT_LEFT or GLUT_ENTERED

It’s called when a mouse enters or leaves a window and one or more buttons are pressed

glutEntryFunc ( mouse_entry_func );

Page 39: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

3905/05/23 16:50

39Comp 175C - Computer Graphics Dan Hebert

Windows Events – Reshape Event

- Redraw the objects

- Keep the aspect ratio

It’s called when the window is resized or reshaped.

glutReshapeFunc ( reshape_callback_func );

Page 40: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

4005/05/23 16:50

40Comp 175C - Computer Graphics Dan Hebert

Windows Events – Reshape Event

glutReshapeFunc ( reshape_callback_func );

void reshape_callback_func ( int width, int height )

- width, height: the width and height of the new window

Page 41: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

4105/05/23 16:50

41Comp 175C - Computer Graphics Dan Hebert

Windows Events – Visibility Event

- Redraw the objects

It’s called when the visibility of a window changes.

glutVisibilityFunc ( visibility_callback_func );

Page 42: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

4205/05/23 16:50

42Comp 175C - Computer Graphics Dan Hebert

Windows Events – Visibility Event

glutVisibilityFunc ( visibility_callback_func );

void visibility_callback_func ( int state )

-state: GLUT_NOT_VISIBLE or GLUT_VISIBLE

-GLUT_VISIBLE does not distinguish a window being totally versus partially visible.

-GLUT_NOT_VISIBLE - means no part of the window is visible, i.e., until the window’s visibility changes, all further rendering to the window is discarded.

Page 43: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

4305/05/23 16:50

43Comp 175C - Computer Graphics Dan Hebert

Display Callback

void display_callback_func ( void )

{

Do all of your drawing here!

}

It’s called when the window should be redisplayed. For example, when the window is first opened. This callback function is necessary for a GLUT program

glutDisplayFunc ( display_callback_func );

Page 44: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

4405/05/23 16:50

44Comp 175C - Computer Graphics Dan Hebert

Idle Callback

void idle_callback_func ( void )

{

t += 10;

…..

}

It’s called when nothing else is going on. It is very useful for animation and continuous update.

glutIdleFunc ( idle_callback_func );

Page 45: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

4505/05/23 16:50

45Comp 175C - Computer Graphics Dan Hebert

Menu Status (State) Callback

Called with the value GLUT_MENU_IN_USE for its value parameter when pop-up menus are in use by the user; called with the value GLUT_MENU_NOT_IN_USE for its status parameter when pop-up menus are no longer in use. The x and y parameters indicate the location in window coordinates of the button press that caused the menu to go into use, or the location where the menu was released (may be outside the window). MenuState is a deprecated version of MenuStatus (no x,y parameters).

glutMenuStatusFunc(int status, int x, int y);

glutMenuStateFunc(int status);

Page 46: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

4605/05/23 16:50

46Comp 175C - Computer Graphics Dan Hebert

Menu Management

GLUT provides the pop-up menu functions that can be used with mouse for interactive control.

glutCreateMenu ( menu_callback_func );

void menu_callback_func ( int ID )

{

put your menu control here!}

Page 47: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

4705/05/23 16:50

47Comp 175C - Computer Graphics Dan Hebert

Menu Creation

glutAddMenuEntry ( const char *label, int value );

- label: the displayed name of this menu item.

- value: the ID passed to the menu callback function when this entry is selected.

Page 48: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

4805/05/23 16:50

48Comp 175C - Computer Graphics Dan Hebert

Menu Creation

glutAttachMenu ( int button );

- button: which mouse button is associated with the menu. GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON

Page 49: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

4905/05/23 16:50

49Comp 175C - Computer Graphics Dan Hebert

Implementing Choice: Menus in GLUT

0Four steps:- Create menu: glutCreateMenu(menu);- Define menu entries: glutAddMenuEntry- Attach menu to a mouse button:

glutAttachMenu- Define callback function: void menu(int id);

Page 50: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

5005/05/23 16:50

50Comp 175C - Computer Graphics Dan Hebert

Creating a Menu in GLUTint glutCreateMenu(void (*func)(int value));

Creates a new pop-up menu.

Returns a unique integer identifier for the menu.

Takes as argument a pointer to a single callback functionthat takes an integer argument. The integer argument of the callback is mapped to the menu choice.

Sets the current menu to the newly created menu.

Menu Identifier Callback Function Choice

Page 51: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

5105/05/23 16:50

51Comp 175C - Computer Graphics Dan Hebert

Associating a Menu with a Mouse Key

void glutAttachMenu(int button);

Associates the selected button with the current menu.

button is selected from the GLUT defined button constants:

GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON

Page 52: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

5205/05/23 16:50

52Comp 175C - Computer Graphics Dan Hebert

Adding Entries to the Menu

void glutAddMenuEntry(char *name, int value);

String to appearin menu entry.

Value to be passedto callback function.

Adds a menu entry to the bottom of the current menu.

Page 53: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

5305/05/23 16:50

53Comp 175C - Computer Graphics Dan Hebert

Building a Sub-menu

void glutAddSubMenu(char *name, int menu);

ASCII string to display in the menuitem from which to cascade sub-menu.

Identifier of menu to cascadefrom this sub-menu item.

Page 54: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

5405/05/23 16:50

54Comp 175C - Computer Graphics Dan Hebert

Window Management

• Standard GLUT initialization

glutInit (int argc, char ** argv)

• Display model

glutInitDisplayMode (unsigned int mode)

• Window size and position

glutInitWindowSize (int width, int height)

glutInitWindowPosition(int x, int y)

• Create window

glutCreateWindow (char *name);

Page 55: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

5505/05/23 16:50

55Comp 175C - Computer Graphics Dan Hebert

You Must Register Callback

void main (int argc, char **argv)

{

……

glutDisplayFunc ( display ); // display callback

glutReshapeFunc ( resize ); // window resize

glutKeyboardFunc ( key ); // keyboard callback

……

}

Page 56: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

5605/05/23 16:50

56Comp 175C - Computer Graphics Dan Hebert

Demonstration (look at code)

Page 57: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

5705/05/23 16:50

57Comp 175C - Computer Graphics Dan Hebert

Put Thing Together – A example

Question:0 Create a viewport in one window. The viewport is 256x256

pixels in size and has a background color of white. It shows a square with surface of blue color.

0 Using mouse right_button depressed to pop up a menu. The pop_up menu has just one entry that allows user to exit the program.

Page 58: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

5805/05/23 16:50

58Comp 175C - Computer Graphics Dan Hebert

Main Function

void main (int argc, char **argv)

{

glutInit ( &argc, argv );

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize (256, 256);

glutCreateWindow (“My First Program");

myinit ();

glutDisplayFunc ( square ); // register callback… More???

glutMainLoop ();

}

Page 59: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

5905/05/23 16:50

59Comp 175C - Computer Graphics Dan Hebert

OpenGL Initialization

void myinit (void)

{

glClearColor (1.0, 1.0, 1.0, 0.0); // background

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluOrtho2D (0.0, 256.0, 0.0, 256.0);

glMatrixMode(GL_MODELVIEW);

glClear(GL_COLOR_BUFFER_BIT); // clear the window

}

Page 60: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

6005/05/23 16:50

60Comp 175C - Computer Graphics Dan Hebert

Display Callback Function

void square (void)

{

typedef GLfloat point2D[2];

point2D vertices[4] = {{100.0, 100.0},{200.0, 100.0},{200.0, 200.0},{100.0, 200.0}};

glClear(GL_COLOR_BUFFER_BIT); // clear the window

glColor3f (0.0, 0.0, 1.0);

glBegin(GL_POLYGON);

glVertex2fv (vertices[0]); glVertex2fv (vertices[1]);

glVertex2fv (vertices[2]); glVertex2fv (vertices[3]);

glEnd();

glFlush();

}

Page 61: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

6105/05/23 16:50

61Comp 175C - Computer Graphics Dan Hebert

What it Looks Like

Page 62: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

6205/05/23 16:50

62Comp 175C - Computer Graphics Dan Hebert

Add Interaction

void main (int argc, char **argv)

{

glutInit ( &argc, argv );

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize (256, 256);

glutCreateWindow (“My First Program");

myinit ();

glutCreateMenu (menu); // register menu callback

glutAddMenuEntry(“Quit”, 1);

glutAttachMenu(GLUT_RIGHT_BUTTON);

glutDisplayFunc ( square ); // register display callback

glutMainLoop ();

}

Page 63: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

6305/05/23 16:50

63Comp 175C - Computer Graphics Dan Hebert

Menu Callback Function

void menu (int ID)

{

if ( ID == 1 )

exit (1);

}

Page 64: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

6405/05/23 16:50

64Comp 175C - Computer Graphics Dan Hebert

What it Looks Like

Page 65: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

6505/05/23 16:50

65Comp 175C - Computer Graphics Dan Hebert

Problems

Create a program with a multi-tiered menu as follows : Line Width – 4 entries to set line width from 2-5 Line Color – 5 entries to set line color (blue, green, black, white, red) Draw line – no entries, draws a line from (200, 200, 0) to (400, 400, 0)

Create a program which senses mouse movement and mouse button 3 When mouse movement occurs, display a blue point When mouse button 3 is pressed, end the program

Page 66: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

6605/05/23 16:50

66Comp 175C - Computer Graphics Dan Hebert

Problems (continued)

Create a program which senses key presses When “p” is pressed, draw a polygon When “s” is pressed, draw a square When “l” is pressed, draw a line When “q” is pressed, end program

Page 67: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

6705/05/23 16:50

67Comp 175C - Computer Graphics Dan Hebert

Example: Simple Square Drawing Program

0 Open a window.0 Clear it to black.0 Draw a box at location of the mouse each time the left button

is clicked. Color of box should be randomly selected from RGB space.

0 Clear window when resized.0 Quit when right button is clicked.

Page 68: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

6805/05/23 16:50

68Comp 175C - Computer Graphics Dan Hebert

Square Program Source CodeSlide 1

/* This program illustrates the use of the glut library forinterfacing with a Window System */

/* The program opens a window, clears it to black,then draws a box at the location of the mouse each time theleft button is clicked. The right button exits the program

The program also reacts correctly when the window ismoved or resized by clearing the new window to black*/

#include <GL/gl.h>#include <GL/glut.h>

/* globals */

GLsizei wh = 500, ww = 500; /* initial window size */GLfloat size = 3.0; /* half side length of square */

Page 69: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

6905/05/23 16:50

69Comp 175C - Computer Graphics Dan Hebert

Square Program Source CodeSlide 2

void drawSquare(int x, int y){

y=wh-y; glColor3ub( (char) random()%256, (char) random()%256, (char) random()%256); glBegin(GL_POLYGON); glVertex2f(x+size, y+size);

glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd(); glFlush();

}

Page 70: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

7005/05/23 16:50

70Comp 175C - Computer Graphics Dan Hebert

Square Program Source CodeSlide 3

void myReshape(GLsizei w, GLsizei h){

/* adjust clipping box */

glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0, (GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();

/* adjust viewport and clear */

glViewport(0,0,w,h); glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glFlush();

/* set global size for use by drawing routine */

ww = w; wh = h; }

Page 71: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

7105/05/23 16:50

71Comp 175C - Computer Graphics Dan Hebert

Square Program Source CodeSlide 4

void myinit(void){

glViewport(0,0,ww,wh);

/* Pick 2D clipping window to match size of screen window This choice avoids having to scale object coordinateseach time window is resized */

glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble) ww , 0.0, (GLdouble) wh , -1.0, 1.0);

/* set clear color to black and clear window */

glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glFlush();

/* callback routine for reshape event */

glutReshapeFunc(myReshape);

}

Page 72: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

7205/05/23 16:50

72Comp 175C - Computer Graphics Dan Hebert

Square Program Source CodeSlide 5

void mouse(int btn, int state, int x, int y){ if(btn==GLUT_RIGHT_BUTTON&state==GLUT_DOWN) exit();}

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

glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutCreateWindow("square");

myinit (); glutReshapeFunc (myReshape); glutMouseFunc (mouse); glutMotionFunc(drawSquare);

glutMainLoop();

}

Page 73: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

7305/05/23 16:50

73Comp 175C - Computer Graphics Dan Hebert

Output of “Square” Program

Page 74: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

7405/05/23 16:50

74Comp 175C - Computer Graphics Dan Hebert

Example: Paint Program

0 Select from simple geometric shapes to draw.0 Select from 8 colors.0 Turn fill on or off.0 Increase or decrease size of points.0 Quit or clear screen under menu control.

Page 75: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

7505/05/23 16:50

75Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 1

/* This program illustrates the use of theglut library forinterfacing with a window system */

/* Description of operation of program isin Chapter 3*/

#define NULL 0#include <GL/gl.h>#include <GL/glut.h>

void mouse(int, int, int, int);void display(void);void idle(void);void drawSquare(int, int);void myReshape(GLsizei, GLsizei);

void myinit(void);

void screen_box(int, int, int);void right_menu(int);void middle_menu(int);void color_menu(int);void pixel_menu(int);void fill_menu(int);long time(int);int pick(int, int);

Page 76: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

7605/05/23 16:50

76Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 2

/* globals */

GLsizei wh = 500, ww = 500; /* initialwindow size */GLfloat size = 3.0; /* half side lengthof square */int base; /* font list base */long baset; /*time base */GLfloat r = 1.0, g = 1.0, b = 1.0; /*drawing color */int fill = 0; /* fill flag */

void drawSquare(int x, int y){

y=wh-y; glColor3ub( (char) random()%256,(char) random()%256, (char)random()%256); glBegin(GL_POLYGON); glVertex2f(x+size, y+size); glVertex2f(x-size, y+size); glVertex2f(x-size, y-size); glVertex2f(x+size, y-size); glEnd();}

Page 77: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

7705/05/23 16:50

77Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 3

/* rehaping routine called whenever windowis resizedor moved */

void myReshape(GLsizei w, GLsizei h){

/* adjust clipping box */

glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble)w, 0.0,(GLdouble)h, -1.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();

/* adjust viewport and clear */

glViewport(0,0,w,h); glClearColor (1.0, 1.0, 1.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); display(); glFlush();

Page 78: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

7805/05/23 16:50

78Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 4

/* set global size for use by drawingroutine */

ww = w; wh = h;}

void myinit(void){

/* set up a font in display list */ int i;baset = time(0); base = glGenLists(128); for(i=0;i<128;i++) { glNewList(base+i,GL_COMPILE);

glutBitmapCharacter(GLUT_BITMAP_9_BY_15,i); glEndList(); } glListBase(base);

Page 79: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

7905/05/23 16:50

79Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 5

glViewport(0,0,ww,wh);

/* Pick 2D clipping window to match size ofX windowThis choice avoids having to scale objectcoordinateseach time window is resized */

glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0.0, (GLdouble) ww , 0.0,(GLdouble) wh , -1.0, 1.0);

/* set clear color to black and clearwindow */

glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glFlush();}

Page 80: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

8005/05/23 16:50

80Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 6

void mouse(int btn, int state, int x, inty){ static int draw_mode = 0; /* drawingmode */ static int count; int where; static int xp[2],yp[2];

if(btn==GLUT_LEFT_BUTTON&state==GLUT_DOWN) { glPushAttrib(GL_ALL_ATTRIB_BITS); glutIdleFunc(NULL);

where = pick(x,y); glColor3f(r, g, b); if(where != 0) { count = 0; draw_mode = where; } else if(draw_mode == 1 & count == 0) { count = 1; xp[0] = x; yp[0] = y; }

Page 81: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

8105/05/23 16:50

81Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 7

else if(draw_mode == 1 & count != 0) { glBegin(GL_LINES); glVertex2i(x,wh-y); glVertex2i(xp[0],wh-yp[0]); glEnd(); draw_mode=0; count=0; } else if(draw_mode == 2 & count == 0) { count = 1; xp[0] = x; yp[0] = y; }

Page 82: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

8205/05/23 16:50

82Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 8

else if(draw_mode == 2 & count != 0) { if(fill) glBegin(GL_POLYGON); else glBegin(GL_LINE_LOOP); glVertex2i(x,wh-y); glVertex2i(x,wh-yp[0]); glVertex2i(xp[0],wh-yp[0]); glVertex2i(xp[0],wh-y); glEnd(); draw_mode=0; count=0; } else if(draw_mode == 3 & count == 0) { count = 1; xp[0] = x; yp[0] = y; } else if(draw_mode == 3 & count == 1) { count = 2; xp[1] = x; yp[1] = y; }

Page 83: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

8305/05/23 16:50

83Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 9

else if(draw_mode == 3 & count == 2) { if(fill) glBegin(GL_POLYGON); else glBegin(GL_LINE_LOOP); glVertex2i(xp[0],wh-yp[0]); glVertex2i(xp[1],wh-yp[1]); glVertex2i(x,wh-y); glEnd(); draw_mode=0; count=0; } else if(draw_mode == 4 ) { drawSquare(x,y); count++; }

glutIdleFunc(idle); glPopAttrib(); glFlush(); }}

Page 84: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

8405/05/23 16:50

84Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 10

int pick(int x, int y){ y = wh - y; if(y < wh-ww/10) return 0; else if(x < ww/10) return 1; else if(x < ww/5) return 2; else if(x < 3*ww/10) return 3; else if(x < 2*ww/5) return 4; else return 0;}

void screen_box(int x, int y, int s ){ glBegin(GL_QUADS); glVertex2i(x, y); glVertex2i(x+s, y); glVertex2i(x+s, y+s); glVertex2i(x, y+s); glEnd();}

Page 85: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

8505/05/23 16:50

85Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 11

void idle(void){

char out[]="00:00:00"; long t, time(); int min, sec, hr;

glPushAttrib(GL_ALL_ATTRIB_BITS); t=time(0)-baset; hr=t/3600; min = (t - 3600*hr) /60; sec = (t - 3600*hr - 60*min); hr = hr%24; out[0]='0'+hr/10; out[1]='0'+hr%10; out[3]='0'+min/10; out[4]='0'+min%10; out[6]='0'+sec/10; out[7]='0'+sec%10;

Page 86: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

8605/05/23 16:50

86Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 12

glRasterPos2i(ww-80, wh-15); glColor3f(0.0,0.0,0.0); glBegin(GL_QUADS); glVertex2i(ww-80, wh-15); glVertex2i(ww, wh-15); glVertex2i(ww, wh); glVertex2i(ww-80, wh); glEnd(); glColor3f(1.0,1.0,1.0); glCallLists( strlen(out) , GL_BYTE,out); glFlush(); glPopAttrib();}

Page 87: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

8705/05/23 16:50

87Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 13

void right_menu(int id){ glutIdleFunc(NULL); if(id == 1) exit(); else display(); glutIdleFunc(idle);}

void middle_menu(int id){ glutIdleFunc(NULL); glutIdleFunc(idle);

}

void color_menu(int id){ glutIdleFunc(NULL); if(id == 1) {r = 1.0; g = 0.0; b = 0.0;} else if(id == 2) {r = 0.0; g = 1.0; b =0.0;} else if(id == 3) {r = 0.0; g = 0.0; b =1.0;} else if(id == 4) {r = 0.0; g = 1.0; b =1.0;}

Page 88: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

8805/05/23 16:50

88Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 14

else if(id == 5) {r = 1.0; g = 0.0; b =1.0;} else if(id == 6) {r = 1.0; g = 1.0; b =0.0;} else if(id == 7) {r = 1.0; g = 1.0; b =1.0;} else if(id == 8) {r = 0.0; g = 0.0; b =0.0;} glutIdleFunc(idle);}

void pixel_menu(int id){ glutIdleFunc(NULL); if (id == 1) size = 2 * size; else if (size > 1) size = size/2; glutIdleFunc(idle);}

void fill_menu(int id){ glutIdleFunc(NULL); if (id == 1) fill = 1; else fill = 0; glutIdleFunc(idle);}

Page 89: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

8905/05/23 16:50

89Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 15

void display(void){ glPushAttrib(GL_ALL_ATTRIB_BITS); glutIdleFunc(NULL); glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); screen_box(0,wh-ww/10,ww/10); glColor3f(1.0, 0.0, 0.0); screen_box(ww/10,wh-ww/10,ww/10); glColor3f(0.0, 1.0, 0.0); screen_box(ww/5,wh-ww/10,ww/10); glColor3f(0.0, 0.0, 1.0); screen_box(3*ww/10,wh-ww/10,ww/10); glColor3f(0.0, 0.0, 0.0); screen_box(ww/10+ww/40,wh-ww/10+ww/40,ww/20); glBegin(GL_LINES); glVertex2i(wh/40,wh-ww/20); glVertex2i(wh/40+ww/20,wh-ww/20); glEnd();

Page 90: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

9005/05/23 16:50

90Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 16

glBegin(GL_TRIANGLES); glVertex2i(ww/5+ww/40,wh-ww/10+ww/40); glVertex2i(ww/5+ww/20,wh-ww/40); glVertex2i(ww/5+3*ww/40,wh-ww/10+ww/40); glEnd(); glPointSize(3.0); glBegin(GL_POINTS); glVertex2i(3*ww/10+ww/20, wh-ww/20); glEnd(); glutIdleFunc(idle); glFlush(); glPopAttrib();}

Page 91: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

9105/05/23 16:50

91Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 17

int main(int argc, char** argv){ int c_menu, p_menu, f_menu;

glutInit(&argc,argv); glutInitDisplayMode (GLUT_SINGLE |GLUT_RGB); glutCreateWindow("square"); glutDisplayFunc(display); c_menu = glutCreateMenu(color_menu); glutAddMenuEntry("Red",1); glutAddMenuEntry("Green",2); glutAddMenuEntry("Blue",3); glutAddMenuEntry("Cyan",4); glutAddMenuEntry("Magenta",5); glutAddMenuEntry("Yellow",6); glutAddMenuEntry("White",7); glutAddMenuEntry("Black",8); p_menu = glutCreateMenu(pixel_menu); glutAddMenuEntry("increase pixel size",1); glutAddMenuEntry("decrease pixel size",2); f_menu = glutCreateMenu(fill_menu); glutAddMenuEntry("fill on", 1); glutAddMenuEntry("fill off", 2);

Page 92: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

9205/05/23 16:50

92Comp 175C - Computer Graphics Dan Hebert

Source Code for Paint ProgramSlide 18

glutCreateMenu(right_menu); glutAddMenuEntry("quit",1); glutAddMenuEntry("clear",2); glutAttachMenu(GLUT_RIGHT_BUTTON); glutCreateMenu(middle_menu); glutAddSubMenu("Colors", c_menu); glutAddSubMenu("Pixel Size", p_menu); glutAddSubMenu("Fill", f_menu); glutAttachMenu(GLUT_MIDDLE_BUTTON); myinit (); glutReshapeFunc (myReshape); glutMouseFunc (mouse); glutIdleFunc(idle); glutMainLoop();

}

Page 93: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

9305/05/23 16:50

93Comp 175C - Computer Graphics Dan Hebert

Output of “Paint” Program

Page 94: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

9405/05/23 16:50

94Comp 175C - Computer Graphics Dan Hebert

Animating a Program

Look at single and change from single to double buffering

Page 95: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

9505/05/23 16:50

95Comp 175C - Computer Graphics Dan Hebert

Single Buffering vs Double Buffering

Single buffering is good for static graphics, double buffering is necessary for animated graphics

glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB );

glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB ); and

glutSwapBuffers ();

Page 96: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

9605/05/23 16:50

96Comp 175C - Computer Graphics Dan Hebert

Animating a Program continued)

Run singledouble program

Page 97: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

9705/05/23 16:50

97Comp 175C - Computer Graphics Dan Hebert

Problems

Create a program which does the following : Draws a triangle, not an equilateral triangle Rotates the triangle and moves it across the screen Senses left mouse button down and moves the triangle to the mouse position, it then continues rotating and moving Senses right mouse button down and ends the program

Page 98: 12/22/2016 21:38 1 Comp 175C - Computer Graphics Dan Hebert Computer Graphics Comp 175 Chapter 3 Instructor: Dan Hebert

9805/05/23 16:50

98Comp 175C - Computer Graphics Dan Hebert

Homework Assignment

0 Develop an OpenGl program which has the following :- A menu and 1 or more submenus attached to the right

mouse button to select 1 of 4 different graphics to draw on the screen

- Keyboard input to determine which color to draw the selected graphic

- Mouse movement to define where to draw the selected graphic

0 Graphic can be as simple as a primitive (triangle, square, etc) or a more complex drawing

0 Due February 21 at start of class