0% found this document useful (0 votes)
2 views29 pages

Chapter 04

The document discusses various topics in computer graphics input and interaction, including basic input devices, window-based programming, keyboard events, mouse events, reshape events, and idle events. It provides details on event-driven programming, callback functions, and how to register callbacks for different types of events in OpenGL using GLUT. It also covers double buffering and GLUT widgets like menus.

Uploaded by

Nhat Minh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views29 pages

Chapter 04

The document discusses various topics in computer graphics input and interaction, including basic input devices, window-based programming, keyboard events, mouse events, reshape events, and idle events. It provides details on event-driven programming, callback functions, and how to register callbacks for different types of events in OpenGL using GLUT. It also covers double buffering and GLUT widgets like menus.

Uploaded by

Nhat Minh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Hochiminh city University of Technology

Faculty of Computer Science and Engineering

COMPUTER GRAPHICS

CHAPTER 04:
Input & Interaction
OUTLINE
 Basic Input Devices
 Window-based Programming
 Keyboard Event
 Mouse Event
 Reshape Event
 Idle Event

Faculty of Computer Science and Engineering - HCMUT Slide 2


Basic input devices
 Physical Devices

mouse trackball
light pen

data tablet joy stick space ball

Faculty of Computer Science and Engineering - HCMUT Slide 3


Windows-based programming
 Event Mode
– Most systems have more than one input device, each
of which can be triggered at an arbitrary time by a
user
– Each trigger generates an event whose measure is
put in an event queue which can be examined by the
user program

Faculty of Computer Science and Engineering - HCMUT Slide 4


Windows-based programming
 Event-driven programming
 Event queue
 Callback function
 Register callback function
• glutDisplayFunc(myDisplay)
• glutReshapeFunc(myReshape)
• glutMouseFunc(myMouse)
• glutKeyboardFunc(myKeyboard)

Faculty of Computer Science and Engineering - HCMUT Slide 5


Windows-based programming

Faculty of Computer Science and Engineering - HCMUT Slide 6


Windows-based programming

ID:….
Event:…
X: …
Y: …
……

Faculty of Computer Science and Engineering - HCMUT Slide 7


Windows-based programming

Faculty of Computer Science and Engineering - HCMUT Slide 8


Windows-based programming
Event Queue

Faculty of Computer Science and Engineering - HCMUT Slide 9


Windows-based programming

Faculty of Computer Science and Engineering - HCMUT Slide 10


Windows-based programming

Faculty of Computer Science and Engineering - HCMUT Slide 11


Keyboard Event
 Normal Key
- Callback function Prototype:
void myKeyboard(unsigned char key, int x, int y);
- Register callback function
glutKeyboardFunc(myKeyboard);
- Example
void myKeyboard(unsigned char key, int x, int y){
if(key == ‘Q’ | key == ‘q’)
exit(0);
}

Faculty of Computer Science and Engineering - HCMUT Slide 12


Keyboard Event
 Function Key
- Callback function Prototype:
void myKeyboard(unsigned char key, int x, int y);
- Register callback function
glutSpecialFunc(myKeyboard);
- Example
void myKeyboard(unsigned char key, int x, int y){
if (key == GLUT_KEY_LEFT) angle = angle + 5;
else if (key == GLUT_KEY_RIGHT) angle = angle - 5;
glutPostRedisplay();
}

Faculty of Computer Science and Engineering - HCMUT Slide 13


Mouse Event
 Mouse Click
- Callback function Prototype:
void myMouse(int button, int state, int x, int y);
• which button (GLUT_LEFT_BUTTON,
GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON)
caused event
• state of that button (GLUT_UP, GLUT_DOWN)
• Mouse Position in window
- Register callback function
glutMouseFunc(myMouse);

Faculty of Computer Science and Engineering - HCMUT Slide 14


Mouse Event
 Mouse Click
glutInitWindowSize(W, H);
Y’
X
t
x’, y’ W
l r X’ x, y
b
H
glOrtho(l, r, b, t, -1, 1); Y
x’ = A.x + B
r = A.W + B x’ = ((r-l)/W).x + l y’ = ((b-t)/H).y + t
l = A.0 + B

Faculty of Computer Science and Engineering - HCMUT Slide 15


Mouse Event
 Mouse Move
– glutMotionFunc: mouse buttons are pressed
– glutPassiveMotionFunc: no mouse buttons are
pressed
– Callback Function Prototype
void myMoveMouse(int x, int y)

Faculty of Computer Science and Engineering - HCMUT Slide 16


Reshape Event

original
reshaped

Faculty of Computer Science and Engineering - HCMUT Slide 17


Reshape Event
 The reshape callback
– glutReshapeFunc(myreshape);
– void myReshape( int w, int h)
• Returns width and height of new window (in pixels)
– A redisplay is posted automatically at end of execution
of the callback
– GLUT has a default reshape callback but you probably
want to define your own
– The reshape callback is good place to put viewing
functions because it is invoked when the window is
first opened

Faculty of Computer Science and Engineering - HCMUT Slide 18


Reshape Event
void myReshape(int w, int h){
glViewport(0, 0, w, h);
}

Faculty of Computer Science and Engineering - HCMUT Slide 19


Reshape Event

2*(h/w)

h -2 2

w -2*(h/w)

Faculty of Computer Science and Engineering - HCMUT Slide 20


Reshape Event
void myReshape(int w, int h){
float factor = 2;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-factor, factor, -factor * h / w,
factor * h / w, -10.0, 10.0);
else
glOrtho(-factor * w / h, factor * w / h,
-factor, factor, -10.0, 10.0);
}

Faculty of Computer Science and Engineering - HCMUT Slide 21


Reshape Event

-2*(w/h) 2*(w/h)

-2
Faculty of Computer Science and Engineering - HCMUT Slide 22
Reshape Event
void myReshape(int w, int h){
float factor = 2;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-factor, factor, -factor * h / w,
factor * h / w, -10.0, 10.0);
else
glOrtho(-factor * w / h, factor * w / h,
-factor, factor, -10.0, 10.0);
}

Faculty of Computer Science and Engineering - HCMUT Slide 23


Idle Event
 The idle callback is executed whenever there are no events in
the event queue
• glutIdleFunc(myidle)
• Useful for animations
void myidle() {
/* change something */
t += dt
glutPostRedisplay();
}
void mydisplay() {
glClear();
/* draw something that depends on t */
glutSwapBuffers();
}

Faculty of Computer Science and Engineering - HCMUT Slide 24


Double Buffer
 Double Buffering
– Instead of one color buffer, we use two
• Front Buffer: one that is displayed but not written to
• Back Buffer: one that is written to but not displayed
– Program then requests a double buffer in main.c
• glutInitDisplayMode(GL_RGB | GL_DOUBLE)
• At the end of the display callback buffers are swapped
• void mydisplay() {
glClear(GL_COLOR_BUFFER_BIT|….)
/* draw graphics here */
glutSwapBuffers()
}

Faculty of Computer Science and Engineering - HCMUT Slide 25


Toolkits and Widgets
 Most window systems provide a toolkit or library of
functions for building user interfaces that use special
types of windows called widgets
 Widget sets include tools such as
– Menus
– Slidebars
– Dials
– Input boxes
 But toolkits tend to be platform dependent
 GLUT provides a few widgets including menus

Faculty of Computer Science and Engineering - HCMUT Slide 26


Toolkits and Widgets
 Menus
– GLUT supports pop-up menus
• A menu can have submenus
– Three steps
• Define entries for the menu
• Define action for each menu item
→Action carried out if entry selected
• Attach menu to a mouse button

Faculty of Computer Science and Engineering - HCMUT Slide 27


Toolkits and Widgets
 Menus

Faculty of Computer Science and Engineering - HCMUT Slide 28


Toolkits and Widgets
 Menu
– Menu callback
void mymenu(int id) {
if(id == 1) glClear();
if(id == 2) exit(0);
}
– Note each menu has an id that is returned when it
is created
– Add submenus by
• glutAddSubMenu(char *submenu_name,
submenu id)

Faculty of Computer Science and Engineering - HCMUT Slide 29

Common questions

Powered by AI

An OpenGL application manages continuous animations using idle events by executing the idle callback whenever there are no pending events in the event queue. This callback can update animation parameters, such as object positions or angles, incrementally over time. By adjusting variables that affect rendering, the application can create the appearance of smooth motion. After making changes, glutPostRedisplay is called to trigger a redisplay, ensuring the window content is refreshed with the updated states, maintaining seamless animations .

In the event-driven programming model of an OpenGL application, the event queue operates as a mechanism to handle multiple asynchronous inputs from various input devices. Each time an event is triggered, such as a mouse click or a keyboard press, it is placed in the event queue. The program sequentially processes these events, triggering registered callback functions in response to specific events. This structure allows applications to respond to user inputs only when necessary, optimizing resource usage and enabling complex interactivity within the application .

In an event-driven OpenGL application, mouse events are handled using callback functions registered with GLUT. These functions can handle specific mouse events, such as clicks and movements. For example, glutMouseFunc is used to register a callback function for mouse click events, where different states such as button press or release can be captured using parameters indicating the button involved and its state. Mouse motion with buttons pressed is managed by glutMotionFunc, whereas movements without buttons pressed are captured by glutPassiveMotionFunc, both of which require a callback function to process the new x and y coordinates of the mouse .

Double buffering in OpenGL plays a critical role in rendering smooth graphics by minimizing flickering and visual artifacts. It involves using two color buffers: the front buffer and the back buffer. The front buffer is displayed to the user, while the back buffer is where the rendering happens. Once the rendering is complete, the buffers are swapped, making the back buffer visible without exposing incomplete rendering processes. This is implemented by requesting a double buffer with glutInitDisplayMode(GL_RGB | GL_DOUBLE) and using glutSwapBuffers at the end of the display callback to swap the back and front buffers .

The glOrtho function in the reshape callback is significant for configuring the orthographic projection of rendering in response to changes in window dimensions. It ensures that the aspect ratio is maintained or adjusted as the window is resized. Depending on whether the width is less than or equal to the height or vice versa, glOrtho adjusts its left, right, bottom, and top parameters to preserve the intended view of the scene, compensating for any distortions due to non-square window shapes. For instance, when width is less, parameters are scaled accordingly to maintain proportion .

GLUT's use of callback functions enhances interactive graphics programming by providing a structured way to handle simultaneous user interactions with minimal coding complexity. By registering specific functions to handle events like keyboard input, mouse movements, or window reshaping, applications can dynamically respond to user actions in real-time. This modularity allows developers to implement complex user interactions without continuously polling for input, thus improving performance and allowing better focus on rendering and application logic .

Platform dependency is a concern when using toolkits and widgets in OpenGL because different operating systems and hardware environments may have varying implementations and support for interface elements, leading to inconsistent user experiences and additional development overhead to maintain cross-platform compatibility. GLUT addresses this issue by providing a simplified, system-independent API for managing basic windowing features and user interface elements like menus. This abstraction allows developers to create consistent applications across platforms without delving into the specifics of each system's native GUI libraries .

GLUT provides several advantages for creating user interfaces with menus in OpenGL applications. First, it abstracts the complexities involved in managing system-dependent toolkit functionalities, offering a platform-independent way of handling menus. GLUT supports pop-up menus and allows them to have multiple entries and submenus, making the interface flexible and interactive. This high-level menu management simplifies the development process, as developers only need to define menu entries, corresponding actions, and attach the menu to mouse buttons. GLUT's support for these features makes it an efficient choice for rapid prototyping and educational purposes .

GLUT's reshape callback can be customized beyond its default behavior by defining a bespoke function that manages how the rendering context adapts to new window sizes. While GLUT provides a default callback for reshaping events, developers often override it to implement custom viewing transformations or other window-specific configurations. This can include recalculating aspect ratios, modifying zoom levels, or applying special effects depending on the new window dimensions or user settings, thus providing enhanced control over the visual presentation of the application .

The reshape callback function in an OpenGL application manages changes in window dimensions by defining a new viewport and projection matrix according to the new width and height of the window. Specifically, it uses functions like glViewport to set the viewport parameters and glOrtho to adjust the projection parameters. The function checks whether the width is less than or equal to the height to determine the aspect ratio changes and adjusts the orthographic projection correspondingly .

You might also like