-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
481 lines (368 loc) · 15.8 KB
/
main.cpp
File metadata and controls
481 lines (368 loc) · 15.8 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
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
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "glm/gtx/string_cast.hpp"
#include "opencv2/opencv.hpp"
#include "imgui.h"
#include "imgui_impl_opengl3.h"
#include "imgui_impl_glfw.h"
#include "shader.h"
#include "camera.h"
#include <iostream>
#include "openvr.h"
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void processInput(GLFWwindow *window);
// settings
bool vr_enabled = true;
const unsigned int SCR_WIDTH = 1000;
const unsigned int SCR_HEIGHT = 500;
const unsigned int RENDER_WIDTH = 4000;
const unsigned int RENDER_HEIGHT = 4000;
// camera
struct Cameras{
private:
glm::vec3 worldup = {0.0f, 1.0f, 0.0f};
public:
Camera left = Camera(glm::vec3(0.0f, 0.0f, 3.0f), worldup);
Camera right = Camera(glm::vec3(0.0f, 0.0f, 3.0f), worldup);
Camera *array[2] = {&left, &right};
} Cameras;
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
// timing
float deltaTime = 0.0f; // time between current frame and last frame
float lastFrame = 0.0f;
float imageAspect = 1.0f;
float vertices[] = {
-1.0f, -1.0f, 1.0f, 0.0f, 1.0f,
1.0f, -1.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f, 1.0f, 0.0f,
-1.0f, 1.0f, 1.0f, 0.0f, 0.0f,
-1.0f, -1.0f, 1.0f, 0.0f, 1.0f,
};
// world space positions of our cubes
glm::vec3 cubePositions[] = {
glm::vec3( 0.0f, 2.0f, -4.0f),
};
GLuint makeQuadTexture(const cv::Mat& mat){
GLuint texture;
cv::Mat image = mat.clone();
cv::cvtColor(image, image, cv::COLOR_BGR2RGBA);
imageAspect = (float) image.rows / image.cols;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.cols, image.rows, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.data);
glGenerateMipmap(GL_TEXTURE_2D);
return texture;
}
glm::mat4 convertSteamVRmatToGLM( const vr::HmdMatrix34_t &matPose ) {
glm::mat4 matrixObj(
matPose.m[0][0], matPose.m[1][0], matPose.m[2][0], 0.0,
matPose.m[0][1], matPose.m[1][1], matPose.m[2][1], 0.0,
matPose.m[0][2], matPose.m[1][2], matPose.m[2][2], 0.0,
matPose.m[0][3], matPose.m[1][3], matPose.m[2][3], 1.0f
);
return matrixObj;
}
glm::mat4 getHMDMatrixPoseEye( vr::Hmd_Eye nEye ) {
vr::HmdMatrix34_t matEye = vr::VRSystem()->GetEyeToHeadTransform(nEye );
glm::mat4 mat(
matEye.m[0][0], matEye.m[1][0], matEye.m[2][0], 0.0,
matEye.m[0][1], matEye.m[1][1], matEye.m[2][1], 0.0,
matEye.m[0][2], matEye.m[1][2], matEye.m[2][2], 0.0,
matEye.m[0][3], matEye.m[1][3], matEye.m[2][3], 1.0f
);
return mat;
}
glm::mat4 getHMDMatrixProjectionEye( vr::Hmd_Eye nEye ) {
vr::HmdMatrix44_t mat = vr::VRSystem()->GetProjectionMatrix( nEye, 0.1, 100.0f);
return glm::mat4(
mat.m[0][0], mat.m[1][0], mat.m[2][0], mat.m[3][0],
mat.m[0][1], mat.m[1][1], mat.m[2][1], mat.m[3][1],
mat.m[0][2], mat.m[1][2], mat.m[2][2], mat.m[3][2],
mat.m[0][3], mat.m[1][3], mat.m[2][3], mat.m[3][3]
);
}
std::string g_inputPath = "w.jpg";
void dropCallback(GLFWwindow *window, int count, const char** paths){
if (count > 0)
g_inputPath = paths[0];
}
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glfw window creation
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "OpenGL SteamVR", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetDropCallback(window, dropCallback);
glfwSwapInterval(0);
// tell GLFW to capture our mouse
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// Initialize ImGui
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui::CreateContext();
ImGui_ImplGlfw_InitForOpenGL( window, true );
ImGui_ImplOpenGL3_Init( "#version 330" );
// Initialize OpenVR
if (vr::VR_IsHmdPresent() && vr_enabled) {
auto VRError = vr::VRInitError_None;
auto VRSystem = vr::VR_Init(&VRError, vr::VRApplication_Scene);
if (VRError != vr::VRInitError_None){
std::cout << "OpenVR initialization failed: " << vr::VR_GetVRInitErrorAsEnglishDescription(VRError) << std::endl;
return 1;
}
}else{
std::cout << "HMD not found" << std::endl;
}
GLuint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
unsigned int rbo;
glGenRenderbuffers(1, &rbo);
glBindRenderbuffer(GL_RENDERBUFFER, rbo);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, RENDER_WIDTH, RENDER_HEIGHT);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rbo);
// configure global opengl state
// -----------------------------
glEnable(GL_DEPTH_TEST);
GLuint rightEyeTexture;
glGenTextures(1, &rightEyeTexture);
glBindTexture(GL_TEXTURE_2D, rightEyeTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, RENDER_WIDTH, RENDER_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GLuint leftEyeTexture;
glGenTextures(1, &leftEyeTexture);
glBindTexture(GL_TEXTURE_2D, leftEyeTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, RENDER_WIDTH, RENDER_HEIGHT, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, 0);
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// Quad texture
while ( g_inputPath.empty() ){
glfwPollEvents();
}
cv::Mat input_image = cv::imread(g_inputPath);
cv::Rect roi_left(0, 0, input_image.cols / 2, input_image.rows);
cv::Rect roi_right(input_image.cols / 2, 0, input_image.cols / 2, input_image.rows);
cv::Mat left_half(input_image, roi_left);
cv::Mat right_half(input_image, roi_right);
GLuint leftColor = makeQuadTexture(left_half);
GLuint rightColor = makeQuadTexture(right_half);
Shader ourShader("../camera.vs", "../camera.fs");
ourShader.use();
// ourShader.setInt("leftColor", 0);
vr::TrackedDevicePose_t vrTrackedDevicePose[vr::k_unMaxTrackedDeviceCount];
while (!glfwWindowShouldClose(window)) {
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, RENDER_WIDTH, RENDER_HEIGHT);
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
vr::HmdMatrix44_t steamProjectionMatrix{};
vr::HmdMatrix34_t steamEyeDisparityMatrix = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
glm::mat4 eyeDisparity;
glm::mat4 projection;
// vr::HmdMatrix34_t hmdPose = vr::VRSystem()->GetDeviceToAbsoluteTrackingPose(vr::k_unTrackedDeviceIndex_Hmd, vr::k_ulInvalidInputValue)->mDeviceToAbsoluteTracking;
for (Camera *cam : Cameras.array) {
if(cam == &Cameras.left){
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, leftEyeTexture, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, leftColor);
if(vr_enabled){
projection = getHMDMatrixProjectionEye(vr::Eye_Left);
eyeDisparity = getHMDMatrixPoseEye(vr::Eye_Left);
}
}
if(cam == &Cameras.right){
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, rightEyeTexture, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, rightColor);
if(vr_enabled){
projection = getHMDMatrixProjectionEye(vr::Eye_Right);
eyeDisparity = getHMDMatrixPoseEye(vr::Eye_Right);
}
}
glm::mat4 hmdPose = convertSteamVRmatToGLM( vrTrackedDevicePose[0].mDeviceToAbsoluteTracking );
vrTrackedDevicePose[0].eTrackingResult;
hmdPose = glm::inverse(hmdPose);
// Moving the quad and applying aspect ratio
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[0]);
model = glm::scale(model, glm::vec3(1.0f, imageAspect, 1.0f) );
glm::mat4 mvp = projection * hmdPose * eyeDisparity * model;
ourShader.use();
ourShader.setMat4("mvp", mvp);
// Render quad
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Pass textures to OpenVR
if(vr_enabled){
vr::VRCompositor()->WaitGetPoses(vrTrackedDevicePose, vr::k_unMaxTrackedDeviceCount, nullptr, 0);
vr::Texture_t leftEye = {(void *) (uintptr_t) leftEyeTexture, vr::TextureType_OpenGL, vr::ColorSpace_Gamma};
vr::Texture_t rightEye = {(void *) (uintptr_t) rightEyeTexture, vr::TextureType_OpenGL, vr::ColorSpace_Gamma};
int error = 0;
error |= vr::VRCompositor()->Submit(vr::Eye_Left, &leftEye);
error |= vr::VRCompositor()->Submit(vr::Eye_Right, &rightEye);
if(error != vr::VRCompositorError_None)
std::cout << "Submit error: " << error;
}
// Render ImGui
int flags = ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoScrollWithMouse | ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoBringToFrontOnFocus;
ImVec2 size = ImVec2(SCR_WIDTH / 2, SCR_HEIGHT);
// Flip verically
ImVec2 uv0 = {0, 1};
ImVec2 uv1 = {1, 0};
ImGui::SetNextWindowPos(ImVec2(0,0));
ImGui::SetNextWindowSize( size );
ImGui::Begin("LOL", nullptr, flags);
ImGui::Image( (void*)(intptr_t) leftEyeTexture, size, uv0, uv1);
ImGui::End();
ImGui::SetNextWindowPos(ImVec2(SCR_WIDTH / 2,0));
ImGui::SetNextWindowSize( size );
ImGui::Begin("LOLXD", nullptr, flags);
ImGui::Image( (void*)(intptr_t) rightEyeTexture, size, uv0, uv1 );
ImGui::End();
std::ostringstream out;
out.precision(2);
std::cout << glm::to_string(eyeDisparity) << std::endl;
ImGui::SetNextWindowPos(ImVec2(SCR_WIDTH / 2 - 10,0));
ImGui::SetNextWindowSize( ImVec2(200, 100) );
ImGui::Begin("LOLXDE");
ImGui::Text("%s", glm::to_string(eyeDisparity).c_str() );
ImGui::End();
// End of frame
glfwPollEvents();
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData( ImGui::GetDrawData() );
glfwSwapBuffers(window);
}
// Cleanup
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
ImGui_ImplGlfw_Shutdown();
ImGui_ImplOpenGL3_Shutdown();
ImGui::DestroyContext();
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
for (Camera *cam : Cameras.array) {
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
cam->ProcessKeyboard(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
cam->ProcessKeyboard(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
cam->ProcessKeyboard(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
cam->ProcessKeyboard(RIGHT, deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS)
Cameras.right.Position[0] += -0.5 * deltaTime;
if (glfwGetKey(window, GLFW_KEY_V) == GLFW_PRESS)
Cameras.right.Position[0] += 0.5 * deltaTime;
// std::cout << Cameras.right.Position[0] << std::endl;
float zoomSpeed = 0;
if (glfwGetKey(window, GLFW_KEY_Z) == GLFW_PRESS){
zoomSpeed = 3;
}
if (glfwGetKey(window, GLFW_KEY_X) == GLFW_PRESS){
zoomSpeed = -3;
}
if(zoomSpeed){
cubePositions[0][2] += zoomSpeed * deltaTime;
}
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
// glfw: whenever the mouse moves, this callback is called
// -------------------------------------------------------
void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
{
float xpos = static_cast<float>(xposIn);
float ypos = static_cast<float>(yposIn);
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = ypos - lastY; // reversed since y-coordinates go from bottom to top
lastX = xpos;
lastY = ypos;
for (Camera *cam : Cameras.array) {
cam->ProcessMouseMovement(xoffset, yoffset);
}
}
// glfw: whenever the mouse scroll wheel scrolls, this callback is called
// ----------------------------------------------------------------------
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
for (Camera *cam : Cameras.array) {
cam->ProcessMouseScroll(static_cast<float>(yoffset));
}
}