OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
ProjectionSample.cpp
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 // Internal Includes
28 #include <osvr/Util/Angles.h>
29 #include <osvr/Util/EigenExtras.h>
30 
31 // Library/third-party includes
32 // - none
33 
34 // Standard includes
35 #include <iostream>
36 #include <vector>
37 #include <cmath>
38 #include <utility>
39 
40 using osvr::util::degrees;
42 
43 typedef std::vector<double> BoundsList;
45 
47  public:
48  ProjectionDemo(double zNear, double zFar)
50  zNear, zFar,
51  computeSymmetricFOVRect(50. * degrees, 40. * degrees, zNear)) {}
52  ProjectionDemo(double zNear, double zFar, osvr::util::Rectd &&inputRect)
53  : near(zNear), far(zFar), rect(std::move(inputRect)) {
54  xBounds.assign({-1, 1});
55  yBounds.assign({-1, 1});
56  std::cout << "Near: " << near << "\tFar: " << far << "\n";
57  std::cout << rect << std::endl;
58  }
59  template <opts::OptionType Options = 0> inline void tryProjection() {
60  BoundsList zBounds{
61  {(osvr::util::projection_options::IsZOutputUnsigned<Options>::value
62  ? 0.
63  : -1.),
64  1}};
65  std::cout
66  << "\n Left handed input: " << std::boolalpha
67  << osvr::util::projection_options::IsLeftHandedInput<Options>::value
68  << "\n";
69  std::cout
70  << "Unsigned Z output: " << std::boolalpha
71  << osvr::util::projection_options::IsZOutputUnsigned<Options>::value
72  << "\n";
73  auto projection =
74  osvr::util::parameterizedCreateProjectionMatrix<Options>(rect, near,
75  far);
76  std::cout << "Projection matrix:\n";
77  std::cout << projection << std::endl;
78 
79  std::cout << "Frustum corners:\n";
80  Eigen::Matrix4d inv = projection.inverse();
81  for (auto z : zBounds) {
82  for (auto y : yBounds) {
83  for (auto x : xBounds) {
84  Eigen::Vector4d bound(x, y, z, 1);
85  std::cout
86  << bound.transpose() << "\t<-\t"
87  << osvr::util::extractPoint(inv * bound).transpose()
88  << "\n";
89  }
90  }
91  }
92  }
93 
94  private:
95  double near;
96  double far;
97  osvr::util::Rectd rect;
98  BoundsList xBounds;
99  BoundsList yBounds;
100 };
101 
102 int main(int argc, char *argv[]) {
103  BoundsList xBounds;
104  xBounds.push_back(-1);
105  xBounds.push_back(1);
106 
107  BoundsList yBounds(xBounds);
108 #if 1
109  BoundsList zBounds(xBounds);
110 #else
111  BoundsList zBounds{{0., 1.}};
112 #endif
113 
114  double near = 0.1;
115  double far = 1000;
116  auto rect =
117  osvr::util::computeSymmetricFOVRect(50. * degrees, 40. * degrees, near);
118  std::cout << "Near: " << near << "\tFar: " << far << "\n";
119  std::cout << rect << std::endl;
120  auto projection = osvr::util::createProjectionMatrix(rect, near, far);
121  std::cout << "Projection matrix:\n";
122  std::cout << projection << std::endl;
123 
124  std::cout << "Frustum corners:\n";
125  Eigen::Matrix4d inv = projection.inverse();
126  for (auto z : zBounds) {
127  for (auto y : yBounds) {
128  for (auto x : xBounds) {
129  Eigen::Vector4d bound(x, y, z, 1);
130  std::cout << bound.transpose() << "\t<-\t"
131  << osvr::util::extractPoint(inv * bound).transpose()
132  << "\n";
133  }
134  }
135  }
136 
137  {
138  ProjectionDemo demo(0.1, 1000);
139  demo.tryProjection<0>();
140  demo.tryProjection<opts::LeftHandedInput>();
141  demo.tryProjection<opts::ZOutputUnsigned>();
142  demo.tryProjection<opts::LeftHandedInput | opts::ZOutputUnsigned>();
143  return 0;
144  }
145  return 0;
146 }
Rectd computeSymmetricFOVRect(AngleGeneric< System > hFov, AngleGeneric< System > vFov)
Compute a rectangle at unit distance for the given fov values.
detail::SameLayoutVector< 3, Derived > extractPoint(Eigen::MatrixBase< Derived > const &homogenous)
Pulls the 3D point or vector from a 4D vec, performing division by w if it is nonzero.
Definition: EigenExtras.h:82
Eigen::Matrix4d createProjectionMatrix(Rectd const &bounds, double near, double far)
Takes in points at the near clipping plane, as well as the near and far clipping planes. Result matrix maps [l, r] and [b, t] to [-1, 1], and [n, f] to [-1, 1] (should be configurable)
Header.