cpp-capture.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #include <librealsense/rs.hpp>
5 #include "example.hpp"
6 
7 #include <sstream>
8 #include <iostream>
9 #include <iomanip>
10 #include <thread>
11 #include <algorithm>
12 #include <map>
13 #include <memory>
14 
16 bool align_depth_to_color = false;
17 bool align_color_to_depth = false;
19 
20 // Split the screen into 640X480 tiles, according to the number of supported streams. Define layout as follows : tiles -> <columds,rows>
21 const std::map<size_t, std::pair<int, int>> tiles_map = { { 1,{ 1,1 } },
22  { 2,{ 2,1 } },
23  { 3,{ 2,2 } },
24  { 4,{ 2,2 } },
25  { 5,{ 3,2 } }, // E.g. five tiles, split into 3 columns by 2 rows mosaic
26  { 6,{ 3,2 } }};
27 
28 int main(int argc, char * argv[]) try
29 {
31  //rs::log_to_file(rs::log_severity::debug, "librealsense.log");
32 
34  if (ctx.get_device_count() == 0) throw std::runtime_error("No device detected. Is it plugged in?");
35  rs::device & dev = *ctx.get_device(0);
36 
37  std::vector<rs::stream> supported_streams;
38 
39  for (int i = (int)rs::capabilities::depth; i <= (int)rs::capabilities::fish_eye; i++)
40  if (dev.supports((rs::capabilities)i))
41  supported_streams.push_back((rs::stream)i);
42 
43  // Configure all supported streams to run at 30 frames per second
44  for (auto & stream : supported_streams)
46 
47  // Compute field of view for each enabled stream
48  for (auto & stream : supported_streams)
49  {
50  if (!dev.is_stream_enabled(stream)) continue;
51  auto intrin = dev.get_stream_intrinsics(stream);
52  std::cout << "Capturing " << stream << " at " << intrin.width << " x " << intrin.height;
53  std::cout << std::setprecision(1) << std::fixed << ", fov = " << intrin.hfov() << " x " << intrin.vfov() << ", distortion = " << intrin.model() << std::endl;
54  }
55 
56  // Start our device
57  dev.start();
58 
59  // Open a GLFW window
60  glfwInit();
61  std::ostringstream ss; ss << "CPP Capture Example (" << dev.get_name() << ")";
62 
63  int rows = tiles_map.at(supported_streams.size()).second;
64  int cols = tiles_map.at(supported_streams.size()).first;
65  int tile_w = 640; // pixels
66  int tile_h = 480; // pixels
67  GLFWwindow * win = glfwCreateWindow(tile_w*cols, tile_h*rows, ss.str().c_str(), 0, 0);
68  glfwSetWindowUserPointer(win, &dev);
69  glfwSetKeyCallback(win, [](GLFWwindow * win, int key, int scancode, int action, int mods)
70  {
71  auto dev = reinterpret_cast<rs::device *>(glfwGetWindowUserPointer(win));
72  if (action != GLFW_RELEASE) switch (key)
73  {
77  case GLFW_KEY_E:
79  {
81  std::cout << "Setting emitter to " << value << std::endl;
83  }
84  break;
85  case GLFW_KEY_A:
87  {
89  std::cout << "Setting auto exposure to " << value << std::endl;
91  }
92  break;
93  }
94  });
96 
97  while (!glfwWindowShouldClose(win))
98  {
99  // Wait for new images
100  glfwPollEvents();
101  dev.wait_for_frames();
102 
103  // Clear the framebuffer
104  int w, h;
105  glfwGetFramebufferSize(win, &w, &h);
106  glViewport(0, 0, w, h);
107  glClear(GL_COLOR_BUFFER_BIT);
108 
109  // Draw the images
110  glPushMatrix();
111  glfwGetWindowSize(win, &w, &h);
112  glOrtho(0, w, h, 0, -1, +1);
115  buffers[2].show(dev, rs::stream::infrared, 0, h / rows, tile_w, tile_h);
116  buffers[3].show(dev, rs::stream::infrared2, w / cols, h / rows, tile_w, tile_h);
118  buffers[4].show(dev, rs::stream::fisheye, 2 * w / cols, 0, tile_w, tile_h);
119  glPopMatrix();
120  glfwSwapBuffers(win);
121  }
122 
123  glfwDestroyWindow(win);
124  glfwTerminate();
125  return EXIT_SUCCESS;
126 }
127 catch (const rs::error & e)
128 {
129  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
130  return EXIT_FAILURE;
131 }
132 catch (const std::exception & e)
133 {
134  std::cerr << e.what() << std::endl;
135  return EXIT_FAILURE;
136 }
void log_to_console(log_severity min_severity)
Definition: rs.hpp:1104
Provides convenience methods relating to devices.
Definition: rs.hpp:567
intrinsics get_stream_intrinsics(stream stream) const
Retrieves intrinsic camera parameters for specific stream.
Definition: rs.hpp:788
void enable_stream(stream stream, int width, int height, format format, int framerate, output_buffer_format output_buffer_type=output_buffer_format::continous)
Enables specific stream and requests specific properties.
Definition: rs.hpp:704
const GLint * first
Definition: glext.h:368
GLFWAPI void glfwGetWindowSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the client area of the specified window.
Definition: window.c:456
bool color_rectification_enabled
Definition: cpp-capture.cpp:18
const std::string & get_failed_args() const
Definition: rs.hpp:315
rs_error * e
GLFWAPI void * glfwGetWindowUserPointer(GLFWwindow *window)
Returns the user pointer of the specified window.
Definition: window.c:612
const GLuint * buffers
Definition: glext.h:529
Exposes librealsense functionality for C++ compilers.
GLuint GLuint stream
Definition: glext.h:1774
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1944
#define GLFW_KEY_E
Definition: glfw3.h:291
struct GLFWwindow GLFWwindow
Opaque window object.
Definition: glfw3.h:722
#define GLFW_KEY_A
Definition: glfw3.h:287
#define GLFW_RELEASE
The key or mouse button was released.
Definition: glfw3.h:225
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
void show(float rx, float ry, float rw, float rh) const
Definition: example.hpp:189
const std::map< size_t, std::pair< int, int > > tiles_map
Definition: cpp-capture.cpp:21
GLFWAPI void glfwSetWindowUserPointer(GLFWwindow *window, void *pointer)
Sets the user pointer of the specified window.
Definition: window.c:605
stream
Streams are different types of data provided by RealSense devices.
Definition: rs.hpp:24
bool is_stream_enabled(stream stream) const
Determines if specific stream is enabled.
Definition: rs.hpp:733
Context.
Definition: rs.hpp:319
const char * get_name() const
Retrieves human-readable device model string.
Definition: rs.hpp:578
typedef int(WINAPI *PFNWGLRELEASEPBUFFERDCARBPROC)(HPBUFFERARB hPbuffer
void start(rs::source source=rs::source::video)
Begins streaming on all enabled streams for this device.
Definition: rs.hpp:879
GLFWAPI void glfwSwapBuffers(GLFWwindow *window)
Swaps the front and back buffers of the specified window.
Definition: context.c:544
GLFWAPI void glfwMakeContextCurrent(GLFWwindow *window)
Makes the context of the specified window current for the calling thread.
Definition: context.c:531
double get_option(option option)
Retrieves current value of single option.
Definition: rs.hpp:954
int main(int argc, char *argv[])
Definition: cpp-capture.cpp:28
bool align_color_to_depth
Definition: cpp-capture.cpp:17
#define GLFW_KEY_D
Definition: glfw3.h:290
auto ctx
GLsizei const GLfloat * value
Definition: glext.h:693
int width
Definition: rs.h:302
bool supports_option(option option) const
Determines if device allows specific option to be queried and set.
Definition: rs.hpp:664
bool align_depth_to_color
Definition: cpp-capture.cpp:16
GLFWAPI GLFWwindow * glfwCreateWindow(int width, int height, const char *title, GLFWmonitor *monitor, GLFWwindow *share)
Creates a window and its associated context.
Definition: window.c:116
rs_device * dev
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:369
GLFWAPI void glfwGetFramebufferSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the framebuffer of the specified window.
Definition: window.c:484
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
GLFWAPI GLFWkeyfun glfwSetKeyCallback(GLFWwindow *window, GLFWkeyfun cbfun)
Sets the key callback.
Definition: input.c:458
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:682
bool supports(capabilities capability) const
Determines device capabilities.
Definition: rs.hpp:1005
device * get_device(int index)
Definition: rs.hpp:354
#define GLFW_KEY_R
Definition: glfw3.h:304
void set_option(option option, double value)
Sets current value of single option.
Definition: rs.hpp:976
const std::string & get_failed_function() const
Definition: rs.hpp:314
capabilities
Specifies various capabilities of a RealSense device.
Definition: rs.hpp:169
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:726
void wait_for_frames()
Blocks until new frames are available.
Definition: rs.hpp:985
int get_device_count() const
Definition: rs.hpp:343
#define GLFW_KEY_C
Definition: glfw3.h:289
GLFWAPI int glfwWindowShouldClose(GLFWwindow *window)
Checks the close flag of the specified window.
Definition: window.c:406


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Fri Mar 13 2020 03:16:17