opengl3.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 
6 #include "rendering.h"
7 
8 #include <string>
9 #include <unordered_map>
10 #include <vector>
11 #include <memory>
12 
13 void _check_gl_error(const char *file, int line);
14 void clear_gl_errors();
15 
16 #ifndef NDEBUG
17 #define check_gl_error() _check_gl_error(__FILE__,__LINE__)
18 #else
19 #ifndef _DEBUG
20 #define check_gl_error() _check_gl_error(__FILE__,__LINE__)
21 #else
22 #define check_gl_error()
23 #endif
24 #endif
25 
26 namespace rs2
27 {
28  enum class shader_type
29  {
30  vertex,
31  fragment
32  };
33 
34  struct int3
35  {
36  int x, y, z;
37  };
38 
39  struct obj_mesh
40  {
42  std::vector<int3> indexes;
43  std::vector<float3> positions;
44  std::vector<float3> normals;
45  std::vector<float2> uvs;
46  std::vector<float3> tangents;
47  };
48 
49  class shader
50  {
51  public:
52  shader(const std::string& code, shader_type type);
53  ~shader();
54 
55  unsigned int get_id() const { return _id; }
56 
57  private:
58  unsigned int _id;
59  };
60 
62  {
63  public:
65  ~shader_program();
66 
67  void attach(const shader& shader);
68  void link();
69 
70  void begin() const;
71  void end() const;
72 
73  static std::unique_ptr<shader_program> load(
74  const std::string& vertex_shader,
75  const std::string& fragment_shader,
76  const char* input0 = nullptr,
77  const char* input1 = nullptr,
78  const char* output0 = nullptr,
79  const char* output1 = nullptr);
80 
81  unsigned int get_id() const { return _id; }
82 
83  int get_uniform_location(const std::string& name);
84  void load_uniform(int location, float value);
85  void load_uniform(int location, const float2& vec);
86  void load_uniform(int location, const float3& vec);
87  void load_uniform(int location, bool value);
88  void load_uniform(int location, int value);
89  void load_uniform(int location, const matrix4& matrix);
90 
91  void bind_attribute(int attr, const std::string& name);
92 
93  private:
94  std::vector<const shader*> _shaders;
95  unsigned int _id;
96  };
97 
98  enum class vbo_type
99  {
100  array_buffer,
102  };
103 
104  class vbo
105  {
106  public:
108  vbo(vbo&& other);
109  ~vbo();
110 
111  void upload(int attribute, const float* xyz, int size, int count, bool dynamic = false);
112  void upload(const int3* indx, int count);
113 
114  void draw_points();
115  void draw_triangles();
116  void draw_indexed_triangles();
117 
118  void bind();
119  void unbind();
120 
121  uint32_t size() const { return _size; }
122  private:
123  vbo(const vbo& other) = delete;
124  static int convert_type(vbo_type type);
125 
127  uint32_t _size = 0;
129  };
130 
131  struct float3;
132  struct float2;
133  struct int3;
134 
135  struct obj_mesh;
136 
137  class vao
138  {
139  public:
140  static std::unique_ptr<vao> create(const obj_mesh& m);
141 
142  vao(const float3* vert, const float2* uvs, const float3* normals,
143  const float3* tangents, int vert_count, const int3* indx, int indx_count);
144  ~vao();
145  void bind();
146  void unbind();
147  void draw();
148  void draw_points();
149 
150  // void update_uvs(const float2* uvs);
151  void update_positions(const float3* vert);
152 
153  vao(vao&& other);
154 
155  private:
156  vao(const vao& other) = delete;
157 
160  vbo _vertexes, _normals, _indexes, _uvs, _tangents;
161  };
162 
164  {
165  public:
167 
168  void begin();
169  void end();
170 
171  void set_opacity(float opacity);
172 
173  void set_position_and_scale(
174  const float2& position,
175  const float2& scale);
176  protected:
177  texture_2d_shader(std::unique_ptr<shader_program> shader);
178 
179  std::unique_ptr<shader_program> _shader;
180 
181  static const char* default_vertex_shader();
182 
183  private:
184  void init();
185 
189  };
190 
192  {
193  public:
195 
196  void set_ray_center(const float2& center);
197  void set_power(float power);
198  private:
201  };
202 
204  {
205  public:
207  : _position(std::move(pos)),
208  _scale(std::move(scale)),
209  _geometry(vao::create(create_mesh()))
210  {
211 
212  }
213 
215  : texture_visualizer({ 0.f, 0.f }, { 1.f, 1.f }) {}
216 
217  void set_position(float2 pos) { _position = pos; }
218  void set_scale(float2 scale) { _scale = scale; }
219 
221  void draw(texture_2d_shader& shader, uint32_t tex1, uint32_t tex2);
222 
223  private:
224  static obj_mesh create_mesh();
225 
228  std::shared_ptr<vao> _geometry;
229  };
230 
232  {
233  public:
235  : visualizer_2d(std::make_shared<texture_2d_shader>())
236  {
237  }
238 
239  visualizer_2d(std::shared_ptr<texture_2d_shader> shader)
240  : tex_2d_shader(shader)
241  {
242  }
243 
244  void draw_texture(uint32_t tex, float opacity = 1.f);
245  void draw_texture(uint32_t tex1, uint32_t tex2, float opacity = 1.f);
246 
248  {
249  tex_2d_shader->begin();
250  _visualizer.set_position(pos);
251  _visualizer.set_scale(scale);
252  _visualizer.draw(*tex_2d_shader, tex);
253  tex_2d_shader->end();
254  }
255 
257  {
258  tex_2d_shader->begin();
259  _visualizer.set_position(pos);
260  _visualizer.set_scale(scale);
261  _visualizer.draw(*tex_2d_shader, tex1, tex2);
262  tex_2d_shader->end();
263  }
264 
265  texture_2d_shader& get_shader() { return *tex_2d_shader; }
266 
267  private:
269  std::shared_ptr<texture_2d_shader> tex_2d_shader;
270  };
271 
272  inline obj_mesh make_grid(int a, int b)
273  {
274  obj_mesh res;
275 
276  auto toidx = [&](int i, int j) {
277  return i * b + j;
278  };
279 
280  for (auto i = 0; i < a; i++)
281  {
282  for (auto j = 0; j < b; j++)
283  {
284  float3 point{ (float)j, (float)i, 1.f };
285  res.positions.push_back(point);
286  res.normals.push_back(float3{ 0.f, 0.f, -1.f });
287 
288  res.uvs.emplace_back(float2{ (i+0.5f)/(float)(a), (j+0.5f)/(float)(b) });
289 
290  if (i < a - 1 && j < b - 1)
291  {
292  auto curr = toidx(i, j);
293  auto next_a = toidx(i + 1, j);
294  auto next_b = toidx(i, j + 1);
295  auto next_ab = toidx(i + 1, j + 1);
296  res.indexes.emplace_back(int3{ curr, next_a, next_b });
297  res.indexes.emplace_back(int3{ next_a, next_ab, next_b });
298  }
299  }
300  }
301 
302  return res;
303  }
304 
305  class fbo
306  {
307  public:
308  fbo(int w, int h);
309 
310  void createTextureAttachment(uint32_t texture);
311 
312  void createDepthTextureAttachment(uint32_t texture);
313 
314  void bind();
315 
316  void unbind();
317 
318  void createDepthBufferAttachment();
319 
320  ~fbo();
321 
322  std::string get_status();
323 
324  void set_dims(int w, int h) { _w = w; _h = h; }
325 
326  int get_width() const { return _w; }
327  int get_height() const { return _h; }
328  uint32_t get() const { return _id; }
329  private:
331  uint32_t _db = 0;
332  int _w, _h;
333  int32_t _viewport[4];
334  };
335 }
std::vector< float3 > positions
Definition: opengl3.h:43
uint32_t _id
Definition: opengl3.h:158
void _check_gl_error(const char *file, int line)
Definition: opengl3.cpp:690
obj_mesh make_grid(int a, int b)
Definition: opengl3.h:272
GLuint GLuint end
GLboolean GLboolean GLboolean b
GLint y
GLuint const GLchar * name
unsigned int get_id() const
Definition: opengl3.h:55
uint32_t _id
Definition: opengl3.h:126
GLuint GLenum matrix
Definition: glext.h:11553
void clear_gl_errors()
Definition: opengl3.cpp:722
const GLfloat * m
Definition: glext.h:6814
GLenum GLenum GLenum GLenum GLenum scale
Definition: glext.h:10806
int _vertex_count
Definition: opengl3.h:159
std::unique_ptr< shader_program > _shader
Definition: opengl3.h:179
GLint location
GLfloat value
std::vector< float3 > tangents
Definition: opengl3.h:46
texture_2d_shader & get_shader()
Definition: opengl3.h:265
Definition: cah-model.h:10
GLdouble GLdouble GLdouble w
void draw_texture(float2 pos, float2 scale, uint32_t tex1, uint32_t tex2)
Definition: opengl3.h:256
GLsizei const GLchar *const * string
visualizer_2d(std::shared_ptr< texture_2d_shader > shader)
Definition: opengl3.h:239
std::shared_ptr< texture_2d_shader > tex_2d_shader
Definition: opengl3.h:269
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
point
Definition: rmse.py:166
vbo_type _type
Definition: opengl3.h:128
The texture class.
Definition: example.hpp:402
std::vector< const shader * > _shaders
Definition: opengl3.h:94
GLboolean GLboolean GLboolean GLboolean a
not_this_one begin(...)
GLdouble f
std::vector< float3 > normals
Definition: opengl3.h:44
uint32_t _scale_location
Definition: opengl3.h:187
texture_visualizer(float2 pos, float2 scale)
Definition: opengl3.h:206
GLsizeiptr size
uint32_t _opacity_location
Definition: opengl3.h:188
std::vector< float2 > uvs
Definition: opengl3.h:45
void set_scale(float2 scale)
Definition: opengl3.h:218
static const textual_icon link
Definition: model-views.h:257
GLdouble x
unsigned int uint32_t
Definition: stdint.h:80
int z
Definition: opengl3.h:36
static const textual_icon upload
Definition: model-views.h:235
int get_height() const
Definition: opengl3.h:327
void set_dims(int w, int h)
Definition: opengl3.h:324
static void draw(void)
Definition: gears.c:173
uint32_t size() const
Definition: opengl3.h:121
void init(void)
Definition: boing.c:180
GLint j
unsigned int _id
Definition: opengl3.h:95
shader_type
Definition: opengl3.h:28
void set_position(float2 pos)
Definition: opengl3.h:217
vbo _vertexes
Definition: opengl3.h:160
int get_width() const
Definition: opengl3.h:326
uint32_t _id
Definition: opengl3.h:330
int _w
Definition: opengl3.h:332
std::vector< int3 > indexes
Definition: opengl3.h:42
texture_visualizer _visualizer
Definition: opengl3.h:268
vbo_type
Definition: opengl3.h:98
GLenum type
std::shared_ptr< vao > _geometry
Definition: opengl3.h:228
GLint GLsizei count
typename::boost::move_detail::remove_reference< T >::type && move(T &&t) BOOST_NOEXCEPT
xyz
Definition: rmse.py:152
float rs2_vector::* pos
uint32_t _position_location
Definition: opengl3.h:186
int i
GLuint res
Definition: glext.h:8856
GLuint shader
signed int int32_t
Definition: stdint.h:77
unsigned int _id
Definition: opengl3.h:58
std::string name
Definition: opengl3.h:41
unsigned int get_id() const
Definition: opengl3.h:81
void draw_texture(float2 pos, float2 scale, uint32_t tex)
Definition: opengl3.h:247


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