Allolib  1.0
C++ Components For Interactive Multimedia
al_PerProjection.hpp
1 #ifndef INLCUDE_AL_PERPROJECTION_HPP
2 #define INLCUDE_AL_PERPROJECTION_HPP
3 
4 #include <cmath>
5 #include <fstream>
6 #include <iostream>
7 #include <vector>
8 
9 #include "al/graphics/al_FBO.hpp"
10 #include "al/graphics/al_Graphics.hpp"
11 #include "al/graphics/al_Shader.hpp"
12 #include "al/graphics/al_Shapes.hpp"
13 #include "al/graphics/al_Texture.hpp"
14 #include "al/graphics/al_VAOMesh.hpp"
15 #include "al/graphics/al_Viewpoint.hpp"
16 #include "al/math/al_Matrix4.hpp"
17 
18 #ifdef AL_WINDOWS
19 #undef near
20 #undef far
21 #endif
22 
23 namespace al {
24 
25 Mat4f get_cube_mat(int face);
26 
27 struct bhlw {
28  float b;
29  float h;
30  float l;
31  float w;
32 };
33 
34 bhlw viewport_for_cubemap_face(int idx);
35 
37 public:
38  static int const sampletex_binding_point = 10;
39  static int const textures_bidning_point = 11;
40 };
41 
42 inline std::string perprojection_samplevert() {
43  return R"(
44 #version 330
45 uniform mat4 al_ModelViewMatrix;
46 uniform mat4 al_ProjectionMatrix;
47 
48 layout (location = 0) in vec3 position;
49 layout (location = 2) in vec2 texcoord;
50 
51 out vec2 texcoord_;
52 
53 void main() {
54 gl_Position = al_ProjectionMatrix * al_ModelViewMatrix * vec4(position, 1.0);
55 texcoord_ = texcoord;
56 }
57 )";
58 }
59 
60 inline std::string perprojection_samplefrag() {
61  return R"(
62 #version 330
63 uniform sampler2D sample_tex;
64 uniform sampler2D color_tex;
65 uniform mat4 R;
66 uniform float tanFovDiv2;
67 in vec2 texcoord_;
68 out vec4 frag_color;
69 void main() {
70 vec4 sample = texture(sample_tex, texcoord_);
71 vec3 dir = sample.rgb;
72 vec3 p_coord = (R * vec4(dir, 0)).xyz;
73 p_coord.xy /= -p_coord.z;
74 p_coord.xy /= tanFovDiv2;
75 vec3 sampled_color = texture(color_tex, p_coord.xy / 2.0 + 0.5).rgb;
76 frag_color = vec4(sampled_color * sample.a, 1.0);
77 }
78 )";
79 }
80 
82 public:
83  std::string id;
84  float b, h, l, w;
85  int active;
86  std::string filepath;
87  int width, height;
88  // std::vector<Vec3f> warp_data;
89  // std::vector<float> blend_data;
90  std::vector<Vec4f> warp_and_blend_data;
91 };
92 
94 public:
95  std::vector<ProjectionViewport> viewports;
96  void load_allosphere_calibration(const char *path, const char *hostname);
97  void load_desktop_mode_calibration();
98 };
99 
101 public:
102  struct ProjectionInfo {
103  std::shared_ptr<Texture> texture[2];
104  std::shared_ptr<Texture> warp_texture;
105  Mat4f pc_matrix, p_matrix, r_matrix;
106  float tanFovDiv2; // tan(fov)/2
107  };
108 
109  WarpBlendData warpblend_;
110  int res_ = 4096;
111  Pose pose_;
112  Viewpoint view_{pose_};
113  Viewport viewport_;
114  std::vector<ProjectionInfo> projection_infos_;
115  RBO rbo_;
116  FBO fbo_;
117  // ShaderProgram pp_shader_;
118  ShaderProgram composite_shader_;
119  Lens lens_;
120  Graphics *g;
121  VAOMesh texquad;
122  bool calibration_loaded = false;
123  bool did_begin = false;
124  int current_eye = 0;
125  int current_projection = 0;
126 
127  // for saving previous settings
128  Lens prev_lens_;
129  ShaderProgram *prev_shader_;
130 
131  Mat4f get_rotation_matrix(Vec3f axis, float angle);
132 
133  // load_calibration_data needed to be called before this function
134  // also projection_infos_.resize(warpblend_.viewports.size());
135  void update_resolution(int resolution);
136 
137  // void sphereRadius(float r) {
138  // lens_.focalLength(r);
139  // if (did_begin) g->lens(lens_);
140  //}
141 
142  // void init(float near=0.1, float far=1000, float radius = 1e10)
143  void init(const al::Lens &lens);
144 
145  void load_calibration_data(const char *path, const char *hostname);
146 
147  // void load_and_init_as_desktop_config(float near=0.1, float far=1000, float
148  // radius = 1e10) {
149  void load_and_init_as_desktop_config(const al::Lens &lens);
150 
151  // use when near and far changed (only use after init)
152  // fov value of lens will not be used in omni drawing
153  // focallength value will be updated when drawing is actually done
154  // by uploading to shader as glsl-uniform value
155  void updateLens(const Lens &lens);
156 
157  // changing lens and pose after begin does not have effect on omni rendering
158  void begin(Graphics &graphics, const al::Lens &lens, const al::Pose pose);
159 
160  // must only be used between begin and end
161  void set_eye(int i);
162 
163  int num_projections() { return projection_infos_.size(); }
164 
165  // must only use between begin and end
166  void set_projection(int index);
167 
168  Mat4f get_r_mat_for_current_projection() {
169  return projection_infos_[current_projection].r_matrix;
170  }
171 
172  Mat4f get_p_mat_for_current_projection() {
173  return projection_infos_[current_projection].p_matrix;
174  }
175 
176  void end();
177 
178  // void pose(Pose const& p) { pose_ = p; if (did_begin)
179  // g->viewMatrix(view_mat(pose_)); } Pose& pose() { return pose_; } Pose
180  // const& pose() const { return pose_; }
181 
182  void composite(Graphics &g, int eye = 0);
183 
184  void composite_desktop(Graphics &g, int eye = 0);
185 };
186 } // namespace al
187 
188 #endif
Frame buffer object class.
Definition: al_FBO.hpp:150
Interface for loading fonts and rendering text.
Definition: al_Graphics.hpp:63
A local coordinate frame.
Definition: al_Pose.hpp:63
Render buffer object class.
Definition: al_FBO.hpp:83
Shader program object.
Definition: al_Shader.hpp:116
VAOMesh classStores gpu objects as shared pointer to that this class can be copied for moved this is ...
Definition: al_VAOMesh.hpp:34
Viewpoint within a scene.
Definition: al_App.hpp:23
T angle(const Vec< N, T > &a, const Vec< N, T > &b)
Returns angle, in interval [0, pi], between two vectors.
Definition: al_Vec.hpp:634
Mat< 4, float > Mat4f
float 4x4 matrix
Definition: al_Mat.hpp:61