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 "rendering.h"
6 #include "option.h"
9 
10 using namespace rs2;
11 
12 struct short3
13 {
14  uint16_t x, y, z;
15 };
16 
17 #include <res/d435.h>
18 #include <res/d415.h>
19 #include <res/d455.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  {
129 
130  register_option(RS2_OPTION_FILTER_MAGNITUDE, std::make_shared<librealsense::float_option>(option_range{ 0, 1, 0, 1 }));
132 
133  for (auto&& mesh : camera_mesh)
134  {
135  for (auto& xyz : mesh.positions)
136  {
137  xyz = xyz / 1000.f;
138  xyz.x *= -1;
139  xyz.y *= -1;
140  }
141  }
142 
143 
144  initialize();
145  }
146 
147  bool starts_with(const std::string& s, const std::string& prefix)
148  {
149  auto i = s.begin(), j = prefix.begin();
150  for (; i != s.end() && j != prefix.end() && *i == *j;
151  i++, j++);
152  return j == prefix.end();
153  }
154 
156  {
157  //scoped_timer t("camera_renderer");
158 
159  const auto& dev = ((frame_interface*)f.get())->get_sensor()->get_device();
160 
161  int index = -1;
162 
163  if (dev.supports_info(RS2_CAMERA_INFO_NAME))
164  {
165  auto dev_name = dev.get_info(RS2_CAMERA_INFO_NAME);
166  if (starts_with(dev_name, "Intel RealSense D415")) index = 0;
167  if (starts_with(dev_name, "Intel RealSense D435")) index = 1;
168  if (starts_with(dev_name, "Intel RealSense D45")) index = 2;
169  };
170 
171  auto opacity = clamp(_opacity_opt->query(), 0.0, 1.0);
172 
173  if (index >= 0)
174  {
175  perform_gl_action([&]()
176  {
177  //scoped_timer t("camera_renderer.gl");
178 
179  if (glsl_enabled())
180  {
181  _shader->begin();
185  );
186  _shader->set_opacity(opacity);
187  _camera_model[index]->draw();
188  _shader->end();
189  }
190  else
191  {
193  glPushMatrix();
194 
197 
198  glLoadMatrixf(v * t);
199 
201  auto& mesh = camera_mesh[index];
202  for (auto& i : mesh.indexes)
203  {
204  auto v0 = mesh.positions[i.x];
205  auto v1 = mesh.positions[i.y];
206  auto v2 = mesh.positions[i.z];
207  glVertex3fv(&v0.x);
208  glVertex3fv(&v1.x);
209  glVertex3fv(&v2.x);
210  glColor4f(opacity * 0.036f, opacity * 0.044f, opacity * 0.051f, opacity);
211  }
212  glEnd();
213 
214  glPopMatrix();
215  }
216  });
217  }
218 
219  return f;
220  }
221  }
222 }
glMatrixMode
#define glMatrixMode
Definition: glad/glad/glad.h:2263
librealsense
Definition: algo.h:18
librealsense::gl::camera_renderer::_opacity_opt
option * _opacity_opt
Definition: camera-shader.h:57
rs2::frame
Definition: rs_frame.hpp:345
rs2::frame_source
Definition: rs_processing.hpp:18
RS2_GL_MATRIX_TRANSFORMATION
@ RS2_GL_MATRIX_TRANSFORMATION
Definition: rs_processing_gl.h:35
v2
const GLdouble * v2
Definition: glad/glad/glad.h:1784
uint16_t
unsigned short uint16_t
Definition: stdint.h:79
rmse.xyz
xyz
Definition: rmse.py:152
glLoadMatrixf
#define glLoadMatrixf
Definition: glad/glad/glad.h:2257
GL_MODELVIEW
#define GL_MODELVIEW
Definition: glad/glad/glad.h:525
rs2::shader_program::load
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:579
librealsense::gl::gpu_rendering_object::initialize
void initialize()
Definition: synthetic-stream-gl.h:278
index
GLuint index
Definition: glad/glad/glad.h:2777
camera-shader.h
v
GLdouble v
Definition: glad/glad/glad.h:2144
string
GLsizei const GLchar *const * string
Definition: glad/glad/glad.h:2861
device-interface.h
glEnd
#define glEnd
Definition: glad/glad/glad.h:1654
librealsense::options_container::register_option
void register_option(rs2_option id, std::shared_ptr< option > option)
Definition: options-container.h:50
d415.h
void
typedef void(APIENTRY *GLDEBUGPROC)(GLenum source
example3 - opencv deploy.idx
idx
Definition: example3 - opencv deploy.py:49
sensor-interface.h
GL_TRIANGLES
#define GL_TRIANGLES
Definition: glad/glad/glad.h:151
librealsense::gl::camera_renderer::process_frame
rs2::frame process_frame(const rs2::frame_source &source, const rs2::frame &f) override
Definition: camera-shader.cpp:155
glBegin
#define glBegin
Definition: glad/glad/glad.h:1546
rendering.h
librealsense::frame_interface
Definition: frame-interface.h:20
librealsense::gl::camera_renderer::_shader
std::shared_ptr< camera_shader > _shader
Definition: camera-shader.h:54
RS2_CAMERA_INFO_NAME
@ RS2_CAMERA_INFO_NAME
Definition: rs_sensor.h:23
librealsense::gl::starts_with
bool starts_with(const std::string &s, const std::string &prefix)
Definition: camera-shader.cpp:147
librealsense::stream_filter_processing_block
Definition: synthetic-stream.h:134
librealsense::option_range
Definition: option-interface.h:14
glVertex3fv
#define glVertex3fv
Definition: glad/glad/glad.h:1936
d435.h
RS2_GL_MATRIX_PROJECTION
@ RS2_GL_MATRIX_PROJECTION
Definition: rs_processing_gl.h:36
camera_renderer::camera_renderer
camera_renderer()
Definition: rs-motion.cpp:70
librealsense::gl::load_model
obj_mesh load_model(load_function f)
Definition: camera-shader.cpp:114
f
GLdouble f
Definition: glad/glad/glad.h:1517
i
int i
Definition: rs-pcl-color.cpp:54
z
GLdouble GLdouble z
Definition: glad/glad/glad.h:1733
uncompress_d435_obj
void uncompress_d435_obj(std::vector< float3 > &vertex_data, std::vector< float3 > &normals, std::vector< short3 > &index_data)
Definition: d435.h:11
j
GLint j
Definition: glad/glad/glad.h:2165
fragment_shader_text
static const char * fragment_shader_text
Definition: camera-shader.cpp:34
rs2
Definition: animated.h:9
librealsense::gl::camera_renderer::_camera_model
std::vector< std::unique_ptr< rs2::vao > > _camera_model
Definition: camera-shader.h:55
unit-test-config.src
string src
Definition: unit-test-config.py:88
glPopMatrix
#define glPopMatrix
Definition: glad/glad/glad.h:2275
rs2::obj_mesh
Definition: opengl3.h:40
test-projection-from-recording.dev
dev
Definition: test-projection-from-recording.py:17
v0
GLfloat v0
Definition: glad/glad/glad.h:2867
librealsense::gl::matrix_container::get_matrix
const rs2::matrix4 & get_matrix(rs2_gl_matrix_type type) const
Definition: synthetic-stream-gl.h:200
short3
Definition: rs-motion.cpp:8
RS2_OPTION_FILTER_MAGNITUDE
@ RS2_OPTION_FILTER_MAGNITUDE
Definition: rs_option.h:64
d455.h
librealsense::options_container::get_option
option & get_option(rs2_option id) override
Definition: options-container.h:32
opencv_pointcloud_viewer.view
def view(v)
Definition: opencv_pointcloud_viewer.py:168
vertex_shader_text
static const char * vertex_shader_text
Definition: camera-shader.cpp:21
rs2::vao::create
static std::unique_ptr< vao > create(const obj_mesh &m)
Definition: opengl3.cpp:115
test-unit-transform.model
model
Definition: test-unit-transform.py:27
mesh
static GLuint mesh
Definition: heightmap.c:113
librealsense::gl::load_function
void(* load_function)(std::vector< rs2::float3 > &, std::vector< rs2::float3 > &, std::vector< short3 > &)
Definition: camera-shader.cpp:111
t
GLdouble t
Definition: glad/glad/glad.h:1829
rs2::matrix4
Definition: matrix4.h:16
librealsense::gl::gpu_object::glsl_enabled
bool glsl_enabled() const
Definition: synthetic-stream-gl.h:252
librealsense::gl::gpu_rendering_object::perform_gl_action
void perform_gl_action(T action)
Definition: synthetic-stream-gl.h:286
rs2::obj_mesh::positions
std::vector< float3 > positions
Definition: opengl3.h:44
glPushMatrix
#define glPushMatrix
Definition: glad/glad/glad.h:2278
x
GLdouble x
Definition: glad/glad/glad.h:2279
option.h
clamp
#define clamp(x)
v1
GLdouble GLdouble GLint GLint GLdouble v1
Definition: glad/glad/glad.h:2114
init
void init(void)
Definition: boing.c:180
end
GLuint GLuint end
Definition: glad/glad/glad.h:2395
librealsense::option::query
virtual float query() const =0
rs2::obj_mesh::normals
std::vector< float3 > normals
Definition: opengl3.h:45
glColor4f
#define glColor4f
Definition: glad/glad/glad.h:1612
s
GLdouble s
Definition: glad/glad/glad.h:2441
RS2_GL_MATRIX_CAMERA
@ RS2_GL_MATRIX_CAMERA
Definition: rs_processing_gl.h:37
librealsense::gl::camera_renderer::camera_mesh
std::vector< rs2::obj_mesh > camera_mesh
Definition: camera-shader.h:53
rs2::obj_mesh::indexes
std::vector< int3 > indexes
Definition: opengl3.h:43
uncompress_d455_obj
void uncompress_d455_obj(std::vector< float3 > &vertex_data, std::vector< float3 > &normals, std::vector< short3 > &index_data)
Definition: d455.h:11
uncompress_d415_obj
void uncompress_d415_obj(std::vector< float3 > &vertex_data, std::vector< float3 > &normals, std::vector< short3 > &index_data)
Definition: d415.h:11
y
GLint y
Definition: glad/glad/glad.h:1397


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Mon Apr 22 2024 02:12:55