00001
00002
00003
00004 #include <librealsense/rs.hpp>
00005 #include "example.hpp"
00006
00007 #include <sstream>
00008 #include <iostream>
00009 #include <iomanip>
00010 #include <thread>
00011 #include <algorithm>
00012 #include <map>
00013 #include <memory>
00014
00015 texture_buffer buffers[RS_STREAM_COUNT];
00016 bool align_depth_to_color = false;
00017 bool align_color_to_depth = false;
00018 bool color_rectification_enabled = false;
00019
00020
00021 const std::map<size_t, std::pair<int, int>> tiles_map = { { 1,{ 1,1 } },
00022 { 2,{ 2,1 } },
00023 { 3,{ 2,2 } },
00024 { 4,{ 2,2 } },
00025 { 5,{ 3,2 } },
00026 { 6,{ 3,2 } }};
00027
00028 int main(int argc, char * argv[]) try
00029 {
00030 rs::log_to_console(rs::log_severity::warn);
00031
00032
00033 rs::context ctx;
00034 if (ctx.get_device_count() == 0) throw std::runtime_error("No device detected. Is it plugged in?");
00035 rs::device & dev = *ctx.get_device(0);
00036
00037 std::vector<rs::stream> supported_streams;
00038
00039 for (int i = (int)rs::capabilities::depth; i <= (int)rs::capabilities::fish_eye; i++)
00040 if (dev.supports((rs::capabilities)i))
00041 supported_streams.push_back((rs::stream)i);
00042
00043
00044 for (auto & stream : supported_streams)
00045 dev.enable_stream(stream, rs::preset::best_quality);
00046
00047
00048 for (auto & stream : supported_streams)
00049 {
00050 if (!dev.is_stream_enabled(stream)) continue;
00051 auto intrin = dev.get_stream_intrinsics(stream);
00052 std::cout << "Capturing " << stream << " at " << intrin.width << " x " << intrin.height;
00053 std::cout << std::setprecision(1) << std::fixed << ", fov = " << intrin.hfov() << " x " << intrin.vfov() << ", distortion = " << intrin.model() << std::endl;
00054 }
00055
00056
00057 dev.start();
00058
00059
00060 glfwInit();
00061 std::ostringstream ss; ss << "CPP Capture Example (" << dev.get_name() << ")";
00062
00063 int rows = tiles_map.at(supported_streams.size()).second;
00064 int cols = tiles_map.at(supported_streams.size()).first;
00065 int tile_w = 640;
00066 int tile_h = 480;
00067 GLFWwindow * win = glfwCreateWindow(tile_w*cols, tile_h*rows, ss.str().c_str(), 0, 0);
00068 glfwSetWindowUserPointer(win, &dev);
00069 glfwSetKeyCallback(win, [](GLFWwindow * win, int key, int scancode, int action, int mods)
00070 {
00071 auto dev = reinterpret_cast<rs::device *>(glfwGetWindowUserPointer(win));
00072 if (action != GLFW_RELEASE) switch (key)
00073 {
00074 case GLFW_KEY_R: color_rectification_enabled = !color_rectification_enabled; break;
00075 case GLFW_KEY_C: align_color_to_depth = !align_color_to_depth; break;
00076 case GLFW_KEY_D: align_depth_to_color = !align_depth_to_color; break;
00077 case GLFW_KEY_E:
00078 if (dev->supports_option(rs::option::r200_emitter_enabled))
00079 {
00080 int value = !dev->get_option(rs::option::r200_emitter_enabled);
00081 std::cout << "Setting emitter to " << value << std::endl;
00082 dev->set_option(rs::option::r200_emitter_enabled, value);
00083 }
00084 break;
00085 case GLFW_KEY_A:
00086 if (dev->supports_option(rs::option::r200_lr_auto_exposure_enabled))
00087 {
00088 int value = !dev->get_option(rs::option::r200_lr_auto_exposure_enabled);
00089 std::cout << "Setting auto exposure to " << value << std::endl;
00090 dev->set_option(rs::option::r200_lr_auto_exposure_enabled, value);
00091 }
00092 break;
00093 }
00094 });
00095 glfwMakeContextCurrent(win);
00096
00097 while (!glfwWindowShouldClose(win))
00098 {
00099
00100 glfwPollEvents();
00101 dev.wait_for_frames();
00102
00103
00104 int w, h;
00105 glfwGetFramebufferSize(win, &w, &h);
00106 glViewport(0, 0, w, h);
00107 glClear(GL_COLOR_BUFFER_BIT);
00108
00109
00110 glPushMatrix();
00111 glfwGetWindowSize(win, &w, &h);
00112 glOrtho(0, w, h, 0, -1, +1);
00113 buffers[0].show(dev, align_color_to_depth ? rs::stream::color_aligned_to_depth : (color_rectification_enabled ? rs::stream::rectified_color : rs::stream::color), 0, 0, tile_w, tile_h);
00114 buffers[1].show(dev, align_depth_to_color ? (color_rectification_enabled ? rs::stream::depth_aligned_to_rectified_color : rs::stream::depth_aligned_to_color) : rs::stream::depth, w / cols, 0, tile_w, tile_h);
00115 buffers[2].show(dev, rs::stream::infrared, 0, h / rows, tile_w, tile_h);
00116 buffers[3].show(dev, rs::stream::infrared2, w / cols, h / rows, tile_w, tile_h);
00117 if (dev.is_stream_enabled(rs::stream::fisheye))
00118 buffers[4].show(dev, rs::stream::fisheye, 2 * w / cols, 0, tile_w, tile_h);
00119 glPopMatrix();
00120 glfwSwapBuffers(win);
00121 }
00122
00123 glfwDestroyWindow(win);
00124 glfwTerminate();
00125 return EXIT_SUCCESS;
00126 }
00127 catch (const rs::error & e)
00128 {
00129 std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
00130 return EXIT_FAILURE;
00131 }
00132 catch (const std::exception & e)
00133 {
00134 std::cerr << e.what() << std::endl;
00135 return EXIT_FAILURE;
00136 }