camera-shader.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2019 Intel Corporation. All Rights Reserved.
3 
4 #include "camera-shader.h"
5 #include <glad/glad.h>
6 #include "option.h"
7 
8 using namespace rs2;
9 
10 struct short3
11 {
12  uint16_t x, y, z;
13 };
14 
15 #include <res/d435.h>
16 #include <res/d415.h>
17 #include <res/sr300.h>
18 #include <res/t265.h>
19 #include <res/l500.h>
20 
21 static const char* vertex_shader_text =
22 "#version 110\n"
23 "\n"
24 "attribute vec3 position;\n"
25 "uniform mat4 transformationMatrix;\n"
26 "uniform mat4 projectionMatrix;\n"
27 "uniform mat4 cameraMatrix;\n"
28 "\n"
29 "void main(void) {\n"
30 " vec4 worldPosition = transformationMatrix * vec4(position.xyz, 1.0);\n"
31 " gl_Position = projectionMatrix * cameraMatrix * worldPosition;\n"
32 "}\n";
33 
34 static const char* fragment_shader_text =
35 "#version 110\n"
36 "uniform float opacity;"
37 "\n"
38 "void main(void) {\n"
39 " gl_FragColor = vec4(opacity * (36.0 / 1000.0), opacity * (44.0 / 1000.0), opacity * (51.0 / 1000.0), opacity);\n"
40 "}\n";
41 
42 using namespace rs2;
43 
44 namespace librealsense
45 {
46  namespace gl
47  {
48  camera_shader::camera_shader()
49  {
50  _shader = shader_program::load(
53 
54  init();
55  }
56 
58  {
59  _shader->bind_attribute(0, "position");
60 
61  _transformation_matrix_location = _shader->get_uniform_location("transformationMatrix");
62  _projection_matrix_location = _shader->get_uniform_location("projectionMatrix");
63  _camera_matrix_location = _shader->get_uniform_location("cameraMatrix");
64  _opacity_location = _shader->get_uniform_location("opacity");
65  }
66 
67  void camera_shader::begin() { _shader->begin(); }
68  void camera_shader::end() { _shader->end(); }
69 
70  void camera_shader::set_mvp(const matrix4& model,
71  const matrix4& view,
72  const matrix4& projection)
73  {
74  _shader->load_uniform(_transformation_matrix_location, model);
75  _shader->load_uniform(_camera_matrix_location, view);
76  _shader->load_uniform(_projection_matrix_location, projection);
77  }
78 
79  void camera_shader::set_opacity(float opacity)
80  {
81  _shader->load_uniform(_opacity_location, opacity);
82  }
83 
84  void camera_renderer::cleanup_gpu_resources()
85  {
86  _shader.reset();
87  _camera_model.clear();
88  }
89 
90  void camera_renderer::create_gpu_resources()
91  {
92  if (glsl_enabled())
93  {
94  _shader = std::make_shared<camera_shader>();
95 
96  for (auto&& mesh : camera_mesh)
97  {
98  _camera_model.push_back(vao::create(mesh));
99  }
100  }
101  }
102 
103  camera_renderer::~camera_renderer()
104  {
105  perform_gl_action([&]()
106  {
107  cleanup_gpu_resources();
108  });
109  }
110 
111  typedef void (*load_function)(std::vector<rs2::float3>&,
112  std::vector<rs2::float3>&, std::vector<short3>&);
113 
115  {
116  obj_mesh res;
117  std::vector<short3> idx;
118  f(res.positions, res.normals, idx);
119  for (auto i : idx)
120  res.indexes.push_back({ i.x, i.y, i.z });
121  return res;
122  }
123 
125  {
131 
132  register_option(RS2_OPTION_FILTER_MAGNITUDE, std::make_shared<librealsense::float_option>(option_range{ 0, 1, 0, 1 }));
134 
135  for (auto&& mesh : camera_mesh)
136  {
137  for (auto& xyz : mesh.positions)
138  {
139  xyz = xyz / 1000.f;
140  xyz.x *= -1;
141  xyz.y *= -1;
142  }
143  }
144 
145 
146  initialize();
147  }
148 
149  bool starts_with(const std::string& s, const std::string& prefix)
150  {
151  auto i = s.begin(), j = prefix.begin();
152  for (; i != s.end() && j != prefix.end() && *i == *j;
153  i++, j++);
154  return j == prefix.end();
155  }
156 
158  {
159  //scoped_timer t("camera_renderer");
160 
161  const auto& dev = ((frame_interface*)f.get())->get_sensor()->get_device();
162 
163  int index = -1;
164 
165  if (dev.supports_info(RS2_CAMERA_INFO_NAME))
166  {
167  auto dev_name = dev.get_info(RS2_CAMERA_INFO_NAME);
168  if (starts_with(dev_name, "Intel RealSense D415")) index = 0;
169  if (starts_with(dev_name, "Intel RealSense D435")) index = 1;
170  if (starts_with(dev_name, "Intel RealSense SR30")) index = 2;
171  if (starts_with(dev_name, "Intel RealSense T26")) index = 3;
172  if (starts_with(dev_name, "Intel RealSense L5")) index = 4;
173  };
174 
175  auto opacity = clamp(_opacity_opt->query(), 0.0, 1.0);
176 
177  if (index >= 0)
178  {
179  perform_gl_action([&]()
180  {
181  //scoped_timer t("camera_renderer.gl");
182 
183  if (glsl_enabled())
184  {
185  _shader->begin();
189  );
190  _shader->set_opacity(opacity);
191  _camera_model[index]->draw();
192  _shader->end();
193  }
194  else
195  {
197  glPushMatrix();
198 
201 
202  glLoadMatrixf(v * t);
203 
205  auto& mesh = camera_mesh[index];
206  for (auto& i : mesh.indexes)
207  {
208  auto v0 = mesh.positions[i.x];
209  auto v1 = mesh.positions[i.y];
210  auto v2 = mesh.positions[i.z];
211  glVertex3fv(&v0.x);
212  glVertex3fv(&v1.x);
213  glVertex3fv(&v2.x);
214  glColor4f(opacity * 0.036f, opacity * 0.044f, opacity * 0.051f, opacity);
215  }
216  glEnd();
217 
218  glPopMatrix();
219  }
220  });
221  }
222 
223  return f;
224  }
225  }
226 }
std::vector< float3 > positions
Definition: opengl3.h:43
GLuint GLuint end
static const char * fragment_shader_text
GLint y
rs2::frame process_frame(const rs2::frame_source &source, const rs2::frame &f) override
typedef void(APIENTRY *GLDEBUGPROC)(GLenum source
rs2_frame * get() const
Definition: rs_frame.hpp:590
static std::unique_ptr< vao > create(const obj_mesh &m)
Definition: opengl3.cpp:112
GLdouble s
#define glBegin
void uncompress_d415_obj(std::vector< float3 > &vertex_data, std::vector< float3 > &normals, std::vector< short3 > &index_data)
Definition: d415.h:8
#define glPopMatrix
#define clamp(x)
#define glVertex3fv
unsigned short uint16_t
Definition: stdint.h:79
Definition: cah-model.h:10
GLsizei const GLchar *const * string
GLfloat v0
GLenum src
Definition: glext.h:1751
GLdouble GLdouble z
void uncompress_d435_obj(std::vector< float3 > &vertex_data, std::vector< float3 > &normals, std::vector< short3 > &index_data)
Definition: d435.h:8
#define glColor4f
option & get_option(rs2_option id) override
Definition: options.h:58
virtual float query() const =0
void uncompress_L515_obj(std::vector< float3 > &vertex_data, std::vector< float3 > &normals, std::vector< short3 > &index_data)
Definition: l500.h:8
GLuint index
void register_option(rs2_option id, std::shared_ptr< option > option)
Definition: options.h:86
GLdouble t
bool starts_with(const std::string &s, const std::string &prefix)
not_this_one begin(...)
obj_mesh load_model(load_function f)
GLdouble f
std::vector< float3 > normals
Definition: opengl3.h:44
GLdouble x
#define glPushMatrix
#define glLoadMatrixf
void uncompress_t265_obj(std::vector< float3 > &vertex_data, std::vector< float3 > &normals, std::vector< short3 > &index_data)
Definition: t265.h:8
void init(void)
Definition: boing.c:180
#define glEnd
GLint j
static const char * vertex_shader_text
std::vector< std::unique_ptr< rs2::vao > > _camera_model
Definition: camera-shader.h:55
#define GL_MODELVIEW
GLdouble GLdouble GLint GLint GLdouble v1
std::vector< int3 > indexes
Definition: opengl3.h:42
static std::unique_ptr< shader_program > load(const std::string &vertex_shader, const std::string &fragment_shader, const char *input0=nullptr, const char *input1=nullptr, const char *output0=nullptr, const char *output1=nullptr)
Definition: opengl3.cpp:576
void uncompress_sr305_obj(std::vector< float3 > &vertex_data, std::vector< float3 > &normals, std::vector< short3 > &index_data)
static GLuint mesh
Definition: heightmap.c:113
xyz
Definition: rmse.py:152
std::vector< rs2::obj_mesh > camera_mesh
Definition: camera-shader.h:53
int i
GLuint res
Definition: glext.h:8856
const GLdouble * v2
const rs2::matrix4 & get_matrix(rs2_gl_matrix_type type) const
std::shared_ptr< camera_shader > _shader
Definition: camera-shader.h:54
GLdouble v
void(* load_function)(std::vector< rs2::float3 > &, std::vector< rs2::float3 > &, std::vector< short3 > &)
#define glMatrixMode
#define GL_TRIANGLES


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:45:07