aboutsummaryrefslogtreecommitdiffstats
path: root/Samples/OculusRoomTiny/Win32_OculusRoomTiny_Util.cpp
blob: f371ce1d11a19287cb3f482d5ca6afa9a80bec3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/************************************************************************************

Filename    :   Win32_OculusRoomTiny_Util.cpp
Content     :   Win32 system interface & app/graphics initialization ligic  
Created     :   October 4, 2012

Copyright   :   Copyright 2012 Oculus, Inc. All Rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************************/

#include "RenderTiny_D3D11_Device.h"
#include "OVR_CAPI.h"
#include "../CommonSrc/Platform/Win32_Gamepad.h"

// Win32 System Variables
HWND                hWnd = NULL;
HINSTANCE           hInstance;    
POINT               WindowCenter; 

// User inputs
bool                Quit = 0;
uint8_t             MoveForward = 0,
                    MoveBack = 0,
                    MoveLeft = 0,
                    MoveRight = 0;

bool                ShiftDown = false,
                    ControlDown = false;

// Freezes the scene during timewarp rendering.
bool                FreezeEyeRender = false;   

float               AdditionalYawFromMouse = 0;

// Movement speed, in m/s applied during keyboard motion.
const float         MoveSpeed   = 3.0f;

extern ovrHmd       HMD;

// Functions from Win32_OculusRoomTiny.cpp
int     Init();
void    ProcessAndRender();
void    Release();

// Gamepad Variables
OvrPlatform::Win32::GamepadManager GamepadManager;
OvrPlatform::GamepadState LastGamepadState = OvrPlatform::GamepadState();

//-------------------------------------------------------------------------------------

void OnKey(unsigned vk, bool down)
{
    if (down && HMD)
        ovrHmd_DismissHSWDisplay(HMD);

    switch (vk)
    {
    case 'Q':       if (down && ControlDown) Quit = true;                         break;
    case VK_ESCAPE: if (!down)               Quit = true;                         break;

    case 'R':       if (down && HMD) ovrHmd_RecenterPose(HMD);                    break;

    case 'W':       MoveForward = down ? (MoveForward | 1) : (MoveForward & ~1);  break;
    case 'S':       MoveBack    = down ? (MoveBack    | 1) : (MoveBack    & ~1);  break;
    case 'A':       MoveLeft    = down ? (MoveLeft    | 1) : (MoveLeft    & ~1);  break;
    case 'D':       MoveRight   = down ? (MoveRight   | 1) : (MoveRight   & ~1);  break;

    case VK_UP:     MoveForward = down ? (MoveForward | 2) : (MoveForward & ~2);  break;
    case VK_DOWN:   MoveBack    = down ? (MoveBack    | 2) : (MoveBack    & ~2);  break;
    
    case 'F':       FreezeEyeRender = !down ? !FreezeEyeRender : FreezeEyeRender; break;
    
    case VK_SHIFT:  ShiftDown = down;                                             break;
    case VK_CONTROL:ControlDown = down;                                           break;
    }

}

void OnMouseMove(int x)
{
     const float                Sensitivity = 1.0f;
     AdditionalYawFromMouse -= (Sensitivity * x)/ 360.0f;
}

bool Util_RespondToControls(float & EyeYaw, Vector3f & EyePos, Quatf PoseOrientation)
{
    #if 0//Optional debug output
    char debugString[1000];
    sprintf_s(debugString,"Pos = (%0.2f, %0.2f, %0.2f)\n",EyePos.x,EyePos.y,EyePos.z);
    OutputDebugStringA(debugString);
    #endif

    //Mouse rotation
    EyeYaw += AdditionalYawFromMouse;
    AdditionalYawFromMouse = 0;

    //Get HeadYaw
    float tempHeadPitch, tempHeadRoll, HeadYaw, GamepadYaw;
    PoseOrientation.GetEulerAngles<Axis_Y, Axis_X, Axis_Z>(&HeadYaw,&tempHeadPitch, &tempHeadRoll);

    //Move on Eye pos from controls
    Vector3f localMoveVector(0,0,0);

    if (MoveForward) localMoveVector += Vector3f(0,0,-1); 
    if (MoveBack)    localMoveVector += Vector3f(0,0,+1); 
    if (MoveRight)   localMoveVector += Vector3f(1,0,0); 
    if (MoveLeft)    localMoveVector += Vector3f(-1,0,0);

    //respond to gamepad
    OvrPlatform::GamepadState gamepadstate;
    if (GamepadManager.GetGamepadState(1, &gamepadstate))
    {
        if (gamepadstate.Buttons != 0)
            ovrHmd_DismissHSWDisplay(HMD);
        LastGamepadState = gamepadstate;
    }

    //move according to gamepad input
    localMoveVector += Vector3f(LastGamepadState.LX, 0, -LastGamepadState.LY);

    //prevents double speed using gamepad and 
    if (localMoveVector.x > 1) localMoveVector.x = 1;
    if (localMoveVector.y > 1) localMoveVector.y = 1;

    //rotate according to gamepad input
    const float gamepad_sensitivity = .025;
    GamepadYaw = -gamepad_sensitivity * LastGamepadState.RX;

    EyeYaw += GamepadYaw;

    Matrix4f yawRotate = Matrix4f::RotationY(EyeYaw + HeadYaw);

    Vector3f    orientationVector = yawRotate.Transform(localMoveVector);

    const float deltaTime = 1.0f/60.0f;
    orientationVector *= MoveSpeed * deltaTime * (ShiftDown ? 3.0f : 1.0f);
    EyePos += orientationVector;

    //Some rudimentary limitation of movement, so not to go through walls
    const float minDistanceToWall = 0.30f;
    EyePos.x = max(EyePos.x,-10.0f + minDistanceToWall);
    EyePos.x = min(EyePos.x, 10.0f - minDistanceToWall);
    EyePos.z = max(EyePos.z,-20.0f + minDistanceToWall);

    //Return if need to freeze or not
    return(FreezeEyeRender);
}


LRESULT CALLBACK systemWindowProc(HWND arg_hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
        case(WM_NCCREATE):  hWnd = arg_hwnd; break;

        case WM_MOUSEMOVE:	{
                            // Convert mouse motion to be relative
                            // (report the offset and re-center).
                            POINT newPos = { LOWORD(lp), HIWORD(lp) };
                            ::ClientToScreen(hWnd, &newPos);
                            if ((newPos.x == WindowCenter.x) && (newPos.y == WindowCenter.y))
                                break;
                            ::SetCursorPos(WindowCenter.x, WindowCenter.y);
                            OnMouseMove(newPos.x - WindowCenter.x);
                            break;
                            }

        case WM_MOVE:		RECT r;
                            GetClientRect(hWnd, &r);
                            WindowCenter.x = r.right/2;
                            WindowCenter.y = r.bottom/2;
                            ::ClientToScreen(hWnd, &WindowCenter);
                            break;

        case WM_KEYDOWN:    OnKey((unsigned)wp, true);    break;
        case WM_KEYUP:      OnKey((unsigned)wp, false);   break;
        case WM_CREATE:     SetTimer(hWnd, 0, 100, NULL); break;
        case WM_TIMER:      KillTimer(hWnd, 0);

        case WM_SETFOCUS:   
                            SetCursorPos(WindowCenter.x, WindowCenter.y);
                            SetCapture(hWnd);
                            ShowCursor(FALSE);
                            break;
       case WM_KILLFOCUS:
                            ReleaseCapture();
                            ShowCursor(TRUE);
                            break;

        case WM_QUIT:
        case WM_CLOSE:      Quit = true;
                            return 0;
    }

    return DefWindowProc(hWnd, msg, wp, lp);
}


HWND Util_InitWindowAndGraphics(Recti vp, int fullscreen, int multiSampleCount, bool UseAppWindowFrame, RenderDevice ** returnedDevice)
{
    RendererParams  renderParams;

    // Window
    WNDCLASS wc;
    memset(&wc, 0, sizeof(wc));
    wc.lpszClassName = L"OVRAppWindow";
    wc.style         = CS_OWNDC;
    wc.lpfnWndProc   = systemWindowProc;
    wc.cbWndExtra    = NULL;
    RegisterClass(&wc);

    DWORD wsStyle = WS_POPUP;
    DWORD sizeDivisor = 1;
   
    if (UseAppWindowFrame)
    {
        // If using our driver, displaya window frame with a smaller window.
        // Original HMD resolution is still passed into the renderer for proper swap chain.
        wsStyle |= WS_OVERLAPPEDWINDOW;
        renderParams.Resolution = vp.GetSize();
        sizeDivisor = 2;
    }

    RECT winSize = { 0, 0, vp.w / sizeDivisor, vp.h / sizeDivisor};
    AdjustWindowRect(&winSize, wsStyle, false);
    hWnd = CreateWindowA("OVRAppWindow", "OculusRoomTiny",
                         wsStyle |WS_VISIBLE,
                         vp.x, vp.y,
                         winSize.right-winSize.left, winSize.bottom-winSize.top,
                         NULL, NULL, hInstance, NULL);

    POINT center = { vp.w / 2 / sizeDivisor, vp.h / 2 / sizeDivisor};
    ::ClientToScreen(hWnd, &center);
    WindowCenter = center;

    if (!hWnd) return(NULL);

    // Graphics
    renderParams.Multisample = multiSampleCount; 
    renderParams.Fullscreen  = fullscreen;

    *returnedDevice = RenderDevice::CreateDevice(renderParams, (void*)hWnd);

    return(hWnd);
}


void Util_ReleaseWindowAndGraphics(RenderDevice * prender)
{    
    if (prender)
        prender->Release();

    if (hWnd)
    {
        // Release window resources.
        ::DestroyWindow(hWnd);
        UnregisterClass(L"OVRAppWindow", hInstance);
    }
}


//-------------------------------------------------------------------------------------
// ***** Program Startup
// 
int WINAPI WinMain(HINSTANCE hinst, HINSTANCE, LPSTR , int)
{
    hInstance = hinst;

    if (!Init())
    {
        // Processes messages and calls OnIdle() to do rendering.
       while (!Quit)
        {
            MSG msg;
            if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            else
            {
                 ProcessAndRender();

                // Keep sleeping when we're minimized.
                if (IsIconic(hWnd)) Sleep(10);
            }
        }
    }
    Release();
    OVR_ASSERT(!_CrtDumpMemoryLeaks());
    return (0);
}