haptic & direct user input with directinput ® 8 api graphics lab. korea univ

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

Upload: pearl-stewart

Post on 17-Jan-2016

229 views

Category:

Documents


4 download

TRANSCRIPT

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

Haptic & Direct User Input with DirectInput®8

API

Graphics Lab.

Korea Univ.

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

cgvr.korea.ac.kr

CGVR

2 Graphics Lab. / Korea Univ.

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

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

cgvr.korea.ac.kr

CGVR

3 Graphics Lab. / Korea Univ.

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

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

DirectInput®8 API

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

cgvr.korea.ac.kr

CGVR

5 Graphics Lab. / Korea Univ.

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

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

cgvr.korea.ac.kr

CGVR

6 Graphics Lab. / Korea Univ.

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

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

cgvr.korea.ac.kr

CGVR

7 Graphics Lab. / Korea Univ.

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

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

cgvr.korea.ac.kr

CGVR

8 Graphics Lab. / Korea Univ.

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

}

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

cgvr.korea.ac.kr

CGVR

9 Graphics Lab. / Korea Univ.

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;

}

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

cgvr.korea.ac.kr

CGVR

10 Graphics Lab. / Korea Univ.

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;

}

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

cgvr.korea.ac.kr

CGVR

11 Graphics Lab. / Korea Univ.

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;

}

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

cgvr.korea.ac.kr

CGVR

12 Graphics Lab. / Korea Univ.

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

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

cgvr.korea.ac.kr

CGVR

13 Graphics Lab. / Korea Univ.

Example - Keyboard

Gaining Access to the Device

// The application must acquire the device before retrieving data from it

if (g_lpDIDevice)

g_lpDIDevice->Acquire();

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

cgvr.korea.ac.kr

CGVR

14 Graphics Lab. / Korea Univ.

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 }

}

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

cgvr.korea.ac.kr

CGVR

15 Graphics Lab. / Korea Univ.

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;

}

}

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

cgvr.korea.ac.kr

CGVR

16 Graphics Lab. / Korea Univ.

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;

}

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

cgvr.korea.ac.kr

CGVR

17 Graphics Lab. / Korea Univ.

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;

}

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

Haptic Device

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

cgvr.korea.ac.kr

CGVR

19 Graphics Lab. / Korea Univ.

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

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

cgvr.korea.ac.kr

CGVR

20 Graphics Lab. / Korea Univ.

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 Feedbackin DirectInput®

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

cgvr.korea.ac.kr

CGVR

21 Graphics Lab. / Korea Univ.

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

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

cgvr.korea.ac.kr

CGVR

22 Graphics Lab. / Korea Univ.

Graphical Representation of Ramp Force with Envelope

Periodic Force

Envelope

Resultant Force

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

cgvr.korea.ac.kr

CGVR

23 Graphics Lab. / Korea Univ.

Simple Demonstration

Force EditorProvided by MicroSoft DirectX®

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

cgvr.korea.ac.kr

CGVR

24 Graphics Lab. / Korea Univ.

Simple Demonstration

•Force Added

•Loading Effect File

•Gatling

•Crash

•Engine Vibration