cpp-restart.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 
00004 #include <librealsense/rs.hpp>
00005 #include "example.hpp"
00006 
00007 #define _USE_MATH_DEFINES
00008 #include <math.h>
00009 #include <sstream>
00010 #include <iostream>
00011 #include <iomanip>
00012 #include <thread>
00013 
00014 texture_buffer buffers[RS_STREAM_COUNT];
00015 
00016 int main(int argc, char * argv[]) try
00017 {
00018     rs::log_to_console(rs::log_severity::warn);
00019     //rs::log_to_file(rs::log_severity::debug, "librealsense.log");
00020 
00021     rs::context ctx;
00022     if(ctx.get_device_count() == 0) throw std::runtime_error("No device detected. Is it plugged in?");
00023     rs::device & dev = *ctx.get_device(0);
00024 
00025     // Open a GLFW window
00026     glfwInit();
00027     std::ostringstream ss; ss << "CPP Restart Example (" << dev.get_name() << ")";
00028     GLFWwindow * win = glfwCreateWindow(1280, 960, ss.str().c_str(), 0, 0);
00029     glfwMakeContextCurrent(win);
00030 
00031     for(int i=0; i<20; ++i)
00032     {
00033         try
00034         {
00035             if(dev.is_streaming()) dev.stop();
00036 
00037             std::this_thread::sleep_for(std::chrono::milliseconds(100));
00038 
00039             for(int j=0; j<4; ++j)
00040             {
00041                 auto s = (rs::stream)j;
00042                 if(dev.is_stream_enabled(s)) dev.disable_stream(s);
00043             }
00044 
00045             switch(i)
00046             {
00047             case 0:
00048                 dev.enable_stream(rs::stream::color, 640, 480, rs::format::yuyv, 60);
00049                 break;
00050             case 1:
00051                 dev.enable_stream(rs::stream::color, 640, 480, rs::format::rgb8, 60);
00052                 break;
00053             case 2:
00054                 dev.enable_stream(rs::stream::color, 640, 480, rs::format::bgr8, 60);
00055                 break;
00056             case 3:
00057                 dev.enable_stream(rs::stream::color, 640, 480, rs::format::rgba8, 60);
00058                 break;
00059             case 4:
00060                 dev.enable_stream(rs::stream::color, 640, 480, rs::format::bgra8, 60);
00061                 break;
00062             case 5:
00063                 dev.enable_stream(rs::stream::color, 1920, 1080, rs::format::rgb8, 0);
00064                 break;
00065             case 6:
00066                 dev.enable_stream(rs::stream::depth, rs::preset::largest_image);
00067                 break;
00068             case 7:
00069                 dev.enable_stream(rs::stream::depth, 480, 360, rs::format::z16, 60);
00070                 break;
00071             case 8:
00072                 dev.enable_stream(rs::stream::depth, 320, 240, rs::format::z16, 60);
00073                 break;
00074             case 9:
00075                 dev.enable_stream(rs::stream::infrared, rs::preset::largest_image);
00076                 break;
00077             case 10:
00078                 dev.enable_stream(rs::stream::infrared, 492, 372, rs::format::y8, 60);
00079                 break;
00080             case 11:
00081                 dev.enable_stream(rs::stream::infrared, 320, 240, rs::format::y8, 60);
00082                 break;
00083             case 12:
00084                 dev.enable_stream(rs::stream::infrared, 0, 0, rs::format::y16, 60);
00085                 break;
00086             case 13:
00087                 dev.enable_stream(rs::stream::infrared, 0, 0, rs::format::y8, 60);
00088                 dev.enable_stream(rs::stream::infrared2, 0, 0, rs::format::y8, 60);
00089                 break;
00090             case 14:
00091                 dev.enable_stream(rs::stream::infrared, 0, 0, rs::format::y16, 60);
00092                 dev.enable_stream(rs::stream::infrared2, 0, 0, rs::format::y16, 60);
00093                 break;
00094             case 15:
00095                 dev.enable_stream(rs::stream::depth, rs::preset::best_quality);
00096                 dev.enable_stream(rs::stream::infrared, 0, 0, rs::format::y8, 0);
00097                 break;
00098             case 16:
00099                 dev.enable_stream(rs::stream::depth, rs::preset::best_quality);
00100                 dev.enable_stream(rs::stream::infrared, 0, 0, rs::format::y16, 0);
00101                 break;
00102             case 17:
00103                 dev.enable_stream(rs::stream::depth, rs::preset::best_quality);
00104                 dev.enable_stream(rs::stream::color, rs::preset::best_quality);
00105                 break;
00106             case 18:
00107                 dev.enable_stream(rs::stream::depth, rs::preset::best_quality);
00108                 dev.enable_stream(rs::stream::color, rs::preset::best_quality);
00109                 dev.enable_stream(rs::stream::infrared, rs::preset::best_quality);
00110                 break;
00111             case 19:
00112                 dev.enable_stream(rs::stream::depth, rs::preset::best_quality);
00113                 dev.enable_stream(rs::stream::color, rs::preset::best_quality);
00114                 dev.enable_stream(rs::stream::infrared, rs::preset::best_quality);
00115                 dev.enable_stream(rs::stream::infrared2, rs::preset::best_quality);
00116                 break;
00117             }
00118 
00119             dev.start();
00120             for(int j=0; j<120; ++j)
00121             {
00122                 // Wait for new images
00123                 glfwPollEvents();
00124                 if(glfwWindowShouldClose(win)) goto done;
00125                 dev.wait_for_frames();
00126 
00127                 // Clear the framebuffer
00128                 int w,h;
00129                 glfwGetWindowSize(win, &w, &h);
00130                 glViewport(0, 0, w, h);
00131                 glClear(GL_COLOR_BUFFER_BIT);
00132 
00133                 // Draw the images
00134                 glPushMatrix();
00135                 glfwGetWindowSize(win, &w, &h);
00136                 glOrtho(0, w, h, 0, -1, +1);
00137                 buffers[0].show(dev, rs::stream::color, 0, 0, w/2, h/2);
00138                 buffers[1].show(dev, rs::stream::depth, w/2, 0, w-w/2, h/2);
00139                 buffers[2].show(dev, rs::stream::infrared, 0, h/2, w/2, h-h/2);
00140                 buffers[3].show(dev, rs::stream::infrared2, w/2, h/2, w-w/2, h-h/2);
00141                 glPopMatrix();
00142                 glfwSwapBuffers(win);
00143             }
00144         }
00145         catch(const rs::error & e)
00146         {
00147             std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n    " << e.what() << std::endl;
00148             std::cout << "Skipping mode " << i << std::endl;
00149         }
00150     }
00151 done:
00152     glfwDestroyWindow(win);
00153     glfwTerminate();
00154     return EXIT_SUCCESS;
00155 }
00156 catch(const rs::error & e)
00157 {
00158     std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n    " << e.what() << std::endl;
00159     return EXIT_FAILURE;
00160 }
00161 catch(const std::exception & e)
00162 {
00163     std::cerr << e.what() << std::endl;
00164     return EXIT_FAILURE;
00165 }


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