c-tutorial-2-streams.c
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 /*******************************************************\
00005 * librealsense tutorial #2 - Accessing multiple streams *
00006 \*******************************************************/
00007 
00008 /* First include the librealsense C header file */
00009 #include <librealsense/rs.h>
00010 #include <stdlib.h>
00011 #include <stdint.h>
00012 #include <stdio.h>
00013 
00014 /* Also include GLFW to allow for graphical display */
00015 #include <GLFW/glfw3.h>
00016 
00017 /* Function calls to librealsense may raise errors of type rs_error */
00018 rs_error * e = 0;
00019 void check_error()
00020 {
00021     if(e)
00022     {
00023         printf("rs_error was raised when calling %s(%s):\n", rs_get_failed_function(e), rs_get_failed_args(e));
00024         printf("    %s\n", rs_get_error_message(e));
00025         exit(EXIT_FAILURE);
00026     }
00027 }
00028 
00029 int main()
00030 {
00031     /* Create a context object. This object owns the handles to all connected realsense devices. */
00032     rs_context * ctx = rs_create_context(RS_API_VERSION, &e);
00033     check_error();
00034     printf("There are %d connected RealSense devices.\n", rs_get_device_count(ctx, &e));
00035     check_error();
00036     if(rs_get_device_count(ctx, &e) == 0) return EXIT_FAILURE;
00037 
00038     /* This tutorial will access only a single device, but it is trivial to extend to multiple devices */
00039     rs_device * dev = rs_get_device(ctx, 0, &e);
00040     check_error();
00041     printf("\nUsing device 0, an %s\n", rs_get_device_name(dev, &e));
00042     check_error();
00043     printf("    Serial number: %s\n", rs_get_device_serial(dev, &e));
00044     check_error();
00045     printf("    Firmware version: %s\n", rs_get_device_firmware_version(dev, &e));
00046     check_error();
00047 
00048     /* Configure all streams to run at VGA resolution at 60 frames per second */
00049     rs_enable_stream(dev, RS_STREAM_DEPTH, 640, 480, RS_FORMAT_Z16, 60, &e);
00050     check_error();
00051     rs_enable_stream(dev, RS_STREAM_COLOR, 640, 480, RS_FORMAT_RGB8, 60, &e);
00052     check_error();
00053     rs_enable_stream(dev, RS_STREAM_INFRARED, 640, 480, RS_FORMAT_Y8, 60, &e);
00054     check_error();
00055     rs_enable_stream(dev, RS_STREAM_INFRARED2, 640, 480, RS_FORMAT_Y8, 60, NULL); /* Pass NULL to ignore errors */
00056     rs_start_device(dev, &e);
00057     check_error();
00058 
00059     /* Open a GLFW window to display our output */
00060     glfwInit();
00061     GLFWwindow * win = glfwCreateWindow(1280, 960, "librealsense tutorial #2", NULL, NULL);
00062     glfwMakeContextCurrent(win);
00063     while(!glfwWindowShouldClose(win))
00064     {
00065         /* Wait for new frame data */
00066         glfwPollEvents();
00067         rs_wait_for_frames(dev, &e);
00068         check_error();
00069 
00070         glClear(GL_COLOR_BUFFER_BIT);
00071         glPixelZoom(1, -1);
00072 
00073         /* Display depth data by linearly mapping depth between 0 and 2 meters to the red channel */
00074         glRasterPos2f(-1, 1);
00075         glPixelTransferf(GL_RED_SCALE, 0xFFFF * rs_get_device_depth_scale(dev, &e) / 2.0f);
00076         check_error();
00077         glDrawPixels(640, 480, GL_RED, GL_UNSIGNED_SHORT, rs_get_frame_data(dev, RS_STREAM_DEPTH, &e));
00078         check_error();
00079         glPixelTransferf(GL_RED_SCALE, 1.0f);
00080 
00081         /* Display color image as RGB triples */
00082         glRasterPos2f(0, 1);
00083         glDrawPixels(640, 480, GL_RGB, GL_UNSIGNED_BYTE, rs_get_frame_data(dev, RS_STREAM_COLOR, &e));
00084         check_error();
00085 
00086         /* Display infrared image by mapping IR intensity to visible luminance */
00087         glRasterPos2f(-1, 0);
00088         glDrawPixels(640, 480, GL_LUMINANCE, GL_UNSIGNED_BYTE, rs_get_frame_data(dev, RS_STREAM_INFRARED, &e));
00089         check_error();
00090 
00091         /* Display second infrared image by mapping IR intensity to visible luminance */
00092         if(rs_is_stream_enabled(dev, RS_STREAM_INFRARED2, NULL))
00093         {
00094             glRasterPos2f(0, 0);
00095             glDrawPixels(640, 480, GL_LUMINANCE, GL_UNSIGNED_BYTE, rs_get_frame_data(dev, RS_STREAM_INFRARED2, &e));
00096         }
00097 
00098         glfwSwapBuffers(win);
00099     }
00100     
00101     return EXIT_SUCCESS;
00102 }


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