c-tutorial-2-streams.c
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 /*******************************************************\
5 * librealsense tutorial #2 - Accessing multiple streams *
6 \*******************************************************/
7 
8 /* First include the librealsense C header file */
9 #include <librealsense/rs.h>
10 #include <stdlib.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 
14 /* Also include GLFW to allow for graphical display */
15 #include <GLFW/glfw3.h>
16 
17 /* Function calls to librealsense may raise errors of type rs_error */
18 rs_error * e = 0;
20 {
21  if(e)
22  {
23  printf("rs_error was raised when calling %s(%s):\n", rs_get_failed_function(e), rs_get_failed_args(e));
24  printf(" %s\n", rs_get_error_message(e));
25  exit(EXIT_FAILURE);
26  }
27 }
28 
29 int main()
30 {
31  /* Create a context object. This object owns the handles to all connected realsense devices. */
33  check_error();
34  printf("There are %d connected RealSense devices.\n", rs_get_device_count(ctx, &e));
35  check_error();
36  if(rs_get_device_count(ctx, &e) == 0) return EXIT_FAILURE;
37 
38  /* This tutorial will access only a single device, but it is trivial to extend to multiple devices */
39  rs_device * dev = rs_get_device(ctx, 0, &e);
40  check_error();
41  printf("\nUsing device 0, an %s\n", rs_get_device_name(dev, &e));
42  check_error();
43  printf(" Serial number: %s\n", rs_get_device_serial(dev, &e));
44  check_error();
45  printf(" Firmware version: %s\n", rs_get_device_firmware_version(dev, &e));
46  check_error();
47 
48  /* Configure all streams to run at VGA resolution at 60 frames per second */
49  rs_enable_stream(dev, RS_STREAM_DEPTH, 640, 480, RS_FORMAT_Z16, 60, &e);
50  check_error();
51  rs_enable_stream(dev, RS_STREAM_COLOR, 640, 480, RS_FORMAT_RGB8, 60, &e);
52  check_error();
53  rs_enable_stream(dev, RS_STREAM_INFRARED, 640, 480, RS_FORMAT_Y8, 60, &e);
54  check_error();
55  rs_enable_stream(dev, RS_STREAM_INFRARED2, 640, 480, RS_FORMAT_Y8, 60, NULL); /* Pass NULL to ignore errors */
56  rs_start_device(dev, &e);
57  check_error();
58 
59  /* Open a GLFW window to display our output */
60  glfwInit();
61  GLFWwindow * win = glfwCreateWindow(1280, 960, "librealsense tutorial #2", NULL, NULL);
63  while(!glfwWindowShouldClose(win))
64  {
65  /* Wait for new frame data */
67  rs_wait_for_frames(dev, &e);
68  check_error();
69 
70  glClear(GL_COLOR_BUFFER_BIT);
71  glPixelZoom(1, -1);
72 
73  /* Display depth data by linearly mapping depth between 0 and 2 meters to the red channel */
74  glRasterPos2f(-1, 1);
75  glPixelTransferf(GL_RED_SCALE, 0xFFFF * rs_get_device_depth_scale(dev, &e) / 2.0f);
76  check_error();
77  glDrawPixels(640, 480, GL_RED, GL_UNSIGNED_SHORT, rs_get_frame_data(dev, RS_STREAM_DEPTH, &e));
78  check_error();
79  glPixelTransferf(GL_RED_SCALE, 1.0f);
80 
81  /* Display color image as RGB triples */
82  glRasterPos2f(0, 1);
83  glDrawPixels(640, 480, GL_RGB, GL_UNSIGNED_BYTE, rs_get_frame_data(dev, RS_STREAM_COLOR, &e));
84  check_error();
85 
86  /* Display infrared image by mapping IR intensity to visible luminance */
87  glRasterPos2f(-1, 0);
88  glDrawPixels(640, 480, GL_LUMINANCE, GL_UNSIGNED_BYTE, rs_get_frame_data(dev, RS_STREAM_INFRARED, &e));
89  check_error();
90 
91  /* Display second infrared image by mapping IR intensity to visible luminance */
93  {
94  glRasterPos2f(0, 0);
95  glDrawPixels(640, 480, GL_LUMINANCE, GL_UNSIGNED_BYTE, rs_get_frame_data(dev, RS_STREAM_INFRARED2, &e));
96  }
97 
98  glfwSwapBuffers(win);
99  }
100 
101  return EXIT_SUCCESS;
102 }
#define RS_API_VERSION
Definition: rs.h:28
Definition: rs.cpp:16
int rs_get_device_count(const rs_context *context, rs_error **error)
Determines number of connected devices.
Definition: rs.cpp:113
const void * rs_get_frame_data(const rs_device *device, rs_stream stream, rs_error **error)
Retrieves the contents of the latest frame on a stream.
Definition: rs.cpp:490
const char * rs_get_device_name(const rs_device *device, rs_error **error)
Retrieves human-readable device model string.
Definition: rs.cpp:128
const char * rs_get_error_message(const rs_error *error)
Returns static pointer to error message.
Definition: rs.cpp:751
float rs_get_device_depth_scale(const rs_device *device, rs_error **error)
Retrieves mapping between the units of the depth image and meters.
Definition: rs.cpp:420
Exposes librealsense functionality for C compilers.
struct GLFWwindow GLFWwindow
Opaque window object.
Definition: glfw3.h:722
const char * rs_get_failed_function(const rs_error *error)
Returns static pointer to name of a failing function in case of error.
Definition: rs.cpp:749
GLFWAPI int glfwInit(void)
Initializes the GLFW library.
rs_context * rs_create_context(int api_version, rs_error **error)
Creates RealSense context that is required for the rest of the API.
Definition: rs.cpp:75
GLfloat f
Definition: glext.h:1868
const char * rs_get_device_firmware_version(const rs_device *device, rs_error **error)
Retrieves the version of the firmware currently installed on the device.
Definition: rs.cpp:156
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
void rs_enable_stream(rs_device *device, rs_stream stream, int width, int height, rs_format format, int framerate, rs_error **error)
Enables a specific stream and requests specific properties.
Definition: rs.cpp:220
const char * rs_get_failed_args(const rs_error *error)
Returns static pointer to arguments of a failing function in case of error.
Definition: rs.cpp:750
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
rs_error * e
void check_error()
rs_device * dev
rs_device * rs_get_device(rs_context *context, int index, rs_error **error)
Retrieves connected device by index.
Definition: rs.cpp:120
const char * rs_get_device_serial(const rs_device *device, rs_error **error)
Retrieves unique serial number of the device.
Definition: rs.cpp:135
void rs_wait_for_frames(rs_device *device, rs_error **error)
Blocks until new frames are available.
Definition: rs.cpp:428
GLFWAPI void glfwPollEvents(void)
Processes all pending events.
Definition: window.c:682
int main()
void rs_start_device(rs_device *device, rs_error **error)
Begins streaming on all enabled streams for this device.
Definition: rs.cpp:383
int rs_is_stream_enabled(const rs_device *device, rs_stream stream, rs_error **error)
Determines if a specific stream is enabled.
Definition: rs.cpp:249
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:11