1 /*
2 This file is part of Magnum.
3
4 Original authors — credit is appreciated but not required:
5
6 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 —
7 Vladimír Vondruš <mosra@centrum.cz>
8 2018 — Jonathan Hale <squareys@googlemail.com>
9
10 This is free and unencumbered software released into the public domain.
11
12 Anyone is free to copy, modify, publish, use, compile, sell, or distribute
13 this software, either in source code form or as a compiled binary, for any
14 purpose, commercial or non-commercial, and by any means.
15
16 In jurisdictions that recognize copyright laws, the author or authors of
17 this software dedicate any and all copyright interest in the software to
18 the public domain. We make this dedication for the benefit of the public
19 at large and to the detriment of our heirs and successors. We intend this
20 dedication to be an overt act of relinquishment in perpetuity of all
21 present and future rights to this software under copyright law.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
27 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
28 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31 #include <Magnum/Magnum.h>
32 #include <Magnum/GL/Buffer.h>
33 #include <Magnum/GL/Context.h>
34 #include <Magnum/GL/DefaultFramebuffer.h>
35 #include <Magnum/GL/Framebuffer.h>
36 #include <Magnum/GL/Mesh.h>
37 #include <Magnum/GL/Renderer.h>
38 #include <Magnum/Math/Vector3.h>
39 #include <Magnum/MeshTools/CompressIndices.h>
40 #include <Magnum/MeshTools/Interleave.h>
41 #include <Magnum/Platform/Sdl2Application.h>
42 #include <Magnum/Primitives/Cylinder.h>
43 #include <Magnum/Primitives/UVSphere.h>
44 #include <Magnum/Shaders/Phong.h>
45 #include <Magnum/Trade/MeshData3D.h>
46
47 #include <Leap.h>
48
49 namespace Magnum { namespace Examples {
50
51 using namespace Math::Literals;
52
53 class LeapMotionExample: public Platform::Application {
54 public:
55 explicit LeapMotionExample(const Arguments& arguments);
56
57 private:
58 void drawEvent() override;
59 void drawBone(const Leap::Bone& bone, bool start, bool end, const Color3& color);
60
61 GL::Buffer _buffers[4];
62 GL::Mesh _cylinder{NoCreate};
63 GL::Mesh _sphere{NoCreate};
64
65 Shaders::Phong _shader;
66
67 const Matrix4 _projectionMatrix = Matrix4::perspectiveProjection(90.0_degf, 3.0f/2.0f, 0.01f, 25.0f);
68 const Matrix4 _viewMatrix = Matrix4::translation({0.0f, -3.0f, -5.0f});
69
70 /* LeapMotion specific members */
71 Leap::Controller _controller;
72 };
73
LeapMotionExample(const Arguments & arguments)74 LeapMotionExample::LeapMotionExample(const Arguments& arguments):
75 Platform::Application(arguments, Configuration{}.setTitle("Magnum Leap Motion Example"), GLConfiguration{}.setSampleCount(16))
76 {
77 /* To use LeapMotion with VR, you should set
78 _controller.setPolicy(Leap::Controller::PolicyFlag::POLICY_OPTIMIZE_HMD); */
79
80 GL::Renderer::enable(GL::Renderer::Feature::DepthTest);
81
82 /* As we sample the Leap controller every frame, enabling vsyc will cause more latency,
83 but seems to produce more stable results.
84 To reduce latency and fully utlize the ~115 fps hand tracking, disable vsync. */
85 setSwapInterval(1);
86
87 Containers::Array<char> indexData;
88 MeshIndexType indexType;
89 UnsignedInt indexStart, indexEnd;
90 {
91 /* Setup cylinder mesh */
92 const Trade::MeshData3D cylinder = Primitives::cylinderSolid(2, 16, 0.5f);
93 _buffers[0].setData(MeshTools::interleave(cylinder.positions(0), cylinder.normals(0)), GL::BufferUsage::StaticDraw);
94
95 std::tie(indexData, indexType, indexStart, indexEnd) = MeshTools::compressIndices(cylinder.indices());
96 _buffers[1].setData(indexData, GL::BufferUsage::StaticDraw);
97
98 _cylinder = GL::Mesh{cylinder.primitive()};
99 _cylinder.setCount(cylinder.indices().size())
100 .addVertexBuffer(_buffers[0], 0, Shaders::Phong::Position{}, Shaders::Phong::Normal{})
101 .setIndexBuffer(_buffers[1], 0, indexType, indexStart, indexEnd);
102 }
103 {
104 /* Setup sphere mesh */
105 const Trade::MeshData3D sphere = Primitives::uvSphereSolid(16, 16);
106 _buffers[2].setData(MeshTools::interleave(sphere.positions(0), sphere.normals(0)), GL::BufferUsage::StaticDraw);
107
108 std::tie(indexData, indexType, indexStart, indexEnd) = MeshTools::compressIndices(sphere.indices());
109 _buffers[3].setData(indexData, GL::BufferUsage::StaticDraw);
110
111 _sphere = GL::Mesh{sphere.primitive()};
112 _sphere.setCount(sphere.indices().size())
113 .addVertexBuffer(_buffers[2], 0, Shaders::Phong::Position{}, Shaders::Phong::Normal{})
114 .setIndexBuffer(_buffers[3], 0, indexType, indexStart, indexEnd);
115 }
116
117 /* Setup shader */
118 _shader.setSpecularColor(Color3(1.0f))
119 .setShininess(20)
120 .setLightPosition({0.0f, 5.0f, 5.0f});
121 }
122
drawEvent()123 void LeapMotionExample::drawEvent() {
124 /* Poll leap controller */
125 GL::defaultFramebuffer.clear(GL::FramebufferClear::Color|GL::FramebufferClear::Depth);
126
127 /* Render scene */
128 if(_controller.isConnected()) {
129 const Matrix4 viewProjMatrix = _projectionMatrix*_viewMatrix;
130 _shader.setProjectionMatrix(viewProjMatrix);
131
132 Leap::Frame frame = _controller.frame();
133 for(const Leap::Hand& hand : frame.hands()) {
134 const Color3 handColor = hand.isRight() ? Color3{1.0f, 0.0f, 0.0f} : Color3{0.0f, 1.0f, 1.0f};
135
136 for(const Leap::Finger& finger : hand.fingers()) {
137 for(int b = 0; b < 4; ++b) {
138 /* Leave out first bones of ring and middle finger, looks better */
139 if(b == 0 && (finger.type() == Leap::Finger::Type::TYPE_MIDDLE || finger.type() == Leap::Finger::Type::TYPE_RING)) continue;
140
141 const auto& bone = finger.bone(Leap::Bone::Type(b));
142 /* Only draw end on last bones */
143 drawBone(bone, true, b == 3, handColor);
144 }
145 }
146 }
147 }
148
149 swapBuffers();
150 redraw();
151 }
152
drawBone(const Leap::Bone & bone,bool start,bool end,const Color3 & color)153 void LeapMotionExample::drawBone(const Leap::Bone& bone, bool start, bool end, const Color3& color) {
154 const Vector3 boneCenter = 0.01f*Vector3::from(bone.center().toFloatPointer());
155 const Vector3 boneDirection = 0.01f*Vector3::from(bone.direction().toFloatPointer());
156 const Float boneLength = 0.01f*bone.length();
157
158 const Matrix4 rotX = Matrix4::rotationX(90.0_degf);
159 /* Draw cylinder */
160 {
161 const Matrix4 transformation = Matrix4::translation(boneCenter)*Matrix4::from(bone.basis().toArray4x4())*rotX*Matrix4::scaling({0.06f, boneLength, 0.06f});
162 _shader.setDiffuseColor(Color3{1.0f, 1.0f, 1.0f})
163 .setTransformationMatrix(transformation)
164 .setNormalMatrix(transformation.normalMatrix());
165 _cylinder.draw(_shader);
166 }
167
168 /* Draw sphere at start of the bone */
169 if(start) {
170 const Vector3 prevJoint = 0.01f*Vector3::from(bone.prevJoint().toFloatPointer());
171 const Matrix4 transformation = Matrix4::translation(prevJoint)*rotX*Matrix4::scaling(Vector3{0.08f});
172 _shader.setDiffuseColor(color)
173 .setTransformationMatrix(transformation)
174 .setNormalMatrix(transformation.normalMatrix());
175 _sphere.draw(_shader);
176 }
177
178 /* Draw sphere at end of the bone */
179 if(end) {
180 const Vector3 nextJoint = 0.01f*Vector3::from(bone.nextJoint().toFloatPointer());
181 const Matrix4 transformation = Matrix4::translation(nextJoint)*rotX*Matrix4::scaling(Vector3{0.08f});
182 _shader.setDiffuseColor(color)
183 .setTransformationMatrix(transformation)
184 .setNormalMatrix(transformation.normalMatrix());
185 _sphere.draw(_shader);
186 }
187 }
188
189 }}
190
191 MAGNUM_APPLICATION_MAIN(Magnum::Examples::LeapMotionExample)
192