Presentation is loading. Please wait.

Presentation is loading. Please wait.

Haptic & Direct User Input with DirectInput ® 8 API Graphics Lab. Korea Univ.

Similar presentations


Presentation on theme: "Haptic & Direct User Input with DirectInput ® 8 API Graphics Lab. Korea Univ."— Presentation transcript:

1 Haptic & Direct User Input with DirectInput ® 8 API Graphics Lab. Korea Univ.

2 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 2 User Input & Virtual World DirectInput ® 8 API Basic Steps of DirectInput ® Programming Examples Using Haptic Device Basic Concepts of Force Feedback in DirectInput ® Demonstration Contents

3 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 3 Interaction Styles and Metaphors 3D “ real world ” metaphor as opposed to a 2D “ paper ” metaphor Be easy and intuitive to explore virtual world Communicate with Virtual World fast and with less delay Typical forms of input devices in PC Keyboard, Mouse, Joystick, Trackball etc. User Input & Virtual World

4 DirectInput ® 8 API

5 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 5 Faster access to input data by communicating directly with the hardware drivers rather than relying on Microsoft Windows ® messages. Support with any type of input devices as well as force feedback Each DirectInputDevice object in turn has device objects, which are individual controls or switches such as keys, buttons, or axes DirectInput ® API

6 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 6 DirectInput does not provide any advantages for applications that use the keyboard for text entry or the mouse for navigation with mouse cursor Buffered data vs. Immediate data Buffered data - A record of events that are stored until an application retrieves them Immediate data - A snapshot of the current state of a device DirectInput ® API

7 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 7 Creating DirectInput Object Creating the DirectInput Device Setting Data Format Setting Device Behavior Gaining Access to the Device Retrieving Data from the Device Closing Down the DirectInput System Basic Steps of DirectInput ® Programming

8 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 8 Example - Keyboard Creating DirectInput Object HINSTANCE g_hinst; HRESULT hr; LPDIRECTINPUT8 g_lpDI; // DirectInput8 Interface hr = DirectInput8Create( g_hinst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&g_lpDI, NULL); if FAILED(hr) { // DirectInput not available; take appropriate action }

9 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 9 Example - Keyboard Creating DirectInput Device HRESULT hr; LPDIRECTINPUTDEVICE8 g_lpDIDevice; // Device for Keyboard hr = g_lpDI->CreateDevice(GUID_SysKeyboard, &g_lpDIDevice, NULL); // GUID_SysMouse // GUID of Joystick is need to enumerate if FAILED(hr) { DI_Term(); // Terminate DirectInput  Described later return FALSE; }

10 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 10 Example - Keyboard Setting Data Format hr = g_lpDIDevice->SetDataFormat(&c_dfDIKeyboard); // c_dfDIMouse, c_dfDIMouse2  Mouse // c_dfDIJoystick, c_dfDIJoystick2  Joystick things // These are predefined global variables if FAILED(hr) { DI_Term(); return FALSE; }

11 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 11 Example - Keyboard Setting Device Behavior // Set the cooperative level hr = g_lpDIDevice->SetCooperativeLevel(g_hwnd, // Window handle DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); // Set foreground access and non-exclusive mode if FAILED(hr) { DI_Term(); return FALSE; }

12 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 12 Example – Keyboard (Behavior Flags) DISCL_BACKGROUND The application requires background access. DISCL_EXCLUSIVE The application requires exclusive access. DISCL_FOREGROUND The application requires foreground access. If foreground access is granted, the device is automatically unacquired when the associated window moves to the background. DISCL_NONEXCLUSIVE The application requires nonexclusive access. DISCL_NOWINKEY Disable the Windows logo key. Applications must specify either DISCL_FOREGROUND or DISCL_BACKGROUND; it is an error to specify both or neither. Similarly, applications must specify either DISCL_EXCLUSIVE or DISCL_NONEXCLUSIVE

13 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 13 Example - Keyboard Gaining Access to the Device // The application must acquire the device before retrieving data from it if (g_lpDIDevice) g_lpDIDevice->Acquire();

14 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 14 Example - Keyboard Retrieving data from Keyboard void ProcessKBInput()// Create custom function that will be called in the loop continuously { char buffer[256]; HRESULT hr; hr = g_lpDIDevice->GetDeviceState(sizeof(buffer),(LPVOID)&buffer);// use immediate data if FAILED(hr) { // If it failed, the device has probably been lost. Try to acquire again. hr = g_lpDIDevice ->Acquire(); while( hr == DIERR_INPUTLOST ) // Try until acquired hr = g_lpDIDevice ->Acquire(); return; } // If an element's high bit is 1, the key was down, if not, the key was down if (buffer[DIK_RIGHT] & 0x80){ // Right arrow key pressed} else if(buffer[DIK_LEFT] & 0x80){ // Left arrow key pressed} if (buffer[DIK_UP] & 0x80) { // Up arrow key pressed } else if (buffer[DIK_DOWN] & 0x80){ // Down arrow key pressed} }

15 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 15 Example - Keyboard Closing down the DirectInput System void DI_Term() // Terminate DirectInput { if (g_lpDI) { if (g_lpDIDevice) {// Always unacquire device before Release g_lpDIDevice->Unacquire(); g_lpDIDevice->Release(); g_lpDIDevice = NULL; } g_lpDI->Release(); g_lpDI = NULL; }

16 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 16 Example - Mouse Retrieve mouse data in immediate mode HRESULT ProcessData( HWND hDlg )// Immediate access { HRESULT hr; DIMOUSESTATE2 dims2; // DirectInput mouse state structure if( NULL == g_pMouse ) return S_OK; // Get the input's device state, and put the state in dims ZeroMemory( &dims2, sizeof(dims2) ); hr = g_pMouse->GetDeviceState( sizeof(DIMOUSESTATE2), &dims2 ); if( FAILED(hr) ) { // If input is lost then acquire and keep trying hr = g_pMouse->Acquire(); while( hr == DIERR_INPUTLOST ) hr = g_pMouse->Acquire(); return S_OK; }

17 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 17 Example - Mouse Retrieve mouse data and display in text _stprintf( strNewText, // Display retrieved data in text TEXT("(X=% 3.3d, Y=% 3.3d, Z=% 3.3d) B0=%c B1=%c B2=%c B3=%c B4=%c B5=%c B6=%c B7=%c"), dims2.lX, // X-Axis dims2.lY, // Y-Axis dims2.lZ,// Z-Axis (Wheel) (dims2.rgbButtons[0] & 0x80) ? '1' : '0',// Buttons.... (dims2.rgbButtons[1] & 0x80) ? '1' : '0', (dims2.rgbButtons[2] & 0x80) ? '1' : '0', (dims2.rgbButtons[3] & 0x80) ? '1' : '0', (dims2.rgbButtons[4] & 0x80) ? '1' : '0', (dims2.rgbButtons[5] & 0x80) ? '1' : '0', (dims2.rgbButtons[6] & 0x80) ? '1' : '0', (dims2.rgbButtons[7] & 0x80) ? '1' : '0'); return S_OK; }

18 Haptic Device

19 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 19 What is haptic interaction? A haptic interface is a force reflecting device which allows a user to touch, feel, manipulate, create and/or alter simulated 3D objects in a virtual environment Haptic Of or relating to the sense of touch; tactile Greek haptikos, from haptesthai, to grasp, touch Haptics touch, tactile, force-feedback, texture, heat, vibration Using Haptic Device

20 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 20 A particular instance of force feedback is called an effect, and the push or resistance is called the force. Most effects fall into one of the following categories: Constant force  A steady force in a single direction. Ramp force  A force that steadily increases or decreases in magnitude. Periodic effect  A force that pulsates according to a defined wave pattern. Condition  A reaction to motion or position along an axis. Two examples are a friction effect that generates resistance to movement of the joystick, and a spring effect that pushes the stick back toward a certain position after it has been moved from that position. Basic Concepts of Force Feedback in DirectInput ®

21 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 21 Magnitude Magnitude of Force : -10,000 to 10,000 Direction Direction of Force Duration Measured in microseconds Period Duration of one cycle, also measured in microseconds Envelope Defines an attack value and a fade value, which modify the beginning and ending magnitude of the effect Sustain level Defined by the basic magnitude of the force to which the envelope is being applied Components of a Force

22 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 22 Graphical Representation of Ramp Force with Envelope Periodic Force Envelope Resultant Force

23 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 23 Simple Demonstration Force Editor Provided by MicroSoft DirectX ®

24 CGVR Graphics Lab. / Korea Univ. cgvr.korea.ac.kr 24 Simple Demonstration Force Added Loading Effect File Gatling Crash Engine Vibration


Download ppt "Haptic & Direct User Input with DirectInput ® 8 API Graphics Lab. Korea Univ."

Similar presentations


Ads by Google