cpp-restart.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 #define _USE_MATH_DEFINES
8 #include <math.h>
9 #include <sstream>
10 #include <iostream>
11 #include <iomanip>
12 #include <thread>
13 
15 
16 int main(int argc, char * argv[]) try
17 {
19  //rs::log_to_file(rs::log_severity::debug, "librealsense.log");
20 
22  if(ctx.get_device_count() == 0) throw std::runtime_error("No device detected. Is it plugged in?");
23  rs::device & dev = *ctx.get_device(0);
24 
25  // Open a GLFW window
26  glfwInit();
27  std::ostringstream ss; ss << "CPP Restart Example (" << dev.get_name() << ")";
28  GLFWwindow * win = glfwCreateWindow(1280, 960, ss.str().c_str(), 0, 0);
30 
31  for(int i=0; i<20; ++i)
32  {
33  try
34  {
35  if(dev.is_streaming()) dev.stop();
36 
37  std::this_thread::sleep_for(std::chrono::milliseconds(100));
38 
39  for(int j=0; j<4; ++j)
40  {
41  auto s = (rs::stream)j;
42  if(dev.is_stream_enabled(s)) dev.disable_stream(s);
43  }
44 
45  switch(i)
46  {
47  case 0:
49  break;
50  case 1:
52  break;
53  case 2:
55  break;
56  case 3:
58  break;
59  case 4:
61  break;
62  case 5:
64  break;
65  case 6:
67  break;
68  case 7:
70  break;
71  case 8:
73  break;
74  case 9:
76  break;
77  case 10:
79  break;
80  case 11:
82  break;
83  case 12:
85  break;
86  case 13:
89  break;
90  case 14:
93  break;
94  case 15:
97  break;
98  case 16:
101  break;
102  case 17:
105  break;
106  case 18:
110  break;
111  case 19:
116  break;
117  }
118 
119  dev.start();
120  for(int j=0; j<120; ++j)
121  {
122  // Wait for new images
123  glfwPollEvents();
124  if(glfwWindowShouldClose(win)) goto done;
125  dev.wait_for_frames();
126 
127  // Clear the framebuffer
128  int w,h;
129  glfwGetWindowSize(win, &w, &h);
130  glViewport(0, 0, w, h);
131  glClear(GL_COLOR_BUFFER_BIT);
132 
133  // Draw the images
134  glPushMatrix();
135  glfwGetWindowSize(win, &w, &h);
136  glOrtho(0, w, h, 0, -1, +1);
137  buffers[0].show(dev, rs::stream::color, 0, 0, w/2, h/2);
138  buffers[1].show(dev, rs::stream::depth, w/2, 0, w-w/2, h/2);
139  buffers[2].show(dev, rs::stream::infrared, 0, h/2, w/2, h-h/2);
140  buffers[3].show(dev, rs::stream::infrared2, w/2, h/2, w-w/2, h-h/2);
141  glPopMatrix();
142  glfwSwapBuffers(win);
143  }
144  }
145  catch(const rs::error & e)
146  {
147  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
148  std::cout << "Skipping mode " << i << std::endl;
149  }
150  }
151 done:
152  glfwDestroyWindow(win);
153  glfwTerminate();
154  return EXIT_SUCCESS;
155 }
156 catch(const rs::error & e)
157 {
158  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
159  return EXIT_FAILURE;
160 }
161 catch(const std::exception & e)
162 {
163  std::cerr << e.what() << std::endl;
164  return EXIT_FAILURE;
165 }
void log_to_console(log_severity min_severity)
Definition: rs.hpp:1104
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
GLFWAPI void glfwGetWindowSize(GLFWwindow *window, int *width, int *height)
Retrieves the size of the client area of the specified window.
Definition: window.c:456
const std::string & get_failed_args() const
Definition: rs.hpp:315
rs_error * e
const GLuint * buffers
Definition: glext.h:529
Exposes librealsense functionality for C++ compilers.
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1944
struct GLFWwindow GLFWwindow
Opaque window object.
Definition: glfw3.h:722
void stop(rs::source source=rs::source::video)
Ends streaming on all streams for this device.
Definition: rs.hpp:887
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
void show(float rx, float ry, float rw, float rh) const
Definition: example.hpp:189
stream
Streams are different types of data provided by RealSense devices.
Definition: rs.hpp:24
int main(int argc, char *argv[])
Definition: cpp-restart.cpp:16
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
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
static int done
auto ctx
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
void disable_stream(stream stream)
Disables specific stream.
Definition: rs.hpp:723
GLdouble s
Definition: glext.h:231
rs_device * dev
GLFWAPI void glfwDestroyWindow(GLFWwindow *window)
Destroys the specified window and its context.
Definition: window.c:369
GLFWAPI void glfwTerminate(void)
Terminates the GLFW library.
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:682
bool is_streaming() const
Determines if device is currently streaming.
Definition: rs.hpp:896
device * get_device(int index)
Definition: rs.hpp:354
const std::string & get_failed_function() const
Definition: rs.hpp:314
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
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