From 4b14e9fb39d87389b9bf7aad3a594b72df62c9f3 Mon Sep 17 00:00:00 2001 From: Avaer Kazmer Date: Tue, 30 Apr 2019 20:21:57 -0400 Subject: [PATCH 1/9] Initial zed SDK integration bootstrap --- binding.gyp | 9 + .../bindings/include/bindings.h | 3 +- deps/exokit-bindings/zed/include/zed.h | 36 ++++ deps/exokit-bindings/zed/src/zed.cc | 166 ++++++++++++++++++ exokit.cpp | 3 + 5 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 deps/exokit-bindings/zed/include/zed.h create mode 100644 deps/exokit-bindings/zed/src/zed.cc diff --git a/binding.gyp b/binding.gyp index 3a8aa558ab..18e25dfc09 100644 --- a/binding.gyp +++ b/binding.gyp @@ -27,6 +27,7 @@ 'deps/oculus/src/*.cpp', 'deps/openvr/src/*.cpp', 'deps/exokit-bindings/leapmotion/src/*.cc', + 'deps/exokit-bindings/zed/src/*.cc', ], 'include_dirs': [ " // Stub out on Android for now until get libcef working on Android. #if !defined(ANDROID) - #include +#include #endif #if _WIN32 +#include #include #endif #if defined(LUMIN) diff --git a/deps/exokit-bindings/zed/include/zed.h b/deps/exokit-bindings/zed/include/zed.h new file mode 100644 index 0000000000..9c598d1ba3 --- /dev/null +++ b/deps/exokit-bindings/zed/include/zed.h @@ -0,0 +1,36 @@ +#ifndef _ZED_H_ +#define _ZED_H_ + +#include +#include +#include + +#include + +#include + +#include + +using namespace v8; +using namespace node; + +namespace zed { + +class Zed : public ObjectWrap { +public: + static Local Initialize(Isolate *isolate); + +protected: + Zed(); + ~Zed(); + + static NAN_METHOD(New); + static NAN_METHOD(RequestPresent); + static NAN_METHOD(WaitGetPoses); +}; + +} + +Local makeZed(); + +#endif diff --git a/deps/exokit-bindings/zed/src/zed.cc b/deps/exokit-bindings/zed/src/zed.cc new file mode 100644 index 0000000000..20d94cbd4d --- /dev/null +++ b/deps/exokit-bindings/zed/src/zed.cc @@ -0,0 +1,166 @@ +#include + +#include + +// using namespace sl; + +namespace zed { + +Zed::Zed() {} + +Zed::~Zed() {} + +NAN_METHOD(Zed::New) { + Nan::HandleScope scope; + + Local zedObj = info.This(); + Zed *zed = new Zed(); + zed->Wrap(zedObj); + + info.GetReturnValue().Set(zedObj); +} + +NAN_METHOD(Zed::RequestPresent) { + sl::Camera zed; + // Setup configuration parameters for the ZED + sl::InitParameters parameters; + parameters.coordinate_units = sl::UNIT_METER; + parameters.coordinate_system = sl::COORDINATE_SYSTEM_RIGHT_HANDED_Y_UP; // OpenGL coordinates system + + // Open the ZED + sl::ERROR_CODE zed_error = zed.open(parameters); + if(zed_error != sl::ERROR_CODE::SUCCESS) { + std::cout << zed_error << std::endl; + zed.close(); + return; + } + + sl::CameraParameters camera_parameters = zed.getCameraInformation().calibration_parameters.left_cam; + + // sl::Mat image; // current left image + sl::Pose pose; // positional tracking data + + sl::Mesh map; // current incemental mesh + + sl::SpatialMappingParameters spatial_mapping_parameters; + sl::TRACKING_STATE tracking_state = sl::TRACKING_STATE_OFF; + sl::SPATIAL_MAPPING_STATE mapping_state = sl::SPATIAL_MAPPING_STATE_NOT_ENABLED; + bool mapping_activated = false; // indicates if the spatial mapping is running or not + std::chrono::high_resolution_clock::time_point ts_last = std::chrono::high_resolution_clock::now(); // time stamp of the last mesh request + + // Enable positional tracking before starting spatial mapping + zed.enableTracking(); + + { + sl::Transform init_pose; + zed.resetTracking(init_pose); + + // Configure Spatial Mapping parameters + spatial_mapping_parameters.resolution_meter = sl::SpatialMappingParameters::get(sl::SpatialMappingParameters::MAPPING_RESOLUTION_MEDIUM); + spatial_mapping_parameters.use_chunk_only = true; + spatial_mapping_parameters.save_texture = true; + spatial_mapping_parameters.map_type = sl::SpatialMappingParameters::SPATIAL_MAP_TYPE_MESH; + // Enable spatial mapping + try { + zed.enableSpatialMapping(spatial_mapping_parameters); + std::cout << "Spatial Mapping will output a " << spatial_mapping_parameters.map_type << std::endl; + } catch(std::string e) { + std::cout <<"Error enable Spatial Mapping "<< e << std::endl; + } + } + + for (;;) { + if(zed.grab() == sl::SUCCESS) { + // Retrieve image in GPU memory + // zed.retrieveImage(image, sl::VIEW_LEFT, sl::MEM_GPU); + // Update pose data (used for projection of the mesh over the current image) + tracking_state = zed.getPosition(pose); + + // Compute elapse time since the last call of Camera::requestMeshAsync() + auto duration = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - ts_last).count(); + // Ask for a mesh update if 500ms have spend since last request + if(duration > 2000) { + zed.requestSpatialMapAsync(); + ts_last = std::chrono::high_resolution_clock::now(); + } + + if(zed.getSpatialMapRequestStatusAsync() == sl::SUCCESS) { + zed.retrieveSpatialMapAsync(map); + + sl::MeshFilterParameters filter_params; + filter_params.set(sl::MeshFilterParameters::MESH_FILTER_MEDIUM); + // Filter the extracted mesh + map.filter(filter_params, true); + + map.applyTexture(sl::MESH_TEXTURE_RGB); + + sl::Texture &texture = map.texture; + sl::Mat &textureMaterial = texture.data; + sl::Resolution &textureResolution = textureMaterial.getResolution(); + sl::uchar1 *tex = textureMaterial.getPtr(sl::MEM_GPU); + + sl::Mesh::chunkList chunks = map.getSurroundingList(pose.pose_data, 5); + for (auto iter = chunks.begin(); iter != chunks.end(); iter++) { + auto chunkIndex = *iter; + sl::Chunk &chunk = map[chunkIndex]; + const sl::float3 &position = chunk.barycenter; + std::vector &positions = chunk.vertices; + std::vector &normals = chunk.normals; + std::vector &uvs = chunk.uv; + std::vector &indices = chunk.triangles; + } + + //Save as an OBJ file + bool error_save = map.save("C:\\Users\\avaer\\Documents\\GitHub\\exokit\\mesh_gen.obj"); + if(error_save) { + std::cout << ">> Mesh saved" << std::endl; + } else { + std::cout << ">> Failed to save mesh" << std::endl; + } + + break; + } + } + } + + // image.free(); + map.clear(); + + zed.disableSpatialMapping(); + zed.disableTracking(); + zed.close(); +} + +NAN_METHOD(Zed::WaitGetPoses) { + // XXX +} + +Local Zed::Initialize(Isolate *isolate) { + Nan::EscapableHandleScope scope; + + // constructor + Local ctor = Nan::New(New); + ctor->InstanceTemplate()->SetInternalFieldCount(1); + ctor->SetClassName(JS_STR("Zed")); + + // prototype + Local proto = ctor->PrototypeTemplate(); + Nan::SetMethod(proto, "RequestPresent", RequestPresent); + Nan::SetMethod(proto, "WaitGetPoses", WaitGetPoses); + + Local ctorFn = Nan::GetFunction(ctor).ToLocalChecked(); + + return scope.Escape(ctorFn); +} + +} + +Local makeZed() { + Isolate *isolate = Isolate::GetCurrent(); + + Nan::EscapableHandleScope scope; + + Local exports = zed::Zed::Initialize(isolate); + + return scope.Escape(exports); +} \ No newline at end of file diff --git a/exokit.cpp b/exokit.cpp index b944e789e8..545955e149 100644 --- a/exokit.cpp +++ b/exokit.cpp @@ -97,6 +97,9 @@ void InitExports(Local exports) { exports->Set(v8::String::NewFromUtf8(Isolate::GetCurrent(), "nativeMl"), ml); #endif + Local zed = makeZed(); + exports->Set(v8::String::NewFromUtf8(Isolate::GetCurrent(), "nativeZed"), zed); + #if defined(ANDROID) #define NATIVE_PLATFORM "android" #elif defined(LUMIN) From 5d79ea97512f3c09b1c38654a995f12b7cac6f55 Mon Sep 17 00:00:00 2001 From: Avaer Kazmer Date: Wed, 1 May 2019 00:09:35 -0400 Subject: [PATCH 2/9] Major zed SDK work --- binding.gyp | 2 + deps/exokit-bindings/zed/include/zed.h | 13 ++ deps/exokit-bindings/zed/src/zed.cc | 220 ++++++++++++++++--------- 3 files changed, 159 insertions(+), 76 deletions(-) diff --git a/binding.gyp b/binding.gyp index 18e25dfc09..ac79768630 100644 --- a/binding.gyp +++ b/binding.gyp @@ -79,6 +79,7 @@ " #include +#include +#include using namespace v8; using namespace node; @@ -26,7 +28,18 @@ class Zed : public ObjectWrap { static NAN_METHOD(New); static NAN_METHOD(RequestPresent); + static NAN_METHOD(ExitPresent); static NAN_METHOD(WaitGetPoses); + + sl::Camera camera; + sl::Mesh mesh; + std::chrono::high_resolution_clock::time_point ts_last; + GLuint tex; + NATIVEwindow *window; + int textureWidth; + int textureHeight; + cudaGraphicsResource *pcuImageRes; + Nan::Persistent cbFn; }; } diff --git a/deps/exokit-bindings/zed/src/zed.cc b/deps/exokit-bindings/zed/src/zed.cc index 20d94cbd4d..2a508676a2 100644 --- a/deps/exokit-bindings/zed/src/zed.cc +++ b/deps/exokit-bindings/zed/src/zed.cc @@ -6,7 +6,7 @@ namespace zed { -Zed::Zed() {} +Zed::Zed() : tex(0), window(nullptr), textureWidth(0), textureHeight(0), pcuImageRes(nullptr) {} Zed::~Zed() {} @@ -21,39 +21,38 @@ NAN_METHOD(Zed::New) { } NAN_METHOD(Zed::RequestPresent) { - sl::Camera zed; + Zed *zed = ObjectWrap::Unwrap(info.This()); + WebGLRenderingContext *gl = ObjectWrap::Unwrap(Local::Cast(info[0])); + Local localCbFn = Local::Cast(info[1]); + // Setup configuration parameters for the ZED sl::InitParameters parameters; parameters.coordinate_units = sl::UNIT_METER; parameters.coordinate_system = sl::COORDINATE_SYSTEM_RIGHT_HANDED_Y_UP; // OpenGL coordinates system // Open the ZED - sl::ERROR_CODE zed_error = zed.open(parameters); + sl::ERROR_CODE zed_error = zed->camera.open(parameters); if(zed_error != sl::ERROR_CODE::SUCCESS) { std::cout << zed_error << std::endl; - zed.close(); + zed->camera.close(); return; } - sl::CameraParameters camera_parameters = zed.getCameraInformation().calibration_parameters.left_cam; + sl::CameraParameters camera_parameters = zed->camera.getCameraInformation().calibration_parameters.left_cam; // sl::Mat image; // current left image - sl::Pose pose; // positional tracking data - - sl::Mesh map; // current incemental mesh sl::SpatialMappingParameters spatial_mapping_parameters; - sl::TRACKING_STATE tracking_state = sl::TRACKING_STATE_OFF; - sl::SPATIAL_MAPPING_STATE mapping_state = sl::SPATIAL_MAPPING_STATE_NOT_ENABLED; - bool mapping_activated = false; // indicates if the spatial mapping is running or not - std::chrono::high_resolution_clock::time_point ts_last = std::chrono::high_resolution_clock::now(); // time stamp of the last mesh request - + // sl::SPATIAL_MAPPING_STATE mapping_state = sl::SPATIAL_MAPPING_STATE_NOT_ENABLED; + // bool mapping_activated = false; // indicates if the spatial mapping is running or not + zed->ts_last = std::chrono::high_resolution_clock::now(); // time stamp of the last mesh request + // Enable positional tracking before starting spatial mapping - zed.enableTracking(); - + zed->camera.enableTracking(); + { sl::Transform init_pose; - zed.resetTracking(init_pose); + zed->camera.resetTracking(init_pose); // Configure Spatial Mapping parameters spatial_mapping_parameters.resolution_meter = sl::SpatialMappingParameters::get(sl::SpatialMappingParameters::MAPPING_RESOLUTION_MEDIUM); @@ -62,77 +61,145 @@ NAN_METHOD(Zed::RequestPresent) { spatial_mapping_parameters.map_type = sl::SpatialMappingParameters::SPATIAL_MAP_TYPE_MESH; // Enable spatial mapping try { - zed.enableSpatialMapping(spatial_mapping_parameters); + zed->camera.enableSpatialMapping(spatial_mapping_parameters); std::cout << "Spatial Mapping will output a " << spatial_mapping_parameters.map_type << std::endl; } catch(std::string e) { - std::cout <<"Error enable Spatial Mapping "<< e << std::endl; + std::cout <<"Error enabling Spatial Mapping "<< e << std::endl; } } - for (;;) { - if(zed.grab() == sl::SUCCESS) { - // Retrieve image in GPU memory - // zed.retrieveImage(image, sl::VIEW_LEFT, sl::MEM_GPU); - // Update pose data (used for projection of the mesh over the current image) - tracking_state = zed.getPosition(pose); - - // Compute elapse time since the last call of Camera::requestMeshAsync() - auto duration = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - ts_last).count(); - // Ask for a mesh update if 500ms have spend since last request - if(duration > 2000) { - zed.requestSpatialMapAsync(); - ts_last = std::chrono::high_resolution_clock::now(); - } - - if(zed.getSpatialMapRequestStatusAsync() == sl::SUCCESS) { - zed.retrieveSpatialMapAsync(map); - - sl::MeshFilterParameters filter_params; - filter_params.set(sl::MeshFilterParameters::MESH_FILTER_MEDIUM); - // Filter the extracted mesh - map.filter(filter_params, true); - - map.applyTexture(sl::MESH_TEXTURE_RGB); - - sl::Texture &texture = map.texture; - sl::Mat &textureMaterial = texture.data; - sl::Resolution &textureResolution = textureMaterial.getResolution(); - sl::uchar1 *tex = textureMaterial.getPtr(sl::MEM_GPU); - - sl::Mesh::chunkList chunks = map.getSurroundingList(pose.pose_data, 5); - for (auto iter = chunks.begin(); iter != chunks.end(); iter++) { - auto chunkIndex = *iter; - sl::Chunk &chunk = map[chunkIndex]; - const sl::float3 &position = chunk.barycenter; - std::vector &positions = chunk.vertices; - std::vector &normals = chunk.normals; - std::vector &uvs = chunk.uv; - std::vector &indices = chunk.triangles; - } - - //Save as an OBJ file - bool error_save = map.save("C:\\Users\\avaer\\Documents\\GitHub\\exokit\\mesh_gen.obj"); - if(error_save) { - std::cout << ">> Mesh saved" << std::endl; - } else { - std::cout << ">> Failed to save mesh" << std::endl; - } - - break; - } - } - } + glGenTextures(1, &zed->tex); + zed->window = windowsystem::CreateNativeWindow(1, 1, false, gl->windowHandle); + zed->cbFn.Reset(localCbFn); + // cudaError_t cudaError = cudaGraphicsGLRegisterImage(&zed->pcuImageRes, zed->tex, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsWriteDiscard); + // cudaError = cudaGraphicsUnregisterResource(zed->pcuImageRes); +} + +NAN_METHOD(Zed::ExitPresent) { + Zed *zed = ObjectWrap::Unwrap(info.This()); // image.free(); - map.clear(); + zed->mesh.clear(); - zed.disableSpatialMapping(); - zed.disableTracking(); - zed.close(); + zed->camera.disableSpatialMapping(); + zed->camera.disableTracking(); + zed->camera.close(); } NAN_METHOD(Zed::WaitGetPoses) { - // XXX + Zed *zed = ObjectWrap::Unwrap(info.This()); + + if (zed->camera.grab() == sl::SUCCESS) { + // Retrieve image in GPU memory + // zed.retrieveImage(image, sl::VIEW_LEFT, sl::MEM_GPU); + // Update pose data (used for projection of the mesh over the current image) + sl::Pose pose; // positional tracking data + sl::TRACKING_STATE tracking_state = zed->camera.getPosition(pose); + + // Compute elapse time since the last call of Camera::requestMeshAsync() + auto duration = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - zed->ts_last).count(); + // Ask for a mesh update if 500ms have spend since last request + if(duration > 2000) { + zed->camera.requestSpatialMapAsync(); + zed->ts_last = std::chrono::high_resolution_clock::now(); + } + + if(zed->camera.getSpatialMapRequestStatusAsync() == sl::SUCCESS) { + zed->camera.retrieveSpatialMapAsync(zed->mesh); + + sl::MeshFilterParameters filter_params; + filter_params.set(sl::MeshFilterParameters::MESH_FILTER_MEDIUM); + // Filter the extracted mesh + zed->mesh.filter(filter_params, true); + + zed->mesh.applyTexture(sl::MESH_TEXTURE_RGB); + + sl::Texture &texture = zed->mesh.texture; + sl::Mat &textureMaterial = texture.data; + // sl::Resolution &textureResolution = textureMaterial.getResolution(); + // sl::uchar1 *tex = textureMaterial.getPtr(sl::MEM_GPU); + + size_t width = textureMaterial.getWidth(); + size_t height = textureMaterial.getHeight(); + if (width != zed->textureWidth || height != zed->textureHeight) { + if (zed->pcuImageRes) { + cudaError_t cudaError = cudaGraphicsUnregisterResource(zed->pcuImageRes); + zed->pcuImageRes = nullptr; + } + + windowsystem::SetCurrentWindowContext(zed->window); + + glBindTexture(GL_TEXTURE_2D, zed->tex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL); + + cudaError_t cudaError = cudaGraphicsGLRegisterImage(&zed->pcuImageRes, zed->tex, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsWriteDiscard); + + zed->textureWidth = width; + zed->textureHeight = height; + } + + cudaArray_t arrIm; + cudaGraphicsMapResources(1, &zed->pcuImageRes, 0); + cudaGraphicsSubResourceGetMappedArray(&arrIm, zed->pcuImageRes, 0, 0); + cudaMemcpy2DToArray(arrIm, 0, 0, textureMaterial.getPtr(sl::MEM_GPU), textureMaterial.getStepBytes(sl::MEM_GPU), width * sizeof(sl::uchar3), height, cudaMemcpyDeviceToDevice); + cudaGraphicsUnmapResources(1, &zed->pcuImageRes, 0); + + Local asyncObject = Nan::New(); + AsyncResource asyncResource(Isolate::GetCurrent(), asyncObject, "Zed::WaitGetPoses"); + + Local localCbFn = Nan::New(zed->cbFn); + const float range = 5; + sl::Mesh::chunkList chunks = zed->mesh.getSurroundingList(pose.pose_data, range); + Local result; + if (chunks.size() > 0) { + Local array = Nan::New(chunks.size()); + + Local texStr = JS_STR("tex"); + Local texObj = Nan::New(); + texObj->Set(JS_STR("id"), JS_INT(zed->tex)); + + for (size_t i = 0; i < chunks.size(); i++) { + size_t chunkIndex = chunks[i]; + sl::Chunk &chunk = zed->mesh[chunkIndex]; + const sl::float3 &position = chunk.barycenter; + std::vector &positions = chunk.vertices; + size_t numPositions = positions.size() * sizeof(positions[0]) / sizeof(float); + std::vector &normals = chunk.normals; + size_t numNormals = normals.size() * sizeof(normals[0]) / sizeof(float); + std::vector &uvs = chunk.uv; + size_t numUvs = uvs.size() * sizeof(uvs[0]) / sizeof(float); + std::vector &indices = chunk.triangles; + size_t numIndices = indices.size() * sizeof(indices[0]) / sizeof(uint32_t); + + Local chunkObj = Nan::New(); + chunkObj->Set(JS_STR("positionArray"), Float32Array::New(ArrayBuffer::New(Isolate::GetCurrent(), positions[0].v, numPositions * sizeof(float)), 0, numPositions)); + chunkObj->Set(JS_STR("normalArray"), Float32Array::New(ArrayBuffer::New(Isolate::GetCurrent(), normals[0].v, numNormals * sizeof(float)), 0, numNormals)); + chunkObj->Set(JS_STR("uvArray"), Float32Array::New(ArrayBuffer::New(Isolate::GetCurrent(), uvs[0].v, numUvs * sizeof(float)), 0, numUvs)); + chunkObj->Set(JS_STR("indexArray"), Uint32Array::New(ArrayBuffer::New(Isolate::GetCurrent(), indices[0].v, numIndices * sizeof(uint32_t)), 0, numIndices)); + chunkObj->Set(texStr, texObj); + array->Set(i, chunkObj); + } + + result = array; + } else { + result = Nan::Null(); + } + Local argv[] = { + result, + }; + asyncResource.MakeCallback(localCbFn, sizeof(argv)/sizeof(argv[0]), argv); + + /* //Save as an OBJ file + bool error_save = zed->mesh.save("C:\\Users\\avaer\\Documents\\GitHub\\exokit\\mesh_gen.obj"); + if(error_save) { + std::cout << ">> Mesh saved" << std::endl; + } else { + std::cout << ">> Failed to save mesh" << std::endl; + } */ + } + } } Local Zed::Initialize(Isolate *isolate) { @@ -146,6 +213,7 @@ Local Zed::Initialize(Isolate *isolate) { // prototype Local proto = ctor->PrototypeTemplate(); Nan::SetMethod(proto, "RequestPresent", RequestPresent); + Nan::SetMethod(proto, "ExitPresent", ExitPresent); Nan::SetMethod(proto, "WaitGetPoses", WaitGetPoses); Local ctorFn = Nan::GetFunction(ctor).ToLocalChecked(); From 894c52b79038c90ed589bfa2db471a9c5ada1615 Mon Sep 17 00:00:00 2001 From: Avaer Kazmer Date: Tue, 14 May 2019 14:41:07 -0400 Subject: [PATCH 3/9] Initial ZED SDK integration in Window.js --- src/Window.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Window.js b/src/Window.js index 636f10e8d5..1124c77139 100644 --- a/src/Window.js +++ b/src/Window.js @@ -99,6 +99,7 @@ const { nativeOculusVR, nativeOculusMobileVr, nativeMl, + nativeZed, nativeBrowser, nativeWindow, } = require('./native-bindings'); @@ -814,6 +815,14 @@ const _makeRequestAnimationFrame = window => (fn, priority = 0) => { } }, }; + let zedContext = null; + window.zed = { + requestMeshing(fn) { + zedContext = new nativeZed(); + const context = GlobalContext.contexts.find(context => context.canvas.ownerDocument === this.ownerDocument); + zedContext.RequestPresent(context, fn); + }, + }; window.DOMParser = class DOMParser { parseFromString(htmlString, type) { const _recurse = node => { @@ -1162,6 +1171,10 @@ const _makeRequestAnimationFrame = window => (fn, priority = 0) => { } } + if (zedContext) { + zedContext.WaitGetPoses(); + } + _tickLocalRafs(); return _composeLocalLayers(layered); }; From b5b182a8d73226dfa5a4151545ea758237afa81d Mon Sep 17 00:00:00 2001 From: Avaer Kazmer Date: Wed, 15 May 2019 12:14:02 -0400 Subject: [PATCH 4/9] Clean up zed API native binding --- deps/exokit-bindings/zed/src/zed.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/deps/exokit-bindings/zed/src/zed.cc b/deps/exokit-bindings/zed/src/zed.cc index 2a508676a2..995732ace1 100644 --- a/deps/exokit-bindings/zed/src/zed.cc +++ b/deps/exokit-bindings/zed/src/zed.cc @@ -22,8 +22,7 @@ NAN_METHOD(Zed::New) { NAN_METHOD(Zed::RequestPresent) { Zed *zed = ObjectWrap::Unwrap(info.This()); - WebGLRenderingContext *gl = ObjectWrap::Unwrap(Local::Cast(info[0])); - Local localCbFn = Local::Cast(info[1]); + Local localCbFn = Local::Cast(info[0]); // Setup configuration parameters for the ZED sl::InitParameters parameters; @@ -68,8 +67,9 @@ NAN_METHOD(Zed::RequestPresent) { } } + zed->window = windowsystem::CreateWindowHandle(1, 1, false); + windowsystem::SetCurrentWindowContext(zed->window); glGenTextures(1, &zed->tex); - zed->window = windowsystem::CreateNativeWindow(1, 1, false, gl->windowHandle); zed->cbFn.Reset(localCbFn); // cudaError_t cudaError = cudaGraphicsGLRegisterImage(&zed->pcuImageRes, zed->tex, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsWriteDiscard); // cudaError = cudaGraphicsUnregisterResource(zed->pcuImageRes); From 548e0a69fd68b2f0db4bb11289c0b77f3af6d976 Mon Sep 17 00:00:00 2001 From: Avaer Kazmer Date: Wed, 15 May 2019 12:14:15 -0400 Subject: [PATCH 5/9] Clean up zed API js binding --- src/Window.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Window.js b/src/Window.js index 1124c77139..25c4ee893d 100644 --- a/src/Window.js +++ b/src/Window.js @@ -819,8 +819,7 @@ const _makeRequestAnimationFrame = window => (fn, priority = 0) => { window.zed = { requestMeshing(fn) { zedContext = new nativeZed(); - const context = GlobalContext.contexts.find(context => context.canvas.ownerDocument === this.ownerDocument); - zedContext.RequestPresent(context, fn); + zedContext.RequestPresent(fn); }, }; window.DOMParser = class DOMParser { From c012ea5d34d16e85bb0b84b631eb5090210bc617 Mon Sep 17 00:00:00 2001 From: Avaer Kazmer Date: Wed, 15 May 2019 22:33:34 -0400 Subject: [PATCH 6/9] Small zed api cleanup --- src/Window.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Window.js b/src/Window.js index 25c4ee893d..700cf7a702 100644 --- a/src/Window.js +++ b/src/Window.js @@ -820,6 +820,7 @@ const _makeRequestAnimationFrame = window => (fn, priority = 0) => { requestMeshing(fn) { zedContext = new nativeZed(); zedContext.RequestPresent(fn); + return zedContext.emitter = new EventEmitter(); }, }; window.DOMParser = class DOMParser { @@ -1171,7 +1172,10 @@ const _makeRequestAnimationFrame = window => (fn, priority = 0) => { } if (zedContext) { - zedContext.WaitGetPoses(); + const meshes = zedContext.WaitGetPoses(); + if (meshes) { + zedContext.emitter.emit('meshes', meshes); + } } _tickLocalRafs(); From d570c5b85e663d03869516f0035a9c791e63835c Mon Sep 17 00:00:00 2001 From: Avaer Kazmer Date: Thu, 16 May 2019 11:35:50 -0400 Subject: [PATCH 7/9] Major zed SDK binding API refactoring --- deps/exokit-bindings/zed/include/zed.h | 17 +- deps/exokit-bindings/zed/src/zed.cc | 249 ++++++++++++++++--------- src/Window.js | 8 +- 3 files changed, 178 insertions(+), 96 deletions(-) diff --git a/deps/exokit-bindings/zed/include/zed.h b/deps/exokit-bindings/zed/include/zed.h index c5bcf9debd..90e5b24343 100644 --- a/deps/exokit-bindings/zed/include/zed.h +++ b/deps/exokit-bindings/zed/include/zed.h @@ -6,8 +6,11 @@ #include #include +#include #include +#include +#include #include #include @@ -22,7 +25,7 @@ class Zed : public ObjectWrap { public: static Local Initialize(Isolate *isolate); -protected: +// protected: Zed(); ~Zed(); @@ -31,15 +34,25 @@ class Zed : public ObjectWrap { static NAN_METHOD(ExitPresent); static NAN_METHOD(WaitGetPoses); + void Poll(); + sl::Camera camera; + sl::Translation position; + sl::Orientation orientation; sl::Mesh mesh; + sl::Mesh::chunkList chunks; std::chrono::high_resolution_clock::time_point ts_last; GLuint tex; NATIVEwindow *window; int textureWidth; int textureHeight; cudaGraphicsResource *pcuImageRes; - Nan::Persistent cbFn; + // Nan::Persistent cbFn; + std::mutex mutex; + std::thread thread; + uv_async_t *async; + uv_sem_t reqSem; + Nan::Persistent result; }; } diff --git a/deps/exokit-bindings/zed/src/zed.cc b/deps/exokit-bindings/zed/src/zed.cc index 995732ace1..c80c0b9907 100644 --- a/deps/exokit-bindings/zed/src/zed.cc +++ b/deps/exokit-bindings/zed/src/zed.cc @@ -11,8 +11,6 @@ Zed::Zed() : tex(0), window(nullptr), textureWidth(0), textureHeight(0), pcuImag Zed::~Zed() {} NAN_METHOD(Zed::New) { - Nan::HandleScope scope; - Local zedObj = info.This(); Zed *zed = new Zed(); zed->Wrap(zedObj); @@ -20,57 +18,129 @@ NAN_METHOD(Zed::New) { info.GetReturnValue().Set(zedObj); } +void RunInMainThread(uv_async_t *handle) { + Nan::HandleScope scope; + + Zed *zed = (Zed *)handle->data; + + Local result = Nan::New(zed->chunks.size()); + + Local texStr = JS_STR("tex"); + Local texObj = Nan::New(); + texObj->Set(JS_STR("id"), JS_INT(zed->tex)); + + for (size_t i = 0; i < zed->chunks.size(); i++) { + size_t chunkIndex = zed->chunks[i]; + sl::Chunk &chunk = zed->mesh[chunkIndex]; + const sl::float3 &position = chunk.barycenter; + std::vector &positions = chunk.vertices; + size_t numPositions = positions.size() * sizeof(positions[0]) / sizeof(float); + std::vector &normals = chunk.normals; + size_t numNormals = normals.size() * sizeof(normals[0]) / sizeof(float); + std::vector &uvs = chunk.uv; + size_t numUvs = uvs.size() * sizeof(uvs[0]) / sizeof(float); + std::vector &indices = chunk.triangles; + size_t numIndices = indices.size() * sizeof(indices[0]) / sizeof(uint32_t); + + Local chunkObj = Nan::New(); + + Local arrayBuffer = SharedArrayBuffer::New(Isolate::GetCurrent(), numPositions*sizeof(float) + numNormals*sizeof(float) + numUvs*sizeof(float) + numIndices*sizeof(uint16_t)); + unsigned char *arrayBufferData = (unsigned char *)arrayBuffer->GetContents().Data(); + int index = 0; + + chunkObj->Set(JS_STR("positionArray"), Float32Array::New(arrayBuffer, index, numPositions)); + memcpy(arrayBufferData + index, positions.data(), numPositions*sizeof(float)); + index += numPositions*sizeof(float); + + chunkObj->Set(JS_STR("normalArray"), Float32Array::New(arrayBuffer, index, numNormals)); + memcpy(arrayBufferData + index, normals.data(), numNormals*sizeof(float)); + index += numNormals*sizeof(float); + + chunkObj->Set(JS_STR("uvArray"), Float32Array::New(arrayBuffer, index, numUvs)); + memcpy(arrayBufferData + index, uvs.data(), numUvs*sizeof(float)); + index += numUvs*sizeof(float); + + chunkObj->Set(JS_STR("indexArray"), Uint16Array::New(arrayBuffer, index, numIndices)); + for (size_t i = 0; i < indices.size(); i++) { + sl::uint3 &indexVector = indices[i]; + uint16_t *baseIndex = &(((uint16_t *)(arrayBufferData + index))[i*3]); + baseIndex[0] = (uint16_t)indexVector.x; + baseIndex[1] = (uint16_t)indexVector.y; + baseIndex[2] = (uint16_t)indexVector.z; + } + index += numUvs*sizeof(uint16_t); + + chunkObj->Set(texStr, texObj); + + result->Set(i, chunkObj); + } + + zed->result.Reset(result); + + uv_sem_post(&zed->reqSem); +} + NAN_METHOD(Zed::RequestPresent) { Zed *zed = ObjectWrap::Unwrap(info.This()); - Local localCbFn = Local::Cast(info[0]); - - // Setup configuration parameters for the ZED - sl::InitParameters parameters; - parameters.coordinate_units = sl::UNIT_METER; - parameters.coordinate_system = sl::COORDINATE_SYSTEM_RIGHT_HANDED_Y_UP; // OpenGL coordinates system - - // Open the ZED - sl::ERROR_CODE zed_error = zed->camera.open(parameters); - if(zed_error != sl::ERROR_CODE::SUCCESS) { - std::cout << zed_error << std::endl; - zed->camera.close(); - return; - } - sl::CameraParameters camera_parameters = zed->camera.getCameraInformation().calibration_parameters.left_cam; + uv_loop_t *loop = windowsystembase::GetEventLoop(); + zed->async = new uv_async_t(); + uv_async_init(loop, zed->async, RunInMainThread); + zed->async->data = zed; + uv_sem_init(&zed->reqSem, 0); + zed->thread = std::thread([zed]() -> void { + // Setup configuration parameters for the ZED + sl::InitParameters parameters; + parameters.coordinate_units = sl::UNIT_METER; + parameters.coordinate_system = sl::COORDINATE_SYSTEM_RIGHT_HANDED_Y_UP; // OpenGL coordinates system + + // Open the ZED + sl::ERROR_CODE zed_error = zed->camera.open(parameters); + if(zed_error != sl::ERROR_CODE::SUCCESS) { + std::cout << zed_error << std::endl; + zed->camera.close(); + return; + } + + sl::CameraParameters camera_parameters = zed->camera.getCameraInformation().calibration_parameters.left_cam; - // sl::Mat image; // current left image + // sl::Mat image; // current left image - sl::SpatialMappingParameters spatial_mapping_parameters; - // sl::SPATIAL_MAPPING_STATE mapping_state = sl::SPATIAL_MAPPING_STATE_NOT_ENABLED; - // bool mapping_activated = false; // indicates if the spatial mapping is running or not - zed->ts_last = std::chrono::high_resolution_clock::now(); // time stamp of the last mesh request + sl::SpatialMappingParameters spatial_mapping_parameters; + // sl::SPATIAL_MAPPING_STATE mapping_state = sl::SPATIAL_MAPPING_STATE_NOT_ENABLED; + // bool mapping_activated = false; // indicates if the spatial mapping is running or not + zed->ts_last = std::chrono::high_resolution_clock::now(); // time stamp of the last mesh request - // Enable positional tracking before starting spatial mapping - zed->camera.enableTracking(); + // Enable positional tracking before starting spatial mapping + zed->camera.enableTracking(); - { - sl::Transform init_pose; - zed->camera.resetTracking(init_pose); - - // Configure Spatial Mapping parameters - spatial_mapping_parameters.resolution_meter = sl::SpatialMappingParameters::get(sl::SpatialMappingParameters::MAPPING_RESOLUTION_MEDIUM); - spatial_mapping_parameters.use_chunk_only = true; - spatial_mapping_parameters.save_texture = true; - spatial_mapping_parameters.map_type = sl::SpatialMappingParameters::SPATIAL_MAP_TYPE_MESH; - // Enable spatial mapping - try { + { + sl::Transform init_pose; + zed->camera.resetTracking(init_pose); + + // Configure Spatial Mapping parameters + spatial_mapping_parameters.resolution_meter = sl::SpatialMappingParameters::get(sl::SpatialMappingParameters::MAPPING_RESOLUTION_MEDIUM); + spatial_mapping_parameters.use_chunk_only = true; + spatial_mapping_parameters.save_texture = true; + spatial_mapping_parameters.map_type = sl::SpatialMappingParameters::SPATIAL_MAP_TYPE_MESH; + // Enable spatial mapping + try { zed->camera.enableSpatialMapping(spatial_mapping_parameters); std::cout << "Spatial Mapping will output a " << spatial_mapping_parameters.map_type << std::endl; - } catch(std::string e) { + } catch(std::string e) { std::cout <<"Error enabling Spatial Mapping "<< e << std::endl; + } } - } - zed->window = windowsystem::CreateWindowHandle(1, 1, false); - windowsystem::SetCurrentWindowContext(zed->window); - glGenTextures(1, &zed->tex); - zed->cbFn.Reset(localCbFn); + zed->window = windowsystem::CreateWindowHandle(1, 1, false); + windowsystem::SetCurrentWindowContext(zed->window); + glGenTextures(1, &zed->tex); + + for (;;) { + zed->Poll(); + } + }); + // zed->cbFn.Reset(localCbFn); // cudaError_t cudaError = cudaGraphicsGLRegisterImage(&zed->pcuImageRes, zed->tex, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsWriteDiscard); // cudaError = cudaGraphicsUnregisterResource(zed->pcuImageRes); } @@ -86,33 +156,41 @@ NAN_METHOD(Zed::ExitPresent) { zed->camera.close(); } -NAN_METHOD(Zed::WaitGetPoses) { - Zed *zed = ObjectWrap::Unwrap(info.This()); - +void Zed::Poll() { + Zed *zed = this; + if (zed->camera.grab() == sl::SUCCESS) { // Retrieve image in GPU memory // zed.retrieveImage(image, sl::VIEW_LEFT, sl::MEM_GPU); // Update pose data (used for projection of the mesh over the current image) - sl::Pose pose; // positional tracking data + sl::Pose pose; sl::TRACKING_STATE tracking_state = zed->camera.getPosition(pose); + { + std::lock_guard lock(zed->mutex); + + zed->position = pose.getTranslation(); + zed->orientation = pose.getOrientation(); + } + // Compute elapse time since the last call of Camera::requestMeshAsync() auto duration = std::chrono::duration_cast(std::chrono::high_resolution_clock::now() - zed->ts_last).count(); // Ask for a mesh update if 500ms have spend since last request - if(duration > 2000) { + if(duration > 500) { zed->camera.requestSpatialMapAsync(); zed->ts_last = std::chrono::high_resolution_clock::now(); } - if(zed->camera.getSpatialMapRequestStatusAsync() == sl::SUCCESS) { + if(zed->camera.getSpatialMapRequestStatusAsync() == sl::SUCCESS) { zed->camera.retrieveSpatialMapAsync(zed->mesh); sl::MeshFilterParameters filter_params; filter_params.set(sl::MeshFilterParameters::MESH_FILTER_MEDIUM); + // Filter the extracted mesh - zed->mesh.filter(filter_params, true); + // zed->mesh.filter(filter_params, true); - zed->mesh.applyTexture(sl::MESH_TEXTURE_RGB); + // zed->mesh.applyTexture(sl::MESH_TEXTURE_RGB); sl::Texture &texture = zed->mesh.texture; sl::Mat &textureMaterial = texture.data; @@ -145,52 +223,15 @@ NAN_METHOD(Zed::WaitGetPoses) { cudaGraphicsSubResourceGetMappedArray(&arrIm, zed->pcuImageRes, 0, 0); cudaMemcpy2DToArray(arrIm, 0, 0, textureMaterial.getPtr(sl::MEM_GPU), textureMaterial.getStepBytes(sl::MEM_GPU), width * sizeof(sl::uchar3), height, cudaMemcpyDeviceToDevice); cudaGraphicsUnmapResources(1, &zed->pcuImageRes, 0); - - Local asyncObject = Nan::New(); - AsyncResource asyncResource(Isolate::GetCurrent(), asyncObject, "Zed::WaitGetPoses"); - Local localCbFn = Nan::New(zed->cbFn); const float range = 5; - sl::Mesh::chunkList chunks = zed->mesh.getSurroundingList(pose.pose_data, range); - Local result; - if (chunks.size() > 0) { - Local array = Nan::New(chunks.size()); - - Local texStr = JS_STR("tex"); - Local texObj = Nan::New(); - texObj->Set(JS_STR("id"), JS_INT(zed->tex)); - - for (size_t i = 0; i < chunks.size(); i++) { - size_t chunkIndex = chunks[i]; - sl::Chunk &chunk = zed->mesh[chunkIndex]; - const sl::float3 &position = chunk.barycenter; - std::vector &positions = chunk.vertices; - size_t numPositions = positions.size() * sizeof(positions[0]) / sizeof(float); - std::vector &normals = chunk.normals; - size_t numNormals = normals.size() * sizeof(normals[0]) / sizeof(float); - std::vector &uvs = chunk.uv; - size_t numUvs = uvs.size() * sizeof(uvs[0]) / sizeof(float); - std::vector &indices = chunk.triangles; - size_t numIndices = indices.size() * sizeof(indices[0]) / sizeof(uint32_t); - - Local chunkObj = Nan::New(); - chunkObj->Set(JS_STR("positionArray"), Float32Array::New(ArrayBuffer::New(Isolate::GetCurrent(), positions[0].v, numPositions * sizeof(float)), 0, numPositions)); - chunkObj->Set(JS_STR("normalArray"), Float32Array::New(ArrayBuffer::New(Isolate::GetCurrent(), normals[0].v, numNormals * sizeof(float)), 0, numNormals)); - chunkObj->Set(JS_STR("uvArray"), Float32Array::New(ArrayBuffer::New(Isolate::GetCurrent(), uvs[0].v, numUvs * sizeof(float)), 0, numUvs)); - chunkObj->Set(JS_STR("indexArray"), Uint32Array::New(ArrayBuffer::New(Isolate::GetCurrent(), indices[0].v, numIndices * sizeof(uint32_t)), 0, numIndices)); - chunkObj->Set(texStr, texObj); - array->Set(i, chunkObj); - } - - result = array; - } else { - result = Nan::Null(); + zed->chunks = zed->mesh.getSurroundingList(pose.pose_data, range); + + if (zed->chunks.size() > 0) { + uv_async_send(zed->async); + uv_sem_wait(&zed->reqSem); } - Local argv[] = { - result, - }; - asyncResource.MakeCallback(localCbFn, sizeof(argv)/sizeof(argv[0]), argv); - + /* //Save as an OBJ file bool error_save = zed->mesh.save("C:\\Users\\avaer\\Documents\\GitHub\\exokit\\mesh_gen.obj"); if(error_save) { @@ -202,6 +243,30 @@ NAN_METHOD(Zed::WaitGetPoses) { } } +NAN_METHOD(Zed::WaitGetPoses) { + Zed *zed = ObjectWrap::Unwrap(info.This()); + Local position = Local::Cast(info[0]); + Local orientation = Local::Cast(info[1]); + + float *positions = (float *)((char *)position->Buffer()->GetContents().Data() + position->ByteOffset()); + float *orientations = (float *)((char *)orientation->Buffer()->GetContents().Data() + orientation->ByteOffset()); + + { + std::lock_guard lock(zed->mutex); + + memcpy(positions, zed->position.v, 3*sizeof(float)); + memcpy(orientations, zed->orientation.v, 4*sizeof(float)); + } + + if (!zed->result.IsEmpty()) { + Local result = Nan::New(zed->result); + zed->result.Reset(); + info.GetReturnValue().Set(result); + } else { + info.GetReturnValue().Set(Nan::Null()); + } +} + Local Zed::Initialize(Isolate *isolate) { Nan::EscapableHandleScope scope; diff --git a/src/Window.js b/src/Window.js index 700cf7a702..6765fb7c40 100644 --- a/src/Window.js +++ b/src/Window.js @@ -820,7 +820,11 @@ const _makeRequestAnimationFrame = window => (fn, priority = 0) => { requestMeshing(fn) { zedContext = new nativeZed(); zedContext.RequestPresent(fn); - return zedContext.emitter = new EventEmitter(); + zedContext.emitter = new EventEmitter(); + zedContext.emitter.position = new Float32Array(3); + zedContext.emitter.orientation = new Float32Array(4); + zedContext.emitter.orientation[3] = 1; + return zedContext.emitter; }, }; window.DOMParser = class DOMParser { @@ -1172,7 +1176,7 @@ const _makeRequestAnimationFrame = window => (fn, priority = 0) => { } if (zedContext) { - const meshes = zedContext.WaitGetPoses(); + const meshes = zedContext.WaitGetPoses(zedContext.emitter.position, zedContext.emitter.orientation); if (meshes) { zedContext.emitter.emit('meshes', meshes); } From d4885c3f642d8e7f66464d26f7152cb2e2a434f3 Mon Sep 17 00:00:00 2001 From: Avaer Kazmer Date: Sat, 18 May 2019 12:09:55 -0400 Subject: [PATCH 8/9] Add initial camera texture tracking --- deps/exokit-bindings/zed/include/zed.h | 10 +++- deps/exokit-bindings/zed/src/zed.cc | 63 +++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/deps/exokit-bindings/zed/include/zed.h b/deps/exokit-bindings/zed/include/zed.h index 90e5b24343..79f3e8dffc 100644 --- a/deps/exokit-bindings/zed/include/zed.h +++ b/deps/exokit-bindings/zed/include/zed.h @@ -20,6 +20,8 @@ using namespace v8; using namespace node; namespace zed { + +bool cudaSafeCall(cudaError_t err); class Zed : public ObjectWrap { public: @@ -43,10 +45,16 @@ class Zed : public ObjectWrap { sl::Mesh::chunkList chunks; std::chrono::high_resolution_clock::time_point ts_last; GLuint tex; + GLuint leftTex; + GLuint rightTex; NATIVEwindow *window; int textureWidth; int textureHeight; - cudaGraphicsResource *pcuImageRes; + // cudaGraphicsResource *pcuImageRes; + sl::Mat leftImage; + sl::Mat rightImage; + cudaGraphicsResource *leftCudaImageResource; + cudaGraphicsResource *rightCudaImageResource; // Nan::Persistent cbFn; std::mutex mutex; std::thread thread; diff --git a/deps/exokit-bindings/zed/src/zed.cc b/deps/exokit-bindings/zed/src/zed.cc index c80c0b9907..6222f11ffa 100644 --- a/deps/exokit-bindings/zed/src/zed.cc +++ b/deps/exokit-bindings/zed/src/zed.cc @@ -6,7 +6,15 @@ namespace zed { -Zed::Zed() : tex(0), window(nullptr), textureWidth(0), textureHeight(0), pcuImageRes(nullptr) {} +bool cudaSafeCall(cudaError_t err) { + if (err != cudaSuccess) { + printf("Cuda error [%d]: %s.\n", err, cudaGetErrorString(err)); + return false; + } + return true; +} + +Zed::Zed() : tex(0), leftTex(0), rightTex(0), window(nullptr), textureWidth(0), textureHeight(0), /*pcuImageRes(nullptr),*/ leftCudaImageResource(nullptr), rightCudaImageResource(nullptr) {} Zed::~Zed() {} @@ -102,7 +110,8 @@ NAN_METHOD(Zed::RequestPresent) { return; } - sl::CameraParameters camera_parameters = zed->camera.getCameraInformation().calibration_parameters.left_cam; + sl::CameraParameters camLeft = zed->camera.getCameraInformation().calibration_parameters.left_cam; + sl::CameraParameters camRight = zed->camera.getCameraInformation().calibration_parameters.right_cam; // sl::Mat image; // current left image @@ -134,7 +143,30 @@ NAN_METHOD(Zed::RequestPresent) { zed->window = windowsystem::CreateWindowHandle(1, 1, false); windowsystem::SetCurrentWindowContext(zed->window); - glGenTextures(1, &zed->tex); + // glGenTextures(1, &zed->tex); + + { + glGenTextures(1, &zed->leftTex); + glBindTexture(GL_TEXTURE_2D, zed->leftTex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, camLeft.image_size.width, camLeft.image_size.height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL); + glBindTexture(GL_TEXTURE_2D, 0); + + cudaSafeCall(cudaGraphicsGLRegisterImage(&zed->leftCudaImageResource, zed->leftTex, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsWriteDiscard)); + // cudaSafeCall(cudaGraphicsUnregisterResource(zed->leftCudaImageResource)); + } + { + glGenTextures(1, &zed->rightTex); + glBindTexture(GL_TEXTURE_2D, zed->rightTex); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, camLeft.image_size.width, camLeft.image_size.height, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, NULL); + glBindTexture(GL_TEXTURE_2D, 0); + + cudaSafeCall(cudaGraphicsGLRegisterImage(&zed->rightCudaImageResource, zed->rightTex, GL_TEXTURE_2D, cudaGraphicsRegisterFlagsWriteDiscard)); + // cudaSafeCall(cudaGraphicsUnregisterResource(zed->rightCudaImageResource)); + } for (;;) { zed->Poll(); @@ -160,11 +192,13 @@ void Zed::Poll() { Zed *zed = this; if (zed->camera.grab() == sl::SUCCESS) { - // Retrieve image in GPU memory - // zed.retrieveImage(image, sl::VIEW_LEFT, sl::MEM_GPU); // Update pose data (used for projection of the mesh over the current image) sl::Pose pose; sl::TRACKING_STATE tracking_state = zed->camera.getPosition(pose); + + // Retrieve image in GPU memory + zed->camera.retrieveImage(zed->leftImage, sl::VIEW_LEFT, sl::MEM_GPU); + zed->camera.retrieveImage(zed->rightImage, sl::VIEW_RIGHT, sl::MEM_GPU); { std::lock_guard lock(zed->mutex); @@ -192,7 +226,22 @@ void Zed::Poll() { // zed->mesh.applyTexture(sl::MESH_TEXTURE_RGB); - sl::Texture &texture = zed->mesh.texture; + { + cudaArray_t ArrIm; + cudaSafeCall(cudaGraphicsMapResources(1, &zed->leftCudaImageResource, 0)); + cudaSafeCall(cudaGraphicsSubResourceGetMappedArray(&ArrIm, zed->leftCudaImageResource, 0, 0)); + cudaSafeCall(cudaMemcpy2DToArray(ArrIm, 0, 0, zed->leftImage.getPtr(sl::MEM_GPU), zed->leftImage.getStepBytes(sl::MEM_GPU), zed->leftImage.getPixelBytes()*zed->leftImage.getWidth(), zed->leftImage.getHeight(), cudaMemcpyDeviceToDevice)); + cudaSafeCall(cudaGraphicsUnmapResources(1, &zed->leftCudaImageResource, 0)); + } + { + cudaArray_t ArrIm; + cudaSafeCall(cudaGraphicsMapResources(1, &zed->rightCudaImageResource, 0)); + cudaSafeCall(cudaGraphicsSubResourceGetMappedArray(&ArrIm, zed->rightCudaImageResource, 0, 0)); + cudaSafeCall(cudaMemcpy2DToArray(ArrIm, 0, 0, zed->rightImage.getPtr(sl::MEM_GPU), zed->rightImage.getStepBytes(sl::MEM_GPU), zed->rightImage.getPixelBytes()*zed->rightImage.getWidth(), zed->rightImage.getHeight(), cudaMemcpyDeviceToDevice)); + cudaSafeCall(cudaGraphicsUnmapResources(1, &zed->rightCudaImageResource, 0)); + } + + /* sl::Texture &texture = zed->mesh.texture; sl::Mat &textureMaterial = texture.data; // sl::Resolution &textureResolution = textureMaterial.getResolution(); // sl::uchar1 *tex = textureMaterial.getPtr(sl::MEM_GPU); @@ -222,7 +271,7 @@ void Zed::Poll() { cudaGraphicsMapResources(1, &zed->pcuImageRes, 0); cudaGraphicsSubResourceGetMappedArray(&arrIm, zed->pcuImageRes, 0, 0); cudaMemcpy2DToArray(arrIm, 0, 0, textureMaterial.getPtr(sl::MEM_GPU), textureMaterial.getStepBytes(sl::MEM_GPU), width * sizeof(sl::uchar3), height, cudaMemcpyDeviceToDevice); - cudaGraphicsUnmapResources(1, &zed->pcuImageRes, 0); + cudaGraphicsUnmapResources(1, &zed->pcuImageRes, 0); */ const float range = 5; zed->chunks = zed->mesh.getSurroundingList(pose.pose_data, range); From 70e7113e89f350d4f14ca10518f70c918c4e484c Mon Sep 17 00:00:00 2001 From: Avaer Kazmer Date: Sun, 2 Jun 2019 11:32:00 -0400 Subject: [PATCH 9/9] Add initial zed meshing example html --- examples/meshing_zed.html | 324 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 324 insertions(+) create mode 100644 examples/meshing_zed.html diff --git a/examples/meshing_zed.html b/examples/meshing_zed.html new file mode 100644 index 0000000000..394470a415 --- /dev/null +++ b/examples/meshing_zed.html @@ -0,0 +1,324 @@ + + + + + + meshing_ml + + + + + + +

meshing_ml

+ + + +