-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpoutStereoWindow.cpp
More file actions
315 lines (275 loc) · 12.3 KB
/
SpoutStereoWindow.cpp
File metadata and controls
315 lines (275 loc) · 12.3 KB
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
305
306
307
308
309
310
311
312
313
314
315
#include "pch.h"
#include "SpoutStereoWindow.h"
#include "Generated Files/PixelShader.h"
#include "Generated Files/VertexShader.h"
using namespace DirectX;
using namespace DirectX::SimpleMath;
using Microsoft::WRL::ComPtr;
SpoutStereoWindow::SpoutStereoWindow() : m_lastReceivingFromSpout(false)
{
}
SpoutStereoWindow::~SpoutStereoWindow()
{
for (auto tile = m_tiles.begin(); tile != m_tiles.end(); tile++) {
delete (*tile);
}
m_tiles.clear();
}
void
SpoutStereoWindow::Initialize(HWND window, bool stereo)
{
m_window = window;
m_stereo = stereo;
// create tiles based on config file settings
std::vector<std::string> tileNames = ConfigVal::Get("TILES", std::vector<std::string>());
for (auto tileName = tileNames.begin(); tileName != tileNames.end(); tileName++) {
SpoutStereoTile* tile = new SpoutStereoTile();
tile->Initialize(*tileName, this);
m_tiles.push_back(tile);
}
// Init MinVR Event Connection
m_openMinVREventConnection = ConfigVal::Get("OPEN_MINVR_EVENT_CONNECTION", true);
m_port = ConfigVal::Get("MINVR_EVENT_CONNECTION_PORT", 9030);
m_readWriteTimeoutMs = ConfigVal::Get("MINVR_EVENT_CONNECTION_READ_WRITE_TIMEOUT_MS", 500);
if (m_openMinVREventConnection) {
MinVR3Net::Init();
MinVR3Net::CreateListener(m_port, &m_listenerFd);
}
// Init DX input devices
m_keyboard = std::make_unique<Keyboard>();
m_mouse = std::make_unique<Mouse>();
m_mouse->SetWindow(window);
}
void
SpoutStereoWindow::CreateDeviceResources(Microsoft::WRL::ComPtr<ID3D11Device> d3dDevice,
Microsoft::WRL::ComPtr<ID3D11DeviceContext> d3dContext)
{
m_d3dDevice = d3dDevice;
m_d3dContext = d3dContext;
m_font = std::make_unique<SpriteFont>(m_d3dDevice.Get(), L"CourierNew-32.spritefont");
m_spriteBatch = std::make_unique<SpriteBatch>(m_d3dContext.Get());
// Create simple shaders for fullscreen quad (these are compiled into header files during the build)
// Right-click on the .hlsl files and go to Properties to configure this.
DX::ThrowIfFailed(
m_d3dDevice->CreateVertexShader(g_vertexshader, sizeof(g_vertexshader), nullptr, &m_fullscreenVertexShader)
);
DX::ThrowIfFailed(
m_d3dDevice->CreatePixelShader(g_pixelshader, sizeof(g_pixelshader), nullptr, &m_fullscreenPixelShader)
);
for (auto tile = m_tiles.begin(); tile != m_tiles.end(); tile++) {
(*tile)->CreateDeviceResources(m_d3dDevice, m_d3dContext);
}
}
void
SpoutStereoWindow::ReleaseDeviceResources()
{
for (auto tile = m_tiles.begin(); tile != m_tiles.end(); tile++) {
(*tile)->ReleaseDeviceResources();
}
m_font.reset();
m_spriteBatch.reset();
m_d3dContext.Reset();
m_d3dDevice.Reset();
}
void
SpoutStereoWindow::CreateWindowResources()
{
for (auto tile = m_tiles.begin(); tile != m_tiles.end(); tile++) {
(*tile)->CreateWindowResources();
}
}
void
SpoutStereoWindow::ReleaseWindowResources()
{
for (auto tile = m_tiles.begin(); tile != m_tiles.end(); tile++) {
(*tile)->ReleaseWindowResources();
}
}
void
SpoutStereoWindow::OnSpoutOpenStream()
{
// Restore the window in case it is currently minimized
ShowWindow(m_window, SW_RESTORE);
// Turn off debug graphics for all tiles
for (auto tile = m_tiles.begin(); tile != m_tiles.end(); tile++) {
(*tile)->setShowDebugGraphics(false);
}
}
void
SpoutStereoWindow::OnSpoutCloseStream()
{
// Minimize the window
ShowWindow(m_window, SW_MINIMIZE);
// Turn on debug graphics for all tiles
for (auto tile = m_tiles.begin(); tile != m_tiles.end(); tile++) {
(*tile)->setShowDebugGraphics(true);
}
}
void
SpoutStereoWindow::ResetInputTrackers()
{
m_keyboardStateTracker.Reset();
m_mouseStateTracker.Reset();
}
void
SpoutStereoWindow::PollKeyUpDownEvent(DirectX::Keyboard::Keys keyId, const std::string& keyName, std::vector<VREvent*>* eventList)
{
if (m_keyboardStateTracker.IsKeyPressed(keyId)) {
eventList->push_back(new VREvent("Keyboard/" + keyName + "/Down"));
}
if (m_keyboardStateTracker.released.W) {
eventList->push_back(new VREvent("Keyboard/" + keyName + "/Up"));
}
}
void
SpoutStereoWindow::Update()
{
for (auto tile = m_tiles.begin(); tile != m_tiles.end(); tile++) {
(*tile)->Update();
}
bool receivingFromSpoutThisFrame = receivingFromSpout();
if (receivingFromSpoutThisFrame && !m_lastReceivingFromSpout) {
OnSpoutOpenStream();
}
else if (!receivingFromSpoutThisFrame && m_lastReceivingFromSpout) {
OnSpoutCloseStream();
}
m_lastReceivingFromSpout = receivingFromSpoutThisFrame;
m_keyboardStateTracker.Update(m_keyboard->GetState());
m_mouseStateTracker.Update(m_mouse->GetState());
if (m_keyboardStateTracker.pressed.OemQuestion) {
// Minimize the window
ShowWindow(m_window, SW_MINIMIZE);
}
if (m_openMinVREventConnection) {
// Accept new connections from any clients trying to connect
while (MinVR3Net::IsReadyToRead(&m_listenerFd)) {
SOCKET newClientFd;
if (MinVR3Net::TryAcceptConnection(m_listenerFd, &newClientFd)) {
m_clientFds.push_back(newClientFd);
m_clientDescs.push_back(MinVR3Net::GetAddressAndPort(newClientFd));
}
}
// Convert input events from DirectX to MinVR VREvents
std::vector<VREvent*> events;
if ((m_mouseStateTracker.GetLastState().x != m_mouse->GetState().x) ||
(m_mouseStateTracker.GetLastState().y != m_mouse->GetState().y)) {
events.push_back(new VREventVector2("Mouse/Position", m_mouse->GetState().x, m_mouse->GetState().y));
}
if (m_mouseStateTracker.leftButton == m_mouseStateTracker.PRESSED) {
events.push_back(new VREventVector2("Mouse/Left DOWN", m_mouse->GetState().x, m_mouse->GetState().y));
}
if (m_mouseStateTracker.leftButton == m_mouseStateTracker.RELEASED) {
events.push_back(new VREventVector2("Mouse/Left UP", m_mouse->GetState().x, m_mouse->GetState().y));
}
if (m_mouseStateTracker.middleButton == m_mouseStateTracker.PRESSED) {
events.push_back(new VREventVector2("Mouse/Middle DOWN", m_mouse->GetState().x, m_mouse->GetState().y));
}
if (m_mouseStateTracker.middleButton == m_mouseStateTracker.RELEASED) {
events.push_back(new VREventVector2("Mouse/Middle UP", m_mouse->GetState().x, m_mouse->GetState().y));
}
if (m_mouseStateTracker.rightButton == m_mouseStateTracker.PRESSED) {
events.push_back(new VREventVector2("Mouse/Right DOWN", m_mouse->GetState().x, m_mouse->GetState().y));
}
if (m_mouseStateTracker.rightButton == m_mouseStateTracker.RELEASED) {
events.push_back(new VREventVector2("Mouse/Right UP", m_mouse->GetState().x, m_mouse->GetState().y));
}
PollKeyUpDownEvent(Keyboard::Keys::A, "A", &events);
PollKeyUpDownEvent(Keyboard::Keys::B, "B", &events);
PollKeyUpDownEvent(Keyboard::Keys::C, "C", &events);
PollKeyUpDownEvent(Keyboard::Keys::D, "D", &events);
PollKeyUpDownEvent(Keyboard::Keys::E, "E", &events);
PollKeyUpDownEvent(Keyboard::Keys::F, "F", &events);
PollKeyUpDownEvent(Keyboard::Keys::G, "G", &events);
PollKeyUpDownEvent(Keyboard::Keys::H, "H", &events);
PollKeyUpDownEvent(Keyboard::Keys::I, "I", &events);
PollKeyUpDownEvent(Keyboard::Keys::J, "J", &events);
PollKeyUpDownEvent(Keyboard::Keys::K, "K", &events);
PollKeyUpDownEvent(Keyboard::Keys::L, "L", &events);
PollKeyUpDownEvent(Keyboard::Keys::M, "M", &events);
PollKeyUpDownEvent(Keyboard::Keys::N, "N", &events);
PollKeyUpDownEvent(Keyboard::Keys::O, "O", &events);
PollKeyUpDownEvent(Keyboard::Keys::P, "P", &events);
PollKeyUpDownEvent(Keyboard::Keys::Q, "Q", &events);
PollKeyUpDownEvent(Keyboard::Keys::R, "R", &events);
PollKeyUpDownEvent(Keyboard::Keys::S, "S", &events);
PollKeyUpDownEvent(Keyboard::Keys::T, "T", &events);
PollKeyUpDownEvent(Keyboard::Keys::U, "U", &events);
PollKeyUpDownEvent(Keyboard::Keys::V, "V", &events);
PollKeyUpDownEvent(Keyboard::Keys::W, "W", &events);
PollKeyUpDownEvent(Keyboard::Keys::X, "X", &events);
PollKeyUpDownEvent(Keyboard::Keys::Y, "Y", &events);
PollKeyUpDownEvent(Keyboard::Keys::Z, "Z", &events);
PollKeyUpDownEvent(Keyboard::Keys::D0, "0", &events);
PollKeyUpDownEvent(Keyboard::Keys::D1, "1", &events);
PollKeyUpDownEvent(Keyboard::Keys::D2, "2", &events);
PollKeyUpDownEvent(Keyboard::Keys::D3, "3", &events);
PollKeyUpDownEvent(Keyboard::Keys::D4, "4", &events);
PollKeyUpDownEvent(Keyboard::Keys::D5, "5", &events);
PollKeyUpDownEvent(Keyboard::Keys::D6, "6", &events);
PollKeyUpDownEvent(Keyboard::Keys::D7, "7", &events);
PollKeyUpDownEvent(Keyboard::Keys::D8, "8", &events);
PollKeyUpDownEvent(Keyboard::Keys::D9, "9", &events);
PollKeyUpDownEvent(Keyboard::Keys::Space, "Space", &events);
PollKeyUpDownEvent(Keyboard::Keys::Enter, "Enter", &events);
PollKeyUpDownEvent(Keyboard::Keys::Left, "LeftArrow", &events);
PollKeyUpDownEvent(Keyboard::Keys::Right, "RightArrow", &events);
PollKeyUpDownEvent(Keyboard::Keys::Up, "UpArrow", &events);
PollKeyUpDownEvent(Keyboard::Keys::Down, "DownArrow", &events);
PollKeyUpDownEvent(Keyboard::Keys::OemComma, "Comma", &events);
PollKeyUpDownEvent(Keyboard::Keys::OemPeriod, "Period", &events);
PollKeyUpDownEvent(Keyboard::Keys::LeftShift, "LeftShift", &events);
PollKeyUpDownEvent(Keyboard::Keys::RightShift, "RightShift", &events);
PollKeyUpDownEvent(Keyboard::Keys::LeftAlt, "LeftAlt", &events);
PollKeyUpDownEvent(Keyboard::Keys::RightAlt, "RightAlt", &events);
PollKeyUpDownEvent(Keyboard::Keys::LeftControl, "LeftControl", &events);
PollKeyUpDownEvent(Keyboard::Keys::RightControl, "RightControl", &events);
PollKeyUpDownEvent(Keyboard::Keys::Tab, "Tab", &events);
PollKeyUpDownEvent(Keyboard::Keys::Delete, "Delete", &events);
PollKeyUpDownEvent(Keyboard::Keys::OemPlus, "Plus", &events);
PollKeyUpDownEvent(Keyboard::Keys::OemMinus, "Minus", &events);
PollKeyUpDownEvent(Keyboard::Keys::Escape, "Esc", &events);
std::vector<SOCKET> disconnectedFds;
for (int j = 0; j < m_clientFds.size(); j++) {
int e = 0;
bool success = true;
while ((e < events.size()) && (success)) {
success = MinVR3Net::SendVREvent(&m_clientFds[j], *(events[e]), m_readWriteTimeoutMs);
e++;
}
if (!success) {
// If there was a problem sending, then assume this client disconnected
disconnectedFds.push_back(m_clientFds[j]);
}
}
// Remove any disconnected clients from the list
for (int i = 0; i < disconnectedFds.size(); i++) {
auto it = std::find(m_clientFds.begin(), m_clientFds.end(), disconnectedFds[i]);
if (it != m_clientFds.end()) {
int index = it - m_clientFds.begin();
std::cout << "Dropped connection from " << m_clientDescs[index] << std::endl;
// officially close the socket
MinVR3Net::CloseSocket(&disconnectedFds[i]);
// remove the client from both lists
m_clientFds.erase(m_clientFds.begin() + index);
m_clientDescs.erase(m_clientDescs.begin() + index);
}
}
} // end MinVR event connection updates
}
void
SpoutStereoWindow::Draw(ComPtr<ID3D11RenderTargetView> renderTargetViewLeft,
ComPtr<ID3D11RenderTargetView> renderTargetViewRight)
{
// LEFT EYE
m_d3dContext->OMSetRenderTargets(1, renderTargetViewLeft.GetAddressOf(), nullptr);
m_d3dContext->ClearRenderTargetView(renderTargetViewLeft.Get(), Colors::Red);
if (renderTargetViewRight) {
// RIGHT EYE
m_d3dContext->OMSetRenderTargets(1, renderTargetViewRight.GetAddressOf(), nullptr);
m_d3dContext->ClearRenderTargetView(renderTargetViewRight.Get(), Colors::Blue);
}
for (auto tile = m_tiles.begin(); tile != m_tiles.end(); tile++) {
(*tile)->Draw(renderTargetViewLeft, renderTargetViewRight);
}
}