Skip to content

Commit

Permalink
add streaming examples
Browse files Browse the repository at this point in the history
  • Loading branch information
adujardin committed Apr 19, 2019
1 parent 3103281 commit fa8a855
Show file tree
Hide file tree
Showing 9 changed files with 1,376 additions and 0 deletions.
61 changes: 61 additions & 0 deletions camera streaming/receiver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
SET(execName ZED_Streaming_Receiver)
CMAKE_MINIMUM_REQUIRED(VERSION 2.4)
PROJECT(${execName})

option(LINK_SHARED_ZED "Link with the ZED SDK shared executable" ON)

if (NOT LINK_SHARED_ZED AND MSVC)
message(FATAL_ERROR "LINK_SHARED_ZED OFF : ZED SDK static libraries not available on Windows")
endif()

if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 OLD)
cmake_policy(SET CMP0015 OLD)
endif(COMMAND cmake_policy)

SET(EXECUTABLE_OUTPUT_PATH ".")
SET(SPECIAL_OS_LIBS "")

find_package(ZED 2 REQUIRED)
find_package(GLUT REQUIRED)
find_package(GLEW REQUIRED)
find_package(OpenGL REQUIRED)

IF(NOT WIN32)
SET(SPECIAL_OS_LIBS "pthread" "X11")
add_definitions(-Wno-write-strings -fpermissive)
ENDIF()

find_package(CUDA ${ZED_CUDA_VERSION} EXACT REQUIRED)

include_directories(${ZED_INCLUDE_DIRS})
include_directories(${GLEW_INCLUDE_DIRS})
include_directories(${GLUT_INCLUDE_PATH})
include_directories(${CUDA_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)

link_directories(${ZED_LIBRARY_DIR})
link_directories(${GLEW_LIBRARY_DIRS})
link_directories(${GLUT_LIBRARY_DIRS})
link_directories(${OpenGL_LIBRARY_DIRS})
link_directories(${CUDA_LIBRARY_DIRS})
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)

FILE(GLOB_RECURSE SRC_FILES src/*.cpp)
FILE(GLOB_RECURSE HDR_FILES include/*.hpp)

ADD_EXECUTABLE(${execName} ${HDR_FILES} ${SRC_FILES})
add_definitions(-std=c++11 -g -O3 )

if (LINK_SHARED_ZED)
SET(ZED_LIBS ${ZED_LIBRARIES} ${CUDA_CUDA_LIBRARY} ${CUDA_CUDART_LIBRARY} ${CUDA_NPP_LIBRARIES_ZED})
else()
SET(ZED_LIBS ${ZED_STATIC_LIBRARIES} ${CUDA_CUDA_LIBRARY} ${CUDA_LIBRARY})
endif()

TARGET_LINK_LIBRARIES(${execName}
${SPECIAL_OS_LIBS}
${ZED_LIBS}
${OPENGL_LIBRARIES}
${GLUT_LIBRARY}
${GLEW_LIBRARY})
45 changes: 45 additions & 0 deletions camera streaming/receiver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Stereolabs ZED - Depth Sensing with Streaming

This sample captures a 3D point cloud and display it in an OpenGL window. It shows how to:
- Get a 3D point cloud with the API.
- Display point cloud in OpenGL.
- Use a thread to capture the point cloud and update the GL window simultaneously.
- Use streaming input mode. Need ZED_Streaming_Sender to operate.

To retrieve a depth map of the scene, see [Depth Sensing](https://github.com/stereolabs/zed-examples/tree/master/tutorials) tutorial.

## Getting started

- First, download the latest version of the ZED SDK on [stereolabs.com](https://www.stereolabs.com).
- For more information, read the ZED [API documentation](https://www.stereolabs.com/developers/documentation/API/).

### Prerequisites

- Windows 7 64bits or later, Ubuntu 16.04
- [ZED SDK](https://www.stereolabs.com/developers/) and its dependencies ([CUDA](https://developer.nvidia.com/cuda-downloads))

## Build the program

#### Build for Windows

- Create a "build" folder in the source folder
- Open cmake-gui and select the source and build folders
- Generate the Visual Studio `Win64` solution
- Open the resulting solution and change configuration to `Release`
- Build solution

#### Build for Linux

Open a terminal in the sample directory and execute the following command:

mkdir build
cd build
cmake ..
make

## Run the program

- Navigate to the build directory and launch the executable file
- Or open a terminal in the build directory and run the sample :

./ZED_Streaming_Receiver <ip:port>
255 changes: 255 additions & 0 deletions camera streaming/receiver/include/GLViewer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
#ifndef __VIEWER_INCLUDE__
#define __VIEWER_INCLUDE__

#include <vector>
#include <mutex>

#include <sl/Camera.hpp>

#include <GL/glew.h>
#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glut.h> /* OpenGL Utility Toolkit header */

#include <cuda.h>
#include <cuda_gl_interop.h>

#ifndef M_PI
#define M_PI 3.141592653f
#endif

#define MOUSE_R_SENSITIVITY 0.015f
#define MOUSE_UZ_SENSITIVITY 0.75f
#define MOUSE_DZ_SENSITIVITY 1.25f
#define MOUSE_T_SENSITIVITY 10.f
#define KEY_T_SENSITIVITY 0.1f

class CameraGL {
public:

CameraGL() {}
enum DIRECTION {
UP, DOWN, LEFT, RIGHT, FORWARD, BACK
};
CameraGL(sl::Translation position, sl::Translation direction, sl::Translation vertical = sl::Translation(0, 1, 0)); // vertical = Eigen::Vector3f(0, 1, 0)
~CameraGL();

void update();
void setProjection(float horizontalFOV, float verticalFOV, float znear, float zfar);
const sl::Transform& getViewProjectionMatrix() const;

float getHorizontalFOV() const;
float getVerticalFOV() const;

// Set an offset between the eye of the camera and its position
// Note: Useful to use the camera as a trackball camera with z>0 and x = 0, y = 0
// Note: coordinates are in local space
void setOffsetFromPosition(const sl::Translation& offset);
const sl::Translation& getOffsetFromPosition() const;

void setDirection(const sl::Translation& direction, const sl::Translation &vertical);
void translate(const sl::Translation& t);
void setPosition(const sl::Translation& p);
void rotate(const sl::Orientation& rot);
void rotate(const sl::Rotation& m);
void setRotation(const sl::Orientation& rot);
void setRotation(const sl::Rotation& m);

const sl::Translation& getPosition() const;
const sl::Translation& getForward() const;
const sl::Translation& getRight() const;
const sl::Translation& getUp() const;
const sl::Translation& getVertical() const;
float getZNear() const;
float getZFar() const;

static const sl::Translation ORIGINAL_FORWARD;
static const sl::Translation ORIGINAL_UP;
static const sl::Translation ORIGINAL_RIGHT;

sl::Transform projection_;
private:
void updateVectors();
void updateView();
void updateVPMatrix();

sl::Translation offset_;
sl::Translation position_;
sl::Translation forward_;
sl::Translation up_;
sl::Translation right_;
sl::Translation vertical_;

sl::Orientation rotation_;

sl::Transform view_;
sl::Transform vpMatrix_;
float horizontalFieldOfView_;
float verticalFieldOfView_;
float znear_;
float zfar_;
};

class Shader {
public:

Shader() {}
Shader(GLchar* vs, GLchar* fs);
~Shader();
GLuint getProgramId();

static const GLint ATTRIB_VERTICES_POS = 0;
static const GLint ATTRIB_COLOR_POS = 1;
private:
bool compile(GLuint &shaderId, GLenum type, GLchar* src);
GLuint verterxId_;
GLuint fragmentId_;
GLuint programId_;
};

class Simple3DObject {
public:

Simple3DObject() {}
Simple3DObject(sl::Translation position, bool isStatic);
~Simple3DObject();

void addPoint(sl::float3 pt, sl::float3 clr);
void addFace(sl::float3 p1, sl::float3 p2, sl::float3 p3, sl::float3 clr);
void pushToGPU();
void clear();

void setDrawingType(GLenum type);

void draw();

void translate(const sl::Translation& t);
void setPosition(const sl::Translation& p);

void setRT(const sl::Transform& mRT);

void rotate(const sl::Orientation& rot);
void rotate(const sl::Rotation& m);
void setRotation(const sl::Orientation& rot);
void setRotation(const sl::Rotation& m);

const sl::Translation& getPosition() const;

sl::Transform getModelMatrix() const;
private:
std::vector<float> vertices_;
std::vector<float> colors_;
std::vector<unsigned int> indices_;

bool isStatic_;

GLenum drawingType_;

GLuint vaoID_;
/*
Vertex buffer IDs:
- [0]: Vertices coordinates;
- [1]: Indices;
*/
GLuint vboID_[2];

sl::Translation position_;
sl::Orientation rotation_;

};

class PointCloud {
public:
PointCloud();
~PointCloud();

// Initialize Opengl and Cuda buffers
// Warning: must be called in the Opengl thread
void initialize(sl::Resolution res);
// Push a new point cloud
// Warning: can be called from any thread but the mutex "mutexData" must be locked
void pushNewPC(sl::Mat &matXYZRGBA);
// Update the Opengl buffer
// Warning: must be called in the Opengl thread
void update();
// Draw the point cloud
// Warning: must be called in the Opengl thread
void draw(const sl::Transform& vp);
// Close (disable update)
void close();

std::mutex mutexData;
bool isSetup=false;
private:
sl::Mat matGPU_;
bool hasNewPCL_ = false;
Shader shader_;
GLuint shMVPMatrixLoc_;
size_t numBytes_;
float* xyzrgbaMappedBuf_;
GLuint bufferGLID_;
cudaGraphicsResource* bufferCudaID_;
};

// This class manages input events, window and Opengl rendering pipeline
class GLViewer {
public:
GLViewer();
~GLViewer();
bool isAvailable();

void init(int argc, char **argv, sl::CameraParameters param);
void updatePointCloud(sl::Mat &matXYZRGBA);

void exit();
private:
// Rendering loop method called each frame by glutDisplayFunc
void render();
// Everything that needs to be updated before rendering must be done in this method
void update();
// Once everything is updated, every renderable objects must be drawn in this method
void draw();
// Clear and refresh inputs' data
void clearInputs();

// Glut functions callbacks
static void drawCallback();
static void mouseButtonCallback(int button, int state, int x, int y);
static void mouseMotionCallback(int x, int y);
static void reshapeCallback(int width, int height);
static void keyPressedCallback(unsigned char c, int x, int y);
static void keyReleasedCallback(unsigned char c, int x, int y);
static void idle();

bool available;

enum MOUSE_BUTTON {
LEFT = 0,
MIDDLE = 1,
RIGHT = 2,
WHEEL_UP = 3,
WHEEL_DOWN = 4
};

enum KEY_STATE {
UP = 'u',
DOWN = 'd',
FREE = 'f'
};

bool mouseButton_[3];
int mouseWheelPosition_;
int mouseCurrentPosition_[2];
int mouseMotion_[2];
int previousMouseMotion_[2];
KEY_STATE keyStates_[256];
sl::float3 bckgrnd_clr;

Simple3DObject frustum;
PointCloud pointCloud_;
CameraGL camera_;
Shader shader_;
GLuint shMVPMatrixLoc_;
};

#endif /* __VIEWER_INCLUDE__ */
Loading

0 comments on commit fa8a855

Please sign in to comment.