35
Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Embed Size (px)

Citation preview

Page 1: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.1

Page 2: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Mengenalkan basic input devices◦ Physical Devices◦ Logical Devices◦ Input Modes

Event-driven input : Input berbasis event Introduce double buffering for smooth

animations Programming event input with GLUT

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.2

Page 3: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Karaketristik komputer grafik interaktif -Ivan Sutherland (MIT 1963) :◦ User melihat object pada display◦ User menunjuk (picks) object dengan input device (light

pen, mouse, trackball)◦ Perubahan object (moves, rotates, morphs)◦ Repeat

04.3

ChangeImage

Reactto

Change

Graphics System User

InputDevice

Display

Page 4: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Alat input dapat dibedakan bdsk◦ Physical properties

Mouse Keyboard Trackball

◦ Logical Properties What is returned to program via API

A position An object identifier

Modes◦ How and when input is obtained

request event

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.4

Page 5: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.5

mouse trackball light pen

data tablet joy stick space ball

Page 6: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Alat input menyerupai pena yang memancarkan sinar untuk dituliskan langsung pada layar monitor atau pada bidang datar yang khusus

Light pen memiliki sensor yang dapat mengirimkan sinyal cahaya yang kemudian direkam dan dihubungkan dengan kabel ke komputer untuk diintrepretasikan

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.6

Page 7: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Berfungsi seperti mouse tapi penggunaannya dengan memutar bola

Punya tombol left & right click

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.7

Page 8: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Jika Trackball biasa digunakan pada aplikasi 2 dimensi, maka Spaceball digunakan pada aplikasi 3 dimensi, virtual reality dan pemodelan

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.8

Page 9: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Suriyong L., Computer Multimedia 9

IBM spaceball

Page 10: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Cursor Positioning

Alat input seperti tablet akan memberikan data input berupa current position ke komputer

Sedangkat alat input berupa mouse, trackball, and joy stick akan memberikan incremental inputs (or velocities) ke komputer

Pada incremental device (ex:mouse), inputannya berupa :◦ Position: almost relative value, absolute value is rarely

used ◦ Velocity: small deviation, small change, large deviation,

large changeRelative positioning, used by measure moving of the

mouse

04.10

Page 11: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Input dari sudut pandang logika program Perhatikan C dan C++ code berikut

◦ C++: cin >> x;◦ C: scanf (”%d”, &x);

Apa alat inputnya?◦ Can’t tell from the code◦ Could be keyboard, file, output from another

program The code provides logical input

◦ Suatu data input dikembalikan ke program tanpa mempedulikan alat input fisiknya berupa apa

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.11

Page 12: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Figures from Angle's textbook 12

PhysicalDevice

Application Program

Software Driver

Logical Device

Mouse, KeyboardTrackball, etc.

Page 13: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Pada API sebelum Open GL, yaitu GKS dan PHIGS, didefinisikan enam tipe logical input

◦ Locator: return a position in coordinat system◦ Pick: return ID of an object◦ Keyboard: return strings of characters◦ Stroke: return array of positions◦ Valuator: analog input (ex: slidebar), return floating point number◦ Choice: return one of n items (ex: radio button)

Programmer's Hierarchical Interactive Graphics System (PHIGS) : the 1990’s API standards for 3D graphics

Graphical Kernel System (GKS) – API standards for 2D graphics

Input untuk grafis lebih bervariasi daripada input pada program biasa (umumnya berupa numbers, characters, or bits)

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.13

Page 14: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Input devices menggunakan trigger untuk mengirim sinyal ke OS◦ Button on mouse◦ Pressing or releasing a key

When triggered, input devices return information (their measure) to the system◦ Mouse returns position information◦ Keyboard returns ASCII code

Ex: ◦ keyboard:

Measure : string Trigger : when press “enter” or “return”

◦ Mouse: Measure : screen position Trigger : when press button

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.14

Page 15: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Suriyong L., Computer Multimedia 15

Request mode

Event-mode model

Sample mode

Input ModeInput ModeLogical input devices can be operated in three modes: Request, Sample, and Event

Page 16: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Suriyong L., Computer Multimedia 16

◦Sample mode:Measured data return immediately, no trigger required

◦For example the current cursor position, without waiting for a trigger

Sample mode

Page 17: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Input provided to program only when user triggers the device

ex. “scanf” : Collect measure, waiting for trigger Typical of keyboard input

◦ Can erase (backspace), edit, correct until key (the trigger) is depressed

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.17

Page 18: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Pada event mode, aplikasi dapat mengaktifkan beberapa input device secara simultan

Ex. Flight simulator -> joystick, dial, buttons and switches, we do not know which one the pilot will use

Setiap trigger menghasilkan event dimana measure akan ditempatkan padaevent queue untuk kemudian diperiksa oleh program

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.18

Page 19: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Window: resize, expose, iconify Mouse: click one or more buttons Motion: move mouse Keyboard: press or release a key Idle: nonevent

◦ Define what should be done if no other event is in queue

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.19

Page 20: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Setiap program GLUT pada main.c selalu diakhiri denganglutMainLoop();untuk menempatkan program pada infinite event loop

Untuk setiap looping, GLUT :◦ Memeriksa event queue◦ Untuk setiap event pada queue, GLUT mengeksekusi

fungsi callback jika fungsi tsb sudah didefinisikan sebelumnya

◦ Jika definisi fungsi callback belum didefinisikan maka event tsb diabaikan

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.20

Page 21: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Suriyong L., Computer Multimedia 21

glutMainLoop(); /* an event loop function */ GLUT Callback Function

◦ glutDisplayFunc◦ glutMouseFunc◦ glutReshapeFunc◦ glutKeyboardFunc◦ glutIdleFunc

Page 22: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

22

Eventin

Queue

Pop EventFrom Queue

CallFunction n

Switch onEvent Type

CallFunction nCallFunction n

Call IdleFunction

No

Yes

CallFunction nCall

Function nCallsFunction n

Page 23: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Suriyong L., Computer Multimedia 23

void mouse(int button, int state, int x, int y) {if(button==GLUT_LEFT_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); glutDisplayFunc(display); glutMainLoop();}

Mouse CallbackMouse Callback

Page 24: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

24

GLUT_LEFT_BUTTONGLUT_MIDDLE_BUTTONGLUT_RIGHT_BUTTON

GLUT_UPGLUT_DOWN

Page 25: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Suriyong L., Computer Multimedia 25

Glut provide pop-up menu, can use with mouse use:

glutCreateMenu(); // create menu

glutAddEntryMenu(); // add the menu entry Menu which has sub

menu

Page 26: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Suriyong L., Computer Multimedia 26

Page 27: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Suriyong L., Computer Multimedia 27

Window events when◦ Redraw, resize◦ Aspect ratio◦ Attribute of new primitive◦ Use

glutReshapeFunc(reshape_function);

Page 28: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Suriyong L., Computer Multimedia 28

When key pressed Use

◦ glutKeyboardFunc(keyboad_function);

void keyboard_function(unsigned char key, int x, int y) {

if(key == ‘q’ || key ==‘Q’)

exit();

}

Page 29: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Callback Display Func dieksekusi ketika GLUT memerintahkan window untuk di-refresh◦ When the window is first opened◦ When the window is reshaped◦ When the user program decides it wants to change

the display In main.c

◦ glutDisplayFunc(mydisplay) identifies the function to be executed

◦ Every GLUT program must have a display callback

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.29

Page 30: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Pada animasi, fungsi display perlu dipangil berkali-kali -> tidak efisien

We can avoid this problem by instead usingglutPostRedisplay();

With this function, for each iteration of the mainloop, your registered display() function is called

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.30

Page 31: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Single buffering◦ objects are always rendered into the same

framebuffer, which is always being displayed delay of clearing of and re-drawing into frame-buffer

will cause flicker if re-draw takes longer than refresh or refresh and animation not synced

Double buffering◦ keep two buffers (front and back)◦ always display front, always render into back*◦ swap front and back when rendering complete

Page 32: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

Suriyong L., Computer Multimedia 32

Former graphic the use single buffer ◦ Redraw directly on graphic

Flicker may happen especially the animation User need to refresh the display at the display rate (50-

70Hz) The double buffer can resolve

◦ One for display (front buffer)◦ One for redraw (back buffer)◦ Both is swappable for smooth graphic◦ Using glutSwapBuffers() at display function◦ Initial display mode set use GLUT_DOUBLE mode

set at glutInitDisplayMode();◦ (see at rotate square double buffers.exe)

Page 33: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

The idle callback is executed whenever there are no events in the event queue◦ glutIdleFunc(myidle)◦ Useful for animations

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.33

void myidle() {/* change something */

t += dtglutPostRedisplay();

}

Void mydisplay() {glClear();

/* draw something that depends on t */glutSwapBuffers();

}

Page 34: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

The form of all GLUT callbacks is fixed◦ void mydisplay()◦ void mymouse(GLint button, GLint state, GLint x, GLint y)

Must use globals to pass information to callbacks

Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005

04.34

float t; /*global */

void mydisplay(){/* draw something that depends on t}

Page 35: Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 04.104.1

35

glutPassiveMotionFuncglutVisibilityFuncglutEntryFuncglutSpecialFuncglutSpaceballMotionFuncglutSpaceballRotateFuncglutSpaceballButtonFuncglutButtonBoxFuncglutDialsFuncglutTabletMotionFuncglutTabletButtonFuncglutMenuStatusFunc