~sircmpwn/xrgears

ref: c4afd33e13398688fc03f60ef3d0a498bf40bfdd xrgears/vitamin-k/VikCameraStereo.hpp -rw-r--r-- 2.9 KiB
c4afd33e — Lubosz Sarnecki add new abstract classes for interfaces between vks and vkc. 4 years ago
                                                                                
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
/*
 * XRGears
 *
 * Copyright (C) 2016 Sascha Willems - www.saschawillems.de
 * Copyright (C) 2017 Lubosz Sarnecki <lubosz.sarnecki@collabora.co.uk>
 *
 * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
 *
 * Based on Vulkan Examples written by Sascha Willems
 */

#pragma once

#include "../vks/vksBuffer.hpp"
#include "../vks/vksCamera.hpp"

#include "VikCamera.hpp"
#include "VikBuffer.hpp"

class VikCameraStereo : public VikCamera {
 public:
  // Camera and view properties
  float eyeSeparation = 0.08f;
  const float focalLength = 0.5f;
  const float fov = 90.0f;
  const float zNear = 0.1f;
  const float zFar = 256.0f;

  int width, height;

  VikCameraStereo(int w, int h) : width(w), height(h) {}

  void changeEyeSeparation(float delta) {
    eyeSeparation += delta;
  }

  void update(Camera camera) {
    // Geometry shader matrices for the two viewports
    // See http://paulbourke.net/stereographics/stereorender/

    // Calculate some variables
    float aspectRatio = (float)(width * 0.5f) / (float)height;
    float wd2 = zNear * tan(glm::radians(fov / 2.0f));
    float ndfl = zNear / focalLength;
    float left, right;
    float top = wd2;
    float bottom = -wd2;

    glm::vec3 camFront;
    glm::vec3 rotation = glm::vec3();
    camFront.x = -cos(glm::radians(rotation.x)) * sin(glm::radians(rotation.y));
    camFront.y = sin(glm::radians(rotation.x));
    camFront.z = cos(glm::radians(rotation.x)) * cos(glm::radians(rotation.y));
    camFront = glm::normalize(camFront);
    glm::vec3 camRight = glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f)));

    glm::mat4 rotM = glm::mat4();
    glm::mat4 transM;

    rotM = glm::rotate(rotM, glm::radians(camera.rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
    rotM = glm::rotate(rotM, glm::radians(camera.rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
    rotM = glm::rotate(rotM, glm::radians(camera.rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));

    // Left eye
    left = -aspectRatio * wd2 + 0.5f * eyeSeparation * ndfl;
    right = aspectRatio * wd2 + 0.5f * eyeSeparation * ndfl;

    transM = glm::translate(glm::mat4(), camera.position - camRight * (eyeSeparation / 2.0f));

    uboCamera.projection[0] = glm::frustum(left, right, bottom, top, zNear, zFar);
    uboCamera.view[0] = rotM * transM;
    uboCamera.skyView[0] = rotM * glm::translate(glm::mat4(), -camRight * (eyeSeparation / 2.0f));

    // Right eye
    left = -aspectRatio * wd2 - 0.5f * eyeSeparation * ndfl;
    right = aspectRatio * wd2 - 0.5f * eyeSeparation * ndfl;

    transM = glm::translate(glm::mat4(), camera.position + camRight * (eyeSeparation / 2.0f));

    uboCamera.projection[1] = glm::frustum(left, right, bottom, top, zNear, zFar);
    uboCamera.view[1] = rotM * transM;
    uboCamera.skyView[1] = rotM * glm::translate(glm::mat4(), camRight * (eyeSeparation / 2.0f));

    uboCamera.position = camera.position * -1.0f;

    memcpy(uniformBuffer.mapped, &uboCamera, sizeof(uboCamera));
  }
};