forked from hyperlogic/riftty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathriftty.cpp
492 lines (386 loc) · 16 KB
/
riftty.cpp
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
#if (defined _WIN32) || (defined _WIN64)
#include "SDL.h"
#include "SDL_opengl.h"
#else
#include "SDL2/SDL.h"
#endif
#include <stdio.h>
#include <string>
#include "keyboard.h"
#include "opengl.h"
#include "appconfig.h"
#include "render.h"
#include "joystick.h"
#include "text.h"
#include "context.h"
#include "abaci.h"
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include "render.h"
#include "shader.h"
extern "C" {
#include "config.h"
#include "charset.h"
#include "term.h"
#include "child.h"
#include "win.h"
}
#include "../Src/OVR_CAPI.h"
#include "../Src/OVR_CAPI_GL.h"
const float kFeetToMeters = 0.3048;
Vector4f s_clearColor(0.0, 0, 0.3, 1);
// time tracking
unsigned int s_ticks = 0;
static Matrixf s_RotY90 = Matrixf::AxisAngle(Vector3f(0,1,0), PI/2.0f);
ovrHmd s_hmd;
ovrHmdDesc s_hmdDesc;
ovrEyeRenderDesc s_eyeRenderDesc[2];
ovrSizei s_renderTargetSize;
ovrTexture s_eyeTexture[2];
uint32_t s_fbo;
uint32_t s_fboDepth;
uint32_t s_fboTex;
Vector3f s_cameraPos(0, 6 * kFeetToMeters, 0);
void DumpHMDInfo(const ovrHmdDesc& desc)
{
printf("HMDInfo\n");
printf(" ProductName = \"%s\"\n", desc.ProductName);
printf(" Manufacturer = \"%s\"\n", desc.Manufacturer);
printf(" HmdCaps = 0x%x\n", desc.HmdCaps);
printf(" %s Present\n", desc.HmdCaps & ovrHmdCap_Present ? "IS" : "IS NOT");
printf(" %s Available\n", desc.HmdCaps & ovrHmdCap_Available ? "IS" : "IS NOT");
printf(" SensorCaps = 0x%x\n", desc.SensorCaps);
printf(" %s Orientation\n", desc.SensorCaps & ovrSensorCap_Orientation ? "SUPPORTS" : "DOES NOT SUPPORT");
printf(" %s YawCorrection\n", desc.SensorCaps & ovrSensorCap_YawCorrection ? "SUPPORTS" : "DOES NOT SUPPORT");
printf(" %s Position\n", desc.SensorCaps & ovrSensorCap_Position ? "SUPPORTS" : "DOES NOT SUPPORT");
printf(" DistortionCaps = 0x%x\n", desc.DistortionCaps);
printf(" %s Chromatic\n", desc.DistortionCaps & ovrDistortionCap_Chromatic ? "SUPPORTS" : "DOES NOT SUPPORT");
printf(" %s TimeWarp\n", desc.DistortionCaps & ovrDistortionCap_TimeWarp ? "SUPPORTS" : "DOES NOT SUPPORT");
printf(" %s Vignette\n", desc.DistortionCaps & ovrDistortionCap_Vignette ? "SUPPORTS" : "DOES NOT SUPPORT");
printf(" Resolution = %d x %d\n", desc.Resolution.w, desc.Resolution.h);
printf(" WindowsPos = (%d, %d)\n", desc.WindowsPos.x, desc.WindowsPos.y);
printf(" DefaultEyeFov = [(%f, %f, %f, %f), (%f, %f, %f, %f])]\n",
desc.DefaultEyeFov[0].UpTan, desc.DefaultEyeFov[0].DownTan, desc.DefaultEyeFov[0].LeftTan, desc.DefaultEyeFov[0].RightTan,
desc.DefaultEyeFov[1].UpTan, desc.DefaultEyeFov[1].DownTan, desc.DefaultEyeFov[1].LeftTan, desc.DefaultEyeFov[1].RightTan);
printf(" MaxEyeFov = [(%f, %f, %f, %f), (%f, %f, %f, %f])]\n",
desc.MaxEyeFov[0].UpTan, desc.MaxEyeFov[0].DownTan, desc.MaxEyeFov[0].LeftTan, desc.MaxEyeFov[0].RightTan,
desc.MaxEyeFov[1].UpTan, desc.MaxEyeFov[1].DownTan, desc.MaxEyeFov[1].LeftTan, desc.MaxEyeFov[1].RightTan);
printf(" EyeRenderOrder = %s, %s\n", desc.EyeRenderOrder[0] == ovrEye_Left ? "Left" : "Right", desc.EyeRenderOrder[1] == ovrEye_Left ? "Left" : "Right");
}
void CreateRenderTarget(int width, int height);
void RiftSetup()
{
ovr_Initialize();
s_hmd = ovrHmd_Create(0);
if (!s_hmd)
{
s_hmd = ovrHmd_CreateDebug(ovrHmd_DK1);
}
ovrHmd_GetDesc(s_hmd, &s_hmdDesc);
DumpHMDInfo(s_hmdDesc);
uint32_t supportedSensorCaps = ovrSensorCap_Orientation;
uint32_t requiredSensorCaps = ovrSensorCap_Orientation;
ovrBool success = ovrHmd_StartSensor(s_hmd, supportedSensorCaps, requiredSensorCaps);
if (!success) {
fprintf(stderr, "ERROR: HMD does not have required capabilities!\n");
exit(2);
}
// Figure out dimensions of render target
ovrSizei recommenedTex0Size = ovrHmd_GetFovTextureSize(s_hmd, ovrEye_Left, s_hmdDesc.DefaultEyeFov[0], 1.0f);
ovrSizei recommenedTex1Size = ovrHmd_GetFovTextureSize(s_hmd, ovrEye_Right, s_hmdDesc.DefaultEyeFov[1], 1.0f);
s_renderTargetSize.w = recommenedTex0Size.w + recommenedTex1Size.w;
s_renderTargetSize.h = std::max(recommenedTex0Size.h, recommenedTex1Size.h);
CreateRenderTarget(s_renderTargetSize.w, s_renderTargetSize.h);
s_eyeTexture[0].Header.API = ovrRenderAPI_OpenGL;
s_eyeTexture[0].Header.TextureSize = s_renderTargetSize;
s_eyeTexture[0].Header.RenderViewport.Pos = {0, 0};
s_eyeTexture[0].Header.RenderViewport.Size = {s_renderTargetSize.w / 2, s_renderTargetSize.h};
((ovrGLTexture*)(&s_eyeTexture[0]))->OGL.TexId = s_fboTex;
s_eyeTexture[1].Header.API = ovrRenderAPI_OpenGL;
s_eyeTexture[1].Header.TextureSize = s_renderTargetSize;
s_eyeTexture[1].Header.RenderViewport.Pos = {s_renderTargetSize.w / 2, 0};
s_eyeTexture[1].Header.RenderViewport.Size = {s_renderTargetSize.w / 2, s_renderTargetSize.h};
((ovrGLTexture*)(&s_eyeTexture[1]))->OGL.TexId = s_fboTex;
// Configure ovr SDK Rendering
ovrGLConfig cfg;
memset(&cfg, 0, sizeof(ovrGLConfig));
cfg.OGL.Header.API = ovrRenderAPI_OpenGL;
cfg.OGL.Header.RTSize = {s_config->width, s_config->height};
cfg.OGL.Header.Multisample = 0;
// TODO: on windows need to set HWND, on Linux need to set other parameters
if (!ovrHmd_ConfigureRendering(s_hmd, &cfg.Config, s_hmdDesc.DistortionCaps, s_hmdDesc.DefaultEyeFov, s_eyeRenderDesc))
{
fprintf(stderr, "ERROR: HMD configure rendering failed!\n");
exit(3);
}
}
void RiftShutdown()
{
ovrHmd_Destroy(s_hmd);
ovr_Shutdown();
}
void Process(float dt)
{
float kSpeed = 1.5f;
Joystick* joy = JOYSTICK_GetJoystick();
Vector3f vel(joy->axes[Joystick::LeftStickX],
joy->axes[Joystick::RightStickY],
-joy->axes[Joystick::LeftStickY]);
s_cameraPos = s_cameraPos + vel * dt * kSpeed;
child_poll();
}
void Render(float dt)
{
static unsigned int frameIndex = 0;
frameIndex++;
ovrFrameTiming timing = ovrHmd_BeginFrame(s_hmd, 0);
// ovrSensorState ss = ovrHmd_GetSensorState(s_hmd, timing.ScanoutMidpointSeconds);
// TODO: Use this for head tracking...
// TODO: Use player height from SDK
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// render into fbo
glBindFramebuffer(GL_FRAMEBUFFER, s_fbo);
// TODO: enable this when we have more complex rendering.
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
static float t = 0.0;
t += dt;
// clear render target
glViewport(0, 0, s_renderTargetSize.w, s_renderTargetSize.h);
glClearColor(s_clearColor.x, s_clearColor.y, s_clearColor.z, s_clearColor.w);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (int i = 0; i < 2; i++)
{
ovrEyeType eye = s_hmdDesc.EyeRenderOrder[i];
ovrPosef pose = ovrHmd_BeginEyeRender(s_hmd, eye);
glViewport(s_eyeTexture[eye].Header.RenderViewport.Pos.x,
s_eyeTexture[eye].Header.RenderViewport.Pos.y,
s_eyeTexture[eye].Header.RenderViewport.Size.w,
s_eyeTexture[eye].Header.RenderViewport.Size.h);
Quatf q(pose.Orientation.x, pose.Orientation.y, pose.Orientation.z, pose.Orientation.w);
Vector3f p(pose.Position.x, pose.Position.y, pose.Position.z);
Matrixf cameraMatrix = Matrixf::QuatTrans(q, s_cameraPos);
Matrixf viewCenter = cameraMatrix.OrthoInverse();
// let ovr compute projection matrix, cause it's hard.
ovrMatrix4f ovrProj = ovrMatrix4f_Projection(s_eyeRenderDesc[eye].Fov, 0.1f, 10000.0f, true);
// convert to abaci matrix
Matrixf projMatrix = Matrixf::Rows(Vector4f(ovrProj.M[0][0], ovrProj.M[0][1], ovrProj.M[0][2], ovrProj.M[0][3]),
Vector4f(ovrProj.M[1][0], ovrProj.M[1][1], ovrProj.M[1][2], ovrProj.M[1][3]),
Vector4f(ovrProj.M[2][0], ovrProj.M[2][1], ovrProj.M[2][2], ovrProj.M[2][3]),
Vector4f(ovrProj.M[3][0], ovrProj.M[3][1], ovrProj.M[3][2], ovrProj.M[3][3]));
// use EyeRenderDesc.ViewAdjust to do eye offset.
Matrixf viewMatrix = viewCenter * Matrixf::Trans(Vector3f(s_eyeRenderDesc[eye].ViewAdjust.x,
s_eyeRenderDesc[eye].ViewAdjust.y,
s_eyeRenderDesc[eye].ViewAdjust.z));
// compute model matrix for terminal
const float kTermScale = 0.001f;
const Vector3f termOrigin(-2 * kFeetToMeters, 6.75f * kFeetToMeters, -2.5 * kFeetToMeters);
Matrixf modelMatrix = Matrixf::ScaleQuatTrans(Vector3f(kTermScale, -kTermScale, kTermScale),
Quatf::AxisAngle(Vector3f(0, 1, 0), 0),
termOrigin);
RenderBegin();
RenderFloor(projMatrix, viewMatrix, 0.0f);
RenderTextBegin(projMatrix, viewMatrix, modelMatrix);
for (int j = 0; j < win_get_text_count(); j++)
{
gb::Text* text = (gb::Text*)win_get_text(j);
if (text)
{
RenderText(text->GetQuadVec());
}
}
RenderTextEnd();
RenderEnd();
ovrHmd_EndEyeRender(s_hmd, eye, pose, &s_eyeTexture[eye]);
}
ovrHmd_EndFrame(s_hmd);
}
void CreateRenderTarget(int width, int height)
{
printf("Render Target size %d x %d\n", width, height);
// create a fbo
glGenFramebuffers(1, &s_fbo);
GL_ERROR_CHECK("RenderTargetInit1");
// create a depth buffer
glGenRenderbuffers(1, &s_fboDepth);
GL_ERROR_CHECK("RenderTargetInit2");
// bind fbo
glBindFramebuffer(GL_FRAMEBUFFER, s_fbo);
GL_ERROR_CHECK("RenderTargetInit3");
glGenTextures(1, &s_fboTex);
glBindTexture(GL_TEXTURE_2D, s_fboTex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
// attach texture to fbo
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, s_fboTex, 0);
// init depth buffer
glBindRenderbuffer(GL_RENDERBUFFER, s_fboDepth);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, s_fboDepth);
// check status
assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
GL_ERROR_CHECK("RenderTargetInit");
}
static bool s_needsRestart = false;
void RestartHandler(int signal)
{
printf("NEEDS RESTART! pid = %d\n", getpid());
// NOTE: I could call execvp directly here, but the process will
// inherit the signal mask which will not let me hook up the SIGUSR1 handler.
// So we set a flag instead.
s_needsRestart = true;
}
void Restart()
{
printf("RESTART! pid = %d\n", getpid());
char* const argv[] = {"./riftty", NULL};
int err = execvp(*argv, argv);
if (err == -1)
perror("execvp");
}
int main(int argc, char* argv[])
{
printf("START! pid = %d\n", getpid());
if (!system("./restart.rb"))
exit(1);
if (signal(SIGUSR1, RestartHandler) == SIG_ERR) {
fprintf(stderr, "An error occurred while setting a signal handler.\n");
exit(1);
}
bool fullscreen = true;
s_config = new AppConfig(fullscreen, false, 1280, 800);
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK);
SDL_Window* displayWindow;
SDL_Renderer* displayRenderer;
uint32_t flags = SDL_WINDOW_OPENGL | (s_config->fullscreen ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
int err = SDL_CreateWindowAndRenderer(s_config->width, s_config->height, flags, &displayWindow, &displayRenderer);
if (err == -1 || !displayWindow || !displayRenderer) {
fprintf(stderr, "SDL_CreateWindowAndRenderer failed!\n");
}
SDL_RendererInfo displayRendererInfo;
SDL_GetRendererInfo(displayRenderer, &displayRendererInfo);
/* TODO: Check that we have OpenGL */
if ((displayRendererInfo.flags & SDL_RENDERER_ACCELERATED) == 0 ||
(displayRendererInfo.flags & SDL_RENDERER_TARGETTEXTURE) == 0) {
/* TODO: Handle this. We have no render surface and not accelerated. */
fprintf(stderr, "NO RENDERER wtf!\n");
}
atexit(SDL_Quit);
JOYSTICK_Init();
AppConfig& config = *s_config;
config.title = "riftty";
//SDL_WM_SetCaption(config.title.c_str(), config.title.c_str());
//SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
/*
// clear
glClearColor(s_clearColor.x, s_clearColor.y, s_clearColor.z, s_clearColor.w);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapWindow(displayWindow);
*/
RiftSetup();
RenderInit();
win_init();
init_config();
struct passwd *pw = getpwuid(getuid());
const char *homedir = pw->pw_dir;
char config_filename[512];
strncpy(config_filename, homedir, 512);
strncat(config_filename, "/.riftty", 512);
load_config(config_filename);
cs_init(); // TODO: code pages do not want
// TODO: determine this based on window-size & font-size or vice versa.
cfg.rows = 25;
cfg.cols = 80;
// TODO: load config from /etc/riffty or ~/.rifttyrc
finish_config();
win_reconfig();
// TODO: get SHELL from env
cs_reconfig(); // TODO: do not want
term_init();
term_reset();
term_resize(cfg.rows, cfg.cols);
// TODO:
int font_width = 10;
int font_height = 10;
unsigned short term_width = font_width * cfg.cols;
unsigned short term_height = font_height * cfg.rows;
char login[128];
strncpy(login, getlogin(), 128);
const char* login_argv[] = {"login", "-pfl", login, NULL};
unsigned short rows = cfg.rows;
unsigned short cols = cfg.cols;
winsize ws = {rows, cols, term_width, term_height};
child_create(login_argv, &ws);
bool done = false;
while (!done)
{
JOYSTICK_ClearFlags();
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
done = true;
break;
case SDL_MOUSEMOTION:
if (event.motion.state & SDL_BUTTON(1)) {
// move touch
}
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT) {
// start touch
}
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT) {
// end touch
}
break;
case SDL_JOYAXISMOTION:
JOYSTICK_UpdateMotion(&event.jaxis);
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
JOYSTICK_UpdateButton(&event.jbutton);
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
if (ProcessKeyEvent(&event.key)) {
done = true;
}
break;
}
}
if (!done)
{
if (s_needsRestart)
Restart();
unsigned int now = SDL_GetTicks(); // milliseconds
float dt = (now - s_ticks) / 1000.0f; // convert to seconds.
s_ticks = now;
//printf("fps = %.0f\n", 1.0f/dt);
Process(dt);
Render(dt);
}
}
child_kill(true);
win_shutdown();
RiftShutdown();
JOYSTICK_Shutdown();
return 0;
}