cpp-tutorial-2-streams.cpp
Go to the documentation of this file.
00001 // License: Apache 2.0. See LICENSE file in root directory.
00002 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
00003 
00005 // librealsense tutorial #2 - Accessing multiple streams //
00007 
00008 // First include the librealsense C++ header file
00009 #include <librealsense/rs.hpp>
00010 #include <cstdio>
00011 
00012 // Also include GLFW to allow for graphical display
00013 #include <GLFW/glfw3.h>
00014 
00015 int main() try
00016 {
00017     // Create a context object. This object owns the handles to all connected realsense devices.
00018     rs::context ctx;
00019     printf("There are %d connected RealSense devices.\n", ctx.get_device_count());
00020     if(ctx.get_device_count() == 0) return EXIT_FAILURE;
00021 
00022     // This tutorial will access only a single device, but it is trivial to extend to multiple devices
00023     rs::device * dev = ctx.get_device(0);
00024     printf("\nUsing device 0, an %s\n", dev->get_name());
00025     printf("    Serial number: %s\n", dev->get_serial());
00026     printf("    Firmware version: %s\n", dev->get_firmware_version());
00027 
00028     // Configure all streams to run at VGA resolution at 60 frames per second
00029     dev->enable_stream(rs::stream::depth, 640, 480, rs::format::z16, 60);
00030     dev->enable_stream(rs::stream::color, 640, 480, rs::format::rgb8, 60);
00031     dev->enable_stream(rs::stream::infrared, 640, 480, rs::format::y8, 60);
00032     try { dev->enable_stream(rs::stream::infrared2, 640, 480, rs::format::y8, 60); }
00033     catch(...) { printf("Device does not provide infrared2 stream.\n"); }
00034     dev->start();
00035 
00036     // Open a GLFW window to display our output
00037     glfwInit();
00038     GLFWwindow * win = glfwCreateWindow(1280, 960, "librealsense tutorial #2", nullptr, nullptr);
00039     glfwMakeContextCurrent(win);
00040     while(!glfwWindowShouldClose(win))
00041     {
00042         // Wait for new frame data
00043         glfwPollEvents();
00044         dev->wait_for_frames();
00045 
00046         glClear(GL_COLOR_BUFFER_BIT);
00047         glPixelZoom(1, -1);
00048 
00049         // Display depth data by linearly mapping depth between 0 and 2 meters to the red channel
00050         glRasterPos2f(-1, 1);
00051         glPixelTransferf(GL_RED_SCALE, 0xFFFF * dev->get_depth_scale() / 2.0f);
00052         glDrawPixels(640, 480, GL_RED, GL_UNSIGNED_SHORT, dev->get_frame_data(rs::stream::depth));
00053         glPixelTransferf(GL_RED_SCALE, 1.0f);
00054 
00055         // Display color image as RGB triples
00056         glRasterPos2f(0, 1);
00057         glDrawPixels(640, 480, GL_RGB, GL_UNSIGNED_BYTE, dev->get_frame_data(rs::stream::color));
00058 
00059         // Display infrared image by mapping IR intensity to visible luminance
00060         glRasterPos2f(-1, 0);
00061         glDrawPixels(640, 480, GL_LUMINANCE, GL_UNSIGNED_BYTE, dev->get_frame_data(rs::stream::infrared));
00062 
00063         // Display second infrared image by mapping IR intensity to visible luminance
00064         if(dev->is_stream_enabled(rs::stream::infrared2))
00065         {
00066             glRasterPos2f(0, 0);
00067             glDrawPixels(640, 480, GL_LUMINANCE, GL_UNSIGNED_BYTE, dev->get_frame_data(rs::stream::infrared2));
00068         }
00069 
00070         glfwSwapBuffers(win);
00071     }
00072 
00073     return EXIT_SUCCESS;
00074 }
00075 catch(const rs::error & e)
00076 {
00077     // Method calls against librealsense objects may throw exceptions of type rs::error
00078     printf("rs::error was thrown when calling %s(%s):\n", e.get_failed_function().c_str(), e.get_failed_args().c_str());
00079     printf("    %s\n", e.what());
00080     return EXIT_FAILURE;
00081 }


librealsense
Author(s): Sergey Dorodnicov , Mark Horn , Reagan Lopez
autogenerated on Tue Jun 25 2019 19:54:38