cpp-tutorial-2-streams.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 
5 // librealsense tutorial #2 - Accessing multiple streams //
7 
8 // First include the librealsense C++ header file
9 #include <librealsense/rs.hpp>
10 #include <cstdio>
11 
12 // Also include GLFW to allow for graphical display
13 #include <GLFW/glfw3.h>
14 
15 int main() try
16 {
17  // Create a context object. This object owns the handles to all connected realsense devices.
19  printf("There are %d connected RealSense devices.\n", ctx.get_device_count());
20  if(ctx.get_device_count() == 0) return EXIT_FAILURE;
21 
22  // This tutorial will access only a single device, but it is trivial to extend to multiple devices
23  rs::device * dev = ctx.get_device(0);
24  printf("\nUsing device 0, an %s\n", dev->get_name());
25  printf(" Serial number: %s\n", dev->get_serial());
26  printf(" Firmware version: %s\n", dev->get_firmware_version());
27 
28  // Configure all streams to run at VGA resolution at 60 frames per second
29  dev->enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 60);
32  try { dev->enable_stream(rs::stream::infrared2, 640, 480, rs::format::y8, 60); }
33  catch(...) { printf("Device does not provide infrared2 stream.\n"); }
34  dev->start();
35 
36  // Open a GLFW window to display our output
37  glfwInit();
38  GLFWwindow * win = glfwCreateWindow(1280, 960, "librealsense tutorial #2", nullptr, nullptr);
40  while(!glfwWindowShouldClose(win))
41  {
42  // Wait for new frame data
44  dev->wait_for_frames();
45 
46  glClear(GL_COLOR_BUFFER_BIT);
47  glPixelZoom(1, -1);
48 
49  // Display depth data by linearly mapping depth between 0 and 2 meters to the red channel
50  glRasterPos2f(-1, 1);
51  glPixelTransferf(GL_RED_SCALE, 0xFFFF * dev->get_depth_scale() / 2.0f);
52  glDrawPixels(640, 480, GL_RED, GL_UNSIGNED_SHORT, dev->get_frame_data(rs::stream::depth));
53  glPixelTransferf(GL_RED_SCALE, 1.0f);
54 
55  // Display color image as RGB triples
56  glRasterPos2f(0, 1);
57  glDrawPixels(640, 480, GL_RGB, GL_UNSIGNED_BYTE, dev->get_frame_data(rs::stream::color));
58 
59  // Display infrared image by mapping IR intensity to visible luminance
60  glRasterPos2f(-1, 0);
61  glDrawPixels(640, 480, GL_LUMINANCE, GL_UNSIGNED_BYTE, dev->get_frame_data(rs::stream::infrared));
62 
63  // Display second infrared image by mapping IR intensity to visible luminance
65  {
66  glRasterPos2f(0, 0);
67  glDrawPixels(640, 480, GL_LUMINANCE, GL_UNSIGNED_BYTE, dev->get_frame_data(rs::stream::infrared2));
68  }
69 
70  glfwSwapBuffers(win);
71  }
72 
73  return EXIT_SUCCESS;
74 }
75 catch(const rs::error & e)
76 {
77  // Method calls against librealsense objects may throw exceptions of type rs::error
78  printf("rs::error was thrown when calling %s(%s):\n", e.get_failed_function().c_str(), e.get_failed_args().c_str());
79  printf(" %s\n", e.what());
80  return EXIT_FAILURE;
81 }
Provides convenience methods relating to devices.
Definition: rs.hpp:567
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 std::string & get_failed_args() const
Definition: rs.hpp:315
rs_error * e
const char * get_firmware_version() const
Retrieves version of firmware currently installed on device.
Definition: rs.hpp:608
Exposes librealsense functionality for C++ compilers.
const void * get_frame_data(stream stream) const
Retrieves contents of latest frame on a stream.
Definition: rs.hpp:1050
struct GLFWwindow GLFWwindow
Opaque window object.
Definition: glfw3.h:722
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
bool is_stream_enabled(stream stream) const
Determines if specific stream is enabled.
Definition: rs.hpp:733
Context.
Definition: rs.hpp:319
GLfloat f
Definition: glext.h:1868
const char * get_name() const
Retrieves human-readable device model string.
Definition: rs.hpp:578
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
auto ctx
const char * get_serial() const
Retrieves unique serial number of device.
Definition: rs.hpp:588
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
int main()
rs_device * dev
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:682
float get_depth_scale() const
Retrieves mapping between units of depth image and meters.
Definition: rs.hpp:653
device * get_device(int index)
Definition: rs.hpp:354
const std::string & get_failed_function() const
Definition: rs.hpp:314
void wait_for_frames()
Blocks until new frames are available.
Definition: rs.hpp:985
int get_device_count() const
Definition: rs.hpp:343
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