summaryrefslogtreecommitdiffstats
path: root/Samples/OculusRoomTiny/Win32_OculusRoomTiny_Util.cpp
blob: ab0f2524004409221b002b28d911b268bf7fba35 (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
/************************************************************************************

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"

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

// User inputs
bool                Quit = 0;
UByte               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;

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


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

void OnKey(unsigned vk, bool down)
{
    switch (vk)
    {
    case 'Q':       if (down && ControlDown) Quit = true;                         break;
    case VK_ESCAPE: if (!down)               Quit = true;                         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,
                            float deltaTime, 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 HeadPitch, HeadRoll, HeadYaw;
	PoseOrientation.GetEulerAngles<Axis_Y, Axis_X, Axis_Z>(&HeadYaw,&HeadPitch, &HeadRoll);

	//Move on Eye pos from controls
    Vector3f localMoveVector(0,0,0);
    Matrix4f yawRotate = Matrix4f::RotationY(EyeYaw + HeadYaw);

	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);

	Vector3f    orientationVector = yawRotate.Transform(localMoveVector);

    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);
}


RenderDevice* Util_InitWindowAndGraphics(Recti vp, int fullscreen, int multiSampleCount)
{
	// Window
    WNDCLASS wc;
    memset(&wc, 0, sizeof(wc));
    wc.lpszClassName = L"OVRAppWindow";
    wc.style         = CS_OWNDC;
    wc.lpfnWndProc   = systemWindowProc;
    wc.cbWndExtra    = NULL;
    RegisterClass(&wc);
   
	RECT winSize = { 0, 0, vp.w, vp.h };
    AdjustWindowRect(&winSize, WS_POPUP, false);
    hWnd = CreateWindowA("OVRAppWindow", "OculusRoomTiny", WS_POPUP |WS_VISIBLE,
		                 vp.x, vp.y,
                         winSize.right-winSize.left, winSize.bottom-winSize.top,
                         NULL, NULL, hInstance, NULL);

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

	if (!hWnd) return(NULL);

	// Graphics
    RendererParams  renderParams;
	renderParams.Multisample = multiSampleCount; 
    renderParams.Fullscreen  = fullscreen;
    return (RenderDevice::CreateDevice(renderParams, (void*)hWnd));
}


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

	DistortionMeshRelease();

    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_DEBUG_STATEMENT(_CrtDumpMemoryLeaks());
    return (0);
}