Spaces:
Sleeping
Sleeping
3373503aba8db07f42f956473290cf8cf3836907fbd9aa5d2d6eb20784f60c8c
Browse files- third-party/DPVO/Pangolin/components/pango_video/src/drivers/thread.cpp +296 -0
- third-party/DPVO/Pangolin/components/pango_video/src/drivers/transform.cpp +597 -0
- third-party/DPVO/Pangolin/components/pango_video/src/drivers/truncate.cpp +127 -0
- third-party/DPVO/Pangolin/components/pango_video/src/drivers/unpack.cpp +284 -0
- third-party/DPVO/Pangolin/components/pango_video/src/drivers/uvc.cpp +382 -0
- third-party/DPVO/Pangolin/components/pango_video/src/drivers/uvc_mediafoundation.cpp +1091 -0
- third-party/DPVO/Pangolin/components/pango_video/src/drivers/v4l.cpp +852 -0
- third-party/DPVO/Pangolin/components/pango_video/src/stream_encoder_factory.cpp +62 -0
- third-party/DPVO/Pangolin/components/pango_video/src/video.cpp +75 -0
- third-party/DPVO/Pangolin/components/pango_video/src/video_help.cpp +45 -0
- third-party/DPVO/Pangolin/components/pango_video/src/video_input.cpp +220 -0
- third-party/DPVO/Pangolin/components/pango_video/src/video_output.cpp +137 -0
- third-party/DPVO/Pangolin/components/pango_video/tests/tests_video_loading.cpp +26 -0
- third-party/DPVO/Pangolin/components/pango_video/tests/tests_video_uri.cpp +72 -0
- third-party/DPVO/Pangolin/components/pango_windowing/CMakeLists.txt +102 -0
- third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/EmscriptenWindow.h +72 -0
- third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/OsxWindow.h +63 -0
- third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/PangolinNSApplication.h +61 -0
- third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/PangolinNSGLView.h +44 -0
- third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/WinWindow.h +89 -0
- third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/X11GlContext.h +0 -0
- third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/X11Window.h +109 -0
- third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/display_android.h +333 -0
- third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/handler_bitsets.h +40 -0
- third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/handler_enums.h +94 -0
- third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/window.h +136 -0
- third-party/DPVO/Pangolin/components/pango_windowing/src/PangolinNSApplication.mm +92 -0
- third-party/DPVO/Pangolin/components/pango_windowing/src/PangolinNSGLView.mm +318 -0
- third-party/DPVO/Pangolin/components/pango_windowing/src/display_android.cpp +1026 -0
- third-party/DPVO/Pangolin/components/pango_windowing/src/display_emscripten.cpp +324 -0
- third-party/DPVO/Pangolin/components/pango_windowing/src/display_headless.cpp +174 -0
- third-party/DPVO/Pangolin/components/pango_windowing/src/display_osx.mm +213 -0
- third-party/DPVO/Pangolin/components/pango_windowing/src/display_wayland.cpp +1074 -0
- third-party/DPVO/Pangolin/components/pango_windowing/src/display_win.cpp +631 -0
- third-party/DPVO/Pangolin/components/pango_windowing/src/display_x11.cpp +548 -0
- third-party/DPVO/Pangolin/components/pango_windowing/src/window.cpp +14 -0
- third-party/DPVO/Pangolin/components/tinyobj/CMakeLists.txt +11 -0
- third-party/DPVO/Pangolin/components/tinyobj/include/tinyobj/tiny_obj_loader.h +2547 -0
- third-party/DPVO/Pangolin/components/tinyobj/src/tinyobj.cpp +29 -0
- third-party/DPVO/Pangolin/examples/BasicOpenGL/1_gl_intro_classic_triangle.cpp +57 -0
- third-party/DPVO/Pangolin/examples/BasicOpenGL/1_gl_intro_pango_triangle.cpp +48 -0
- third-party/DPVO/Pangolin/examples/BasicOpenGL/2_gl_intro_classic_triangle_vbo.cpp +62 -0
- third-party/DPVO/Pangolin/examples/BasicOpenGL/2_gl_intro_pango_triangle_vbo.cpp +35 -0
- third-party/DPVO/Pangolin/examples/BasicOpenGL/3_gl_intro_classic_triangle_vbo_shader.cpp +155 -0
- third-party/DPVO/Pangolin/examples/BasicOpenGL/3_gl_intro_pango_triangle_vbo_shader.cpp +75 -0
- third-party/DPVO/Pangolin/examples/BasicOpenGL/4_gl_intro_viewport.cpp +79 -0
- third-party/DPVO/Pangolin/examples/BasicOpenGL/5_gl_intro_view_transforms.cpp +98 -0
- third-party/DPVO/Pangolin/examples/BasicOpenGL/CMakeLists.txt +22 -0
- third-party/DPVO/Pangolin/examples/BasicOpenGL/README.md +39 -0
- third-party/DPVO/Pangolin/examples/CMakeLists.txt +15 -0
third-party/DPVO/Pangolin/components/pango_video/src/drivers/thread.cpp
ADDED
@@ -0,0 +1,296 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2014 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#include <pangolin/factory/factory_registry.h>
|
29 |
+
#include <pangolin/video/drivers/thread.h>
|
30 |
+
#include <pangolin/video/iostream_operators.h>
|
31 |
+
#include <pangolin/video/video.h>
|
32 |
+
|
33 |
+
#ifdef DEBUGTHREAD
|
34 |
+
#include <pangolin/utils/timer.h>
|
35 |
+
#define TSTART() pangolin::basetime start,last,now; start = pangolin::TimeNow(); last = start;
|
36 |
+
#define TGRABANDPRINT(...) now = pangolin::TimeNow(); fprintf(stderr,"THREAD: "); fprintf(stderr, __VA_ARGS__); fprintf(stderr, " %fms.\n",1000*pangolin::TimeDiff_s(last, now)); last = now;
|
37 |
+
#define DBGPRINT(...) fprintf(stderr,"THREAD: "); fprintf(stderr, __VA_ARGS__); fprintf(stderr,"\n");
|
38 |
+
#else
|
39 |
+
#define TSTART()
|
40 |
+
#define TGRABANDPRINT(...)
|
41 |
+
#define DBGPRINT(...)
|
42 |
+
#endif
|
43 |
+
|
44 |
+
namespace pangolin
|
45 |
+
{
|
46 |
+
|
47 |
+
const uint64_t grab_fail_thread_sleep_us = 1000;
|
48 |
+
const uint64_t capture_timout_ms = 5000;
|
49 |
+
|
50 |
+
ThreadVideo::ThreadVideo(std::unique_ptr<VideoInterface> &src_, size_t num_buffers, const std::string& name)
|
51 |
+
: src(std::move(src_)), quit_grab_thread(true), thread_name(name)
|
52 |
+
{
|
53 |
+
if(!src) {
|
54 |
+
throw VideoException("ThreadVideo: VideoInterface in must not be null");
|
55 |
+
}
|
56 |
+
videoin.push_back(src.get());
|
57 |
+
|
58 |
+
// // queue init allocates buffers.
|
59 |
+
const size_t buffer_size = videoin[0]->SizeBytes();
|
60 |
+
for(size_t i=0; i < num_buffers; ++i)
|
61 |
+
{
|
62 |
+
queue.returnOrAddUsedBuffer( GrabResult(buffer_size) );
|
63 |
+
}
|
64 |
+
}
|
65 |
+
|
66 |
+
ThreadVideo::~ThreadVideo()
|
67 |
+
{
|
68 |
+
Stop();
|
69 |
+
|
70 |
+
src.reset();
|
71 |
+
}
|
72 |
+
|
73 |
+
//! Implement VideoInput::Start()
|
74 |
+
void ThreadVideo::Start()
|
75 |
+
{
|
76 |
+
// Only start thread if not already running.
|
77 |
+
if(quit_grab_thread) {
|
78 |
+
videoin[0]->Start();
|
79 |
+
quit_grab_thread = false;
|
80 |
+
grab_thread = std::thread(std::ref(*this));
|
81 |
+
}
|
82 |
+
}
|
83 |
+
|
84 |
+
//! Implement VideoInput::Stop()
|
85 |
+
void ThreadVideo::Stop()
|
86 |
+
{
|
87 |
+
quit_grab_thread = true;
|
88 |
+
if(grab_thread.joinable()) {
|
89 |
+
grab_thread.join();
|
90 |
+
}
|
91 |
+
videoin[0]->Stop();
|
92 |
+
}
|
93 |
+
|
94 |
+
//! Implement VideoInput::SizeBytes()
|
95 |
+
size_t ThreadVideo::SizeBytes() const
|
96 |
+
{
|
97 |
+
return videoin[0]->SizeBytes();
|
98 |
+
}
|
99 |
+
|
100 |
+
//! Implement VideoInput::Streams()
|
101 |
+
const std::vector<StreamInfo>& ThreadVideo::Streams() const
|
102 |
+
{
|
103 |
+
return videoin[0]->Streams();
|
104 |
+
}
|
105 |
+
|
106 |
+
const picojson::value& ThreadVideo::DeviceProperties() const
|
107 |
+
{
|
108 |
+
device_properties = GetVideoDeviceProperties(videoin[0]);
|
109 |
+
return device_properties;
|
110 |
+
}
|
111 |
+
|
112 |
+
const picojson::value& ThreadVideo::FrameProperties() const
|
113 |
+
{
|
114 |
+
return frame_properties;
|
115 |
+
}
|
116 |
+
|
117 |
+
uint32_t ThreadVideo::AvailableFrames() const
|
118 |
+
{
|
119 |
+
return (uint32_t)queue.AvailableFrames();
|
120 |
+
}
|
121 |
+
|
122 |
+
bool ThreadVideo::DropNFrames(uint32_t n)
|
123 |
+
{
|
124 |
+
return queue.DropNFrames(n);
|
125 |
+
}
|
126 |
+
|
127 |
+
//! Implement VideoInput::GrabNext()
|
128 |
+
bool ThreadVideo::GrabNext( unsigned char* image, bool wait )
|
129 |
+
{
|
130 |
+
TSTART()
|
131 |
+
|
132 |
+
if(queue.EmptyBuffers() == 0) {
|
133 |
+
pango_print_warn("Thread %s(%12p) has run out of %d buffers\n", thread_name.c_str(), this, (int)queue.AvailableFrames());
|
134 |
+
}
|
135 |
+
|
136 |
+
if(queue.AvailableFrames() == 0 && !wait) {
|
137 |
+
// No frames available, no wait, simply return false.
|
138 |
+
DBGPRINT("GrabNext no available frames no wait.");
|
139 |
+
return false;
|
140 |
+
}else{
|
141 |
+
if(queue.AvailableFrames() == 0 && wait) {
|
142 |
+
if (quit_grab_thread)
|
143 |
+
{
|
144 |
+
return false;
|
145 |
+
}
|
146 |
+
// Must return a frame so block on notification from grab thread.
|
147 |
+
std::unique_lock<std::mutex> lk(cvMtx);
|
148 |
+
DBGPRINT("GrabNext no available frames wait for notification.");
|
149 |
+
if(cv.wait_for(lk, std::chrono::milliseconds(capture_timout_ms)) == std::cv_status::timeout)
|
150 |
+
{
|
151 |
+
pango_print_warn("ThreadVideo: GrabNext blocking read for frames reached timeout.\n");
|
152 |
+
return false;
|
153 |
+
}
|
154 |
+
}
|
155 |
+
|
156 |
+
// At least one valid frame in queue, return it.
|
157 |
+
GrabResult grab = queue.getNext();
|
158 |
+
if(grab.return_status) {
|
159 |
+
DBGPRINT("GrabNext at least one frame available.");
|
160 |
+
const size_t buffer_size = videoin[0]->SizeBytes();
|
161 |
+
std::memcpy(image, grab.buffer.get(), buffer_size);
|
162 |
+
frame_properties = grab.frame_properties;
|
163 |
+
}else{
|
164 |
+
DBGPRINT("GrabNext returned false")
|
165 |
+
}
|
166 |
+
queue.returnOrAddUsedBuffer(std::move(grab));
|
167 |
+
|
168 |
+
TGRABANDPRINT("GrabNext took")
|
169 |
+
return grab.return_status;
|
170 |
+
}
|
171 |
+
}
|
172 |
+
|
173 |
+
//! Implement VideoInput::GrabNewest()
|
174 |
+
bool ThreadVideo::GrabNewest( unsigned char* image, bool wait )
|
175 |
+
{
|
176 |
+
TSTART()
|
177 |
+
if(queue.AvailableFrames() == 0 && !wait) {
|
178 |
+
// No frames available, no wait, simply return false.
|
179 |
+
DBGPRINT("GrabNext no available frames no wait.");
|
180 |
+
return false;
|
181 |
+
}else{
|
182 |
+
if(queue.AvailableFrames() == 0 && wait) {
|
183 |
+
// Must return a frame so block on notification from grab thread.
|
184 |
+
std::unique_lock<std::mutex> lk(cvMtx);
|
185 |
+
DBGPRINT("GrabNewest no available frames wait for notification.");
|
186 |
+
if(cv.wait_for(lk, std::chrono::milliseconds(capture_timout_ms)) == std::cv_status::timeout)
|
187 |
+
{
|
188 |
+
pango_print_warn("ThreadVideo: GrabNewest blocking read for frames reached timeout.\n");
|
189 |
+
return false;
|
190 |
+
}
|
191 |
+
}
|
192 |
+
|
193 |
+
// At least one valid frame in queue, return it.
|
194 |
+
DBGPRINT("GrabNewest at least one frame available.");
|
195 |
+
GrabResult grab = queue.getNewest();
|
196 |
+
const bool success = grab.return_status;
|
197 |
+
if(success) {
|
198 |
+
std::memcpy(image, grab.buffer.get(), videoin[0]->SizeBytes());
|
199 |
+
frame_properties = grab.frame_properties;
|
200 |
+
}
|
201 |
+
queue.returnOrAddUsedBuffer(std::move(grab));
|
202 |
+
TGRABANDPRINT("GrabNewest memcpy of available frame took")
|
203 |
+
|
204 |
+
return success;
|
205 |
+
}
|
206 |
+
}
|
207 |
+
|
208 |
+
void ThreadVideo::operator()()
|
209 |
+
{
|
210 |
+
DBGPRINT("Grab thread Started.")
|
211 |
+
// Spinning thread attempting to read from videoin[0] as fast as possible
|
212 |
+
// relying on the videoin[0] blocking grab.
|
213 |
+
while(!quit_grab_thread) {
|
214 |
+
// Get a buffer from the queue;
|
215 |
+
|
216 |
+
if(queue.EmptyBuffers() > 0) {
|
217 |
+
GrabResult grab = queue.getFreeBuffer();
|
218 |
+
|
219 |
+
// Blocking grab (i.e. GrabNext with wait = true).
|
220 |
+
try{
|
221 |
+
grab.return_status = videoin[0]->GrabNext(grab.buffer.get(), true);
|
222 |
+
}catch(const VideoException& e) {
|
223 |
+
// User doesn't have the opportunity to catch exceptions here.
|
224 |
+
std::string what = e.what();
|
225 |
+
pango_print_warn("ThreadVideo caught VideoException (%s)\n", what.c_str());
|
226 |
+
if (what.find("No such device") != std::string::npos) {
|
227 |
+
pango_print_warn("Device is gone, exiting thread.\n");
|
228 |
+
quit_grab_thread = true;
|
229 |
+
}
|
230 |
+
grab.return_status = false;
|
231 |
+
}catch(const std::exception& e){
|
232 |
+
// User doesn't have the opportunity to catch exceptions here.
|
233 |
+
pango_print_warn("ThreadVideo caught exception (%s)\n", e.what());
|
234 |
+
grab.return_status = false;
|
235 |
+
}
|
236 |
+
|
237 |
+
if(grab.return_status){
|
238 |
+
grab.frame_properties = GetVideoFrameProperties(videoin[0]);
|
239 |
+
}else{
|
240 |
+
std::this_thread::sleep_for(std::chrono::microseconds(grab_fail_thread_sleep_us) );
|
241 |
+
}
|
242 |
+
queue.addValidBuffer(std::move(grab));
|
243 |
+
|
244 |
+
DBGPRINT("Grab thread got frame. valid:%d free:%d",queue.AvailableFrames(),queue.EmptyBuffers())
|
245 |
+
// Let listening threads know we got a frame in case they are waiting.
|
246 |
+
cv.notify_all();
|
247 |
+
}else{
|
248 |
+
std::this_thread::sleep_for(std::chrono::microseconds(grab_fail_thread_sleep_us) );
|
249 |
+
}
|
250 |
+
std::this_thread::yield();
|
251 |
+
}
|
252 |
+
DBGPRINT("Grab thread Stopped.")
|
253 |
+
|
254 |
+
return;
|
255 |
+
}
|
256 |
+
|
257 |
+
std::vector<VideoInterface*>& ThreadVideo::InputStreams()
|
258 |
+
{
|
259 |
+
return videoin;
|
260 |
+
}
|
261 |
+
|
262 |
+
PANGOLIN_REGISTER_FACTORY(ThreadVideo)
|
263 |
+
{
|
264 |
+
struct ThreadVideoFactory final : public TypedFactoryInterface<VideoInterface> {
|
265 |
+
std::map<std::string,Precedence> Schemes() const override
|
266 |
+
{
|
267 |
+
return {{"thread",10}};
|
268 |
+
}
|
269 |
+
const char* Description() const override
|
270 |
+
{
|
271 |
+
return "Queues sub-video frames in thread.";
|
272 |
+
}
|
273 |
+
ParamSet Params() const override
|
274 |
+
{
|
275 |
+
return {{
|
276 |
+
{"num_buffers", "30", "Size of the input queue/buffer for this thread"},
|
277 |
+
{"name","Unnamed","Name of the thread"}
|
278 |
+
}};
|
279 |
+
}
|
280 |
+
std::unique_ptr<VideoInterface> Open(const Uri& uri) override {
|
281 |
+
ParamReader reader(Params(), uri);
|
282 |
+
std::unique_ptr<VideoInterface> subvid = pangolin::OpenVideo(uri.url);
|
283 |
+
const int num_buffers = reader.Get<int>("num_buffers");
|
284 |
+
const std::string name = reader.Get<std::string>("name");
|
285 |
+
return std::unique_ptr<VideoInterface>(new ThreadVideo(subvid, num_buffers, name));
|
286 |
+
}
|
287 |
+
};
|
288 |
+
|
289 |
+
return FactoryRegistry::I()->RegisterFactory<VideoInterface>(std::make_shared<ThreadVideoFactory>());
|
290 |
+
}
|
291 |
+
|
292 |
+
}
|
293 |
+
|
294 |
+
#undef TSTART
|
295 |
+
#undef TGRABANDPRINT
|
296 |
+
#undef DBGPRINT
|
third-party/DPVO/Pangolin/components/pango_video/src/drivers/transform.cpp
ADDED
@@ -0,0 +1,597 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2014 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#include <pangolin/video/drivers/transform.h>
|
29 |
+
#include <pangolin/factory/factory_registry.h>
|
30 |
+
#include <pangolin/video/iostream_operators.h>
|
31 |
+
#include <pangolin/video/video.h>
|
32 |
+
|
33 |
+
#include <unordered_map>
|
34 |
+
#include <vector>
|
35 |
+
|
36 |
+
namespace pangolin
|
37 |
+
{
|
38 |
+
|
39 |
+
|
40 |
+
TransformVideo::TransformVideo(std::unique_ptr<VideoInterface>& src, const std::vector<TransformOptions>& flips)
|
41 |
+
: videoin(std::move(src)), flips(flips), size_bytes(0),buffer(0)
|
42 |
+
{
|
43 |
+
if(!videoin) {
|
44 |
+
throw VideoException("TransformVideo: VideoInterface in must not be null");
|
45 |
+
}
|
46 |
+
|
47 |
+
inputs.push_back(videoin.get());
|
48 |
+
|
49 |
+
for(size_t i=0;i<videoin->Streams().size();i++)
|
50 |
+
switch (flips[i]) {
|
51 |
+
case TransformOptions::FlipX:
|
52 |
+
case TransformOptions::FlipY:
|
53 |
+
case TransformOptions::FlipXY:
|
54 |
+
case TransformOptions::None:
|
55 |
+
streams.push_back(videoin->Streams()[i]);
|
56 |
+
break;
|
57 |
+
|
58 |
+
case TransformOptions::Transpose:
|
59 |
+
case TransformOptions::RotateCW:
|
60 |
+
case TransformOptions::RotateCCW:
|
61 |
+
|
62 |
+
unsigned char*ptr=videoin->Streams()[i].Offset();
|
63 |
+
size_t w=videoin->Streams()[i].Height();
|
64 |
+
size_t h=videoin->Streams()[i].Width();
|
65 |
+
size_t Bpp=videoin->Streams()[i].PixFormat().bpp / 8;
|
66 |
+
|
67 |
+
streams.emplace_back(videoin->Streams()[i].PixFormat(),pangolin::Image<unsigned char>(ptr,w,h,w*Bpp));
|
68 |
+
break;
|
69 |
+
};
|
70 |
+
|
71 |
+
size_bytes = videoin->SizeBytes();
|
72 |
+
buffer = new unsigned char[size_bytes];
|
73 |
+
}
|
74 |
+
|
75 |
+
TransformVideo::~TransformVideo()
|
76 |
+
{
|
77 |
+
delete[] buffer;
|
78 |
+
}
|
79 |
+
|
80 |
+
//! Implement VideoInput::Start()
|
81 |
+
void TransformVideo::Start()
|
82 |
+
{
|
83 |
+
videoin->Start();
|
84 |
+
}
|
85 |
+
|
86 |
+
//! Implement VideoInput::Stop()
|
87 |
+
void TransformVideo::Stop()
|
88 |
+
{
|
89 |
+
videoin->Stop();
|
90 |
+
}
|
91 |
+
|
92 |
+
//! Implement VideoInput::SizeBytes()
|
93 |
+
size_t TransformVideo::SizeBytes() const
|
94 |
+
{
|
95 |
+
return size_bytes;
|
96 |
+
}
|
97 |
+
|
98 |
+
//! Implement VideoInput::Streams()
|
99 |
+
const std::vector<StreamInfo>& TransformVideo::Streams() const
|
100 |
+
{
|
101 |
+
return streams;
|
102 |
+
}
|
103 |
+
|
104 |
+
void PitchedImageCopy(
|
105 |
+
Image<unsigned char>& img_out,
|
106 |
+
const Image<unsigned char>& img_in,
|
107 |
+
size_t bytes_per_pixel
|
108 |
+
) {
|
109 |
+
if( img_out.w != img_in.w || img_out.h != img_in.h ) {
|
110 |
+
throw std::runtime_error("PitchedImageCopy: Incompatible image sizes");
|
111 |
+
}
|
112 |
+
|
113 |
+
for(size_t y=0; y < img_out.h; ++y) {
|
114 |
+
std::memcpy(img_out.RowPtr((int)y), img_in.RowPtr((int)y), bytes_per_pixel * img_in.w);
|
115 |
+
}
|
116 |
+
}
|
117 |
+
|
118 |
+
void FlipY(
|
119 |
+
Image<unsigned char>& img_out,
|
120 |
+
const Image<unsigned char>& img_in,
|
121 |
+
size_t bytes_per_pixel
|
122 |
+
) {
|
123 |
+
if( img_out.w != img_in.w || img_out.h != img_in.h ) {
|
124 |
+
throw std::runtime_error("FlipY: Incompatible image sizes");
|
125 |
+
}
|
126 |
+
|
127 |
+
for(size_t y_out=0; y_out < img_out.h; ++y_out) {
|
128 |
+
const size_t y_in = (img_in.h-1) - y_out;
|
129 |
+
std::memcpy(img_out.RowPtr((int)y_out), img_in.RowPtr((int)y_in), bytes_per_pixel * img_in.w);
|
130 |
+
}
|
131 |
+
}
|
132 |
+
|
133 |
+
template <typename T>
|
134 |
+
void ChainSwap2(T& a, T& b)
|
135 |
+
{
|
136 |
+
T t = a;
|
137 |
+
a = b;
|
138 |
+
b = t;
|
139 |
+
}
|
140 |
+
|
141 |
+
template <typename T>
|
142 |
+
void ChainSwap4(T& a, T& b, T& c, T& d)
|
143 |
+
{
|
144 |
+
T t = a;
|
145 |
+
a = b;
|
146 |
+
b = c;
|
147 |
+
c = d;
|
148 |
+
d = t;
|
149 |
+
}
|
150 |
+
|
151 |
+
template <size_t BPP, size_t TSZ>
|
152 |
+
void TiledFlipX(Image<unsigned char>& img_out, const Image<unsigned char>& img_in)
|
153 |
+
{
|
154 |
+
const size_t w = img_in.w;
|
155 |
+
const size_t h = img_in.h;
|
156 |
+
|
157 |
+
typedef struct
|
158 |
+
{
|
159 |
+
unsigned char d[BPP];
|
160 |
+
} T;
|
161 |
+
T d[TSZ][TSZ];
|
162 |
+
|
163 |
+
for(size_t xin = 0; xin < w; xin += TSZ)
|
164 |
+
for(size_t yin = 0; yin < h; yin += TSZ)
|
165 |
+
{
|
166 |
+
const size_t xspan = std::min(TSZ, w - xin);
|
167 |
+
const size_t yspan = std::min(TSZ, h - yin);
|
168 |
+
const size_t xout = w - xin - TSZ;
|
169 |
+
const size_t yout = yin;
|
170 |
+
|
171 |
+
for(size_t y = 0; y < yspan; y++)
|
172 |
+
memcpy(d[y], img_in.RowPtr(yin + y) + xin * BPP, xspan * BPP);
|
173 |
+
|
174 |
+
for(size_t y = 0; y < TSZ; y++)
|
175 |
+
for(size_t x = 0; x < TSZ / 2; x++)
|
176 |
+
ChainSwap2(d[y][x], d[y][TSZ - 1 - x]);
|
177 |
+
|
178 |
+
for(size_t y = 0; y < yspan; y++)
|
179 |
+
memcpy(img_out.RowPtr(yout + y) + (xout + TSZ - xspan) * BPP, d[y] + TSZ - xspan, xspan * BPP);
|
180 |
+
}
|
181 |
+
}
|
182 |
+
|
183 |
+
template <size_t BPP, size_t TSZ>
|
184 |
+
void TiledRotate180(Image<unsigned char>& img_out, const Image<unsigned char>& img_in)
|
185 |
+
{
|
186 |
+
static_assert(!(TSZ & 1), "Tilesize must be even.");
|
187 |
+
|
188 |
+
const size_t w = img_in.w;
|
189 |
+
const size_t h = img_in.h;
|
190 |
+
|
191 |
+
typedef struct
|
192 |
+
{
|
193 |
+
unsigned char d[BPP];
|
194 |
+
} T;
|
195 |
+
T d[TSZ][TSZ];
|
196 |
+
|
197 |
+
for(size_t xin = 0; xin < w; xin += TSZ)
|
198 |
+
for(size_t yin = 0; yin < h; yin += TSZ)
|
199 |
+
{
|
200 |
+
const size_t xspan = std::min(TSZ, w - xin);
|
201 |
+
const size_t yspan = std::min(TSZ, h - yin);
|
202 |
+
const size_t xout = w - xin - TSZ;
|
203 |
+
const size_t yout = h - yin - TSZ;
|
204 |
+
|
205 |
+
for(size_t y = 0; y < yspan; y++)
|
206 |
+
memcpy(d[y], img_in.RowPtr(yin + y) + xin * BPP, xspan * BPP);
|
207 |
+
|
208 |
+
for(size_t y = 0; y < TSZ / 2; y++)
|
209 |
+
for(size_t x = 0; x < TSZ; x++)
|
210 |
+
ChainSwap2(d[y][x], d[TSZ - 1 - y][TSZ - 1 - x]);
|
211 |
+
|
212 |
+
for(size_t y = TSZ - yspan; y < TSZ; y++)
|
213 |
+
memcpy(img_out.RowPtr(yout + y) + (xout + TSZ - xspan) * BPP, d[y] + TSZ - xspan, xspan * BPP);
|
214 |
+
}
|
215 |
+
}
|
216 |
+
|
217 |
+
template <size_t BPP, size_t TSZ>
|
218 |
+
void TiledTranspose(Image<unsigned char>& img_out, const Image<unsigned char>& img_in)
|
219 |
+
{
|
220 |
+
const size_t w = img_in.w;
|
221 |
+
const size_t h = img_in.h;
|
222 |
+
|
223 |
+
typedef struct
|
224 |
+
{
|
225 |
+
unsigned char d[BPP];
|
226 |
+
} T;
|
227 |
+
T d[TSZ][TSZ];
|
228 |
+
|
229 |
+
for(size_t xin = 0; xin < w; xin += TSZ)
|
230 |
+
for(size_t yin = 0; yin < h; yin += TSZ)
|
231 |
+
{
|
232 |
+
const size_t xspan = std::min(TSZ, w - xin);
|
233 |
+
const size_t yspan = std::min(TSZ, h - yin);
|
234 |
+
const size_t dmin = std::min(xspan, yspan);
|
235 |
+
const size_t dmax = std::max(xspan, yspan);
|
236 |
+
const size_t xout = yin;
|
237 |
+
const size_t yout = xin;
|
238 |
+
|
239 |
+
for(size_t y = 0; y < yspan; y++)
|
240 |
+
memcpy(d[y], img_in.RowPtr(yin + y) + xin * BPP, xspan * BPP);
|
241 |
+
|
242 |
+
for(size_t x = 0; x < dmin; x++)
|
243 |
+
for(size_t y = x + 1; y < dmax; y++)
|
244 |
+
ChainSwap2(d[x][y], d[y][x]);
|
245 |
+
|
246 |
+
for(size_t y = 0; y < xspan; y++)
|
247 |
+
memcpy(img_out.RowPtr(yout + y) + xout * BPP, d[y], yspan * BPP);
|
248 |
+
}
|
249 |
+
}
|
250 |
+
|
251 |
+
template <size_t BPP, size_t TSZ>
|
252 |
+
void TiledRotateCW(Image<unsigned char>& img_out, const Image<unsigned char>& img_in)
|
253 |
+
{
|
254 |
+
static_assert(!(TSZ & 1), "Tilesize must be even.");
|
255 |
+
|
256 |
+
const size_t w = img_in.w;
|
257 |
+
const size_t h = img_in.h;
|
258 |
+
|
259 |
+
typedef struct
|
260 |
+
{
|
261 |
+
unsigned char d[BPP];
|
262 |
+
} T;
|
263 |
+
T d[TSZ][TSZ];
|
264 |
+
|
265 |
+
for(size_t xin = 0; xin < w; xin += TSZ)
|
266 |
+
for(size_t yin = 0; yin < h; yin += TSZ)
|
267 |
+
{
|
268 |
+
const size_t xspan = std::min(TSZ, w - xin);
|
269 |
+
const size_t yspan = std::min(TSZ, h - yin);
|
270 |
+
const size_t xout = h - yin - TSZ;
|
271 |
+
const size_t yout = xin;
|
272 |
+
|
273 |
+
for(size_t y = 0; y < yspan; y++)
|
274 |
+
memcpy(d[y], img_in.RowPtr(yin + y) + xin * BPP, xspan * BPP);
|
275 |
+
|
276 |
+
for(size_t y = 0; y < TSZ / 2; y++)
|
277 |
+
for(size_t x = 0; x < TSZ / 2; x++)
|
278 |
+
ChainSwap4(d[TSZ - 1 - x][y], d[TSZ - 1 - y][TSZ - 1 - x], d[x][TSZ - 1 - y], d[y][x]);
|
279 |
+
|
280 |
+
for(size_t y = 0; y < xspan; y++)
|
281 |
+
memcpy(img_out.RowPtr(yout + y) + (xout + TSZ - yspan) * BPP, d[y] + TSZ - yspan, yspan * BPP);
|
282 |
+
}
|
283 |
+
}
|
284 |
+
|
285 |
+
template <size_t BPP, size_t TSZ>
|
286 |
+
void TiledRotateCCW(Image<unsigned char>& img_out, const Image<unsigned char>& img_in)
|
287 |
+
{
|
288 |
+
static_assert(!(TSZ & 1), "Tilesize must be even.");
|
289 |
+
|
290 |
+
const size_t w = img_in.w;
|
291 |
+
const size_t h = img_in.h;
|
292 |
+
|
293 |
+
typedef struct
|
294 |
+
{
|
295 |
+
unsigned char d[BPP];
|
296 |
+
} T;
|
297 |
+
T d[TSZ][TSZ];
|
298 |
+
|
299 |
+
for(size_t xin = 0; xin < w; xin += TSZ)
|
300 |
+
for(size_t yin = 0; yin < h; yin += TSZ)
|
301 |
+
{
|
302 |
+
const size_t xspan = std::min(TSZ, w - xin);
|
303 |
+
const size_t yspan = std::min(TSZ, h - yin);
|
304 |
+
const size_t xout = yin;
|
305 |
+
const size_t yout = w - xin - TSZ;
|
306 |
+
|
307 |
+
for(size_t y = 0; y < yspan; y++)
|
308 |
+
memcpy(d[y], img_in.RowPtr(yin + y) + xin * BPP, xspan * BPP);
|
309 |
+
|
310 |
+
for(size_t y = 0; y < TSZ / 2; y++)
|
311 |
+
for(size_t x = 0; x < TSZ / 2; x++)
|
312 |
+
ChainSwap4(d[y][x], d[x][TSZ - 1 - y], d[TSZ - 1 - y][TSZ - 1 - x], d[TSZ - 1 - x][y]);
|
313 |
+
|
314 |
+
for(size_t y = TSZ - xspan; y < TSZ; y++)
|
315 |
+
memcpy(img_out.RowPtr(yout + y) + xout * BPP, d[y], yspan * BPP);
|
316 |
+
}
|
317 |
+
}
|
318 |
+
|
319 |
+
void FlipX(Image<unsigned char>& img_out, const Image<unsigned char>& img_in, size_t bytes_per_pixel)
|
320 |
+
{
|
321 |
+
if(bytes_per_pixel == 1)
|
322 |
+
TiledFlipX<1, 160>(img_out, img_in);
|
323 |
+
else if(bytes_per_pixel == 2)
|
324 |
+
TiledFlipX<2, 120>(img_out, img_in);
|
325 |
+
else if(bytes_per_pixel == 3)
|
326 |
+
TiledFlipX<3, 80>(img_out, img_in);
|
327 |
+
else if(bytes_per_pixel == 4)
|
328 |
+
TiledFlipX<4, 80>(img_out, img_in);
|
329 |
+
else if(bytes_per_pixel == 6)
|
330 |
+
TiledFlipX<6, 64>(img_out, img_in);
|
331 |
+
else {
|
332 |
+
for(size_t y = 0; y < img_out.h; ++y) {
|
333 |
+
for(size_t x = 0; x < img_out.w; ++x) {
|
334 |
+
memcpy(img_out.RowPtr((int)y) + (img_out.w - 1 - x) * bytes_per_pixel,
|
335 |
+
img_in.RowPtr((int)y) + x * bytes_per_pixel,
|
336 |
+
bytes_per_pixel);
|
337 |
+
}
|
338 |
+
}
|
339 |
+
}
|
340 |
+
}
|
341 |
+
|
342 |
+
void FlipXY(Image<unsigned char>& img_out, const Image<unsigned char>& img_in, size_t bytes_per_pixel)
|
343 |
+
{
|
344 |
+
if(bytes_per_pixel == 1)
|
345 |
+
TiledRotate180<1, 160>(img_out, img_in);
|
346 |
+
else if(bytes_per_pixel == 2)
|
347 |
+
TiledRotate180<2, 120>(img_out, img_in);
|
348 |
+
else if(bytes_per_pixel == 3)
|
349 |
+
TiledRotate180<3, 80>(img_out, img_in);
|
350 |
+
else if(bytes_per_pixel == 4)
|
351 |
+
TiledRotate180<4, 80>(img_out, img_in);
|
352 |
+
else if(bytes_per_pixel == 6)
|
353 |
+
TiledRotate180<6, 64>(img_out, img_in);
|
354 |
+
else {
|
355 |
+
for(size_t y_out = 0; y_out < img_out.h; ++y_out) {
|
356 |
+
for(size_t x = 0; x < img_out.w; ++x) {
|
357 |
+
const size_t y_in = (img_in.h - 1) - y_out;
|
358 |
+
memcpy(img_out.RowPtr((int)y_out) + (img_out.w - 1 - x) * bytes_per_pixel,
|
359 |
+
img_in.RowPtr((int)y_in) + x * bytes_per_pixel,
|
360 |
+
bytes_per_pixel);
|
361 |
+
}
|
362 |
+
}
|
363 |
+
}
|
364 |
+
}
|
365 |
+
|
366 |
+
void RotateCW(Image<unsigned char>& img_out, const Image<unsigned char>& img_in, size_t bytes_per_pixel)
|
367 |
+
{
|
368 |
+
if(bytes_per_pixel == 1)
|
369 |
+
TiledRotateCW<1, 160>(img_out, img_in);
|
370 |
+
else if(bytes_per_pixel == 2)
|
371 |
+
TiledRotateCW<2, 120>(img_out, img_in);
|
372 |
+
else if(bytes_per_pixel == 3)
|
373 |
+
TiledRotateCW<3, 80>(img_out, img_in);
|
374 |
+
else if(bytes_per_pixel == 4)
|
375 |
+
TiledRotateCW<4, 80>(img_out, img_in);
|
376 |
+
else if(bytes_per_pixel == 6)
|
377 |
+
TiledRotateCW<6, 64>(img_out, img_in);
|
378 |
+
else {
|
379 |
+
for(size_t yout = 0; yout < img_out.h; ++yout)
|
380 |
+
for(size_t xout = 0; xout < img_out.w; ++xout) {
|
381 |
+
size_t xin = yout;
|
382 |
+
size_t yin = img_out.w - 1 - xout;
|
383 |
+
memcpy(img_out.RowPtr((int)yout) + xout * bytes_per_pixel,
|
384 |
+
img_in.RowPtr((int)yin) + xin * bytes_per_pixel,
|
385 |
+
bytes_per_pixel);
|
386 |
+
}
|
387 |
+
}
|
388 |
+
}
|
389 |
+
|
390 |
+
void Transpose(Image<unsigned char>& img_out, const Image<unsigned char>& img_in, size_t bytes_per_pixel)
|
391 |
+
{
|
392 |
+
if(bytes_per_pixel == 1)
|
393 |
+
TiledTranspose<1, 160>(img_out, img_in);
|
394 |
+
else if(bytes_per_pixel == 2)
|
395 |
+
TiledTranspose<2, 120>(img_out, img_in);
|
396 |
+
else if(bytes_per_pixel == 3)
|
397 |
+
TiledTranspose<3, 80>(img_out, img_in);
|
398 |
+
else if(bytes_per_pixel == 4)
|
399 |
+
TiledTranspose<4, 80>(img_out, img_in);
|
400 |
+
else if(bytes_per_pixel == 6)
|
401 |
+
TiledTranspose<6, 64>(img_out, img_in);
|
402 |
+
else {
|
403 |
+
for(size_t yout = 0; yout < img_out.h; ++yout)
|
404 |
+
for(size_t xout = 0; xout < img_out.w; ++xout) {
|
405 |
+
size_t xin = yout;
|
406 |
+
size_t yin = xout;
|
407 |
+
memcpy(img_out.RowPtr((int)yout) + xout * bytes_per_pixel,
|
408 |
+
img_in.RowPtr((int)yin) + xin * bytes_per_pixel,
|
409 |
+
bytes_per_pixel);
|
410 |
+
}
|
411 |
+
}
|
412 |
+
}
|
413 |
+
|
414 |
+
void RotateCCW(Image<unsigned char>& img_out, const Image<unsigned char>& img_in, size_t bytes_per_pixel)
|
415 |
+
{
|
416 |
+
if(bytes_per_pixel == 1)
|
417 |
+
TiledRotateCCW<1, 160>(img_out, img_in);
|
418 |
+
else if(bytes_per_pixel == 2)
|
419 |
+
TiledRotateCCW<2, 120>(img_out, img_in);
|
420 |
+
else if(bytes_per_pixel == 3)
|
421 |
+
TiledRotateCCW<3, 80>(img_out, img_in);
|
422 |
+
else if(bytes_per_pixel == 4)
|
423 |
+
TiledRotateCCW<4, 80>(img_out, img_in);
|
424 |
+
else if(bytes_per_pixel == 6)
|
425 |
+
TiledRotateCCW<6, 64>(img_out, img_in);
|
426 |
+
else {
|
427 |
+
for(size_t yout = 0; yout < img_out.h; ++yout)
|
428 |
+
for(size_t xout = 0; xout < img_out.w; ++xout) {
|
429 |
+
size_t xin = img_out.h - 1 - yout;
|
430 |
+
size_t yin = xout;
|
431 |
+
memcpy(img_out.RowPtr((int)yout) + xout * bytes_per_pixel,
|
432 |
+
img_in.RowPtr((int)yin) + xin * bytes_per_pixel,
|
433 |
+
bytes_per_pixel);
|
434 |
+
}
|
435 |
+
}
|
436 |
+
}
|
437 |
+
|
438 |
+
|
439 |
+
void TransformVideo::Process(unsigned char* buffer_out, const unsigned char* buffer_in)
|
440 |
+
{
|
441 |
+
|
442 |
+
for(size_t s=0; s<streams.size(); ++s) {
|
443 |
+
Image<unsigned char> img_out = Streams()[s].StreamImage(buffer_out);
|
444 |
+
const Image<unsigned char> img_in = videoin->Streams()[s].StreamImage(buffer_in);
|
445 |
+
const size_t bytes_per_pixel = Streams()[s].PixFormat().bpp / 8;
|
446 |
+
|
447 |
+
switch (flips[s]) {
|
448 |
+
case TransformOptions::FlipX:
|
449 |
+
FlipX(img_out, img_in, bytes_per_pixel);
|
450 |
+
break;
|
451 |
+
case TransformOptions::FlipY:
|
452 |
+
FlipY(img_out, img_in, bytes_per_pixel);
|
453 |
+
break;
|
454 |
+
case TransformOptions::FlipXY:
|
455 |
+
FlipXY(img_out, img_in, bytes_per_pixel);
|
456 |
+
break;
|
457 |
+
case TransformOptions::RotateCW:
|
458 |
+
RotateCW(img_out, img_in, bytes_per_pixel);
|
459 |
+
break;
|
460 |
+
case TransformOptions::RotateCCW:
|
461 |
+
RotateCCW(img_out, img_in, bytes_per_pixel);
|
462 |
+
break;
|
463 |
+
case TransformOptions::Transpose:
|
464 |
+
Transpose(img_out, img_in, bytes_per_pixel);
|
465 |
+
break;
|
466 |
+
case TransformOptions::None:
|
467 |
+
PitchedImageCopy(img_out, img_in, bytes_per_pixel);
|
468 |
+
break;
|
469 |
+
default:
|
470 |
+
pango_print_warn("TransformVideo::Process(): Invalid enum %i.\n", int(flips[s]));
|
471 |
+
break;
|
472 |
+
}
|
473 |
+
}
|
474 |
+
|
475 |
+
}
|
476 |
+
|
477 |
+
//! Implement VideoInput::GrabNext()
|
478 |
+
bool TransformVideo::GrabNext( unsigned char* image, bool wait )
|
479 |
+
{
|
480 |
+
if(videoin->GrabNext(buffer,wait)) {
|
481 |
+
Process(image, buffer);
|
482 |
+
return true;
|
483 |
+
}else{
|
484 |
+
return false;
|
485 |
+
}
|
486 |
+
}
|
487 |
+
|
488 |
+
//! Implement VideoInput::GrabNewest()
|
489 |
+
bool TransformVideo::GrabNewest( unsigned char* image, bool wait )
|
490 |
+
{
|
491 |
+
if(videoin->GrabNewest(buffer,wait)) {
|
492 |
+
Process(image, buffer);
|
493 |
+
return true;
|
494 |
+
}else{
|
495 |
+
return false;
|
496 |
+
}
|
497 |
+
}
|
498 |
+
|
499 |
+
std::vector<VideoInterface*>& TransformVideo::InputStreams()
|
500 |
+
{
|
501 |
+
return inputs;
|
502 |
+
}
|
503 |
+
|
504 |
+
unsigned int TransformVideo::AvailableFrames() const
|
505 |
+
{
|
506 |
+
BufferAwareVideoInterface* vpi = dynamic_cast<BufferAwareVideoInterface*>(videoin.get());
|
507 |
+
if(!vpi)
|
508 |
+
{
|
509 |
+
pango_print_warn("Mirror: child interface is not buffer aware.");
|
510 |
+
return 0;
|
511 |
+
}
|
512 |
+
else
|
513 |
+
{
|
514 |
+
return vpi->AvailableFrames();
|
515 |
+
}
|
516 |
+
}
|
517 |
+
|
518 |
+
bool TransformVideo::DropNFrames(uint32_t n)
|
519 |
+
{
|
520 |
+
BufferAwareVideoInterface* vpi = dynamic_cast<BufferAwareVideoInterface*>(videoin.get());
|
521 |
+
if(!vpi)
|
522 |
+
{
|
523 |
+
pango_print_warn("Mirror: child interface is not buffer aware.");
|
524 |
+
return false;
|
525 |
+
}
|
526 |
+
else
|
527 |
+
{
|
528 |
+
return vpi->DropNFrames(n);
|
529 |
+
}
|
530 |
+
}
|
531 |
+
|
532 |
+
const std::map<std::string,TransformOptions> StringToMirrorOptionMap = {
|
533 |
+
{"none", TransformOptions::None},
|
534 |
+
{"transform", TransformOptions::None},
|
535 |
+
{"mirror", TransformOptions::FlipX},
|
536 |
+
{"flipx", TransformOptions::FlipX},
|
537 |
+
{"flip", TransformOptions::FlipY},
|
538 |
+
{"flipy", TransformOptions::FlipY},
|
539 |
+
{"flipxy", TransformOptions::FlipXY},
|
540 |
+
{"transpose", TransformOptions::Transpose},
|
541 |
+
{"rotate", TransformOptions::RotateCW},
|
542 |
+
{"rotatecw", TransformOptions::RotateCW},
|
543 |
+
{"rotateccw", TransformOptions::RotateCCW}
|
544 |
+
};
|
545 |
+
|
546 |
+
std::istream& operator>> (std::istream &is, TransformOptions &mirror)
|
547 |
+
{
|
548 |
+
std::string str_mirror;
|
549 |
+
is >> str_mirror;
|
550 |
+
|
551 |
+
auto default_it = StringToMirrorOptionMap.find(str_mirror);
|
552 |
+
mirror = (default_it != StringToMirrorOptionMap.end()) ?
|
553 |
+
default_it->second : TransformOptions::None;
|
554 |
+
|
555 |
+
return is;
|
556 |
+
}
|
557 |
+
|
558 |
+
PANGOLIN_REGISTER_FACTORY(TransformVideo) {
|
559 |
+
struct TransformVideoFactory final : public TypedFactoryInterface<VideoInterface> {
|
560 |
+
std::map<std::string,Precedence> Schemes() const override {
|
561 |
+
return {
|
562 |
+
{"transform",10}, {"mirror",10}, {"flip",10}, {"rotate",10}, {"transpose",10},
|
563 |
+
{"rotatecw",10}, {"rotateccw",10}, {"flipx",10}, {"flipy",10}, {"flipxy",10}
|
564 |
+
};
|
565 |
+
}
|
566 |
+
const char* Description() const override {
|
567 |
+
return "Filter: Apply one of a number of simple image transforms to the streams.";
|
568 |
+
}
|
569 |
+
ParamSet Params() const override
|
570 |
+
{
|
571 |
+
return {{ {"stream\\d+","none (or scheme name)", "Transform to apply to stream. One of "
|
572 |
+
"(None,FlipX,FlipY,FlipXY,Transpose,RotateCW,RotateCCW)."} }};
|
573 |
+
}
|
574 |
+
std::unique_ptr<VideoInterface> Open(const Uri& uri) override {
|
575 |
+
std::unique_ptr<VideoInterface> subvid = pangolin::OpenVideo(uri.url);
|
576 |
+
auto default_it = StringToMirrorOptionMap.find(uri.scheme);
|
577 |
+
const TransformOptions default_transform = (default_it != StringToMirrorOptionMap.end()) ?
|
578 |
+
default_it->second : TransformOptions::None;
|
579 |
+
|
580 |
+
ParamReader reader(Params(), uri);
|
581 |
+
std::vector<TransformOptions> transforms;
|
582 |
+
|
583 |
+
for(size_t i=0; i < subvid->Streams().size(); ++i){
|
584 |
+
std::stringstream ss;
|
585 |
+
ss << "stream" << i;
|
586 |
+
const std::string key = ss.str();
|
587 |
+
transforms.push_back(reader.Get<TransformOptions>(key, default_transform) );
|
588 |
+
}
|
589 |
+
|
590 |
+
return std::unique_ptr<VideoInterface> (new TransformVideo(subvid, transforms));
|
591 |
+
}
|
592 |
+
};
|
593 |
+
|
594 |
+
return FactoryRegistry::I()->RegisterFactory<VideoInterface>(std::make_shared<TransformVideoFactory>());
|
595 |
+
}
|
596 |
+
|
597 |
+
}
|
third-party/DPVO/Pangolin/components/pango_video/src/drivers/truncate.cpp
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2013 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#include <pangolin/video/drivers/truncate.h>
|
29 |
+
#include <pangolin/factory/factory_registry.h>
|
30 |
+
#include <pangolin/video/iostream_operators.h>
|
31 |
+
#include <pangolin/video/video.h>
|
32 |
+
|
33 |
+
namespace pangolin
|
34 |
+
{
|
35 |
+
|
36 |
+
TruncateVideo::TruncateVideo(std::unique_ptr<VideoInterface> &src_, size_t begin, size_t end)
|
37 |
+
: src(std::move(src_)), streams(src->Streams()), begin(begin), end(end), next_frame_to_grab(0)
|
38 |
+
{
|
39 |
+
videoin.push_back(src.get());
|
40 |
+
|
41 |
+
if(VideoPlaybackInterface* v = GetVideoPlaybackInterface()){
|
42 |
+
// Guard against the obscure case of nested TruncateVideo filters
|
43 |
+
if( !pangolin::FindFirstMatchingVideoInterface<TruncateVideo>(*src) ) {
|
44 |
+
next_frame_to_grab = v->Seek(begin);
|
45 |
+
}
|
46 |
+
}
|
47 |
+
}
|
48 |
+
|
49 |
+
TruncateVideo::~TruncateVideo()
|
50 |
+
{
|
51 |
+
}
|
52 |
+
|
53 |
+
size_t TruncateVideo::SizeBytes() const
|
54 |
+
{
|
55 |
+
return videoin[0]->SizeBytes();
|
56 |
+
}
|
57 |
+
|
58 |
+
const std::vector<StreamInfo>& TruncateVideo::Streams() const
|
59 |
+
{
|
60 |
+
return streams;
|
61 |
+
}
|
62 |
+
|
63 |
+
void TruncateVideo::Start()
|
64 |
+
{
|
65 |
+
videoin[0]->Start();
|
66 |
+
}
|
67 |
+
|
68 |
+
void TruncateVideo::Stop()
|
69 |
+
{
|
70 |
+
videoin[0]->Stop();
|
71 |
+
}
|
72 |
+
|
73 |
+
bool TruncateVideo::GrabNext( unsigned char* image, bool wait )
|
74 |
+
{
|
75 |
+
if(next_frame_to_grab < end) {
|
76 |
+
bool grab_success = videoin[0]->GrabNext(image, wait);
|
77 |
+
return grab_success && (next_frame_to_grab++) >= begin;
|
78 |
+
}
|
79 |
+
return false;
|
80 |
+
}
|
81 |
+
|
82 |
+
bool TruncateVideo::GrabNewest( unsigned char* image, bool wait )
|
83 |
+
{
|
84 |
+
return videoin[0]->GrabNewest(image, wait);
|
85 |
+
}
|
86 |
+
|
87 |
+
std::vector<VideoInterface*>& TruncateVideo::InputStreams()
|
88 |
+
{
|
89 |
+
return videoin;
|
90 |
+
}
|
91 |
+
|
92 |
+
PANGOLIN_REGISTER_FACTORY(TruncateVideo)
|
93 |
+
{
|
94 |
+
struct TruncateVideoFactory : public TypedFactoryInterface<VideoInterface> {
|
95 |
+
std::map<std::string,Precedence> Schemes() const override
|
96 |
+
{
|
97 |
+
return {{"truncate",10}};
|
98 |
+
}
|
99 |
+
const char* Description() const override
|
100 |
+
{
|
101 |
+
return "Truncates the length of a video stream with begin and end markers";
|
102 |
+
}
|
103 |
+
ParamSet Params() const override
|
104 |
+
{
|
105 |
+
return {{
|
106 |
+
{"begin","0","Beginning of the stream"},
|
107 |
+
{"end","size_t::max*","Dynamically set to the max of size_t"},
|
108 |
+
}};
|
109 |
+
}
|
110 |
+
std::unique_ptr<VideoInterface> Open(const Uri& uri) override {
|
111 |
+
std::unique_ptr<VideoInterface> subvid = pangolin::OpenVideo(uri.url);
|
112 |
+
if(subvid->Streams().size() == 0) {
|
113 |
+
throw VideoException("VideoTruncater input must have at least one stream");
|
114 |
+
}
|
115 |
+
|
116 |
+
ParamReader reader(Params(),uri);
|
117 |
+
const size_t start = reader.Get<size_t>("begin");
|
118 |
+
const size_t end = reader.Get<size_t>("end", std::numeric_limits<size_t>::max());
|
119 |
+
|
120 |
+
return std::unique_ptr<VideoInterface>( new TruncateVideo(subvid,start,end) );
|
121 |
+
}
|
122 |
+
};
|
123 |
+
|
124 |
+
return FactoryRegistry::I()->RegisterFactory<VideoInterface>(std::make_shared<TruncateVideoFactory>());
|
125 |
+
}
|
126 |
+
|
127 |
+
}
|
third-party/DPVO/Pangolin/components/pango_video/src/drivers/unpack.cpp
ADDED
@@ -0,0 +1,284 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2014 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#include <pangolin/video/drivers/unpack.h>
|
29 |
+
#include <pangolin/factory/factory_registry.h>
|
30 |
+
#include <pangolin/video/iostream_operators.h>
|
31 |
+
#include <pangolin/video/video.h>
|
32 |
+
|
33 |
+
#ifdef DEBUGUNPACK
|
34 |
+
#include <pangolin/utils/timer.h>
|
35 |
+
#define TSTART() pangolin::basetime start,last,now; start = pangolin::TimeNow(); last = start;
|
36 |
+
#define TGRABANDPRINT(...) now = pangolin::TimeNow(); fprintf(stderr,"UNPACK: "); fprintf(stderr, __VA_ARGS__); fprintf(stderr, " %fms.\n",1000*pangolin::TimeDiff_s(last, now)); last = now;
|
37 |
+
#define DBGPRINT(...) fprintf(stderr,"UNPACK: "); fprintf(stderr, __VA_ARGS__); fprintf(stderr,"\n");
|
38 |
+
#else
|
39 |
+
#define TSTART()
|
40 |
+
#define TGRABANDPRINT(...)
|
41 |
+
#define DBGPRINT(...)
|
42 |
+
#endif
|
43 |
+
|
44 |
+
namespace pangolin
|
45 |
+
{
|
46 |
+
|
47 |
+
UnpackVideo::UnpackVideo(std::unique_ptr<VideoInterface> &src_, PixelFormat out_fmt)
|
48 |
+
: src(std::move(src_)), size_bytes(0), buffer(0)
|
49 |
+
{
|
50 |
+
if( !src || out_fmt.channels != 1) {
|
51 |
+
throw VideoException("UnpackVideo: Only supports single channel output.");
|
52 |
+
}
|
53 |
+
|
54 |
+
videoin.push_back(src.get());
|
55 |
+
|
56 |
+
for(size_t s=0; s< src->Streams().size(); ++s) {
|
57 |
+
const size_t w = src->Streams()[s].Width();
|
58 |
+
const size_t h = src->Streams()[s].Height();
|
59 |
+
|
60 |
+
// Check compatibility of formats
|
61 |
+
const PixelFormat in_fmt = src->Streams()[s].PixFormat();
|
62 |
+
if(in_fmt.channels > 1 || in_fmt.bpp > 16) {
|
63 |
+
throw VideoException("UnpackVideo: Only supports one channel input.");
|
64 |
+
}
|
65 |
+
|
66 |
+
const size_t pitch = (w*out_fmt.bpp)/ 8;
|
67 |
+
streams.push_back(pangolin::StreamInfo( out_fmt, w, h, pitch, reinterpret_cast<uint8_t*>(size_bytes) ));
|
68 |
+
size_bytes += h*pitch;
|
69 |
+
}
|
70 |
+
|
71 |
+
buffer = new unsigned char[src->SizeBytes()];
|
72 |
+
}
|
73 |
+
|
74 |
+
UnpackVideo::~UnpackVideo()
|
75 |
+
{
|
76 |
+
delete[] buffer;
|
77 |
+
}
|
78 |
+
|
79 |
+
//! Implement VideoInput::Start()
|
80 |
+
void UnpackVideo::Start()
|
81 |
+
{
|
82 |
+
videoin[0]->Start();
|
83 |
+
}
|
84 |
+
|
85 |
+
//! Implement VideoInput::Stop()
|
86 |
+
void UnpackVideo::Stop()
|
87 |
+
{
|
88 |
+
videoin[0]->Stop();
|
89 |
+
}
|
90 |
+
|
91 |
+
//! Implement VideoInput::SizeBytes()
|
92 |
+
size_t UnpackVideo::SizeBytes() const
|
93 |
+
{
|
94 |
+
return size_bytes;
|
95 |
+
}
|
96 |
+
|
97 |
+
//! Implement VideoInput::Streams()
|
98 |
+
const std::vector<StreamInfo>& UnpackVideo::Streams() const
|
99 |
+
{
|
100 |
+
return streams;
|
101 |
+
}
|
102 |
+
|
103 |
+
template<typename T>
|
104 |
+
void ConvertFrom8bit(
|
105 |
+
Image<unsigned char>& out,
|
106 |
+
const Image<unsigned char>& in
|
107 |
+
) {
|
108 |
+
for(size_t r=0; r<out.h; ++r) {
|
109 |
+
T* pout = (T*)(out.ptr + r*out.pitch);
|
110 |
+
uint8_t* pin = in.ptr + r*in.pitch;
|
111 |
+
const uint8_t* pin_end = in.ptr + (r+1)*in.pitch;
|
112 |
+
while(pin != pin_end) {
|
113 |
+
*(pout++) = *(pin++);
|
114 |
+
}
|
115 |
+
}
|
116 |
+
}
|
117 |
+
|
118 |
+
template<typename T>
|
119 |
+
void ConvertFrom10bit(
|
120 |
+
Image<unsigned char>& out,
|
121 |
+
const Image<unsigned char>& in
|
122 |
+
) {
|
123 |
+
for(size_t r=0; r<out.h; ++r) {
|
124 |
+
T* pout = (T*)(out.ptr + r*out.pitch);
|
125 |
+
uint8_t* pin = in.ptr + r*in.pitch;
|
126 |
+
const uint8_t* pin_end = in.ptr + (r+1)*in.pitch;
|
127 |
+
while(pin != pin_end) {
|
128 |
+
uint64_t val = *(pin++);
|
129 |
+
val |= uint64_t(*(pin++)) << 8;
|
130 |
+
val |= uint64_t(*(pin++)) << 16;
|
131 |
+
val |= uint64_t(*(pin++)) << 24;
|
132 |
+
val |= uint64_t(*(pin++)) << 32;
|
133 |
+
*(pout++) = T( val & 0x00000003FF);
|
134 |
+
*(pout++) = T((val & 0x00000FFC00) >> 10);
|
135 |
+
*(pout++) = T((val & 0x003FF00000) >> 20);
|
136 |
+
*(pout++) = T((val & 0xFFC0000000) >> 30);
|
137 |
+
}
|
138 |
+
}
|
139 |
+
}
|
140 |
+
|
141 |
+
template<typename T>
|
142 |
+
void ConvertFrom12bit(
|
143 |
+
Image<unsigned char>& out,
|
144 |
+
const Image<unsigned char>& in
|
145 |
+
) {
|
146 |
+
for(size_t r=0; r<out.h; ++r) {
|
147 |
+
T* pout = (T*)(out.ptr + r*out.pitch);
|
148 |
+
uint8_t* pin = in.ptr + r*in.pitch;
|
149 |
+
const uint8_t* pin_end = in.ptr + (r+1)*in.pitch;
|
150 |
+
while(pin != pin_end) {
|
151 |
+
uint32_t val = *(pin++);
|
152 |
+
val |= uint32_t(*(pin++)) << 8;
|
153 |
+
val |= uint32_t(*(pin++)) << 16;
|
154 |
+
*(pout++) = T( val & 0x000FFF);
|
155 |
+
*(pout++) = T((val & 0xFFF000) >> 12);
|
156 |
+
}
|
157 |
+
}
|
158 |
+
}
|
159 |
+
|
160 |
+
void UnpackVideo::Process(unsigned char* image, const unsigned char* buffer)
|
161 |
+
{
|
162 |
+
TSTART()
|
163 |
+
for(size_t s=0; s<streams.size(); ++s) {
|
164 |
+
const Image<unsigned char> img_in = videoin[0]->Streams()[s].StreamImage(buffer);
|
165 |
+
Image<unsigned char> img_out = Streams()[s].StreamImage(image);
|
166 |
+
|
167 |
+
const int bits_in = videoin[0]->Streams()[s].PixFormat().bpp;
|
168 |
+
|
169 |
+
if(Streams()[s].PixFormat().format == "GRAY32F") {
|
170 |
+
if( bits_in == 8) {
|
171 |
+
ConvertFrom8bit<float>(img_out, img_in);
|
172 |
+
}else if( bits_in == 10) {
|
173 |
+
ConvertFrom10bit<float>(img_out, img_in);
|
174 |
+
}else if( bits_in == 12){
|
175 |
+
ConvertFrom12bit<float>(img_out, img_in);
|
176 |
+
}else{
|
177 |
+
throw pangolin::VideoException("Unsupported bitdepths.");
|
178 |
+
}
|
179 |
+
}else if(Streams()[s].PixFormat().format == "GRAY16LE") {
|
180 |
+
if( bits_in == 8) {
|
181 |
+
ConvertFrom8bit<uint16_t>(img_out, img_in);
|
182 |
+
}else if( bits_in == 10) {
|
183 |
+
ConvertFrom10bit<uint16_t>(img_out, img_in);
|
184 |
+
}else if( bits_in == 12){
|
185 |
+
ConvertFrom12bit<uint16_t>(img_out, img_in);
|
186 |
+
}else{
|
187 |
+
throw pangolin::VideoException("Unsupported bitdepths.");
|
188 |
+
}
|
189 |
+
}else{
|
190 |
+
}
|
191 |
+
}
|
192 |
+
TGRABANDPRINT("Unpacking took ")
|
193 |
+
}
|
194 |
+
|
195 |
+
//! Implement VideoInput::GrabNext()
|
196 |
+
bool UnpackVideo::GrabNext( unsigned char* image, bool wait )
|
197 |
+
{
|
198 |
+
if(videoin[0]->GrabNext(buffer,wait)) {
|
199 |
+
Process(image,buffer);
|
200 |
+
return true;
|
201 |
+
}else{
|
202 |
+
return false;
|
203 |
+
}
|
204 |
+
}
|
205 |
+
|
206 |
+
//! Implement VideoInput::GrabNewest()
|
207 |
+
bool UnpackVideo::GrabNewest( unsigned char* image, bool wait )
|
208 |
+
{
|
209 |
+
if(videoin[0]->GrabNewest(buffer,wait)) {
|
210 |
+
Process(image,buffer);
|
211 |
+
return true;
|
212 |
+
}else{
|
213 |
+
return false;
|
214 |
+
}
|
215 |
+
}
|
216 |
+
|
217 |
+
std::vector<VideoInterface*>& UnpackVideo::InputStreams()
|
218 |
+
{
|
219 |
+
return videoin;
|
220 |
+
}
|
221 |
+
|
222 |
+
unsigned int UnpackVideo::AvailableFrames() const
|
223 |
+
{
|
224 |
+
BufferAwareVideoInterface* vpi = dynamic_cast<BufferAwareVideoInterface*>(videoin[0]);
|
225 |
+
if(!vpi)
|
226 |
+
{
|
227 |
+
pango_print_warn("Unpack: child interface is not buffer aware.");
|
228 |
+
return 0;
|
229 |
+
}
|
230 |
+
else
|
231 |
+
{
|
232 |
+
return vpi->AvailableFrames();
|
233 |
+
}
|
234 |
+
}
|
235 |
+
|
236 |
+
bool UnpackVideo::DropNFrames(uint32_t n)
|
237 |
+
{
|
238 |
+
BufferAwareVideoInterface* vpi = dynamic_cast<BufferAwareVideoInterface*>(videoin[0]);
|
239 |
+
if(!vpi)
|
240 |
+
{
|
241 |
+
pango_print_warn("Unpack: child interface is not buffer aware.");
|
242 |
+
return false;
|
243 |
+
}
|
244 |
+
else
|
245 |
+
{
|
246 |
+
return vpi->DropNFrames(n);
|
247 |
+
}
|
248 |
+
}
|
249 |
+
|
250 |
+
PANGOLIN_REGISTER_FACTORY(UnpackVideo)
|
251 |
+
{
|
252 |
+
struct UnpackVideoFactory final : public TypedFactoryInterface<VideoInterface> {
|
253 |
+
std::map<std::string,Precedence> Schemes() const override
|
254 |
+
{
|
255 |
+
return {{"unpack",10}};
|
256 |
+
}
|
257 |
+
const char* Description() const override
|
258 |
+
{
|
259 |
+
return "Converts from a potentially packed pixel format to a higher bit-depth format (e.g. 10 to 16 bit).";
|
260 |
+
}
|
261 |
+
ParamSet Params() const override
|
262 |
+
{
|
263 |
+
return {{
|
264 |
+
{"fmt","GRAY16LE","Destination pixel format."}
|
265 |
+
}};
|
266 |
+
}
|
267 |
+
std::unique_ptr<VideoInterface> Open(const Uri& uri) override {
|
268 |
+
ParamReader reader(Params(),uri);
|
269 |
+
std::unique_ptr<VideoInterface> subvid = pangolin::OpenVideo(uri.url);
|
270 |
+
const std::string fmt = reader.Get("fmt", std::string("GRAY16LE") );
|
271 |
+
return std::unique_ptr<VideoInterface>(
|
272 |
+
new UnpackVideo(subvid, PixelFormatFromString(fmt) )
|
273 |
+
);
|
274 |
+
}
|
275 |
+
};
|
276 |
+
|
277 |
+
return FactoryRegistry::I()->RegisterFactory<VideoInterface>(std::make_shared<UnpackVideoFactory>());
|
278 |
+
}
|
279 |
+
|
280 |
+
}
|
281 |
+
|
282 |
+
#undef TSTART
|
283 |
+
#undef TGRABANDPRINT
|
284 |
+
#undef DBGPRINT
|
third-party/DPVO/Pangolin/components/pango_video/src/drivers/uvc.cpp
ADDED
@@ -0,0 +1,382 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2011 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#include <pangolin/factory/factory_registry.h>
|
29 |
+
#include <pangolin/video/drivers/uvc.h>
|
30 |
+
#include <pangolin/video/iostream_operators.h>
|
31 |
+
|
32 |
+
namespace pangolin
|
33 |
+
{
|
34 |
+
|
35 |
+
UvcVideo::UvcVideo(int vendor_id, int product_id, const char* sn, int device_id, int width, int height, int fps)
|
36 |
+
: ctx_(NULL),
|
37 |
+
dev_(NULL),
|
38 |
+
devh_(NULL),
|
39 |
+
frame_(NULL),
|
40 |
+
is_streaming(false)
|
41 |
+
{
|
42 |
+
uvc_init(&ctx_, NULL);
|
43 |
+
if(!ctx_) {
|
44 |
+
throw VideoException("Unable to open UVC Context");
|
45 |
+
}
|
46 |
+
|
47 |
+
InitDevice(vendor_id, product_id, sn, device_id, width, height, fps);
|
48 |
+
InitPangoDeviceProperties();
|
49 |
+
|
50 |
+
// FIX: CRASHING IF WE DON'T START STREAMING STRAIGHT AWAY
|
51 |
+
|
52 |
+
Start();
|
53 |
+
}
|
54 |
+
|
55 |
+
UvcVideo::~UvcVideo()
|
56 |
+
{
|
57 |
+
DeinitDevice();
|
58 |
+
|
59 |
+
if(devh_) uvc_close(devh_);
|
60 |
+
if(dev_) uvc_unref_device(dev_);
|
61 |
+
|
62 |
+
if (ctx_) {
|
63 |
+
uvc_exit(ctx_);
|
64 |
+
ctx_ = 0;
|
65 |
+
}
|
66 |
+
}
|
67 |
+
|
68 |
+
uvc_error_t UvcVideo::FindDevice(
|
69 |
+
uvc_context_t *ctx, uvc_device_t **dev,
|
70 |
+
int vid, int pid, const char *sn, int device_id) {
|
71 |
+
uvc_error_t ret = UVC_SUCCESS;
|
72 |
+
|
73 |
+
uvc_device_t **list;
|
74 |
+
uvc_device_t *test_dev;
|
75 |
+
|
76 |
+
ret = uvc_get_device_list(ctx, &list);
|
77 |
+
|
78 |
+
int cnt = 0;
|
79 |
+
while(list[cnt++]!=NULL);
|
80 |
+
pango_print_info("UVC Descriptor list contains %d devices.\n", (cnt-1));
|
81 |
+
|
82 |
+
if (ret != UVC_SUCCESS) {
|
83 |
+
return ret;
|
84 |
+
}
|
85 |
+
|
86 |
+
int dev_idx = 0;
|
87 |
+
int num_found = 0;
|
88 |
+
bool found_dev = false;
|
89 |
+
|
90 |
+
while (!found_dev && (test_dev = list[dev_idx++]) != NULL) {
|
91 |
+
uvc_device_descriptor_t *desc;
|
92 |
+
|
93 |
+
if (uvc_get_device_descriptor(test_dev, &desc) != UVC_SUCCESS)
|
94 |
+
continue;
|
95 |
+
|
96 |
+
|
97 |
+
const bool matches = (!vid || desc->idVendor == vid)
|
98 |
+
&& (!pid || desc->idProduct == pid)
|
99 |
+
&& (!sn || (desc->serialNumber && !strcmp(desc->serialNumber, sn)));
|
100 |
+
|
101 |
+
uvc_free_device_descriptor(desc);
|
102 |
+
|
103 |
+
if (matches) {
|
104 |
+
if(device_id == num_found) {
|
105 |
+
found_dev = true;
|
106 |
+
break;
|
107 |
+
}
|
108 |
+
num_found++;
|
109 |
+
}
|
110 |
+
}
|
111 |
+
|
112 |
+
if (found_dev)
|
113 |
+
uvc_ref_device(test_dev);
|
114 |
+
|
115 |
+
uvc_free_device_list(list, 1);
|
116 |
+
|
117 |
+
if (found_dev) {
|
118 |
+
*dev = test_dev;
|
119 |
+
return UVC_SUCCESS;
|
120 |
+
} else {
|
121 |
+
return UVC_ERROR_NO_DEVICE;
|
122 |
+
}
|
123 |
+
}
|
124 |
+
|
125 |
+
void UvcVideo::InitDevice(int vid, int pid, const char* sn, int device_id, int width, int height, int fps)
|
126 |
+
{
|
127 |
+
uvc_error_t find_err = FindDevice(ctx_, &dev_, vid, pid, sn, device_id );
|
128 |
+
if (find_err != UVC_SUCCESS) {
|
129 |
+
uvc_perror(find_err, "uvc_find_device");
|
130 |
+
throw VideoException("Unable to open UVC Device");
|
131 |
+
}
|
132 |
+
if(!dev_) {
|
133 |
+
throw VideoException("Unable to open UVC Device - no pointer returned.");
|
134 |
+
}
|
135 |
+
|
136 |
+
uvc_error_t open_err = uvc_open(dev_, &devh_);
|
137 |
+
if (open_err != UVC_SUCCESS) {
|
138 |
+
uvc_perror(open_err, "uvc_open");
|
139 |
+
uvc_unref_device(dev_);
|
140 |
+
throw VideoException("Unable to open device");
|
141 |
+
}
|
142 |
+
|
143 |
+
//uvc_print_diag(devh_, stderr);
|
144 |
+
|
145 |
+
uvc_error_t mode_err = uvc_get_stream_ctrl_format_size(
|
146 |
+
devh_, &ctrl_,
|
147 |
+
UVC_FRAME_FORMAT_ANY,
|
148 |
+
width, height,
|
149 |
+
fps);
|
150 |
+
|
151 |
+
//uvc_print_stream_ctrl(&ctrl_, stderr);
|
152 |
+
|
153 |
+
if (mode_err != UVC_SUCCESS) {
|
154 |
+
uvc_perror(mode_err, "uvc_get_stream_ctrl_format_size");
|
155 |
+
uvc_close(devh_);
|
156 |
+
uvc_unref_device(dev_);
|
157 |
+
throw VideoException("Unable to set device mode.");
|
158 |
+
}
|
159 |
+
|
160 |
+
uvc_error_t strm_err = uvc_stream_open_ctrl(devh_, &strm_, &ctrl_);
|
161 |
+
if(strm_err != UVC_SUCCESS) {
|
162 |
+
uvc_perror(strm_err, "uvc_stream_open_ctrl");
|
163 |
+
uvc_close(devh_);
|
164 |
+
uvc_unref_device(dev_);
|
165 |
+
throw VideoException("Unable to open device stream.");
|
166 |
+
}
|
167 |
+
|
168 |
+
// Default to greyscale.
|
169 |
+
PixelFormat pfmt = PixelFormatFromString("GRAY8");
|
170 |
+
|
171 |
+
const uvc_format_desc_t* uvc_fmt = uvc_get_format_descs(devh_);
|
172 |
+
while( uvc_fmt->bFormatIndex != ctrl_.bFormatIndex && uvc_fmt ) {
|
173 |
+
uvc_fmt = uvc_fmt->next;
|
174 |
+
}
|
175 |
+
|
176 |
+
if(uvc_fmt) {
|
177 |
+
// TODO: Use uvc_fmt->fourccFormat
|
178 |
+
if( uvc_fmt->bBitsPerPixel == 16 ) {
|
179 |
+
pfmt = PixelFormatFromString("GRAY16LE");
|
180 |
+
}
|
181 |
+
}
|
182 |
+
|
183 |
+
const StreamInfo stream_info(pfmt, width, height, (width*pfmt.bpp)/8, 0);
|
184 |
+
streams.push_back(stream_info);
|
185 |
+
}
|
186 |
+
|
187 |
+
void UvcVideo::InitPangoDeviceProperties()
|
188 |
+
{
|
189 |
+
// Store camera details in device properties
|
190 |
+
device_properties["BusNumber"] = std::to_string(uvc_get_bus_number(dev_));
|
191 |
+
device_properties["DeviceAddress"] = std::to_string(uvc_get_device_address(dev_));
|
192 |
+
device_properties[PANGO_HAS_TIMING_DATA] = true;
|
193 |
+
}
|
194 |
+
|
195 |
+
void UvcVideo::DeinitDevice()
|
196 |
+
{
|
197 |
+
Stop();
|
198 |
+
|
199 |
+
if (frame_) {
|
200 |
+
uvc_free_frame(frame_);
|
201 |
+
frame_ = 0;
|
202 |
+
}
|
203 |
+
}
|
204 |
+
|
205 |
+
void UvcVideo::Start()
|
206 |
+
{
|
207 |
+
if(!is_streaming) {
|
208 |
+
uvc_error_t stream_err = uvc_stream_start(strm_, NULL, this, 0);
|
209 |
+
|
210 |
+
if (stream_err != UVC_SUCCESS) {
|
211 |
+
uvc_perror(stream_err, "uvc_stream_start");
|
212 |
+
uvc_close(devh_);
|
213 |
+
uvc_unref_device(dev_);
|
214 |
+
throw VideoException("Unable to start streaming.");
|
215 |
+
}else{
|
216 |
+
is_streaming = true;
|
217 |
+
}
|
218 |
+
|
219 |
+
if (frame_) {
|
220 |
+
uvc_free_frame(frame_);
|
221 |
+
}
|
222 |
+
|
223 |
+
size_bytes = ctrl_.dwMaxVideoFrameSize;
|
224 |
+
frame_ = uvc_allocate_frame(size_bytes);
|
225 |
+
if(!frame_) {
|
226 |
+
throw VideoException("Unable to allocate frame.");
|
227 |
+
}
|
228 |
+
}
|
229 |
+
}
|
230 |
+
|
231 |
+
void UvcVideo::Stop()
|
232 |
+
{
|
233 |
+
if(is_streaming && devh_) {
|
234 |
+
uvc_stop_streaming(devh_);
|
235 |
+
}
|
236 |
+
is_streaming = false;
|
237 |
+
}
|
238 |
+
|
239 |
+
size_t UvcVideo::SizeBytes() const
|
240 |
+
{
|
241 |
+
return size_bytes;
|
242 |
+
}
|
243 |
+
|
244 |
+
const std::vector<StreamInfo>& UvcVideo::Streams() const
|
245 |
+
{
|
246 |
+
return streams;
|
247 |
+
}
|
248 |
+
|
249 |
+
bool UvcVideo::GrabNext( unsigned char* image, bool wait )
|
250 |
+
{
|
251 |
+
uvc_frame_t* frame = NULL;
|
252 |
+
uvc_error_t err = uvc_stream_get_frame(strm_, &frame, wait ? 0 : -1);
|
253 |
+
|
254 |
+
if(err!= UVC_SUCCESS) {
|
255 |
+
pango_print_error("UvcVideo Error: %s", uvc_strerror(err) );
|
256 |
+
return false;
|
257 |
+
}else{
|
258 |
+
if(frame) {
|
259 |
+
memcpy(image, frame->data, frame->data_bytes );
|
260 |
+
// This is a hack, this ts sould come from the device.
|
261 |
+
frame_properties[PANGO_HOST_RECEPTION_TIME_US] = picojson::value(pangolin::Time_us(pangolin::TimeNow()));
|
262 |
+
return true;
|
263 |
+
}else{
|
264 |
+
if(wait) {
|
265 |
+
pango_print_debug("UvcVideo: No frame data");
|
266 |
+
}
|
267 |
+
return false;
|
268 |
+
}
|
269 |
+
}
|
270 |
+
}
|
271 |
+
|
272 |
+
bool UvcVideo::GrabNewest( unsigned char* image, bool wait )
|
273 |
+
{
|
274 |
+
return GrabNext(image, wait);
|
275 |
+
}
|
276 |
+
|
277 |
+
int UvcVideo::IoCtrl(uint8_t unit, uint8_t ctrl, unsigned char* data, int len, UvcRequestCode req_code)
|
278 |
+
{
|
279 |
+
if(req_code == UVC_SET_CUR) {
|
280 |
+
return uvc_set_ctrl(devh_, unit, ctrl, data, len);
|
281 |
+
}else{
|
282 |
+
return uvc_get_ctrl(devh_, unit, ctrl, data, len, (uvc_req_code)req_code);
|
283 |
+
}
|
284 |
+
}
|
285 |
+
|
286 |
+
bool UvcVideo::SetExposure(int exp_us)
|
287 |
+
{
|
288 |
+
uint32_t e = uint32_t(exp_us);
|
289 |
+
|
290 |
+
if (uvc_set_exposure_abs(devh_, e) < 0) {
|
291 |
+
pango_print_warn("UvcVideo::setExposure() ioctl error: %s\n", strerror(errno));
|
292 |
+
return false;
|
293 |
+
} else {
|
294 |
+
return true;
|
295 |
+
}
|
296 |
+
}
|
297 |
+
|
298 |
+
bool UvcVideo::GetExposure(int& exp_us)
|
299 |
+
{
|
300 |
+
uint32_t e;
|
301 |
+
if (uvc_get_exposure_abs(devh_, &e, uvc_req_code::UVC_GET_CUR) < 0) {
|
302 |
+
pango_print_warn("UvcVideo::GetExposure() ioctl error: %s\n", strerror(errno));
|
303 |
+
return false;
|
304 |
+
} else {
|
305 |
+
exp_us = e;
|
306 |
+
return true;
|
307 |
+
}
|
308 |
+
}
|
309 |
+
|
310 |
+
bool UvcVideo::SetGain(float gain)
|
311 |
+
{
|
312 |
+
uint16_t g = uint16_t(gain);
|
313 |
+
|
314 |
+
if (uvc_set_gain(devh_, g) < 0) {
|
315 |
+
pango_print_warn("UvcVideo::setGain() ioctl error: %s\n", strerror(errno));
|
316 |
+
return false;
|
317 |
+
} else {
|
318 |
+
return true;
|
319 |
+
}
|
320 |
+
}
|
321 |
+
|
322 |
+
bool UvcVideo::GetGain(float& gain)
|
323 |
+
{
|
324 |
+
uint16_t g;
|
325 |
+
if (uvc_get_gain(devh_, &g, uvc_req_code::UVC_GET_CUR) < 0) {
|
326 |
+
pango_print_warn("UvcVideo::GetGain() ioctl error: %s\n", strerror(errno));
|
327 |
+
return false;
|
328 |
+
} else {
|
329 |
+
gain = g;
|
330 |
+
return true;
|
331 |
+
}
|
332 |
+
}
|
333 |
+
|
334 |
+
//! Access JSON properties of device
|
335 |
+
const picojson::value& UvcVideo::DeviceProperties() const
|
336 |
+
{
|
337 |
+
return device_properties;
|
338 |
+
}
|
339 |
+
|
340 |
+
//! Access JSON properties of most recently captured frame
|
341 |
+
const picojson::value& UvcVideo::FrameProperties() const
|
342 |
+
{
|
343 |
+
return frame_properties;
|
344 |
+
}
|
345 |
+
|
346 |
+
PANGOLIN_REGISTER_FACTORY(UvcVideo)
|
347 |
+
{
|
348 |
+
struct UvcVideoFactory final : public TypedFactoryInterface<VideoInterface> {
|
349 |
+
std::map<std::string,Precedence> Schemes() const override
|
350 |
+
{
|
351 |
+
return {{"uvc",10}};
|
352 |
+
}
|
353 |
+
const char* Description() const override
|
354 |
+
{
|
355 |
+
return "Use libuvc to open UVC USB device";
|
356 |
+
}
|
357 |
+
ParamSet Params() const override
|
358 |
+
{
|
359 |
+
return {{
|
360 |
+
{"size","640x480","Image dimension"},
|
361 |
+
{"fps","0","Frames per second (0:unspecified)"},
|
362 |
+
{"num","0","Open the nth device (no need for vid and pid)"},
|
363 |
+
{"vid","0x0000","Open based on USB VID and PID"},
|
364 |
+
{"pid","0x0000","Open based on USB VID and PID"}
|
365 |
+
}};
|
366 |
+
}
|
367 |
+
std::unique_ptr<VideoInterface> Open(const Uri& uri) override {
|
368 |
+
int vid = 0;
|
369 |
+
int pid = 0;
|
370 |
+
std::istringstream(uri.Get<std::string>("vid","0x0000")) >> std::hex >> vid;
|
371 |
+
std::istringstream(uri.Get<std::string>("pid","0x0000")) >> std::hex >> pid;
|
372 |
+
const unsigned int dev_id = uri.Get<int>("num",0);
|
373 |
+
const ImageDim dim = uri.Get<ImageDim>("size", ImageDim(640,480));
|
374 |
+
const unsigned int fps = uri.Get<unsigned int>("fps", 0); // 0 means unspecified.
|
375 |
+
return std::unique_ptr<VideoInterface>( new UvcVideo(vid,pid,0,dev_id,dim.x,dim.y,fps) );
|
376 |
+
}
|
377 |
+
};
|
378 |
+
|
379 |
+
return FactoryRegistry::I()->RegisterFactory<VideoInterface>(std::make_shared<UvcVideoFactory>());
|
380 |
+
}
|
381 |
+
|
382 |
+
}
|
third-party/DPVO/Pangolin/components/pango_video/src/drivers/uvc_mediafoundation.cpp
ADDED
@@ -0,0 +1,1091 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#ifndef NOMINMAX
|
2 |
+
# define NOMINMAX
|
3 |
+
#endif
|
4 |
+
|
5 |
+
#include <mfapi.h>
|
6 |
+
#include <mferror.h>
|
7 |
+
#include <mfidl.h>
|
8 |
+
#include <mfreadwrite.h>
|
9 |
+
|
10 |
+
#include <dshow.h>
|
11 |
+
#include <ks.h>
|
12 |
+
#include <ksmedia.h>
|
13 |
+
#include <ksproxy.h>
|
14 |
+
#include <vidcap.h>
|
15 |
+
|
16 |
+
#include <pangolin/factory/factory_registry.h>
|
17 |
+
#include <pangolin/utils/timer.h>
|
18 |
+
#include <pangolin/video/drivers/uvc_mediafoundation.h>
|
19 |
+
#include <pangolin/video/iostream_operators.h>
|
20 |
+
|
21 |
+
#include <condition_variable>
|
22 |
+
#include <mutex>
|
23 |
+
|
24 |
+
namespace pangolin
|
25 |
+
{
|
26 |
+
|
27 |
+
static constexpr DWORD KS_CONTROL_NODE_ID_INVALID = ~0;
|
28 |
+
|
29 |
+
const GUID GUID_EXTENSION_UNIT_DESCRIPTOR_OV580{
|
30 |
+
0x2ccb0bda, 0x6331, 0x4fdb, 0x85, 0x0e, 0x79, 0x05, 0x4d, 0xbd, 0x56, 0x71
|
31 |
+
};
|
32 |
+
|
33 |
+
class AsyncSourceReader : public IMFSourceReaderCallback
|
34 |
+
{
|
35 |
+
public:
|
36 |
+
AsyncSourceReader(IMFMediaSource* mediaSource, uint64_t timeout_ms) : ref_(1), timeout_(std::chrono::milliseconds(timeout_ms))
|
37 |
+
{
|
38 |
+
HRESULT hr;
|
39 |
+
|
40 |
+
IMFAttributes *pAttributes;
|
41 |
+
hr = MFCreateAttributes(&pAttributes, 1);
|
42 |
+
if (FAILED(hr))
|
43 |
+
{
|
44 |
+
throw VideoException(FormatString("Unable to initialize attributes for source reader (%)", hr));
|
45 |
+
}
|
46 |
+
|
47 |
+
hr = pAttributes->SetUnknown(MF_SOURCE_READER_ASYNC_CALLBACK, this);
|
48 |
+
if (FAILED(hr))
|
49 |
+
{
|
50 |
+
throw VideoException(FormatString("Unable to set async attribute for source reader (%)", hr));
|
51 |
+
}
|
52 |
+
|
53 |
+
hr = MFCreateSourceReaderFromMediaSource(mediaSource, pAttributes, &reader_);
|
54 |
+
if(FAILED(hr))
|
55 |
+
{
|
56 |
+
throw VideoException(FormatString("Unable to create source reader from UVC media source (%)", hr));
|
57 |
+
}
|
58 |
+
}
|
59 |
+
|
60 |
+
// IUnknown methods
|
61 |
+
STDMETHODIMP QueryInterface(REFIID iid, void** ppv)
|
62 |
+
{
|
63 |
+
if (iid != __uuidof(IMFSourceReaderCallback)) {
|
64 |
+
return E_NOINTERFACE;
|
65 |
+
}
|
66 |
+
InterlockedIncrement(&ref_);
|
67 |
+
*ppv = this;
|
68 |
+
return S_OK;
|
69 |
+
}
|
70 |
+
STDMETHODIMP_(ULONG) AddRef()
|
71 |
+
{
|
72 |
+
return InterlockedIncrement(&ref_);
|
73 |
+
}
|
74 |
+
STDMETHODIMP_(ULONG) Release()
|
75 |
+
{
|
76 |
+
ULONG snapped = InterlockedDecrement(&ref_);
|
77 |
+
if (snapped == 0)
|
78 |
+
{
|
79 |
+
delete this;
|
80 |
+
}
|
81 |
+
return snapped;
|
82 |
+
}
|
83 |
+
|
84 |
+
// IMFSourceReaderCallback methods
|
85 |
+
STDMETHODIMP OnReadSample(HRESULT hrStatus, DWORD dwStreamIndex, DWORD dwStreamFlags, LONGLONG llTimestamp, IMFSample *pSample)
|
86 |
+
{
|
87 |
+
{
|
88 |
+
std::lock_guard<std::mutex> lock(sample_lock_);
|
89 |
+
|
90 |
+
sample_hr_ = hrStatus;
|
91 |
+
sample_actual_stream_index_ = dwStreamIndex;
|
92 |
+
sample_stream_flags_ = dwStreamFlags;
|
93 |
+
sample_timestamp_ = llTimestamp;
|
94 |
+
sample_ = pSample;
|
95 |
+
|
96 |
+
if (sample_ != nullptr)
|
97 |
+
{
|
98 |
+
sample_->AddRef();
|
99 |
+
}
|
100 |
+
}
|
101 |
+
|
102 |
+
sample_ready_.notify_one();
|
103 |
+
|
104 |
+
return S_OK;
|
105 |
+
}
|
106 |
+
|
107 |
+
STDMETHODIMP OnEvent(DWORD, IMFMediaEvent *)
|
108 |
+
{
|
109 |
+
return S_OK;
|
110 |
+
}
|
111 |
+
|
112 |
+
STDMETHODIMP OnFlush(DWORD)
|
113 |
+
{
|
114 |
+
return S_OK;
|
115 |
+
}
|
116 |
+
|
117 |
+
IMFSourceReader* AddRefReader()
|
118 |
+
{
|
119 |
+
reader_->AddRef();
|
120 |
+
return reader_;
|
121 |
+
}
|
122 |
+
|
123 |
+
HRESULT ReadSample(DWORD dwStreamIndex, DWORD dwControlFlags, DWORD* pdwActualStreamIndex, DWORD* pdwStreamFlags, LONGLONG* pllTimestamp, IMFSample** ppSample)
|
124 |
+
{
|
125 |
+
auto wait_target = std::chrono::steady_clock::now() + timeout_;
|
126 |
+
|
127 |
+
std::unique_lock<std::mutex> lock(sample_lock_);
|
128 |
+
|
129 |
+
// If there's already a pending read, just piggyback on it and wait. Otherwise, issue now.
|
130 |
+
if (sample_hr_ != E_PENDING)
|
131 |
+
{
|
132 |
+
HRESULT sync_hr = reader_->ReadSample(dwStreamIndex, dwControlFlags, NULL, NULL, NULL, NULL);
|
133 |
+
if (FAILED(sync_hr))
|
134 |
+
{
|
135 |
+
return sync_hr;
|
136 |
+
}
|
137 |
+
|
138 |
+
sample_hr_ = E_PENDING;
|
139 |
+
}
|
140 |
+
|
141 |
+
while (sample_hr_ == E_PENDING)
|
142 |
+
{
|
143 |
+
if (sample_ready_.wait_until(lock, wait_target) == std::cv_status::timeout)
|
144 |
+
{
|
145 |
+
// Leave sample_hr_ as E_PENDING since there's still an oustanding async call.
|
146 |
+
// It may be set later by the callback.
|
147 |
+
return RPC_E_TIMEOUT;
|
148 |
+
}
|
149 |
+
}
|
150 |
+
|
151 |
+
if (SUCCEEDED(sample_hr_))
|
152 |
+
{
|
153 |
+
*pdwActualStreamIndex = sample_actual_stream_index_;
|
154 |
+
*pdwStreamFlags = sample_stream_flags_;
|
155 |
+
*pllTimestamp = sample_timestamp_;
|
156 |
+
|
157 |
+
*ppSample = sample_;
|
158 |
+
sample_ = nullptr;
|
159 |
+
}
|
160 |
+
|
161 |
+
return sample_hr_;
|
162 |
+
}
|
163 |
+
|
164 |
+
private:
|
165 |
+
// Destructor is private. Caller should call Release.
|
166 |
+
virtual ~AsyncSourceReader()
|
167 |
+
{
|
168 |
+
if (reader_ != nullptr)
|
169 |
+
{
|
170 |
+
reader_->Release();
|
171 |
+
}
|
172 |
+
|
173 |
+
if (sample_ != nullptr)
|
174 |
+
{
|
175 |
+
sample_->Release();
|
176 |
+
}
|
177 |
+
}
|
178 |
+
|
179 |
+
LONG ref_;
|
180 |
+
std::chrono::steady_clock::duration timeout_;
|
181 |
+
IMFSourceReader* reader_;
|
182 |
+
|
183 |
+
std::mutex sample_lock_;
|
184 |
+
std::condition_variable sample_ready_;
|
185 |
+
// This just needs to be initialized to anything other than E_PENDING.
|
186 |
+
HRESULT sample_hr_ = E_UNEXPECTED;
|
187 |
+
DWORD sample_actual_stream_index_;
|
188 |
+
DWORD sample_stream_flags_;
|
189 |
+
LONGLONG sample_timestamp_;
|
190 |
+
IMFSample* sample_ = nullptr;
|
191 |
+
};
|
192 |
+
|
193 |
+
UvcMediaFoundationVideo::UvcMediaFoundationVideo(int vendorId, int productId, const std::string& instanceId, size_t width, size_t height, int fps)
|
194 |
+
: size_bytes(0),
|
195 |
+
mediaSource(nullptr),
|
196 |
+
sourceReader(nullptr),
|
197 |
+
baseFilter(nullptr),
|
198 |
+
ksControl(nullptr),
|
199 |
+
ksControlNodeId(KS_CONTROL_NODE_ID_INVALID),
|
200 |
+
camera_control(nullptr),
|
201 |
+
video_control(nullptr),
|
202 |
+
expected_fps(fps)
|
203 |
+
{
|
204 |
+
HRESULT hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
|
205 |
+
if(FAILED(hr))
|
206 |
+
{
|
207 |
+
throw VideoException("CoInitializeEx failed");
|
208 |
+
}
|
209 |
+
|
210 |
+
if(FAILED(MFStartup(MF_VERSION)))
|
211 |
+
{
|
212 |
+
throw VideoException("MfStartup failed");
|
213 |
+
}
|
214 |
+
|
215 |
+
if(!FindDevice(vendorId, productId, instanceId))
|
216 |
+
{
|
217 |
+
throw VideoException("Unable to open UVC media source");
|
218 |
+
}
|
219 |
+
// Use async always (same interface, but allows for timing out).
|
220 |
+
InitDevice(width, height, true);
|
221 |
+
|
222 |
+
device_properties[PANGO_HAS_TIMING_DATA] = true;
|
223 |
+
}
|
224 |
+
|
225 |
+
UvcMediaFoundationVideo::~UvcMediaFoundationVideo()
|
226 |
+
{
|
227 |
+
DeinitDevice();
|
228 |
+
HRESULT hr = MFShutdown();
|
229 |
+
if(FAILED(hr))
|
230 |
+
{
|
231 |
+
pango_print_warn("MFShutdown failed with result %X", hr);
|
232 |
+
}
|
233 |
+
CoUninitialize();
|
234 |
+
}
|
235 |
+
|
236 |
+
void UvcMediaFoundationVideo::Start()
|
237 |
+
{
|
238 |
+
|
239 |
+
}
|
240 |
+
|
241 |
+
void UvcMediaFoundationVideo::Stop()
|
242 |
+
{
|
243 |
+
}
|
244 |
+
|
245 |
+
size_t UvcMediaFoundationVideo::SizeBytes() const
|
246 |
+
{
|
247 |
+
return size_bytes;
|
248 |
+
}
|
249 |
+
|
250 |
+
const std::vector<pangolin::StreamInfo>& UvcMediaFoundationVideo::Streams() const
|
251 |
+
{
|
252 |
+
return streams;
|
253 |
+
}
|
254 |
+
|
255 |
+
|
256 |
+
bool UvcMediaFoundationVideo::GrabNext(unsigned char* image, bool wait)
|
257 |
+
{
|
258 |
+
HRESULT hr;
|
259 |
+
IMFSample* sample = nullptr;
|
260 |
+
DWORD streamIndex = 0;
|
261 |
+
DWORD flags = 0;
|
262 |
+
LONGLONG timeStamp;
|
263 |
+
|
264 |
+
if (asyncSourceReader != nullptr)
|
265 |
+
{
|
266 |
+
hr = asyncSourceReader->ReadSample(
|
267 |
+
(DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, &streamIndex, &flags, &timeStamp, &sample);
|
268 |
+
}
|
269 |
+
else
|
270 |
+
{
|
271 |
+
hr = sourceReader->ReadSample(
|
272 |
+
(DWORD)MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, &streamIndex, &flags, &timeStamp, &sample);
|
273 |
+
}
|
274 |
+
|
275 |
+
if(SUCCEEDED(hr))
|
276 |
+
{
|
277 |
+
if((flags & MF_SOURCE_READERF_ENDOFSTREAM) != 0)
|
278 |
+
{
|
279 |
+
return false;
|
280 |
+
}
|
281 |
+
|
282 |
+
if((flags & MF_SOURCE_READERF_STREAMTICK) != 0)
|
283 |
+
{
|
284 |
+
return false;
|
285 |
+
}
|
286 |
+
// Believe that at this point sample should not be null
|
287 |
+
if(!sample)
|
288 |
+
{
|
289 |
+
return false;
|
290 |
+
}
|
291 |
+
}
|
292 |
+
else if (hr == MF_E_VIDEO_RECORDING_DEVICE_INVALIDATED && wait)
|
293 |
+
{
|
294 |
+
std::this_thread::sleep_for(std::chrono::microseconds(1000000 / expected_fps));
|
295 |
+
}
|
296 |
+
|
297 |
+
IMFMediaBuffer* mediaBuffer = nullptr;
|
298 |
+
if(SUCCEEDED(hr))
|
299 |
+
{
|
300 |
+
hr = sample->ConvertToContiguousBuffer(&mediaBuffer);
|
301 |
+
}
|
302 |
+
if(SUCCEEDED(hr))
|
303 |
+
{
|
304 |
+
// Use the 2D buffer interface if it's available
|
305 |
+
IMF2DBuffer* mediaBuffer2d = nullptr;
|
306 |
+
hr = mediaBuffer->QueryInterface(&mediaBuffer2d);
|
307 |
+
if(SUCCEEDED(hr))
|
308 |
+
{
|
309 |
+
hr = mediaBuffer2d->ContiguousCopyTo(image, (DWORD)size_bytes);
|
310 |
+
mediaBuffer2d->Release();
|
311 |
+
mediaBuffer2d = nullptr;
|
312 |
+
}
|
313 |
+
else
|
314 |
+
{
|
315 |
+
// No 2D buffer is available
|
316 |
+
byte* buffer;
|
317 |
+
DWORD bufferSize = 0;
|
318 |
+
hr = mediaBuffer->Lock(&buffer, nullptr, &bufferSize);
|
319 |
+
if(SUCCEEDED(hr))
|
320 |
+
{
|
321 |
+
size_t copySize = std::min((size_t)bufferSize, size_bytes);
|
322 |
+
memcpy(image, buffer, copySize);
|
323 |
+
mediaBuffer->Unlock();
|
324 |
+
}
|
325 |
+
}
|
326 |
+
}
|
327 |
+
|
328 |
+
if(SUCCEEDED(hr))
|
329 |
+
{
|
330 |
+
using namespace std::chrono;
|
331 |
+
frame_properties[PANGO_HOST_RECEPTION_TIME_US] = picojson::value(pangolin::Time_us(pangolin::TimeNow()));
|
332 |
+
}
|
333 |
+
|
334 |
+
if(mediaBuffer)
|
335 |
+
{
|
336 |
+
mediaBuffer->Release();
|
337 |
+
mediaBuffer = nullptr;
|
338 |
+
}
|
339 |
+
|
340 |
+
if(sample)
|
341 |
+
{
|
342 |
+
sample->Release();
|
343 |
+
sample = nullptr;
|
344 |
+
}
|
345 |
+
|
346 |
+
return SUCCEEDED(hr);
|
347 |
+
}
|
348 |
+
|
349 |
+
bool UvcMediaFoundationVideo::GrabNewest(unsigned char* image, bool wait)
|
350 |
+
{
|
351 |
+
return GrabNext(image, wait);
|
352 |
+
}
|
353 |
+
|
354 |
+
int UvcMediaFoundationVideo::IoCtrl(uint8_t unit, uint8_t ctrl, unsigned char* data, int len, UvcRequestCode req_code)
|
355 |
+
{
|
356 |
+
if(!ksControl || ksControlNodeId == KS_CONTROL_NODE_ID_INVALID)
|
357 |
+
{
|
358 |
+
return -1;
|
359 |
+
}
|
360 |
+
|
361 |
+
HRESULT hr;
|
362 |
+
KSP_NODE s = {};
|
363 |
+
ULONG ulBytesReturned;
|
364 |
+
|
365 |
+
s.Property.Set = GUID_EXTENSION_UNIT_DESCRIPTOR_OV580;
|
366 |
+
s.Property.Id = ctrl;
|
367 |
+
s.NodeId = ksControlNodeId;
|
368 |
+
|
369 |
+
s.Property.Flags = KSPROPERTY_TYPE_TOPOLOGY;
|
370 |
+
if(req_code == UVC_GET_CUR)
|
371 |
+
{
|
372 |
+
s.Property.Flags |= KSPROPERTY_TYPE_GET;
|
373 |
+
}
|
374 |
+
else if(req_code == UVC_SET_CUR)
|
375 |
+
{
|
376 |
+
s.Property.Flags |= KSPROPERTY_TYPE_SET;
|
377 |
+
}
|
378 |
+
else if(req_code == UVC_GET_LEN)
|
379 |
+
{
|
380 |
+
if(len != sizeof(uint16_t)){
|
381 |
+
std::cerr << "UVC_GET_LEN requested with a buffer not the size of uint16_t" << std::endl;
|
382 |
+
return -1;
|
383 |
+
}
|
384 |
+
|
385 |
+
s.Property.Flags |= KSPROPERTY_TYPE_GET;
|
386 |
+
hr = ksControl->KsProperty((PKSPROPERTY)&s, sizeof(s), NULL, 0, &ulBytesReturned);
|
387 |
+
if(hr == HRESULT_FROM_WIN32(ERROR_MORE_DATA))
|
388 |
+
{
|
389 |
+
*(uint16_t*)data = (uint16_t)ulBytesReturned;
|
390 |
+
return 0;
|
391 |
+
}
|
392 |
+
return -1;
|
393 |
+
}
|
394 |
+
|
395 |
+
hr = ksControl->KsProperty((PKSPROPERTY)&s, sizeof(s), data, len, &ulBytesReturned);
|
396 |
+
|
397 |
+
if (hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
|
398 |
+
{
|
399 |
+
std::cerr << "IoCtrl result is " << ulBytesReturned << ", buffer supplied is " << len << ", result truncated." << std::endl;
|
400 |
+
hr = S_OK;
|
401 |
+
}
|
402 |
+
|
403 |
+
if(FAILED(hr))
|
404 |
+
{
|
405 |
+
pango_print_error("KsProperty failed on UVC device");
|
406 |
+
return -1;
|
407 |
+
}
|
408 |
+
|
409 |
+
return 0;
|
410 |
+
}
|
411 |
+
|
412 |
+
// https://technet.microsoft.com/en-us/dd318253(v=vs.90)
|
413 |
+
// Windows exposure time is specified in log base 2
|
414 |
+
long to_100micros(long v)
|
415 |
+
{
|
416 |
+
double res = pow(2.0, v);
|
417 |
+
return static_cast<long>(res * 10000);
|
418 |
+
}
|
419 |
+
|
420 |
+
long from_100micros(long val)
|
421 |
+
{
|
422 |
+
double d = val * 0.0001;
|
423 |
+
double l = (d != 0) ? std::log2(d) : 1;
|
424 |
+
long v = static_cast<long>(std::roundl(l));
|
425 |
+
return v;
|
426 |
+
}
|
427 |
+
|
428 |
+
bool UvcMediaFoundationVideo::GetExposure(int& exp_us)
|
429 |
+
{
|
430 |
+
long val = 0;
|
431 |
+
long flags = 0;
|
432 |
+
|
433 |
+
if(!camera_control)
|
434 |
+
{
|
435 |
+
pango_print_warn("GetExposure called with no camera_control interface");
|
436 |
+
return false;
|
437 |
+
}
|
438 |
+
HRESULT hr = camera_control->Get(CameraControl_Exposure, &val, &flags);
|
439 |
+
|
440 |
+
exp_us = to_100micros(val) * 100;
|
441 |
+
return SUCCEEDED(hr);
|
442 |
+
}
|
443 |
+
|
444 |
+
bool UvcMediaFoundationVideo::SetExposure(int exp_us)
|
445 |
+
{
|
446 |
+
if(!camera_control)
|
447 |
+
{
|
448 |
+
pango_print_warn("SetExposure called with no camera_control interface");
|
449 |
+
return false;
|
450 |
+
}
|
451 |
+
HRESULT hr = camera_control->Set(CameraControl_Exposure, from_100micros(exp_us / 100), CameraControl_Flags_Manual);
|
452 |
+
return SUCCEEDED(hr);
|
453 |
+
}
|
454 |
+
|
455 |
+
bool UvcMediaFoundationVideo::GetGain(float& gain)
|
456 |
+
{
|
457 |
+
if(!video_control)
|
458 |
+
{
|
459 |
+
pango_print_warn("GetGain called without gain controlls populated");
|
460 |
+
return false;
|
461 |
+
}
|
462 |
+
|
463 |
+
long camGain;
|
464 |
+
long flags;
|
465 |
+
HRESULT hr = video_control->Get(VideoProcAmp_Gain, &camGain, &flags);
|
466 |
+
|
467 |
+
if(FAILED(hr))
|
468 |
+
{
|
469 |
+
pango_print_warn("Failed to read the gain. hr:0x%x", hr);
|
470 |
+
return false;
|
471 |
+
}
|
472 |
+
|
473 |
+
// (FW bug) We can't rely on gainCamMin/gainCamMax here, as they don't correspond to the values returned
|
474 |
+
// by this API
|
475 |
+
// (HAL bug) This assumes that the caller knows to normalize back into API space
|
476 |
+
gain = static_cast<float>(camGain);
|
477 |
+
|
478 |
+
|
479 |
+
return true;
|
480 |
+
}
|
481 |
+
|
482 |
+
bool UvcMediaFoundationVideo::SetGain(float gain)
|
483 |
+
{
|
484 |
+
if(!video_control)
|
485 |
+
{
|
486 |
+
pango_print_warn("SetGain called without gain controlls populated");
|
487 |
+
return false;
|
488 |
+
}
|
489 |
+
|
490 |
+
// (FW bug) We can't rely on gainCamMin/gainCamMax here, as they don't correspond to the values supported
|
491 |
+
// by this API
|
492 |
+
// (HAL bug) This assumes that the caller knows to normalize into camera register space [16, 256]
|
493 |
+
long targetGain = gain;
|
494 |
+
|
495 |
+
HRESULT hr = video_control->Set(VideoProcAmp_Gain, targetGain, VideoProcAmp_Flags_Manual);
|
496 |
+
if(FAILED(hr))
|
497 |
+
{
|
498 |
+
pango_print_warn("Failed to set gain value %f. hr:0x%x", gain, hr);
|
499 |
+
return false;
|
500 |
+
}
|
501 |
+
return true;
|
502 |
+
}
|
503 |
+
|
504 |
+
|
505 |
+
const picojson::value& UvcMediaFoundationVideo::DeviceProperties() const
|
506 |
+
{
|
507 |
+
return device_properties;
|
508 |
+
}
|
509 |
+
|
510 |
+
const picojson::value& UvcMediaFoundationVideo::FrameProperties() const
|
511 |
+
{
|
512 |
+
return frame_properties;
|
513 |
+
}
|
514 |
+
|
515 |
+
bool UvcMediaFoundationVideo::FindDevice(int vendorId, int productId, const std::string& instanceId)
|
516 |
+
{
|
517 |
+
// Create attributes for finding UVC devices
|
518 |
+
IMFAttributes* searchAttributes = nullptr;
|
519 |
+
HRESULT hr = MFCreateAttributes(&searchAttributes, 1);
|
520 |
+
if(FAILED(hr))
|
521 |
+
{
|
522 |
+
pango_print_error("Unable to create UVC device search attributes");
|
523 |
+
}
|
524 |
+
|
525 |
+
// Request video capture devices
|
526 |
+
if(SUCCEEDED(hr))
|
527 |
+
{
|
528 |
+
hr = searchAttributes->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
|
529 |
+
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
|
530 |
+
if(FAILED(hr))
|
531 |
+
{
|
532 |
+
pango_print_error("Unable to set UVC source type attribute");
|
533 |
+
}
|
534 |
+
}
|
535 |
+
|
536 |
+
// Enumerate the devices
|
537 |
+
IMFActivate** devices = nullptr;
|
538 |
+
UINT32 deviceCount = 0;
|
539 |
+
if(SUCCEEDED(hr))
|
540 |
+
{
|
541 |
+
hr = MFEnumDeviceSources(searchAttributes, &devices, &deviceCount);
|
542 |
+
if(FAILED(hr))
|
543 |
+
{
|
544 |
+
pango_print_error("Unable to enumerate UVC device sources");
|
545 |
+
}
|
546 |
+
}
|
547 |
+
|
548 |
+
std::wstring symLink;
|
549 |
+
bool activatedDevice = false;
|
550 |
+
if(SUCCEEDED(hr))
|
551 |
+
{
|
552 |
+
std::wstring wInstanceId;
|
553 |
+
wInstanceId.assign(instanceId.begin(), instanceId.end());
|
554 |
+
|
555 |
+
for(UINT32 i = 0; i < deviceCount; ++i)
|
556 |
+
{
|
557 |
+
// Get this device's sym link
|
558 |
+
WCHAR* symLinkWCStr = nullptr;
|
559 |
+
UINT32 symLinkLength = 0;
|
560 |
+
hr = devices[i]->GetAllocatedString(
|
561 |
+
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &symLinkWCStr, &symLinkLength);
|
562 |
+
if(FAILED(hr))
|
563 |
+
{
|
564 |
+
hr = S_OK;
|
565 |
+
continue;
|
566 |
+
}
|
567 |
+
|
568 |
+
std::wstring checkSymLink(symLinkWCStr);
|
569 |
+
|
570 |
+
// Check if this device matches the requested vendor ID and product ID
|
571 |
+
if(!DeviceMatches(checkSymLink, vendorId, productId, wInstanceId))
|
572 |
+
{
|
573 |
+
continue;
|
574 |
+
}
|
575 |
+
|
576 |
+
hr = devices[i]->ActivateObject(IID_PPV_ARGS(&mediaSource));
|
577 |
+
activatedDevice = SUCCEEDED(hr);
|
578 |
+
if(activatedDevice)
|
579 |
+
{
|
580 |
+
symLink = std::move(checkSymLink);
|
581 |
+
}
|
582 |
+
break;
|
583 |
+
}
|
584 |
+
|
585 |
+
if(!activatedDevice)
|
586 |
+
{
|
587 |
+
pango_print_error("Unable to activate UVC device source");
|
588 |
+
hr = E_FAIL;
|
589 |
+
}
|
590 |
+
}
|
591 |
+
|
592 |
+
for(UINT32 i = 0; i < deviceCount; ++i)
|
593 |
+
{
|
594 |
+
devices[i]->Release();
|
595 |
+
devices[i] = nullptr;
|
596 |
+
}
|
597 |
+
devices = nullptr;
|
598 |
+
|
599 |
+
CoTaskMemFree(devices);
|
600 |
+
|
601 |
+
if(searchAttributes != nullptr)
|
602 |
+
{
|
603 |
+
searchAttributes->Release();
|
604 |
+
searchAttributes = nullptr;
|
605 |
+
}
|
606 |
+
|
607 |
+
// Find the DirectShow device
|
608 |
+
ICreateDevEnum* dshowDevices = nullptr;
|
609 |
+
if(SUCCEEDED(hr))
|
610 |
+
{
|
611 |
+
hr = CoCreateInstance(CLSID_SystemDeviceEnum, 0, CLSCTX_INPROC, IID_ICreateDevEnum, (void**)&dshowDevices);
|
612 |
+
}
|
613 |
+
|
614 |
+
IEnumMoniker* videoInputEnumerator = nullptr;
|
615 |
+
if(SUCCEEDED(hr))
|
616 |
+
{
|
617 |
+
hr = dshowDevices->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &videoInputEnumerator, 0);
|
618 |
+
}
|
619 |
+
|
620 |
+
if(SUCCEEDED(hr))
|
621 |
+
{
|
622 |
+
IMoniker* moniker = nullptr;
|
623 |
+
while((hr = videoInputEnumerator->Next(1, &moniker, 0)) == S_OK)
|
624 |
+
{
|
625 |
+
IPropertyBag* propertyBag = nullptr;
|
626 |
+
hr = moniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)&propertyBag);
|
627 |
+
if(FAILED(hr))
|
628 |
+
{
|
629 |
+
moniker->Release();
|
630 |
+
moniker = nullptr;
|
631 |
+
continue;
|
632 |
+
}
|
633 |
+
|
634 |
+
VARIANT variantPath;
|
635 |
+
VariantInit(&variantPath);
|
636 |
+
hr = propertyBag->Read(L"DevicePath", &variantPath, nullptr);
|
637 |
+
|
638 |
+
bool pathMatches;
|
639 |
+
if(SUCCEEDED(hr) && variantPath.vt == VT_BSTR)
|
640 |
+
{
|
641 |
+
// Determine if this is the correct device by comparing its path against the source's symbolic link
|
642 |
+
// This breaks the rules, but it seems to be the only way to make sure it's the correct device
|
643 |
+
|
644 |
+
// DirectShow and MediaFoundation appear to each create their own symbolic link which contains a GUID
|
645 |
+
// Ignore the GUID portion of the link, leaving just the path information
|
646 |
+
size_t braceOffset = symLink.find(L'{');
|
647 |
+
|
648 |
+
pathMatches = 0 == std::wcsncmp(symLink.c_str(), variantPath.bstrVal, braceOffset);
|
649 |
+
}
|
650 |
+
else
|
651 |
+
{
|
652 |
+
pathMatches = false;
|
653 |
+
}
|
654 |
+
|
655 |
+
VARIANT variantFriendlyName;
|
656 |
+
VariantInit(&variantFriendlyName);
|
657 |
+
hr = propertyBag->Read(L"FriendlyName", &variantFriendlyName, nullptr);
|
658 |
+
|
659 |
+
if(!pathMatches)
|
660 |
+
{
|
661 |
+
moniker->Release();
|
662 |
+
moniker = nullptr;
|
663 |
+
continue;
|
664 |
+
}
|
665 |
+
|
666 |
+
// Found the correct video input
|
667 |
+
break;
|
668 |
+
}
|
669 |
+
|
670 |
+
if(moniker)
|
671 |
+
{
|
672 |
+
hr = moniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&baseFilter);
|
673 |
+
}
|
674 |
+
else
|
675 |
+
{
|
676 |
+
hr = E_FAIL;
|
677 |
+
}
|
678 |
+
|
679 |
+
IKsTopologyInfo* ksTopologyInfo = nullptr;
|
680 |
+
if(SUCCEEDED(hr))
|
681 |
+
{
|
682 |
+
hr = baseFilter->QueryInterface(__uuidof(IKsTopologyInfo), (void**)&ksTopologyInfo);
|
683 |
+
}
|
684 |
+
|
685 |
+
DWORD numberOfNodes = 0;
|
686 |
+
if(SUCCEEDED(hr))
|
687 |
+
{
|
688 |
+
hr = ksTopologyInfo->get_NumNodes(&numberOfNodes);
|
689 |
+
}
|
690 |
+
|
691 |
+
GUID nodeGuid;
|
692 |
+
for(DWORD nodeIndex = 0; nodeIndex < numberOfNodes; ++nodeIndex)
|
693 |
+
{
|
694 |
+
if(FAILED(ksTopologyInfo->get_NodeType(nodeIndex, &nodeGuid)))
|
695 |
+
{
|
696 |
+
continue;
|
697 |
+
}
|
698 |
+
|
699 |
+
if(nodeGuid == KSNODETYPE_DEV_SPECIFIC)
|
700 |
+
{
|
701 |
+
// This is the extension node
|
702 |
+
IKsNodeControl* pUnknown = nullptr;
|
703 |
+
hr = ksTopologyInfo->CreateNodeInstance(nodeIndex, __uuidof(IUnknown), (void**)&pUnknown);
|
704 |
+
|
705 |
+
if(SUCCEEDED(hr) && pUnknown != nullptr)
|
706 |
+
{
|
707 |
+
hr = pUnknown->QueryInterface(__uuidof(IKsControl), (void**)&ksControl);
|
708 |
+
}
|
709 |
+
if(SUCCEEDED(hr))
|
710 |
+
{
|
711 |
+
ksControlNodeId = nodeIndex;
|
712 |
+
}
|
713 |
+
|
714 |
+
if(pUnknown)
|
715 |
+
{
|
716 |
+
pUnknown->Release();
|
717 |
+
pUnknown = nullptr;
|
718 |
+
}
|
719 |
+
}
|
720 |
+
}
|
721 |
+
|
722 |
+
if(ksTopologyInfo)
|
723 |
+
{
|
724 |
+
ksTopologyInfo->Release();
|
725 |
+
ksTopologyInfo = nullptr;
|
726 |
+
}
|
727 |
+
|
728 |
+
if(moniker)
|
729 |
+
{
|
730 |
+
moniker->Release();
|
731 |
+
moniker = nullptr;
|
732 |
+
}
|
733 |
+
}
|
734 |
+
|
735 |
+
if(videoInputEnumerator)
|
736 |
+
{
|
737 |
+
videoInputEnumerator->Release();
|
738 |
+
}
|
739 |
+
|
740 |
+
if(dshowDevices)
|
741 |
+
{
|
742 |
+
dshowDevices->Release();
|
743 |
+
}
|
744 |
+
|
745 |
+
return SUCCEEDED(hr);
|
746 |
+
}
|
747 |
+
|
748 |
+
|
749 |
+
const GUID MFVideoFormat_Y10 =
|
750 |
+
{
|
751 |
+
0x20303159,
|
752 |
+
0x0000,
|
753 |
+
0x0010,
|
754 |
+
0x80,
|
755 |
+
0x00,
|
756 |
+
0x00,
|
757 |
+
0xAA,
|
758 |
+
0x00,
|
759 |
+
0x38,
|
760 |
+
0x9b,
|
761 |
+
0x71
|
762 |
+
};
|
763 |
+
|
764 |
+
void UvcMediaFoundationVideo::InitDevice(size_t width, size_t height, bool async)
|
765 |
+
{
|
766 |
+
HRESULT hr;
|
767 |
+
|
768 |
+
if (async)
|
769 |
+
{
|
770 |
+
int64_t timeout_ms = expected_fps ? 4 * 1000 / expected_fps : 500;
|
771 |
+
asyncSourceReader = new AsyncSourceReader(mediaSource, timeout_ms);
|
772 |
+
sourceReader = asyncSourceReader->AddRefReader();
|
773 |
+
}
|
774 |
+
else
|
775 |
+
{
|
776 |
+
hr = MFCreateSourceReaderFromMediaSource(mediaSource, nullptr, &sourceReader);
|
777 |
+
if(FAILED(hr))
|
778 |
+
{
|
779 |
+
throw VideoException(FormatString("Unable to create source reader from UVC media source (%)", hr));
|
780 |
+
}
|
781 |
+
}
|
782 |
+
|
783 |
+
// Find the closest supported resolution
|
784 |
+
UINT32 stride;
|
785 |
+
bool hasValidStride = false;
|
786 |
+
PixelFormat pixelFormat;
|
787 |
+
|
788 |
+
uint32_t bit_depth = 0;
|
789 |
+
IMFMediaType* bestMediaType = nullptr;
|
790 |
+
int bestError = std::numeric_limits<int>::max();
|
791 |
+
UINT32 bestWidth;
|
792 |
+
UINT32 bestHeight;
|
793 |
+
UINT32 bestStride;
|
794 |
+
GUID bestGuid;
|
795 |
+
|
796 |
+
for(DWORD i = 0;; ++i)
|
797 |
+
{
|
798 |
+
IMFMediaType* checkMediaType;
|
799 |
+
hr = sourceReader->GetNativeMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, i, &checkMediaType);
|
800 |
+
if(FAILED(hr))
|
801 |
+
{
|
802 |
+
if(hr == MF_E_NO_MORE_TYPES)
|
803 |
+
{
|
804 |
+
// Reached the end of the available media types
|
805 |
+
hr = S_OK;
|
806 |
+
}
|
807 |
+
else
|
808 |
+
{
|
809 |
+
pango_print_error("Failed to get UVC native media type");
|
810 |
+
}
|
811 |
+
break;
|
812 |
+
}
|
813 |
+
|
814 |
+
UINT32 checkWidth;
|
815 |
+
UINT32 checkHeight;
|
816 |
+
if(FAILED(MFGetAttributeSize(checkMediaType, MF_MT_FRAME_SIZE, &checkWidth, &checkHeight)))
|
817 |
+
{
|
818 |
+
checkWidth = 0;
|
819 |
+
checkHeight = 0;
|
820 |
+
}
|
821 |
+
|
822 |
+
int checkError = abs(int(checkWidth) - int(width)) + abs(int(checkHeight) - int(height));
|
823 |
+
if(bestError > checkError)
|
824 |
+
{
|
825 |
+
// Release the previous best
|
826 |
+
if(bestMediaType)
|
827 |
+
{
|
828 |
+
bestMediaType->Release();
|
829 |
+
}
|
830 |
+
|
831 |
+
bestError = checkError;
|
832 |
+
bestMediaType = checkMediaType;
|
833 |
+
bestWidth = checkWidth;
|
834 |
+
bestHeight = checkHeight;
|
835 |
+
|
836 |
+
if(FAILED(checkMediaType->GetGUID(MF_MT_SUBTYPE, &bestGuid)))
|
837 |
+
{
|
838 |
+
bestGuid = {0};
|
839 |
+
}
|
840 |
+
|
841 |
+
const auto stride_result = checkMediaType->GetUINT32(MF_MT_DEFAULT_STRIDE,&bestStride);
|
842 |
+
hasValidStride = !(stride_result == MF_E_ATTRIBUTENOTFOUND);
|
843 |
+
}
|
844 |
+
else
|
845 |
+
{
|
846 |
+
checkMediaType->Release();
|
847 |
+
}
|
848 |
+
}
|
849 |
+
|
850 |
+
if(bestMediaType)
|
851 |
+
{
|
852 |
+
if(SUCCEEDED(hr))
|
853 |
+
{
|
854 |
+
sourceReader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, nullptr, bestMediaType);
|
855 |
+
width = bestWidth;
|
856 |
+
height = bestHeight;
|
857 |
+
if(hasValidStride)
|
858 |
+
{
|
859 |
+
stride = bestStride;
|
860 |
+
}
|
861 |
+
|
862 |
+
if(bestGuid == MFVideoFormat_YUY2)
|
863 |
+
{
|
864 |
+
pixelFormat = PixelFormatFromString("GRAY8");
|
865 |
+
bit_depth = 8;
|
866 |
+
}
|
867 |
+
else if(bestGuid == MFVideoFormat_Y10)
|
868 |
+
{
|
869 |
+
pixelFormat = PixelFormatFromString("GRAY10");
|
870 |
+
bit_depth = 10;
|
871 |
+
}
|
872 |
+
else if((unsigned char)((bestGuid.Data1 >> 0) & 0xff) == 'Y' && (unsigned char)((bestGuid.Data1 >> 8) & 0xff) == '8')
|
873 |
+
{
|
874 |
+
pixelFormat = PixelFormatFromString("GRAY8");
|
875 |
+
bit_depth = 8;
|
876 |
+
}
|
877 |
+
else
|
878 |
+
{
|
879 |
+
pango_print_warn("Unexpected MFVideoFormat with FOURCC %c.%c.%c.%c",
|
880 |
+
(unsigned char)((bestGuid.Data1 >> 0) & 0xff),
|
881 |
+
(unsigned char)((bestGuid.Data1 >> 8) & 0xff),
|
882 |
+
(unsigned char)((bestGuid.Data1 >> 16) & 0xff),
|
883 |
+
(unsigned char)((bestGuid.Data1 >> 24) & 0xff));
|
884 |
+
}
|
885 |
+
}
|
886 |
+
|
887 |
+
bestMediaType->Release();
|
888 |
+
}
|
889 |
+
else
|
890 |
+
{
|
891 |
+
width = 0;
|
892 |
+
height = 0;
|
893 |
+
}
|
894 |
+
|
895 |
+
if(SUCCEEDED(hr))
|
896 |
+
{
|
897 |
+
PopulateGainControls();
|
898 |
+
}
|
899 |
+
|
900 |
+
if(SUCCEEDED(hr))
|
901 |
+
{
|
902 |
+
hr = mediaSource->QueryInterface(__uuidof(IAMCameraControl), (void**)&camera_control);
|
903 |
+
if(FAILED(hr))
|
904 |
+
{
|
905 |
+
// On failure, soldier on without manual exposure controls
|
906 |
+
pango_print_warn("Failed to populate IAMCameraControl (0x%x). Exposure control will not function", hr);
|
907 |
+
}
|
908 |
+
}
|
909 |
+
|
910 |
+
if(hasValidStride)
|
911 |
+
{
|
912 |
+
size_bytes = stride * height;
|
913 |
+
}
|
914 |
+
else
|
915 |
+
{
|
916 |
+
size_bytes = width * pixelFormat.bpp * height / 8;
|
917 |
+
}
|
918 |
+
pixelFormat.channel_bit_depth = bit_depth;
|
919 |
+
const StreamInfo stream_info(pixelFormat, width, height, size_bytes / height, 0);
|
920 |
+
streams.emplace_back(stream_info);
|
921 |
+
}
|
922 |
+
|
923 |
+
void UvcMediaFoundationVideo::PopulateGainControls()
|
924 |
+
{
|
925 |
+
if(!mediaSource)
|
926 |
+
{
|
927 |
+
pango_print_warn("PopulateGainControls called without a media source");
|
928 |
+
return;
|
929 |
+
}
|
930 |
+
|
931 |
+
HRESULT hr = mediaSource->QueryInterface(__uuidof(IAMVideoProcAmp) , (void**)&video_control);
|
932 |
+
|
933 |
+
// read the min and max values from the camera to allow mapping from our fixed ranges
|
934 |
+
if(SUCCEEDED(hr))
|
935 |
+
{
|
936 |
+
long steppingDelta;
|
937 |
+
long flags;
|
938 |
+
|
939 |
+
// FW bug: this range is being reported as [0, 1024], but the Get/Set pathway handles register values [16, 256]
|
940 |
+
hr = video_control->GetRange(VideoProcAmp_Gain, &gainCamMin, &gainCamMax, &steppingDelta, &gainCamDefault, &flags);
|
941 |
+
|
942 |
+
// should we be checking that flags are correctly set to VideoProcAmp_Flags_Manual ?
|
943 |
+
}
|
944 |
+
|
945 |
+
if(FAILED(hr))
|
946 |
+
{
|
947 |
+
pango_print_warn("Failed to populate gain controls (0x%x)", hr);
|
948 |
+
if(video_control)
|
949 |
+
{
|
950 |
+
video_control->Release();
|
951 |
+
video_control = nullptr;
|
952 |
+
}
|
953 |
+
}
|
954 |
+
|
955 |
+
return;
|
956 |
+
}
|
957 |
+
|
958 |
+
void UvcMediaFoundationVideo::DeinitDevice()
|
959 |
+
{
|
960 |
+
if(ksControl)
|
961 |
+
{
|
962 |
+
ksControl->Release();
|
963 |
+
ksControl = nullptr;
|
964 |
+
}
|
965 |
+
|
966 |
+
ksControlNodeId = KS_CONTROL_NODE_ID_INVALID;
|
967 |
+
|
968 |
+
if(baseFilter)
|
969 |
+
{
|
970 |
+
baseFilter->Release();
|
971 |
+
baseFilter = nullptr;
|
972 |
+
}
|
973 |
+
|
974 |
+
if(sourceReader)
|
975 |
+
{
|
976 |
+
sourceReader->Release();
|
977 |
+
sourceReader = nullptr;
|
978 |
+
}
|
979 |
+
|
980 |
+
if(asyncSourceReader)
|
981 |
+
{
|
982 |
+
asyncSourceReader->Release();
|
983 |
+
asyncSourceReader = nullptr;
|
984 |
+
}
|
985 |
+
|
986 |
+
if(camera_control)
|
987 |
+
{
|
988 |
+
camera_control->Release();
|
989 |
+
camera_control = nullptr;
|
990 |
+
}
|
991 |
+
|
992 |
+
if(video_control)
|
993 |
+
{
|
994 |
+
video_control->Release();
|
995 |
+
video_control = nullptr;
|
996 |
+
}
|
997 |
+
|
998 |
+
if(mediaSource)
|
999 |
+
{
|
1000 |
+
mediaSource->Shutdown();
|
1001 |
+
mediaSource->Release();
|
1002 |
+
mediaSource = nullptr;
|
1003 |
+
}
|
1004 |
+
|
1005 |
+
streams.clear();
|
1006 |
+
}
|
1007 |
+
|
1008 |
+
bool UvcMediaFoundationVideo::DeviceMatches(const std::wstring& symLink, int vendorId, int productId, std::wstring& instanceId)
|
1009 |
+
{
|
1010 |
+
// Example symlink:
|
1011 |
+
// \\?\usb#vid_05a9&pid_0581&mi_00#6&2ff327a4&2&0000#{e5323777-f976-4f5b-9b55-b94699c46e44}\global
|
1012 |
+
// ^^^^^^^^ ^^^^^^^^ ^^^^^^^^^^^^^^^^^
|
1013 |
+
std::wstring symLinkString(symLink);
|
1014 |
+
if(vendorId != 0 && !SymLinkIDMatches(symLinkString, L"vid_", vendorId))
|
1015 |
+
{
|
1016 |
+
return false;
|
1017 |
+
}
|
1018 |
+
|
1019 |
+
if(productId != 0 && !SymLinkIDMatches(symLinkString, L"pid_", productId))
|
1020 |
+
{
|
1021 |
+
return false;
|
1022 |
+
}
|
1023 |
+
|
1024 |
+
std::wstring instanceSearch = L"#" + instanceId + L"#";
|
1025 |
+
if(std::string::npos == symLinkString.find(instanceSearch))
|
1026 |
+
{
|
1027 |
+
return false;
|
1028 |
+
}
|
1029 |
+
|
1030 |
+
return true;
|
1031 |
+
}
|
1032 |
+
|
1033 |
+
bool UvcMediaFoundationVideo::SymLinkIDMatches(const std::wstring& symLink, const wchar_t* idStr, int id)
|
1034 |
+
{
|
1035 |
+
// Find the ID prefix
|
1036 |
+
size_t idOffset = symLink.find(idStr);
|
1037 |
+
if(idOffset == std::wstring::npos)
|
1038 |
+
{
|
1039 |
+
// Unable to find the prefix
|
1040 |
+
return false;
|
1041 |
+
}
|
1042 |
+
|
1043 |
+
// Parse the ID as a hexadecimal number
|
1044 |
+
int found = std::wcstol(&symLink[idOffset + std::wcslen(idStr)], nullptr, 16);
|
1045 |
+
return id == found;
|
1046 |
+
}
|
1047 |
+
|
1048 |
+
PANGOLIN_REGISTER_FACTORY(UvcMediaFoundationVideo)
|
1049 |
+
{
|
1050 |
+
struct UvcMediaFoundationVideoFactory final : public TypedFactoryInterface<VideoInterface>
|
1051 |
+
{
|
1052 |
+
std::map<std::string,Precedence> Schemes() const override
|
1053 |
+
{
|
1054 |
+
return {{"uvc",10}};
|
1055 |
+
}
|
1056 |
+
const char* Description() const override
|
1057 |
+
{
|
1058 |
+
return "Use Windows Media Foundation to open UVC USB device.";
|
1059 |
+
}
|
1060 |
+
ParamSet Params() const override
|
1061 |
+
{
|
1062 |
+
return {{
|
1063 |
+
{"size","640x480","Image dimension"},
|
1064 |
+
{"fps","0","Frames per second (0:unspecified)"},
|
1065 |
+
{"period","0","Specify frame period in microseconds (0:unspecified)"},
|
1066 |
+
{"num","0","Open the nth device (no need for vid and pid)"},
|
1067 |
+
}};
|
1068 |
+
}
|
1069 |
+
std::unique_ptr<VideoInterface> Open(const Uri& uri) override
|
1070 |
+
{
|
1071 |
+
int vendorId = 0;
|
1072 |
+
int productId = 0;
|
1073 |
+
|
1074 |
+
const std::string instanceId = uri.url.substr(uri.url.rfind("\\") + 1);
|
1075 |
+
std::istringstream(uri.url.substr(uri.url.find("vid_", 0) + 4, 4)) >> std::hex >> vendorId;
|
1076 |
+
std::istringstream(uri.url.substr(uri.url.find("pid_", 0) + 4, 4)) >> std::hex >> productId;
|
1077 |
+
const ImageDim dim = uri.Get<ImageDim>("size", ImageDim(640, 480));
|
1078 |
+
unsigned int fps = uri.Get<unsigned int>("fps", 0); // 0 means unspecified
|
1079 |
+
if (fps == 0 && uri.Contains("period")) {
|
1080 |
+
uint32_t period_us = uri.Get<uint32_t>("period", 0);
|
1081 |
+
fps = 1000000 / period_us;
|
1082 |
+
}
|
1083 |
+
|
1084 |
+
return std::unique_ptr<VideoInterface>(new UvcMediaFoundationVideo(vendorId, productId, instanceId, dim.x, dim.y, fps));
|
1085 |
+
}
|
1086 |
+
};
|
1087 |
+
|
1088 |
+
return FactoryRegistry::I()->RegisterFactory<VideoInterface>(std::make_shared<UvcMediaFoundationVideoFactory>());
|
1089 |
+
}
|
1090 |
+
}
|
1091 |
+
|
third-party/DPVO/Pangolin/components/pango_video/src/drivers/v4l.cpp
ADDED
@@ -0,0 +1,852 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project,
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
* Copyright (c) 2011 Steven Lovegrove
|
4 |
+
*
|
5 |
+
* adapted from V4L2 video capture example
|
6 |
+
*
|
7 |
+
* Permission is hereby granted, free of charge, to any person
|
8 |
+
* obtaining a copy of this software and associated documentation
|
9 |
+
* files (the "Software"), to deal in the Software without
|
10 |
+
* restriction, including without limitation the rights to use,
|
11 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
12 |
+
* copies of the Software, and to permit persons to whom the
|
13 |
+
* Software is furnished to do so, subject to the following
|
14 |
+
* conditions:
|
15 |
+
*
|
16 |
+
* The above copyright notice and this permission notice shall be
|
17 |
+
* included in all copies or substantial portions of the Software.
|
18 |
+
*
|
19 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
20 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
21 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
22 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
23 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
24 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
25 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
26 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
27 |
+
*/
|
28 |
+
|
29 |
+
#include <pangolin/factory/factory_registry.h>
|
30 |
+
#include <pangolin/utils/timer.h>
|
31 |
+
#include <pangolin/video/drivers/v4l.h>
|
32 |
+
#include <pangolin/video/iostream_operators.h>
|
33 |
+
|
34 |
+
#include <assert.h>
|
35 |
+
#include <iostream>
|
36 |
+
#include <stdint.h>
|
37 |
+
#include <stdio.h>
|
38 |
+
#include <stdlib.h>
|
39 |
+
#include <string.h>
|
40 |
+
|
41 |
+
#include <errno.h>
|
42 |
+
#include <fcntl.h>
|
43 |
+
#include <linux/usb/video.h>
|
44 |
+
#include <linux/uvcvideo.h>
|
45 |
+
#include <malloc.h>
|
46 |
+
#include <sys/ioctl.h>
|
47 |
+
#include <sys/mman.h>
|
48 |
+
#include <sys/stat.h>
|
49 |
+
#include <sys/time.h>
|
50 |
+
#include <sys/types.h>
|
51 |
+
#include <unistd.h>
|
52 |
+
|
53 |
+
#define CLEAR(x) memset (&(x), 0, sizeof (x))
|
54 |
+
|
55 |
+
using namespace std;
|
56 |
+
|
57 |
+
namespace pangolin
|
58 |
+
{
|
59 |
+
|
60 |
+
static int xioctl(int fd, int request, void* arg)
|
61 |
+
{
|
62 |
+
int r;
|
63 |
+
do r = ioctl (fd, request, arg);
|
64 |
+
while (-1 == r && EINTR == errno);
|
65 |
+
return r;
|
66 |
+
}
|
67 |
+
|
68 |
+
inline std::string V4lToString(int32_t v)
|
69 |
+
{
|
70 |
+
// v = ((__u32)(a) | ((__u32)(b) << 8) | ((__u32)(c) << 16) | ((__u32)(d) << 24))
|
71 |
+
char cc[5];
|
72 |
+
cc[0] = v & 0xff;
|
73 |
+
cc[1] = (v>>8) & 0xff;
|
74 |
+
cc[2] = (v>>16) & 0xff;
|
75 |
+
cc[3] = (v>>24) & 0xff;
|
76 |
+
cc[4] = 0;
|
77 |
+
return std::string(cc);
|
78 |
+
}
|
79 |
+
|
80 |
+
V4lVideo::V4lVideo(const char* dev_name, uint32_t period, io_method io, unsigned iwidth, unsigned iheight, unsigned v4l_format)
|
81 |
+
: io(io), fd(-1), buffers(0), n_buffers(0), running(false), period(period)
|
82 |
+
{
|
83 |
+
open_device(dev_name);
|
84 |
+
init_device(dev_name,iwidth,iheight,0,v4l_format);
|
85 |
+
InitPangoDeviceProperties();
|
86 |
+
|
87 |
+
Start();
|
88 |
+
}
|
89 |
+
|
90 |
+
V4lVideo::~V4lVideo()
|
91 |
+
{
|
92 |
+
if(running)
|
93 |
+
{
|
94 |
+
Stop();
|
95 |
+
}
|
96 |
+
|
97 |
+
uninit_device();
|
98 |
+
close_device();
|
99 |
+
}
|
100 |
+
|
101 |
+
void V4lVideo::InitPangoDeviceProperties()
|
102 |
+
{
|
103 |
+
// Store camera details in device properties
|
104 |
+
device_properties[PANGO_HAS_TIMING_DATA] = true;
|
105 |
+
}
|
106 |
+
|
107 |
+
const std::vector<StreamInfo>& V4lVideo::Streams() const
|
108 |
+
{
|
109 |
+
return streams;
|
110 |
+
}
|
111 |
+
|
112 |
+
size_t V4lVideo::SizeBytes() const
|
113 |
+
{
|
114 |
+
return image_size;
|
115 |
+
}
|
116 |
+
|
117 |
+
bool V4lVideo::GrabNext( unsigned char* image, bool wait )
|
118 |
+
{
|
119 |
+
for (;;) {
|
120 |
+
fd_set fds;
|
121 |
+
struct timeval tv;
|
122 |
+
int r;
|
123 |
+
|
124 |
+
FD_ZERO (&fds);
|
125 |
+
FD_SET (fd, &fds);
|
126 |
+
|
127 |
+
/* Timeout. */
|
128 |
+
tv.tv_sec = 0;
|
129 |
+
tv.tv_usec = 2 * period;
|
130 |
+
|
131 |
+
r = select (fd + 1, &fds, NULL, NULL, &tv);
|
132 |
+
|
133 |
+
if (-1 == r) {
|
134 |
+
if (EINTR == errno)
|
135 |
+
continue;
|
136 |
+
|
137 |
+
// This is a terminal condition that must be propogated up.
|
138 |
+
throw VideoException ("select", strerror(errno));
|
139 |
+
}
|
140 |
+
|
141 |
+
if (0 == r) {
|
142 |
+
// Timeout has occured - This is longer than any reasonable frame interval,
|
143 |
+
// but not necessarily terminal, so return false to indicate that no frame was captured.
|
144 |
+
return false;
|
145 |
+
}
|
146 |
+
|
147 |
+
if (ReadFrame(image, wait))
|
148 |
+
break;
|
149 |
+
|
150 |
+
/* EAGAIN - continue select loop. */
|
151 |
+
}
|
152 |
+
|
153 |
+
return true;
|
154 |
+
}
|
155 |
+
|
156 |
+
bool V4lVideo::GrabNewest( unsigned char* image, bool wait )
|
157 |
+
{
|
158 |
+
// TODO: Implement
|
159 |
+
return GrabNext(image,wait);
|
160 |
+
}
|
161 |
+
|
162 |
+
int V4lVideo::ReadFrame(unsigned char* image, bool wait)
|
163 |
+
{
|
164 |
+
struct v4l2_buffer buf;
|
165 |
+
unsigned int i;
|
166 |
+
|
167 |
+
switch (io) {
|
168 |
+
case IO_METHOD_READ:
|
169 |
+
if (-1 == read (fd, buffers[0].start, buffers[0].length)) {
|
170 |
+
switch (errno) {
|
171 |
+
case EAGAIN:
|
172 |
+
return 0;
|
173 |
+
|
174 |
+
case EIO:
|
175 |
+
/* Could ignore EIO, see spec. */
|
176 |
+
|
177 |
+
/* fall through */
|
178 |
+
|
179 |
+
default:
|
180 |
+
throw VideoException("read", strerror(errno));
|
181 |
+
}
|
182 |
+
}
|
183 |
+
// This is a hack, this ts sould come from the device.
|
184 |
+
frame_properties[PANGO_HOST_RECEPTION_TIME_US] = picojson::value(pangolin::Time_us(pangolin::TimeNow()));
|
185 |
+
|
186 |
+
// process_image(buffers[0].start);
|
187 |
+
memcpy(image,buffers[0].start,buffers[0].length);
|
188 |
+
|
189 |
+
break;
|
190 |
+
|
191 |
+
case IO_METHOD_MMAP:
|
192 |
+
CLEAR (buf);
|
193 |
+
|
194 |
+
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
195 |
+
buf.memory = V4L2_MEMORY_MMAP;
|
196 |
+
|
197 |
+
if (-1 == xioctl (fd, VIDIOC_DQBUF, &buf)) {
|
198 |
+
// Sleep for one period if wait is specified
|
199 |
+
if (wait) {
|
200 |
+
std::this_thread::sleep_for(std::chrono::microseconds(period));
|
201 |
+
}
|
202 |
+
switch (errno) {
|
203 |
+
case EAGAIN:
|
204 |
+
return 0;
|
205 |
+
|
206 |
+
case EIO:
|
207 |
+
/* Could ignore EIO, see spec. */
|
208 |
+
|
209 |
+
/* fall through */
|
210 |
+
|
211 |
+
default:
|
212 |
+
throw VideoException("VIDIOC_DQBUF", strerror(errno));
|
213 |
+
}
|
214 |
+
}
|
215 |
+
// This is a hack, this ts sould come from the device.
|
216 |
+
frame_properties[PANGO_HOST_RECEPTION_TIME_US] = picojson::value(pangolin::Time_us(pangolin::TimeNow()));
|
217 |
+
|
218 |
+
assert (buf.index < n_buffers);
|
219 |
+
|
220 |
+
// process_image (buffers[buf.index].start);
|
221 |
+
memcpy(image,buffers[buf.index].start,buffers[buf.index].length);
|
222 |
+
|
223 |
+
|
224 |
+
if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
|
225 |
+
throw VideoException("VIDIOC_QBUF", strerror(errno));
|
226 |
+
|
227 |
+
break;
|
228 |
+
|
229 |
+
case IO_METHOD_USERPTR:
|
230 |
+
CLEAR (buf);
|
231 |
+
|
232 |
+
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
233 |
+
buf.memory = V4L2_MEMORY_USERPTR;
|
234 |
+
|
235 |
+
if (-1 == xioctl (fd, VIDIOC_DQBUF, &buf)) {
|
236 |
+
switch (errno) {
|
237 |
+
case EAGAIN:
|
238 |
+
return 0;
|
239 |
+
|
240 |
+
case EIO:
|
241 |
+
/* Could ignore EIO, see spec. */
|
242 |
+
|
243 |
+
/* fall through */
|
244 |
+
|
245 |
+
default:
|
246 |
+
throw VideoException("VIDIOC_DQBUF", strerror(errno));
|
247 |
+
}
|
248 |
+
}
|
249 |
+
// This is a hack, this ts sould come from the device.
|
250 |
+
frame_properties[PANGO_HOST_RECEPTION_TIME_US] = picojson::value(pangolin::Time_us(pangolin::TimeNow()));
|
251 |
+
|
252 |
+
for (i = 0; i < n_buffers; ++i)
|
253 |
+
if (buf.m.userptr == (unsigned long) buffers[i].start
|
254 |
+
&& buf.length == buffers[i].length)
|
255 |
+
break;
|
256 |
+
|
257 |
+
assert (i < n_buffers);
|
258 |
+
|
259 |
+
// process_image ((void *) buf.m.userptr);
|
260 |
+
memcpy(image,(void *)buf.m.userptr,buf.length);
|
261 |
+
|
262 |
+
|
263 |
+
if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
|
264 |
+
throw VideoException("VIDIOC_QBUF", strerror(errno));
|
265 |
+
|
266 |
+
break;
|
267 |
+
}
|
268 |
+
|
269 |
+
return 1;
|
270 |
+
}
|
271 |
+
|
272 |
+
void V4lVideo::Stop()
|
273 |
+
{
|
274 |
+
if(running) {
|
275 |
+
enum v4l2_buf_type type;
|
276 |
+
|
277 |
+
switch (io) {
|
278 |
+
case IO_METHOD_READ:
|
279 |
+
/* Nothing to do. */
|
280 |
+
break;
|
281 |
+
|
282 |
+
case IO_METHOD_MMAP:
|
283 |
+
case IO_METHOD_USERPTR:
|
284 |
+
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
285 |
+
|
286 |
+
if (-1 == xioctl (fd, VIDIOC_STREAMOFF, &type)) {
|
287 |
+
// throwing on dtors is not good, commenting out for now
|
288 |
+
// throw VideoException("VIDIOC_STREAMOFF", strerror(errno));
|
289 |
+
pango_print_warn("V4lVideo::Stop() VIDIOC_STREAMOFF error: %s\n", strerror(errno));
|
290 |
+
}
|
291 |
+
|
292 |
+
break;
|
293 |
+
}
|
294 |
+
|
295 |
+
running = false;
|
296 |
+
}
|
297 |
+
}
|
298 |
+
|
299 |
+
void V4lVideo::Start()
|
300 |
+
{
|
301 |
+
if(!running) {
|
302 |
+
unsigned int i;
|
303 |
+
enum v4l2_buf_type type;
|
304 |
+
|
305 |
+
switch (io) {
|
306 |
+
case IO_METHOD_READ:
|
307 |
+
/* Nothing to do. */
|
308 |
+
break;
|
309 |
+
|
310 |
+
case IO_METHOD_MMAP:
|
311 |
+
for (i = 0; i < n_buffers; ++i) {
|
312 |
+
struct v4l2_buffer buf;
|
313 |
+
|
314 |
+
CLEAR (buf);
|
315 |
+
|
316 |
+
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
317 |
+
buf.memory = V4L2_MEMORY_MMAP;
|
318 |
+
buf.index = i;
|
319 |
+
|
320 |
+
if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
|
321 |
+
throw VideoException("VIDIOC_QBUF", strerror(errno));
|
322 |
+
}
|
323 |
+
|
324 |
+
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
325 |
+
|
326 |
+
if (-1 == xioctl (fd, VIDIOC_STREAMON, &type))
|
327 |
+
throw VideoException("VIDIOC_STREAMON", strerror(errno));
|
328 |
+
|
329 |
+
break;
|
330 |
+
|
331 |
+
case IO_METHOD_USERPTR:
|
332 |
+
for (i = 0; i < n_buffers; ++i) {
|
333 |
+
struct v4l2_buffer buf;
|
334 |
+
|
335 |
+
CLEAR (buf);
|
336 |
+
|
337 |
+
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
338 |
+
buf.memory = V4L2_MEMORY_USERPTR;
|
339 |
+
buf.index = i;
|
340 |
+
buf.m.userptr = (unsigned long) buffers[i].start;
|
341 |
+
buf.length = buffers[i].length;
|
342 |
+
|
343 |
+
if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
|
344 |
+
throw VideoException("VIDIOC_QBUF", strerror(errno));
|
345 |
+
}
|
346 |
+
|
347 |
+
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
348 |
+
|
349 |
+
if (-1 == xioctl (fd, VIDIOC_STREAMON, &type))
|
350 |
+
throw VideoException ("VIDIOC_STREAMON", strerror(errno));
|
351 |
+
|
352 |
+
break;
|
353 |
+
}
|
354 |
+
|
355 |
+
running = true;
|
356 |
+
}
|
357 |
+
}
|
358 |
+
|
359 |
+
void V4lVideo::uninit_device()
|
360 |
+
{
|
361 |
+
unsigned int i;
|
362 |
+
|
363 |
+
switch (io) {
|
364 |
+
case IO_METHOD_READ:
|
365 |
+
free (buffers[0].start);
|
366 |
+
break;
|
367 |
+
|
368 |
+
case IO_METHOD_MMAP:
|
369 |
+
for (i = 0; i < n_buffers; ++i)
|
370 |
+
if (-1 == munmap (buffers[i].start, buffers[i].length))
|
371 |
+
throw VideoException ("munmap");
|
372 |
+
break;
|
373 |
+
|
374 |
+
case IO_METHOD_USERPTR:
|
375 |
+
for (i = 0; i < n_buffers; ++i)
|
376 |
+
free (buffers[i].start);
|
377 |
+
break;
|
378 |
+
}
|
379 |
+
|
380 |
+
free (buffers);
|
381 |
+
}
|
382 |
+
|
383 |
+
void V4lVideo::init_read(unsigned int buffer_size)
|
384 |
+
{
|
385 |
+
buffers = (buffer*)calloc (1, sizeof (buffer));
|
386 |
+
|
387 |
+
if (!buffers) {
|
388 |
+
throw VideoException("Out of memory\n");
|
389 |
+
}
|
390 |
+
|
391 |
+
buffers[0].length = buffer_size;
|
392 |
+
buffers[0].start = malloc (buffer_size);
|
393 |
+
|
394 |
+
if (!buffers[0].start) {
|
395 |
+
throw VideoException("Out of memory\n");
|
396 |
+
}
|
397 |
+
}
|
398 |
+
|
399 |
+
void V4lVideo::init_mmap(const char* /*dev_name*/)
|
400 |
+
{
|
401 |
+
struct v4l2_requestbuffers req;
|
402 |
+
|
403 |
+
CLEAR (req);
|
404 |
+
|
405 |
+
req.count = 4;
|
406 |
+
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
407 |
+
req.memory = V4L2_MEMORY_MMAP;
|
408 |
+
|
409 |
+
if (-1 == xioctl (fd, VIDIOC_REQBUFS, &req)) {
|
410 |
+
if (EINVAL == errno) {
|
411 |
+
throw VideoException("does not support memory mapping", strerror(errno));
|
412 |
+
} else {
|
413 |
+
throw VideoException ("VIDIOC_REQBUFS", strerror(errno));
|
414 |
+
}
|
415 |
+
}
|
416 |
+
|
417 |
+
if (req.count < 2) {
|
418 |
+
throw VideoException("Insufficient buffer memory");
|
419 |
+
}
|
420 |
+
|
421 |
+
buffers = (buffer*)calloc(req.count, sizeof(buffer));
|
422 |
+
|
423 |
+
if (!buffers) {
|
424 |
+
throw VideoException( "Out of memory\n");
|
425 |
+
}
|
426 |
+
|
427 |
+
for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
|
428 |
+
struct v4l2_buffer buf;
|
429 |
+
|
430 |
+
CLEAR (buf);
|
431 |
+
|
432 |
+
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
433 |
+
buf.memory = V4L2_MEMORY_MMAP;
|
434 |
+
buf.index = n_buffers;
|
435 |
+
|
436 |
+
if (-1 == xioctl (fd, VIDIOC_QUERYBUF, &buf))
|
437 |
+
throw VideoException ("VIDIOC_QUERYBUF", strerror(errno));
|
438 |
+
|
439 |
+
buffers[n_buffers].length = buf.length;
|
440 |
+
buffers[n_buffers].start =
|
441 |
+
mmap (NULL /* start anywhere */,
|
442 |
+
buf.length,
|
443 |
+
PROT_READ | PROT_WRITE /* required */,
|
444 |
+
MAP_SHARED /* recommended */,
|
445 |
+
fd, buf.m.offset);
|
446 |
+
|
447 |
+
if (MAP_FAILED == buffers[n_buffers].start)
|
448 |
+
throw VideoException ("mmap");
|
449 |
+
}
|
450 |
+
}
|
451 |
+
|
452 |
+
void V4lVideo::init_userp(const char* /*dev_name*/, unsigned int buffer_size)
|
453 |
+
{
|
454 |
+
struct v4l2_requestbuffers req;
|
455 |
+
unsigned int page_size;
|
456 |
+
|
457 |
+
page_size = getpagesize ();
|
458 |
+
buffer_size = (buffer_size + page_size - 1) & ~(page_size - 1);
|
459 |
+
|
460 |
+
CLEAR (req);
|
461 |
+
|
462 |
+
req.count = 4;
|
463 |
+
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
464 |
+
req.memory = V4L2_MEMORY_USERPTR;
|
465 |
+
|
466 |
+
if (-1 == xioctl (fd, VIDIOC_REQBUFS, &req)) {
|
467 |
+
if (EINVAL == errno) {
|
468 |
+
throw VideoException( "Does not support user pointer i/o", strerror(errno));
|
469 |
+
} else {
|
470 |
+
throw VideoException ("VIDIOC_REQBUFS", strerror(errno));
|
471 |
+
}
|
472 |
+
}
|
473 |
+
|
474 |
+
buffers = (buffer*)calloc(4, sizeof(buffer));
|
475 |
+
|
476 |
+
if (!buffers) {
|
477 |
+
throw VideoException( "Out of memory\n");
|
478 |
+
}
|
479 |
+
|
480 |
+
for (n_buffers = 0; n_buffers < 4; ++n_buffers) {
|
481 |
+
buffers[n_buffers].length = buffer_size;
|
482 |
+
buffers[n_buffers].start = memalign (/* boundary */ page_size,
|
483 |
+
buffer_size);
|
484 |
+
|
485 |
+
if (!buffers[n_buffers].start) {
|
486 |
+
throw VideoException( "Out of memory\n");
|
487 |
+
}
|
488 |
+
}
|
489 |
+
}
|
490 |
+
|
491 |
+
void V4lVideo::init_device(const char* dev_name, unsigned iwidth, unsigned iheight, unsigned ifps, unsigned v4l_format, v4l2_field field)
|
492 |
+
{
|
493 |
+
struct v4l2_capability cap;
|
494 |
+
struct v4l2_cropcap cropcap;
|
495 |
+
struct v4l2_crop crop;
|
496 |
+
struct v4l2_format fmt;
|
497 |
+
struct v4l2_streamparm strm;
|
498 |
+
|
499 |
+
unsigned int min;
|
500 |
+
|
501 |
+
if (-1 == xioctl (fd, VIDIOC_QUERYCAP, &cap)) {
|
502 |
+
if (EINVAL == errno) {
|
503 |
+
throw VideoException("Not a V4L2 device", strerror(errno));
|
504 |
+
} else {
|
505 |
+
throw VideoException ("VIDIOC_QUERYCAP", strerror(errno));
|
506 |
+
}
|
507 |
+
}
|
508 |
+
|
509 |
+
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
|
510 |
+
throw VideoException("Not a video capture device");
|
511 |
+
}
|
512 |
+
|
513 |
+
switch (io) {
|
514 |
+
case IO_METHOD_READ:
|
515 |
+
if (!(cap.capabilities & V4L2_CAP_READWRITE)) {
|
516 |
+
throw VideoException("Does not support read i/o");
|
517 |
+
}
|
518 |
+
|
519 |
+
break;
|
520 |
+
|
521 |
+
case IO_METHOD_MMAP:
|
522 |
+
case IO_METHOD_USERPTR:
|
523 |
+
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
|
524 |
+
throw VideoException("Does not support streaming i/o");
|
525 |
+
}
|
526 |
+
|
527 |
+
break;
|
528 |
+
}
|
529 |
+
|
530 |
+
|
531 |
+
/* Select video input, video standard and tune here. */
|
532 |
+
|
533 |
+
CLEAR (cropcap);
|
534 |
+
|
535 |
+
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
536 |
+
|
537 |
+
if (0 == xioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
|
538 |
+
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
539 |
+
crop.c = cropcap.defrect; /* reset to default */
|
540 |
+
|
541 |
+
if (-1 == xioctl (fd, VIDIOC_S_CROP, &crop)) {
|
542 |
+
switch (errno) {
|
543 |
+
case EINVAL:
|
544 |
+
/* Cropping not supported. */
|
545 |
+
break;
|
546 |
+
default:
|
547 |
+
/* Errors ignored. */
|
548 |
+
break;
|
549 |
+
}
|
550 |
+
}
|
551 |
+
} else {
|
552 |
+
/* Errors ignored. */
|
553 |
+
}
|
554 |
+
|
555 |
+
CLEAR (fmt);
|
556 |
+
|
557 |
+
if(iwidth!=0 && iheight!=0) {
|
558 |
+
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
559 |
+
fmt.fmt.pix.width = iwidth;
|
560 |
+
fmt.fmt.pix.height = iheight;
|
561 |
+
fmt.fmt.pix.pixelformat = v4l_format;
|
562 |
+
fmt.fmt.pix.field = field;
|
563 |
+
|
564 |
+
if (-1 == xioctl (fd, VIDIOC_S_FMT, &fmt))
|
565 |
+
throw VideoException("VIDIOC_S_FMT", strerror(errno));
|
566 |
+
}else{
|
567 |
+
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
568 |
+
|
569 |
+
/* Preserve original settings as set by v4l2-ctl for example */
|
570 |
+
if (-1 == xioctl(fd, VIDIOC_G_FMT, &fmt))
|
571 |
+
throw VideoException("VIDIOC_G_FMT", strerror(errno));
|
572 |
+
}
|
573 |
+
|
574 |
+
/* Buggy driver paranoia. */
|
575 |
+
min = fmt.fmt.pix.width * 2;
|
576 |
+
if (fmt.fmt.pix.bytesperline < min)
|
577 |
+
fmt.fmt.pix.bytesperline = min;
|
578 |
+
min = fmt.fmt.pix.bytesperline * fmt.fmt.pix.height;
|
579 |
+
if (fmt.fmt.pix.sizeimage < min)
|
580 |
+
fmt.fmt.pix.sizeimage = min;
|
581 |
+
|
582 |
+
/* Note VIDIOC_S_FMT may change width and height. */
|
583 |
+
width = fmt.fmt.pix.width;
|
584 |
+
height = fmt.fmt.pix.height;
|
585 |
+
image_size = fmt.fmt.pix.sizeimage;
|
586 |
+
|
587 |
+
if(ifps!=0)
|
588 |
+
{
|
589 |
+
CLEAR(strm);
|
590 |
+
strm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
|
591 |
+
strm.parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
|
592 |
+
strm.parm.capture.timeperframe.numerator = 1;
|
593 |
+
strm.parm.capture.timeperframe.denominator = ifps;
|
594 |
+
|
595 |
+
if (-1 == xioctl (fd, VIDIOC_S_PARM, &fmt))
|
596 |
+
throw VideoException("VIDIOC_S_PARM", strerror(errno));
|
597 |
+
|
598 |
+
fps = (float)strm.parm.capture.timeperframe.denominator / strm.parm.capture.timeperframe.numerator;
|
599 |
+
}else{
|
600 |
+
fps = 0;
|
601 |
+
}
|
602 |
+
|
603 |
+
switch (io) {
|
604 |
+
case IO_METHOD_READ:
|
605 |
+
init_read (fmt.fmt.pix.sizeimage);
|
606 |
+
break;
|
607 |
+
|
608 |
+
case IO_METHOD_MMAP:
|
609 |
+
init_mmap (dev_name );
|
610 |
+
break;
|
611 |
+
|
612 |
+
case IO_METHOD_USERPTR:
|
613 |
+
init_userp (dev_name, fmt.fmt.pix.sizeimage);
|
614 |
+
break;
|
615 |
+
}
|
616 |
+
|
617 |
+
uint32_t bit_depth = 0;
|
618 |
+
|
619 |
+
std::string spix="GRAY8";
|
620 |
+
if(fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_GREY) {
|
621 |
+
spix="GRAY8";
|
622 |
+
bit_depth = 8;
|
623 |
+
}else if(fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) {
|
624 |
+
spix="YUYV422";
|
625 |
+
bit_depth = 8;
|
626 |
+
} else if(fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_UYVY) {
|
627 |
+
spix="UYVY422";
|
628 |
+
bit_depth = 8;
|
629 |
+
}else if(fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_Y16) {
|
630 |
+
spix="GRAY16LE";
|
631 |
+
bit_depth = 16;
|
632 |
+
}else if(fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_Y10) {
|
633 |
+
spix="GRAY10";
|
634 |
+
bit_depth = 10;
|
635 |
+
}else if(fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_Y12) {
|
636 |
+
spix="GRAY12";
|
637 |
+
bit_depth = 12;
|
638 |
+
}else if(fmt.fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24) {
|
639 |
+
spix="RGB24";
|
640 |
+
bit_depth = 24;
|
641 |
+
}else{
|
642 |
+
// TODO: Add method to translate from V4L to FFMPEG type.
|
643 |
+
std::cerr << "V4L Format " << V4lToString(fmt.fmt.pix.pixelformat)
|
644 |
+
<< " not recognised. Defaulting to '" << spix << std::endl;
|
645 |
+
}
|
646 |
+
|
647 |
+
PixelFormat pfmt = PixelFormatFromString(spix);
|
648 |
+
pfmt.channel_bit_depth = bit_depth;
|
649 |
+
const StreamInfo stream_info(pfmt, width, height, (width*pfmt.bpp)/8, 0);
|
650 |
+
|
651 |
+
streams.push_back(stream_info);
|
652 |
+
}
|
653 |
+
|
654 |
+
bool V4lVideo::SetExposure(int exposure_us)
|
655 |
+
{
|
656 |
+
struct v4l2_ext_controls ctrls = {};
|
657 |
+
struct v4l2_ext_control ctrl = {};
|
658 |
+
|
659 |
+
ctrl.id = V4L2_CID_EXPOSURE_ABSOLUTE;
|
660 |
+
// v4l specifies exposure in 100us units
|
661 |
+
ctrl.value = int(exposure_us / 100.0);
|
662 |
+
ctrls.ctrl_class = V4L2_CTRL_CLASS_CAMERA;
|
663 |
+
ctrls.count = 1;
|
664 |
+
ctrls.controls = &ctrl;
|
665 |
+
|
666 |
+
if (-1 == xioctl(fd, VIDIOC_S_EXT_CTRLS, &ctrls)){
|
667 |
+
pango_print_warn("V4lVideo::SetExposure() ioctl error: %s\n", strerror(errno));
|
668 |
+
return false;
|
669 |
+
} else {
|
670 |
+
return true;
|
671 |
+
}
|
672 |
+
}
|
673 |
+
|
674 |
+
bool V4lVideo::GetExposure(int& exposure_us)
|
675 |
+
{
|
676 |
+
struct v4l2_ext_controls ctrls = {};
|
677 |
+
struct v4l2_ext_control ctrl = {};
|
678 |
+
|
679 |
+
ctrl.id = V4L2_CID_EXPOSURE_ABSOLUTE;
|
680 |
+
ctrls.ctrl_class = V4L2_CTRL_CLASS_CAMERA;
|
681 |
+
ctrls.count = 1;
|
682 |
+
ctrls.controls = &ctrl;
|
683 |
+
|
684 |
+
if (-1 == xioctl(fd, VIDIOC_G_EXT_CTRLS, &ctrls)){
|
685 |
+
pango_print_warn("V4lVideo::GetExposure() ioctl error: %s\n", strerror(errno));
|
686 |
+
return false;
|
687 |
+
} else {
|
688 |
+
// v4l specifies exposure in 100us units
|
689 |
+
exposure_us = ctrls.controls->value * 100;
|
690 |
+
return true;
|
691 |
+
}
|
692 |
+
}
|
693 |
+
|
694 |
+
bool V4lVideo::SetGain(float gain)
|
695 |
+
{
|
696 |
+
struct v4l2_control control;
|
697 |
+
control.id = V4L2_CID_GAIN;
|
698 |
+
control.value = gain;
|
699 |
+
|
700 |
+
if (-1 == xioctl (fd, VIDIOC_S_CTRL, &control)) {
|
701 |
+
pango_print_warn("V4lVideo::SetGain() ioctl error: %s\n", strerror(errno));
|
702 |
+
return false;
|
703 |
+
} else {
|
704 |
+
return true;
|
705 |
+
}
|
706 |
+
}
|
707 |
+
|
708 |
+
bool V4lVideo::GetGain(float& gain)
|
709 |
+
{
|
710 |
+
struct v4l2_control control;
|
711 |
+
control.id = V4L2_CID_GAIN;
|
712 |
+
|
713 |
+
if (-1 == xioctl (fd, VIDIOC_G_CTRL, &control)) {
|
714 |
+
pango_print_warn("V4lVideo::GetGain() ioctl error: %s\n", strerror(errno));
|
715 |
+
return false;
|
716 |
+
} else {
|
717 |
+
gain = control.value;
|
718 |
+
return true;
|
719 |
+
}
|
720 |
+
}
|
721 |
+
|
722 |
+
void V4lVideo::close_device()
|
723 |
+
{
|
724 |
+
if (-1 == close (fd))
|
725 |
+
throw VideoException("close");
|
726 |
+
|
727 |
+
fd = -1;
|
728 |
+
}
|
729 |
+
|
730 |
+
void V4lVideo::open_device(const char* dev_name)
|
731 |
+
{
|
732 |
+
struct stat st;
|
733 |
+
|
734 |
+
if (-1 == stat (dev_name, &st)) {
|
735 |
+
throw VideoException("Cannot stat device", strerror(errno));
|
736 |
+
}
|
737 |
+
|
738 |
+
if (!S_ISCHR (st.st_mode)) {
|
739 |
+
throw VideoException("Not device");
|
740 |
+
}
|
741 |
+
|
742 |
+
fd = open (dev_name, O_RDWR /* required */ | O_NONBLOCK, 0);
|
743 |
+
|
744 |
+
if (-1 == fd) {
|
745 |
+
throw VideoException("Cannot open device");
|
746 |
+
}
|
747 |
+
}
|
748 |
+
|
749 |
+
int V4lVideo::IoCtrl(uint8_t unit, uint8_t ctrl, unsigned char* data, int len, UvcRequestCode req_code)
|
750 |
+
{
|
751 |
+
struct uvc_xu_control_query xu;
|
752 |
+
xu.unit = unit;
|
753 |
+
xu.selector = ctrl;
|
754 |
+
xu.size = len;
|
755 |
+
xu.data = data;
|
756 |
+
xu.query = req_code;
|
757 |
+
|
758 |
+
int ret = ioctl(fd, UVCIOC_CTRL_QUERY, &xu);
|
759 |
+
if (ret == -1) {
|
760 |
+
pango_print_warn("V4lVideo::IoCtrl() ioctl error: %d\n", errno);
|
761 |
+
return ret;
|
762 |
+
}
|
763 |
+
return 0;
|
764 |
+
}
|
765 |
+
|
766 |
+
//! Access JSON properties of device
|
767 |
+
const picojson::value& V4lVideo::DeviceProperties() const
|
768 |
+
{
|
769 |
+
return device_properties;
|
770 |
+
}
|
771 |
+
|
772 |
+
//! Access JSON properties of most recently captured frame
|
773 |
+
const picojson::value& V4lVideo::FrameProperties() const
|
774 |
+
{
|
775 |
+
return frame_properties;
|
776 |
+
}
|
777 |
+
|
778 |
+
PANGOLIN_REGISTER_FACTORY(V4lVideo)
|
779 |
+
{
|
780 |
+
struct V4lVideoFactory final : public TypedFactoryInterface<VideoInterface> {
|
781 |
+
std::map<std::string,Precedence> Schemes() const override
|
782 |
+
{
|
783 |
+
return {{"v4l",0}, {"uvc",20}};
|
784 |
+
}
|
785 |
+
const char* Description() const override
|
786 |
+
{
|
787 |
+
return "Use V4L to open a video device";
|
788 |
+
}
|
789 |
+
ParamSet Params() const override
|
790 |
+
{
|
791 |
+
return {{
|
792 |
+
{"method","mmap","Possible values are: read, mmap, userptr"},
|
793 |
+
{"size","0x0","Desired image size"},
|
794 |
+
{"format","YUYV422","Desired image format"},
|
795 |
+
{"period","50000","Period in microsecs"},
|
796 |
+
{"ExposureTime","10000","Exposure time in microsecs"},
|
797 |
+
{"Gain","1","Image gain parameter"}
|
798 |
+
}};
|
799 |
+
}
|
800 |
+
std::unique_ptr<VideoInterface> Open(const Uri& uri) override {
|
801 |
+
ParamReader reader(Params(), uri);
|
802 |
+
|
803 |
+
const std::string smethod = reader.Get<std::string>("method");
|
804 |
+
const ImageDim desired_dim = reader.Get<ImageDim>("size");
|
805 |
+
const std::string sformat = reader.Get<std::string>("format");
|
806 |
+
|
807 |
+
unsigned format = V4L2_PIX_FMT_YUYV;
|
808 |
+
|
809 |
+
if(sformat == "GRAY8") {
|
810 |
+
format = V4L2_PIX_FMT_GREY;
|
811 |
+
} else if(sformat == "YUYV422") {
|
812 |
+
format = V4L2_PIX_FMT_YUYV;
|
813 |
+
} else if(sformat == "UYVY422") {
|
814 |
+
format = V4L2_PIX_FMT_UYVY;
|
815 |
+
} else if(sformat == "GRAY16LE") {
|
816 |
+
format = V4L2_PIX_FMT_Y16;
|
817 |
+
} else if(sformat == "GRAY10") {
|
818 |
+
format = V4L2_PIX_FMT_Y10;
|
819 |
+
} else if(sformat == "GRAY12") {
|
820 |
+
format = V4L2_PIX_FMT_Y12;
|
821 |
+
} else if(sformat == "RGB24") {
|
822 |
+
format = V4L2_PIX_FMT_RGB24;
|
823 |
+
}
|
824 |
+
|
825 |
+
io_method method = IO_METHOD_MMAP;
|
826 |
+
|
827 |
+
if(smethod == "read" ) {
|
828 |
+
method = IO_METHOD_READ;
|
829 |
+
}else if(smethod == "mmap" ) {
|
830 |
+
method = IO_METHOD_MMAP;
|
831 |
+
}else if(smethod == "userptr" ) {
|
832 |
+
method = IO_METHOD_USERPTR;
|
833 |
+
}
|
834 |
+
|
835 |
+
uint32_t period = reader.Get<int>("period");
|
836 |
+
|
837 |
+
V4lVideo* video_raw = new V4lVideo(uri.url.c_str(), period, method, desired_dim.x, desired_dim.y, format);
|
838 |
+
if(video_raw && uri.Contains("ExposureTime")) {
|
839 |
+
static_cast<V4lVideo*>(video_raw)->SetExposure(reader.Get<int>("ExposureTime"));
|
840 |
+
}
|
841 |
+
if(video_raw && uri.Contains("Gain")) {
|
842 |
+
static_cast<V4lVideo*>(video_raw)->SetGain(reader.Get<int>("Gain"));
|
843 |
+
}
|
844 |
+
return std::unique_ptr<VideoInterface>(video_raw);
|
845 |
+
}
|
846 |
+
};
|
847 |
+
|
848 |
+
return FactoryRegistry::I()->RegisterFactory<VideoInterface>(std::make_shared<V4lVideoFactory>());
|
849 |
+
}
|
850 |
+
|
851 |
+
|
852 |
+
}
|
third-party/DPVO/Pangolin/components/pango_video/src/stream_encoder_factory.cpp
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/video/stream_encoder_factory.h>
|
2 |
+
|
3 |
+
#include <cctype>
|
4 |
+
#include <pangolin/utils/file_utils.h>
|
5 |
+
#include <pangolin/utils/type_convert.h>
|
6 |
+
|
7 |
+
namespace pangolin {
|
8 |
+
|
9 |
+
StreamEncoderFactory& StreamEncoderFactory::I()
|
10 |
+
{
|
11 |
+
static StreamEncoderFactory instance;
|
12 |
+
return instance;
|
13 |
+
}
|
14 |
+
|
15 |
+
struct EncoderDetails
|
16 |
+
{
|
17 |
+
std::string encoder_name;
|
18 |
+
ImageFileType file_type;
|
19 |
+
float quality;
|
20 |
+
};
|
21 |
+
|
22 |
+
inline EncoderDetails EncoderDetailsFromString(const std::string& encoder_spec)
|
23 |
+
{
|
24 |
+
std::string::const_reverse_iterator rit = encoder_spec.rbegin();
|
25 |
+
for(; std::isdigit(*rit) && rit != encoder_spec.rend(); ++rit );
|
26 |
+
|
27 |
+
// png, tga, ...
|
28 |
+
std::string encoder_name(encoder_spec.begin(), rit.base());
|
29 |
+
ToLower(encoder_name);
|
30 |
+
|
31 |
+
// Quality of encoding for lossy encoders [0..100]
|
32 |
+
float quality = 100.0;
|
33 |
+
if(rit != encoder_spec.rbegin()) {
|
34 |
+
quality = pangolin::Convert<float,std::string>::Do(std::string(rit.base(),encoder_spec.end()));
|
35 |
+
}
|
36 |
+
|
37 |
+
|
38 |
+
return { encoder_name, NameToImageFileType(encoder_name), quality};
|
39 |
+
}
|
40 |
+
|
41 |
+
ImageEncoderFunc StreamEncoderFactory::GetEncoder(const std::string& encoder_spec, const PixelFormat& fmt)
|
42 |
+
{
|
43 |
+
const EncoderDetails encdet = EncoderDetailsFromString(encoder_spec);
|
44 |
+
if(encdet.file_type == ImageFileTypeUnknown)
|
45 |
+
throw std::invalid_argument("Unsupported encoder format: " + encoder_spec);
|
46 |
+
|
47 |
+
return [fmt,encdet](std::ostream& os, const Image<unsigned char>& img){
|
48 |
+
SaveImage(img,fmt,os,encdet.file_type,true,encdet.quality);
|
49 |
+
};
|
50 |
+
}
|
51 |
+
|
52 |
+
ImageDecoderFunc StreamEncoderFactory::GetDecoder(const std::string& encoder_spec, const PixelFormat& fmt)
|
53 |
+
{
|
54 |
+
const EncoderDetails encdet = EncoderDetailsFromString(encoder_spec);
|
55 |
+
PANGO_ENSURE(encdet.file_type != ImageFileTypeUnknown);
|
56 |
+
|
57 |
+
return [fmt,encdet](std::istream& is){
|
58 |
+
return LoadImage(is,encdet.file_type);
|
59 |
+
};
|
60 |
+
}
|
61 |
+
|
62 |
+
}
|
third-party/DPVO/Pangolin/components/pango_video/src/video.cpp
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2011 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#include <pangolin/video/video.h>
|
29 |
+
#include <pangolin/video/video_output.h>
|
30 |
+
#include <pangolin/factory/factory_registry.h>
|
31 |
+
#include <pangolin/factory/RegisterFactoriesVideoInterface.h>
|
32 |
+
#include <pangolin/factory/RegisterFactoriesVideoOutputInterface.h>
|
33 |
+
|
34 |
+
namespace pangolin
|
35 |
+
{
|
36 |
+
|
37 |
+
std::unique_ptr<VideoInterface> OpenVideo(const std::string& str_uri)
|
38 |
+
{
|
39 |
+
return OpenVideo( ParseUri(str_uri) );
|
40 |
+
}
|
41 |
+
|
42 |
+
std::unique_ptr<VideoInterface> OpenVideo(const Uri& uri)
|
43 |
+
{
|
44 |
+
RegisterFactoriesVideoInterface();
|
45 |
+
|
46 |
+
std::unique_ptr<VideoInterface> video =
|
47 |
+
FactoryRegistry::I()->Construct<VideoInterface>(uri);
|
48 |
+
|
49 |
+
if(!video) {
|
50 |
+
throw VideoExceptionNoKnownHandler(uri.scheme);
|
51 |
+
}
|
52 |
+
|
53 |
+
return video;
|
54 |
+
}
|
55 |
+
|
56 |
+
std::unique_ptr<VideoOutputInterface> OpenVideoOutput(const std::string& str_uri)
|
57 |
+
{
|
58 |
+
return OpenVideoOutput( ParseUri(str_uri) );
|
59 |
+
}
|
60 |
+
|
61 |
+
std::unique_ptr<VideoOutputInterface> OpenVideoOutput(const Uri& uri)
|
62 |
+
{
|
63 |
+
RegisterFactoriesVideoOutputInterface();
|
64 |
+
|
65 |
+
std::unique_ptr<VideoOutputInterface> video =
|
66 |
+
FactoryRegistry::I()->Construct<VideoOutputInterface>(uri);
|
67 |
+
|
68 |
+
if(!video) {
|
69 |
+
throw VideoException("No known video handler for URI '" + uri.scheme + "', or device not found.");
|
70 |
+
}
|
71 |
+
|
72 |
+
return video;
|
73 |
+
}
|
74 |
+
|
75 |
+
}
|
third-party/DPVO/Pangolin/components/pango_video/src/video_help.cpp
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/video/video_help.h>
|
2 |
+
#include <pangolin/video/video_interface.h>
|
3 |
+
|
4 |
+
#include <pangolin/factory/RegisterFactoriesVideoInterface.h>
|
5 |
+
#include <pangolin/factory/RegisterFactoriesVideoOutputInterface.h>
|
6 |
+
|
7 |
+
namespace pangolin
|
8 |
+
{
|
9 |
+
|
10 |
+
void PrintPixelFormats(std::ostream& out, bool color)
|
11 |
+
{
|
12 |
+
const std::string c_normal = color ? "\033[0m" : "";
|
13 |
+
const std::string c_alias = color ? "\033[32m" : "";
|
14 |
+
|
15 |
+
out << "Supported pixel format codes (and their respective bits-per-pixel):" << std::endl;
|
16 |
+
std::vector<pangolin::PixelFormat> pixelFormats = pangolin::GetSupportedPixelFormats();
|
17 |
+
for(const auto& format: pixelFormats ){
|
18 |
+
out << c_alias << format.format << c_normal << " (" << format.bpp << "), ";
|
19 |
+
}
|
20 |
+
out << std::endl;
|
21 |
+
}
|
22 |
+
|
23 |
+
void VideoHelp( std::ostream& out, const std::string& scheme_filter, HelpVerbosity verbosity)
|
24 |
+
{
|
25 |
+
RegisterFactoriesVideoInterface();
|
26 |
+
|
27 |
+
#ifndef _WIN32_
|
28 |
+
const bool use_color = true;
|
29 |
+
#else
|
30 |
+
const bool use_color = false;
|
31 |
+
#endif
|
32 |
+
|
33 |
+
if( verbosity >= HelpVerbosity::SYNOPSIS ) {
|
34 |
+
PrintSchemeHelp(out, use_color);
|
35 |
+
out << std::endl;
|
36 |
+
}
|
37 |
+
|
38 |
+
PrintFactoryRegistryDetails(out, *FactoryRegistry::I(), typeid(VideoInterface), scheme_filter, verbosity, use_color);
|
39 |
+
|
40 |
+
if( verbosity >= HelpVerbosity::PARAMS ) {
|
41 |
+
PrintPixelFormats(out, use_color);
|
42 |
+
}
|
43 |
+
}
|
44 |
+
|
45 |
+
}
|
third-party/DPVO/Pangolin/components/pango_video/src/video_input.cpp
ADDED
@@ -0,0 +1,220 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2013 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#include <pangolin/video/video_input.h>
|
29 |
+
#include <pangolin/video/video_output.h>
|
30 |
+
|
31 |
+
namespace pangolin
|
32 |
+
{
|
33 |
+
|
34 |
+
VideoInput::VideoInput()
|
35 |
+
: frame_num(0), record_frame_skip(1), record_once(false), record_continuous(false)
|
36 |
+
{
|
37 |
+
}
|
38 |
+
|
39 |
+
VideoInput::VideoInput(
|
40 |
+
const std::string& input_uri,
|
41 |
+
const std::string& output_uri
|
42 |
+
) : frame_num(0), record_frame_skip(1), record_once(false), record_continuous(false)
|
43 |
+
{
|
44 |
+
Open(input_uri, output_uri);
|
45 |
+
}
|
46 |
+
|
47 |
+
void VideoInput::Open(
|
48 |
+
const std::string& input_uri,
|
49 |
+
const std::string& output_uri
|
50 |
+
)
|
51 |
+
{
|
52 |
+
uri_input = ParseUri(input_uri);
|
53 |
+
uri_output = ParseUri(output_uri);
|
54 |
+
|
55 |
+
if (uri_output.scheme == "file") {
|
56 |
+
// Default to pango output
|
57 |
+
uri_output.scheme = "pango";
|
58 |
+
}
|
59 |
+
|
60 |
+
// Start off playing from video_src
|
61 |
+
video_src = OpenVideo(input_uri);
|
62 |
+
|
63 |
+
// Reset state
|
64 |
+
frame_num = 0;
|
65 |
+
videos.resize(1);
|
66 |
+
videos[0] = video_src.get();
|
67 |
+
}
|
68 |
+
|
69 |
+
void VideoInput::Close()
|
70 |
+
{
|
71 |
+
// Reset this first so that recording data gets written out to disk ASAP.
|
72 |
+
video_recorder.reset();
|
73 |
+
|
74 |
+
video_src.reset();
|
75 |
+
videos.clear();
|
76 |
+
}
|
77 |
+
|
78 |
+
VideoInput::~VideoInput()
|
79 |
+
{
|
80 |
+
Close();
|
81 |
+
}
|
82 |
+
|
83 |
+
const std::string& VideoInput::LogFilename() const
|
84 |
+
{
|
85 |
+
return uri_output.url;
|
86 |
+
}
|
87 |
+
|
88 |
+
std::string& VideoInput::LogFilename()
|
89 |
+
{
|
90 |
+
return uri_output.url;
|
91 |
+
}
|
92 |
+
|
93 |
+
bool VideoInput::Grab( unsigned char* buffer, std::vector<Image<unsigned char> >& images, bool wait, bool newest)
|
94 |
+
{
|
95 |
+
if( !video_src ) throw VideoException("No video source open");
|
96 |
+
|
97 |
+
bool success;
|
98 |
+
|
99 |
+
if(newest) {
|
100 |
+
success = GrabNewest(buffer, wait);
|
101 |
+
}else{
|
102 |
+
success = GrabNext(buffer, wait);
|
103 |
+
}
|
104 |
+
|
105 |
+
if(success) {
|
106 |
+
images.clear();
|
107 |
+
for(size_t s=0; s < Streams().size(); ++s) {
|
108 |
+
images.push_back(Streams()[s].StreamImage(buffer));
|
109 |
+
}
|
110 |
+
}
|
111 |
+
|
112 |
+
return success;
|
113 |
+
}
|
114 |
+
|
115 |
+
void VideoInput::InitialiseRecorder()
|
116 |
+
{
|
117 |
+
video_recorder.reset();
|
118 |
+
video_recorder = OpenVideoOutput(uri_output);
|
119 |
+
video_recorder->SetStreams(
|
120 |
+
video_src->Streams(), uri_input.full_uri,
|
121 |
+
GetVideoDeviceProperties(video_src.get())
|
122 |
+
);
|
123 |
+
}
|
124 |
+
|
125 |
+
void VideoInput::Record()
|
126 |
+
{
|
127 |
+
// Switch sub-video
|
128 |
+
videos.resize(1);
|
129 |
+
videos[0] = video_src.get();
|
130 |
+
|
131 |
+
// Initialise recorder and ensure src is started
|
132 |
+
InitialiseRecorder();
|
133 |
+
video_src->Start();
|
134 |
+
frame_num = 0;
|
135 |
+
record_continuous = true;
|
136 |
+
}
|
137 |
+
|
138 |
+
void VideoInput::RecordOneFrame()
|
139 |
+
{
|
140 |
+
// Append to existing video.
|
141 |
+
if(!video_recorder) {
|
142 |
+
InitialiseRecorder();
|
143 |
+
}
|
144 |
+
record_continuous = false;
|
145 |
+
record_once = true;
|
146 |
+
|
147 |
+
// Switch sub-video
|
148 |
+
videos.resize(1);
|
149 |
+
videos[0] = video_src.get();
|
150 |
+
}
|
151 |
+
|
152 |
+
size_t VideoInput::SizeBytes() const
|
153 |
+
{
|
154 |
+
if( !video_src ) throw VideoException("No video source open");
|
155 |
+
return video_src->SizeBytes();
|
156 |
+
}
|
157 |
+
|
158 |
+
const std::vector<StreamInfo>& VideoInput::Streams() const
|
159 |
+
{
|
160 |
+
return video_src->Streams();
|
161 |
+
}
|
162 |
+
|
163 |
+
void VideoInput::Start()
|
164 |
+
{
|
165 |
+
video_src->Start();
|
166 |
+
}
|
167 |
+
|
168 |
+
void VideoInput::Stop()
|
169 |
+
{
|
170 |
+
if(IsRecording()) {
|
171 |
+
video_recorder.reset();
|
172 |
+
}else{
|
173 |
+
video_src->Stop();
|
174 |
+
}
|
175 |
+
}
|
176 |
+
|
177 |
+
bool VideoInput::GrabNext( unsigned char* image, bool wait )
|
178 |
+
{
|
179 |
+
frame_num++;
|
180 |
+
|
181 |
+
const bool should_record = (record_continuous && !(frame_num % record_frame_skip)) || record_once;
|
182 |
+
|
183 |
+
const bool success = video_src->GrabNext(image, wait);
|
184 |
+
|
185 |
+
if( should_record && video_recorder != 0 && success) {
|
186 |
+
video_recorder->WriteStreams(image, GetVideoFrameProperties(video_src.get()) );
|
187 |
+
record_once = false;
|
188 |
+
}
|
189 |
+
|
190 |
+
return success;
|
191 |
+
}
|
192 |
+
|
193 |
+
bool VideoInput::GrabNewest( unsigned char* image, bool wait )
|
194 |
+
{
|
195 |
+
frame_num++;
|
196 |
+
|
197 |
+
const bool should_record = (record_continuous && !(frame_num % record_frame_skip)) || record_once;
|
198 |
+
const bool success = video_src->GrabNewest(image,wait);
|
199 |
+
|
200 |
+
if( should_record && video_recorder != 0 && success)
|
201 |
+
{
|
202 |
+
video_recorder->WriteStreams(image, GetVideoFrameProperties(video_src.get()) );
|
203 |
+
record_once = false;
|
204 |
+
}
|
205 |
+
|
206 |
+
return success;
|
207 |
+
}
|
208 |
+
|
209 |
+
void VideoInput::SetTimelapse(size_t one_in_n_frames)
|
210 |
+
{
|
211 |
+
record_frame_skip = one_in_n_frames;
|
212 |
+
}
|
213 |
+
|
214 |
+
bool VideoInput::IsRecording() const
|
215 |
+
{
|
216 |
+
return video_recorder != 0;
|
217 |
+
}
|
218 |
+
|
219 |
+
}
|
220 |
+
|
third-party/DPVO/Pangolin/components/pango_video/src/video_output.cpp
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2011-2013 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#include <pangolin/video/video.h>
|
29 |
+
#include <pangolin/video/video_output.h>
|
30 |
+
|
31 |
+
#include <pangolin/video/drivers/pango_video_output.h>
|
32 |
+
|
33 |
+
#include <pangolin/utils/file_utils.h>
|
34 |
+
|
35 |
+
namespace pangolin
|
36 |
+
{
|
37 |
+
VideoOutput::VideoOutput()
|
38 |
+
{
|
39 |
+
}
|
40 |
+
|
41 |
+
VideoOutput::VideoOutput(const std::string& uri)
|
42 |
+
{
|
43 |
+
Open(uri);
|
44 |
+
}
|
45 |
+
|
46 |
+
VideoOutput::~VideoOutput()
|
47 |
+
{
|
48 |
+
}
|
49 |
+
|
50 |
+
bool VideoOutput::IsOpen() const
|
51 |
+
{
|
52 |
+
return recorder.get() != nullptr;
|
53 |
+
}
|
54 |
+
|
55 |
+
void VideoOutput::Open(const std::string& str_uri)
|
56 |
+
{
|
57 |
+
Close();
|
58 |
+
uri = ParseUri(str_uri);
|
59 |
+
recorder = OpenVideoOutput(uri);
|
60 |
+
}
|
61 |
+
|
62 |
+
void VideoOutput::Close()
|
63 |
+
{
|
64 |
+
recorder.reset();
|
65 |
+
}
|
66 |
+
|
67 |
+
const std::vector<StreamInfo>& VideoOutput::Streams() const
|
68 |
+
{
|
69 |
+
return recorder->Streams();
|
70 |
+
}
|
71 |
+
|
72 |
+
void VideoOutput::SetStreams(const std::vector<StreamInfo>& streams,
|
73 |
+
const std::string& uri,
|
74 |
+
const picojson::value& properties)
|
75 |
+
{
|
76 |
+
recorder->SetStreams(streams, uri, properties);
|
77 |
+
}
|
78 |
+
|
79 |
+
int VideoOutput::WriteStreams(const unsigned char* data, const picojson::value& frame_properties)
|
80 |
+
{
|
81 |
+
return recorder->WriteStreams(data, frame_properties);
|
82 |
+
}
|
83 |
+
|
84 |
+
bool VideoOutput::IsPipe() const
|
85 |
+
{
|
86 |
+
return recorder->IsPipe();
|
87 |
+
}
|
88 |
+
|
89 |
+
void VideoOutput::AddStream(const PixelFormat& pf, size_t w, size_t h, size_t pitch)
|
90 |
+
{
|
91 |
+
streams.emplace_back(pf, w, h, pitch, nullptr);
|
92 |
+
}
|
93 |
+
|
94 |
+
void VideoOutput::AddStream(const PixelFormat& pf, size_t w, size_t h)
|
95 |
+
{
|
96 |
+
AddStream(pf, w, h, w * pf.bpp / 8);
|
97 |
+
}
|
98 |
+
|
99 |
+
void VideoOutput::SetStreams(const std::string& uri, const picojson::value& properties)
|
100 |
+
{
|
101 |
+
size_t offset = 0;
|
102 |
+
for(size_t i = 0; i < streams.size(); i++)
|
103 |
+
{
|
104 |
+
streams[i] = StreamInfo(streams[i].PixFormat(),
|
105 |
+
streams[i].Width(),
|
106 |
+
streams[i].Height(),
|
107 |
+
streams[i].Pitch(),
|
108 |
+
(unsigned char*)offset);
|
109 |
+
offset += streams[i].SizeBytes();
|
110 |
+
}
|
111 |
+
SetStreams(streams, uri, properties);
|
112 |
+
}
|
113 |
+
|
114 |
+
size_t VideoOutput::SizeBytes(void) const
|
115 |
+
{
|
116 |
+
size_t total = 0;
|
117 |
+
for(const StreamInfo& si : recorder->Streams())
|
118 |
+
total += si.SizeBytes();
|
119 |
+
return total;
|
120 |
+
}
|
121 |
+
|
122 |
+
std::vector<Image<unsigned char>> VideoOutput::GetOutputImages(unsigned char* buffer) const
|
123 |
+
{
|
124 |
+
std::vector<Image<unsigned char>> images;
|
125 |
+
for(size_t s = 0; s < recorder->Streams().size(); ++s)
|
126 |
+
{
|
127 |
+
images.push_back(recorder->Streams()[s].StreamImage(buffer));
|
128 |
+
}
|
129 |
+
return images;
|
130 |
+
}
|
131 |
+
|
132 |
+
std::vector<Image<unsigned char>> VideoOutput::GetOutputImages(std::vector<unsigned char>& buffer) const
|
133 |
+
{
|
134 |
+
buffer.resize(SizeBytes());
|
135 |
+
return GetOutputImages(buffer.data());
|
136 |
+
}
|
137 |
+
}
|
third-party/DPVO/Pangolin/components/pango_video/tests/tests_video_loading.cpp
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#define CATCH_CONFIG_MAIN
|
2 |
+
#include <catch2/catch.hpp>
|
3 |
+
|
4 |
+
#include <pangolin/video/video.h>
|
5 |
+
#include <pangolin/factory/factory_registry.h>
|
6 |
+
|
7 |
+
TEST_CASE( "Loading built in video driver" ) {
|
8 |
+
// If this throws, we've probably messed up the factory loading stuff again...
|
9 |
+
auto video = pangolin::OpenVideo("test:[size=123x345,n=1,fmt=RGB24]//");
|
10 |
+
|
11 |
+
REQUIRE(video.get());
|
12 |
+
REQUIRE(video->SizeBytes() == 123*345*3);
|
13 |
+
REQUIRE(video->Streams().size() == 1);
|
14 |
+
REQUIRE(video->Streams()[0].PixFormat().format == "RGB24");
|
15 |
+
REQUIRE(video->Streams()[0].Width() == 123);
|
16 |
+
REQUIRE(video->Streams()[0].Height() == 345);
|
17 |
+
|
18 |
+
std::unique_ptr<unsigned char[]> image(new unsigned char[video->SizeBytes()]);
|
19 |
+
const bool success = video->GrabNext(image.get());
|
20 |
+
REQUIRE(success);
|
21 |
+
}
|
22 |
+
|
23 |
+
TEST_CASE( "Error when providing the wrong arguments" )
|
24 |
+
{
|
25 |
+
REQUIRE_THROWS_AS(pangolin::OpenVideo("test:[width=123,height=345,n=3,fmt=RGB24]//"), pangolin::FactoryRegistry::ParameterMismatchException);
|
26 |
+
}
|
third-party/DPVO/Pangolin/components/pango_video/tests/tests_video_uri.cpp
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#define CATCH_CONFIG_MAIN
|
2 |
+
#include <catch2/catch.hpp>
|
3 |
+
|
4 |
+
#include <string>
|
5 |
+
#include <limits>
|
6 |
+
#include <functional>
|
7 |
+
#include <pangolin/factory/factory_registry.h>
|
8 |
+
#include <pangolin/video/video.h>
|
9 |
+
#include <pangolin/video/video_exception.h>
|
10 |
+
#include <pangolin/video/iostream_operators.h>
|
11 |
+
|
12 |
+
template<typename T>
|
13 |
+
inline void ExpectExceptionWithMessageFromAction(
|
14 |
+
std::function<void()> action,
|
15 |
+
const std::string& exceptionMessageBegin
|
16 |
+
) {
|
17 |
+
try
|
18 |
+
{
|
19 |
+
action();
|
20 |
+
|
21 |
+
FAIL("The action succeeded when it should have failed.");
|
22 |
+
}
|
23 |
+
catch(const T& ex)
|
24 |
+
{
|
25 |
+
std::string exceptionMessage = std::string(ex.what());
|
26 |
+
std::string exceptionMessageRelevantPortion
|
27 |
+
= exceptionMessage.substr(0, exceptionMessageBegin.size());
|
28 |
+
REQUIRE(exceptionMessageRelevantPortion.compare(exceptionMessageBegin) == 0);
|
29 |
+
}
|
30 |
+
catch(...)
|
31 |
+
{
|
32 |
+
FAIL("Another kind of exception was thrown to the one expected");
|
33 |
+
}
|
34 |
+
}
|
35 |
+
|
36 |
+
TEST_CASE("Empty Uri Test")
|
37 |
+
{
|
38 |
+
REQUIRE_THROWS_AS(pangolin::OpenVideo(""), pangolin::VideoException);
|
39 |
+
}
|
40 |
+
|
41 |
+
TEST_CASE("Uri Dimension Legal Separator Character")
|
42 |
+
{
|
43 |
+
|
44 |
+
using char_t = std::string::value_type;
|
45 |
+
using char_nl = std::numeric_limits<char_t>;
|
46 |
+
for(char_t c = char_nl::min(); c < char_nl::max(); ++c)
|
47 |
+
{
|
48 |
+
if(c == '\0' || c == '=' || c == ']' || c == ',' || (c >= '0' && c <= '9'))
|
49 |
+
{
|
50 |
+
continue;
|
51 |
+
}
|
52 |
+
|
53 |
+
const int dimWidth = 123, dimHeight = 456;
|
54 |
+
const std::string encodedDim = std::to_string(dimWidth) + c
|
55 |
+
+ std::to_string(dimHeight);
|
56 |
+
const std::string fullUri = "abc:[size=" + encodedDim + ",other=value]";
|
57 |
+
const pangolin::Uri pangoUri = pangolin::ParseUri(fullUri);
|
58 |
+
|
59 |
+
REQUIRE(pangoUri.params.size() == 2);
|
60 |
+
REQUIRE( pangoUri.params[0].first == std::string("size"));
|
61 |
+
REQUIRE( pangoUri.params[1].first == std::string("other"));
|
62 |
+
|
63 |
+
const pangolin::ImageDim dim
|
64 |
+
= pangoUri.Get<pangolin::ImageDim>("size", pangolin::ImageDim(0,0));
|
65 |
+
REQUIRE( dim.x == dimWidth);
|
66 |
+
REQUIRE( dim.y == dimHeight);
|
67 |
+
|
68 |
+
const std::string otherValue = pangoUri.Get<std::string>("other", "");
|
69 |
+
REQUIRE( otherValue == std::string("value"));
|
70 |
+
|
71 |
+
}
|
72 |
+
}
|
third-party/DPVO/Pangolin/components/pango_windowing/CMakeLists.txt
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
get_filename_component(COMPONENT ${CMAKE_CURRENT_LIST_DIR} NAME)
|
2 |
+
|
3 |
+
if(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
|
4 |
+
target_link_libraries(${COMPONENT} PRIVATE "-framework Cocoa" )
|
5 |
+
target_sources( ${COMPONENT}
|
6 |
+
PRIVATE
|
7 |
+
${CMAKE_CURRENT_LIST_DIR}/src/display_osx.mm
|
8 |
+
${CMAKE_CURRENT_LIST_DIR}/src/PangolinNSApplication.mm
|
9 |
+
${CMAKE_CURRENT_LIST_DIR}/src/PangolinNSGLView.mm
|
10 |
+
)
|
11 |
+
target_compile_definitions(${COMPONENT} PUBLIC "PANGO_DEFAULT_WIN_URI=\"cocoa\"")
|
12 |
+
target_compile_options(${COMPONENT} PRIVATE
|
13 |
+
$<$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:GNU>>:
|
14 |
+
-Wno-deprecated-declarations>
|
15 |
+
)
|
16 |
+
PangolinRegisterFactory(WindowInterface OsxWindow)
|
17 |
+
elseif(WIN32 OR WIN64)
|
18 |
+
target_sources( ${COMPONENT} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/display_win.cpp )
|
19 |
+
target_compile_definitions(${COMPONENT} PUBLIC "PANGO_DEFAULT_WIN_URI=\"winapi\"")
|
20 |
+
PangolinRegisterFactory(WindowInterface WinWindow)
|
21 |
+
elseif(EMSCRIPTEN)
|
22 |
+
target_sources( ${COMPONENT} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/display_emscripten.cpp )
|
23 |
+
target_compile_definitions(${COMPONENT} PUBLIC "PANGO_DEFAULT_WIN_URI=\"emscripten\"")
|
24 |
+
PangolinRegisterFactory(WindowInterface EmscriptenWindow)
|
25 |
+
else()
|
26 |
+
find_package(X11 QUIET)
|
27 |
+
if(X11_FOUND)
|
28 |
+
target_sources( ${COMPONENT} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/display_x11.cpp )
|
29 |
+
target_link_libraries(${COMPONENT} PRIVATE ${X11_LIBRARIES} )
|
30 |
+
target_include_directories(${COMPONENT} PRIVATE ${X11_INCLUDE_DIR} )
|
31 |
+
PangolinRegisterFactory(WindowInterface X11Window)
|
32 |
+
endif()
|
33 |
+
|
34 |
+
# Wayland
|
35 |
+
find_package(PkgConfig)
|
36 |
+
pkg_check_modules(WAYLAND_CLIENT wayland-client QUIET)
|
37 |
+
pkg_check_modules(wayland-protocols QUIET wayland-protocols>=1.13)
|
38 |
+
if(WAYLAND_CLIENT_FOUND AND wayland-protocols_FOUND)
|
39 |
+
pkg_check_modules(WAYLAND_EGL wayland-egl REQUIRED)
|
40 |
+
pkg_check_modules(WAYLAND_CURSOR wayland-cursor REQUIRED)
|
41 |
+
pkg_check_modules(xkbcommon REQUIRED xkbcommon)
|
42 |
+
|
43 |
+
target_sources( ${COMPONENT} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/display_wayland.cpp )
|
44 |
+
|
45 |
+
target_link_libraries(${COMPONENT} PRIVATE ${X11_LIBRARIES}
|
46 |
+
${WAYLAND_CLIENT_LIBRARIES}
|
47 |
+
${WAYLAND_EGL_LIBRARIES}
|
48 |
+
${WAYLAND_CURSOR_LIBRARIES}
|
49 |
+
${xkbcommon_LIBRARIES}
|
50 |
+
)
|
51 |
+
|
52 |
+
# find Wayland protocols
|
53 |
+
pkg_get_variable(WAYLAND_PROTOCOLS_DIR wayland-protocols pkgdatadir)
|
54 |
+
|
55 |
+
# find 'wayland-scanner' executable
|
56 |
+
pkg_get_variable(WAYLAND_SCANNER wayland-scanner wayland_scanner)
|
57 |
+
|
58 |
+
# generate protocol implementation
|
59 |
+
set(XDG_PROT_DEF "${WAYLAND_PROTOCOLS_DIR}/stable/xdg-shell/xdg-shell.xml")
|
60 |
+
add_custom_command(
|
61 |
+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-client-protocol.h
|
62 |
+
COMMAND ${WAYLAND_SCANNER} client-header ${XDG_PROT_DEF} xdg-shell-client-protocol.h)
|
63 |
+
add_custom_command(
|
64 |
+
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/xdg-shell-protocol.c
|
65 |
+
COMMAND ${WAYLAND_SCANNER} private-code ${XDG_PROT_DEF} xdg-shell-protocol.c
|
66 |
+
DEPENDS xdg-shell-client-protocol.h)
|
67 |
+
target_include_directories(${COMPONENT} PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
|
68 |
+
target_sources( ${COMPONENT} PRIVATE xdg-shell-protocol.c)
|
69 |
+
|
70 |
+
# register window factory
|
71 |
+
target_compile_definitions(${COMPONENT} PUBLIC "PANGO_DEFAULT_WIN_URI=\"wayland\"")
|
72 |
+
PangolinRegisterFactory(WindowInterface WaylandWindow)
|
73 |
+
else()
|
74 |
+
target_compile_definitions(${COMPONENT} PUBLIC "PANGO_DEFAULT_WIN_URI=\"x11\"")
|
75 |
+
endif()
|
76 |
+
endif()
|
77 |
+
|
78 |
+
# headless offscreen rendering via EGL
|
79 |
+
if(_LINUX_)
|
80 |
+
set(OpenGL_GL_PREFERENCE "GLVND")
|
81 |
+
endif()
|
82 |
+
find_package(OpenGL QUIET COMPONENTS EGL)
|
83 |
+
if(OpenGL_EGL_FOUND)
|
84 |
+
target_sources( ${COMPONENT} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/display_headless.cpp )
|
85 |
+
target_link_libraries(${COMPONENT} PRIVATE ${OPENGL_egl_LIBRARY} )
|
86 |
+
PangolinRegisterFactory(WindowInterface HeadlessWindow)
|
87 |
+
endif()
|
88 |
+
|
89 |
+
|
90 |
+
target_sources(${COMPONENT} PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src/window.cpp)
|
91 |
+
target_link_libraries(${COMPONENT} PUBLIC pango_core pango_opengl )
|
92 |
+
target_include_directories(${COMPONENT} PUBLIC
|
93 |
+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
|
94 |
+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
|
95 |
+
$<INSTALL_INTERFACE:include>
|
96 |
+
)
|
97 |
+
install(DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/include" DESTINATION ${CMAKE_INSTALL_PREFIX})
|
98 |
+
install(DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include" DESTINATION ${CMAKE_INSTALL_PREFIX})
|
99 |
+
|
100 |
+
# Create RegisterFactoriesWindowInterface() method call file to load built drivers and add as source
|
101 |
+
create_factory_registry_file( "${CMAKE_CURRENT_BINARY_DIR}/include/pangolin/factory" WindowInterface )
|
102 |
+
target_sources(${COMPONENT} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include/pangolin/factory/RegisterFactoriesWindowInterface.h")
|
third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/EmscriptenWindow.h
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2018 Andrey Mnatsakanov
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#pragma once
|
29 |
+
|
30 |
+
#include <pangolin/windowing/window.h>
|
31 |
+
|
32 |
+
#include <stdexcept>
|
33 |
+
#include <string>
|
34 |
+
#include <list>
|
35 |
+
|
36 |
+
#include <emscripten.h>
|
37 |
+
#include <emscripten/html5.h>
|
38 |
+
|
39 |
+
namespace pangolin
|
40 |
+
{
|
41 |
+
|
42 |
+
struct EmscriptenWindow : public WindowInterface
|
43 |
+
{
|
44 |
+
public:
|
45 |
+
EmscriptenWindow();
|
46 |
+
|
47 |
+
~EmscriptenWindow();
|
48 |
+
|
49 |
+
void Move(int x, int y) override;
|
50 |
+
|
51 |
+
void Resize(unsigned int w, unsigned int h) override;
|
52 |
+
|
53 |
+
void ShowFullscreen(const TrueFalseToggle on_off) override;
|
54 |
+
|
55 |
+
void MakeCurrent() override;
|
56 |
+
|
57 |
+
void RemoveCurrent() override;
|
58 |
+
|
59 |
+
void SwapBuffers() override;
|
60 |
+
|
61 |
+
void ProcessEvents() override;
|
62 |
+
|
63 |
+
int x;
|
64 |
+
int y;
|
65 |
+
KeyModifierBitmask key_modifier_state;
|
66 |
+
private:
|
67 |
+
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = NULL;
|
68 |
+
GLuint program = 0;
|
69 |
+
bool done_init_events;
|
70 |
+
};
|
71 |
+
|
72 |
+
}
|
third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/OsxWindow.h
ADDED
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2011 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#pragma once
|
29 |
+
|
30 |
+
#include <pangolin/platform.h>
|
31 |
+
#include <pangolin/windowing/window.h>
|
32 |
+
#include <pangolin/windowing/PangolinNSApplication.h>
|
33 |
+
#include <pangolin/windowing/PangolinNSGLView.h>
|
34 |
+
|
35 |
+
namespace pangolin
|
36 |
+
{
|
37 |
+
|
38 |
+
struct OsxWindow : public WindowInterface
|
39 |
+
{
|
40 |
+
OsxWindow(const std::string& title, int width, int height, bool USE_RETINA);
|
41 |
+
|
42 |
+
~OsxWindow();
|
43 |
+
|
44 |
+
void ShowFullscreen(const TrueFalseToggle on_off) override;
|
45 |
+
|
46 |
+
void Move(int x, int y) override;
|
47 |
+
|
48 |
+
void Resize(unsigned int w, unsigned int h) override;
|
49 |
+
|
50 |
+
void MakeCurrent() override;
|
51 |
+
|
52 |
+
void RemoveCurrent() override;
|
53 |
+
|
54 |
+
void ProcessEvents() override;
|
55 |
+
|
56 |
+
void SwapBuffers() override;
|
57 |
+
|
58 |
+
private:
|
59 |
+
NSWindow* _window;
|
60 |
+
PangolinNSGLView *view;
|
61 |
+
};
|
62 |
+
|
63 |
+
}
|
third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/PangolinNSApplication.h
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2011 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#pragma once
|
29 |
+
|
30 |
+
#import <Carbon/Carbon.h>
|
31 |
+
#import <Cocoa/Cocoa.h>
|
32 |
+
#include <pangolin/windowing/window.h>
|
33 |
+
|
34 |
+
////////////////////////////////////////////////////////////////////
|
35 |
+
// PangolinNSApplication
|
36 |
+
////////////////////////////////////////////////////////////////////
|
37 |
+
|
38 |
+
@interface PangolinNSApplication : NSObject {
|
39 |
+
}
|
40 |
+
|
41 |
+
+ (void)run_pre;
|
42 |
+
+ (void)run_step;
|
43 |
+
|
44 |
+
@end
|
45 |
+
|
46 |
+
////////////////////////////////////////////////////////////////////
|
47 |
+
// PangolinWindowDelegate
|
48 |
+
////////////////////////////////////////////////////////////////////
|
49 |
+
|
50 |
+
@interface PangolinWindowDelegate : NSObject <NSWindowDelegate>{
|
51 |
+
@public pangolin::WindowInterface* osx_window;
|
52 |
+
}
|
53 |
+
@end
|
54 |
+
|
55 |
+
////////////////////////////////////////////////////////////////////
|
56 |
+
// PangolinAppDelegate
|
57 |
+
////////////////////////////////////////////////////////////////////
|
58 |
+
|
59 |
+
@interface PangolinAppDelegate : NSObject <NSApplicationDelegate>
|
60 |
+
|
61 |
+
@end
|
third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/PangolinNSGLView.h
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2011 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#pragma once
|
29 |
+
|
30 |
+
#import <Carbon/Carbon.h>
|
31 |
+
#import <Cocoa/Cocoa.h>
|
32 |
+
#include <pangolin/windowing/window.h>
|
33 |
+
|
34 |
+
////////////////////////////////////////////////////////////////////
|
35 |
+
// PangolinNSGLView
|
36 |
+
////////////////////////////////////////////////////////////////////
|
37 |
+
|
38 |
+
@interface PangolinNSGLView : NSOpenGLView
|
39 |
+
{
|
40 |
+
float backing_scale;
|
41 |
+
@public pangolin::WindowInterface* osx_window;
|
42 |
+
}
|
43 |
+
@end
|
44 |
+
|
third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/WinWindow.h
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2011 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#pragma once
|
29 |
+
|
30 |
+
#include <string>
|
31 |
+
#include <pangolin/platform.h>
|
32 |
+
#include <pangolin/windowing/window.h>
|
33 |
+
#include <windowsx.h>
|
34 |
+
|
35 |
+
namespace pangolin
|
36 |
+
{
|
37 |
+
|
38 |
+
struct WinWindow : public WindowInterface
|
39 |
+
{
|
40 |
+
WinWindow(
|
41 |
+
const std::string& title, int width, int height
|
42 |
+
);
|
43 |
+
|
44 |
+
~WinWindow();
|
45 |
+
|
46 |
+
void Move(int x, int y) override;
|
47 |
+
|
48 |
+
void Resize(unsigned int w, unsigned int h) override;
|
49 |
+
|
50 |
+
void ShowFullscreen(const TrueFalseToggle on_off) override;
|
51 |
+
|
52 |
+
void MakeCurrent() override;
|
53 |
+
|
54 |
+
void RemoveCurrent() override;
|
55 |
+
|
56 |
+
void ProcessEvents() override;
|
57 |
+
|
58 |
+
void SwapBuffers() override;
|
59 |
+
|
60 |
+
HGLRC GetGLRenderContext()
|
61 |
+
{
|
62 |
+
return hGLRC;
|
63 |
+
}
|
64 |
+
|
65 |
+
private:
|
66 |
+
void StartFullScreen();
|
67 |
+
void StopFullScreen();
|
68 |
+
|
69 |
+
static LRESULT APIENTRY WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
|
70 |
+
|
71 |
+
LRESULT HandleWinMessages(UINT message, WPARAM wParam, LPARAM lParam);
|
72 |
+
|
73 |
+
void RegisterThisClass(HMODULE hCurrentInst);
|
74 |
+
|
75 |
+
void SetupPixelFormat(HDC hdc);
|
76 |
+
|
77 |
+
void SetupPalette(HDC hDC);
|
78 |
+
|
79 |
+
// Owns the Window
|
80 |
+
HWND hWnd;
|
81 |
+
HDC hDC;
|
82 |
+
HGLRC hGLRC;
|
83 |
+
HPALETTE hPalette;
|
84 |
+
bool bIsFullscreen;
|
85 |
+
RECT cWindowedRect;
|
86 |
+
float afLastMousePos[2];
|
87 |
+
};
|
88 |
+
|
89 |
+
}
|
third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/X11GlContext.h
ADDED
File without changes
|
third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/X11Window.h
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2011 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#pragma once
|
29 |
+
|
30 |
+
#include <pangolin/platform.h>
|
31 |
+
#include <pangolin/windowing/window.h>
|
32 |
+
#include <stdexcept>
|
33 |
+
#include <memory>
|
34 |
+
#include <string>
|
35 |
+
#include <X11/Xlib.h>
|
36 |
+
#include <X11/Xutil.h>
|
37 |
+
#include <GL/glx.h>
|
38 |
+
|
39 |
+
namespace pangolin
|
40 |
+
{
|
41 |
+
|
42 |
+
struct X11Display
|
43 |
+
{
|
44 |
+
X11Display(const char* name = 0) {
|
45 |
+
XInitThreads();
|
46 |
+
display = XOpenDisplay(name);
|
47 |
+
if (!display) {
|
48 |
+
throw std::runtime_error("Pangolin X11: Failed to open X display");
|
49 |
+
}
|
50 |
+
}
|
51 |
+
|
52 |
+
~X11Display() {
|
53 |
+
XCloseDisplay(display);
|
54 |
+
}
|
55 |
+
|
56 |
+
// Owns the display
|
57 |
+
::Display* display;
|
58 |
+
};
|
59 |
+
|
60 |
+
struct X11GlContext : public GlContextInterface
|
61 |
+
{
|
62 |
+
X11GlContext(std::shared_ptr<X11Display> &d, ::GLXFBConfig chosenFbc, std::shared_ptr<X11GlContext> shared_context = std::shared_ptr<X11GlContext>() );
|
63 |
+
~X11GlContext();
|
64 |
+
|
65 |
+
std::shared_ptr<X11Display> display;
|
66 |
+
|
67 |
+
std::shared_ptr<X11GlContext> shared_context;
|
68 |
+
|
69 |
+
// Owns the OpenGl Context
|
70 |
+
::GLXContext glcontext;
|
71 |
+
};
|
72 |
+
|
73 |
+
struct X11Window : public WindowInterface
|
74 |
+
{
|
75 |
+
X11Window(
|
76 |
+
const std::string& title, int width, int height,
|
77 |
+
std::shared_ptr<X11Display>& display, ::GLXFBConfig chosenFbc
|
78 |
+
);
|
79 |
+
|
80 |
+
~X11Window();
|
81 |
+
|
82 |
+
void ShowFullscreen(const TrueFalseToggle on_off) override;
|
83 |
+
|
84 |
+
void Move(int x, int y) override;
|
85 |
+
|
86 |
+
void Resize(unsigned int w, unsigned int h) override;
|
87 |
+
|
88 |
+
void MakeCurrent(GLXContext ctx);
|
89 |
+
|
90 |
+
void MakeCurrent() override;
|
91 |
+
|
92 |
+
void RemoveCurrent() override;
|
93 |
+
|
94 |
+
void SwapBuffers() override;
|
95 |
+
|
96 |
+
void ProcessEvents() override;
|
97 |
+
|
98 |
+
// References the X11 display and context.
|
99 |
+
std::shared_ptr<X11Display> display;
|
100 |
+
std::shared_ptr<X11GlContext> glcontext;
|
101 |
+
|
102 |
+
// Owns the X11 Window and Colourmap
|
103 |
+
::Window win;
|
104 |
+
::Colormap cmap;
|
105 |
+
|
106 |
+
Atom delete_message;
|
107 |
+
};
|
108 |
+
|
109 |
+
}
|
third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/display_android.h
ADDED
@@ -0,0 +1,333 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2013 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#pragma once
|
29 |
+
|
30 |
+
#include <poll.h>
|
31 |
+
#include <pthread.h>
|
32 |
+
#include <sched.h>
|
33 |
+
|
34 |
+
#include <android/configuration.h>
|
35 |
+
#include <android/looper.h>
|
36 |
+
#include <android/native_activity.h>
|
37 |
+
#include <android/log.h>
|
38 |
+
#include <string>
|
39 |
+
|
40 |
+
#include <pangolin/utils/type_convert.h>
|
41 |
+
|
42 |
+
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "pango", __VA_ARGS__))
|
43 |
+
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "pango", __VA_ARGS__))
|
44 |
+
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR, "pango", __VA_ARGS__))
|
45 |
+
|
46 |
+
/* For debug builds, always enable the debug traces in this library */
|
47 |
+
#undef NDEBUG
|
48 |
+
#ifndef NDEBUG
|
49 |
+
# define LOGV(...) ((void)__android_log_print(ANDROID_LOG_VERBOSE, "pango", __VA_ARGS__))
|
50 |
+
#else
|
51 |
+
# define LOGV(...) ((void)0)
|
52 |
+
#endif
|
53 |
+
|
54 |
+
template<typename T> inline
|
55 |
+
void Log(T v) {
|
56 |
+
const std::string sv = pangolin::Convert<std::string,T>::Do(v);
|
57 |
+
LOGI(sv.c_str());
|
58 |
+
}
|
59 |
+
|
60 |
+
template<typename T> inline
|
61 |
+
void Log(const std::string& str, T v)
|
62 |
+
{
|
63 |
+
const std::string sv = pangolin::Convert<std::string,T>::Do(v);
|
64 |
+
LOGI((str + ":" + sv).c_str());
|
65 |
+
}
|
66 |
+
|
67 |
+
namespace pangolin
|
68 |
+
{
|
69 |
+
void CreateAndroidWindowAndBind(std::string name);
|
70 |
+
void ProcessAndroidEvents();
|
71 |
+
void FinishAndroidFrame();
|
72 |
+
}
|
73 |
+
|
74 |
+
|
75 |
+
#ifdef __cplusplus
|
76 |
+
extern "C" {
|
77 |
+
#endif
|
78 |
+
|
79 |
+
struct android_app;
|
80 |
+
|
81 |
+
/**
|
82 |
+
* Data associated with an ALooper fd that will be returned as the "outData"
|
83 |
+
* when that source has data ready.
|
84 |
+
*/
|
85 |
+
struct android_poll_source {
|
86 |
+
// The identifier of this source. May be LOOPER_ID_MAIN or
|
87 |
+
// LOOPER_ID_INPUT.
|
88 |
+
int32_t id;
|
89 |
+
|
90 |
+
// The android_app this ident is associated with.
|
91 |
+
struct android_app* app;
|
92 |
+
|
93 |
+
// Function to call to perform the standard processing of data from
|
94 |
+
// this source.
|
95 |
+
void (*process)(struct android_app* app, struct android_poll_source* source);
|
96 |
+
};
|
97 |
+
|
98 |
+
/**
|
99 |
+
* This is the interface for the standard glue code of a threaded
|
100 |
+
* application. In this model, the application's code is running
|
101 |
+
* in its own thread separate from the main thread of the process.
|
102 |
+
* It is not required that this thread be associated with the Java
|
103 |
+
* VM, although it will need to be in order to make JNI calls any
|
104 |
+
* Java objects.
|
105 |
+
*/
|
106 |
+
struct android_app {
|
107 |
+
// The application can place a pointer to its own state object
|
108 |
+
// here if it likes.
|
109 |
+
void* userData;
|
110 |
+
|
111 |
+
// Fill this in with the function to process main app commands (APP_CMD_*)
|
112 |
+
void (*onAppCmd)(struct android_app* app, int32_t cmd);
|
113 |
+
|
114 |
+
// Fill this in with the function to process input events. At this point
|
115 |
+
// the event has already been pre-dispatched, and it will be finished upon
|
116 |
+
// return. Return 1 if you have handled the event, 0 for any default
|
117 |
+
// dispatching.
|
118 |
+
int32_t (*onInputEvent)(struct android_app* app, AInputEvent* event);
|
119 |
+
|
120 |
+
// The ANativeActivity object instance that this app is running in.
|
121 |
+
ANativeActivity* activity;
|
122 |
+
|
123 |
+
// The current configuration the app is running in.
|
124 |
+
AConfiguration* config;
|
125 |
+
|
126 |
+
// This is the last instance's saved state, as provided at creation time.
|
127 |
+
// It is NULL if there was no state. You can use this as you need; the
|
128 |
+
// memory will remain around until you call android_app_exec_cmd() for
|
129 |
+
// APP_CMD_RESUME, at which point it will be freed and savedState set to NULL.
|
130 |
+
// These variables should only be changed when processing a APP_CMD_SAVE_STATE,
|
131 |
+
// at which point they will be initialized to NULL and you can malloc your
|
132 |
+
// state and place the information here. In that case the memory will be
|
133 |
+
// freed for you later.
|
134 |
+
void* savedState;
|
135 |
+
size_t savedStateSize;
|
136 |
+
|
137 |
+
// The ALooper associated with the app's thread.
|
138 |
+
ALooper* looper;
|
139 |
+
|
140 |
+
// When non-NULL, this is the input queue from which the app will
|
141 |
+
// receive user input events.
|
142 |
+
AInputQueue* inputQueue;
|
143 |
+
|
144 |
+
// When non-NULL, this is the window surface that the app can draw in.
|
145 |
+
ANativeWindow* window;
|
146 |
+
|
147 |
+
// Current content rectangle of the window; this is the area where the
|
148 |
+
// window's content should be placed to be seen by the user.
|
149 |
+
ARect contentRect;
|
150 |
+
|
151 |
+
// Current state of the app's activity. May be either APP_CMD_START,
|
152 |
+
// APP_CMD_RESUME, APP_CMD_PAUSE, or APP_CMD_STOP; see below.
|
153 |
+
int activityState;
|
154 |
+
|
155 |
+
// This is non-zero when the application's NativeActivity is being
|
156 |
+
// destroyed and waiting for the app thread to complete.
|
157 |
+
int destroyRequested;
|
158 |
+
|
159 |
+
// -------------------------------------------------
|
160 |
+
// Below are "private" implementation of the glue code.
|
161 |
+
|
162 |
+
pthread_mutex_t mutex;
|
163 |
+
pthread_cond_t cond;
|
164 |
+
|
165 |
+
int msgread;
|
166 |
+
int msgwrite;
|
167 |
+
|
168 |
+
pthread_t thread;
|
169 |
+
|
170 |
+
struct android_poll_source cmdPollSource;
|
171 |
+
struct android_poll_source inputPollSource;
|
172 |
+
|
173 |
+
int running;
|
174 |
+
int stateSaved;
|
175 |
+
int destroyed;
|
176 |
+
int redrawNeeded;
|
177 |
+
AInputQueue* pendingInputQueue;
|
178 |
+
ANativeWindow* pendingWindow;
|
179 |
+
ARect pendingContentRect;
|
180 |
+
|
181 |
+
const char* application_so;
|
182 |
+
};
|
183 |
+
|
184 |
+
enum {
|
185 |
+
/**
|
186 |
+
* Looper data ID of commands coming from the app's main thread, which
|
187 |
+
* is returned as an identifier from ALooper_pollOnce(). The data for this
|
188 |
+
* identifier is a pointer to an android_poll_source structure.
|
189 |
+
* These can be retrieved and processed with android_app_read_cmd()
|
190 |
+
* and android_app_exec_cmd().
|
191 |
+
*/
|
192 |
+
LOOPER_ID_MAIN = 1,
|
193 |
+
|
194 |
+
/**
|
195 |
+
* Looper data ID of events coming from the AInputQueue of the
|
196 |
+
* application's window, which is returned as an identifier from
|
197 |
+
* ALooper_pollOnce(). The data for this identifier is a pointer to an
|
198 |
+
* android_poll_source structure. These can be read via the inputQueue
|
199 |
+
* object of android_app.
|
200 |
+
*/
|
201 |
+
LOOPER_ID_INPUT = 2,
|
202 |
+
|
203 |
+
/**
|
204 |
+
* Start of user-defined ALooper identifiers.
|
205 |
+
*/
|
206 |
+
LOOPER_ID_USER = 3,
|
207 |
+
};
|
208 |
+
|
209 |
+
enum {
|
210 |
+
/**
|
211 |
+
* Command from main thread: the AInputQueue has changed. Upon processing
|
212 |
+
* this command, android_app->inputQueue will be updated to the new queue
|
213 |
+
* (or NULL).
|
214 |
+
*/
|
215 |
+
APP_CMD_INPUT_CHANGED,
|
216 |
+
|
217 |
+
/**
|
218 |
+
* Command from main thread: a new ANativeWindow is ready for use. Upon
|
219 |
+
* receiving this command, android_app->window will contain the new window
|
220 |
+
* surface.
|
221 |
+
*/
|
222 |
+
APP_CMD_INIT_WINDOW,
|
223 |
+
|
224 |
+
/**
|
225 |
+
* Command from main thread: the existing ANativeWindow needs to be
|
226 |
+
* terminated. Upon receiving this command, android_app->window still
|
227 |
+
* contains the existing window; after calling android_app_exec_cmd
|
228 |
+
* it will be set to NULL.
|
229 |
+
*/
|
230 |
+
APP_CMD_TERM_WINDOW,
|
231 |
+
|
232 |
+
/**
|
233 |
+
* Command from main thread: the current ANativeWindow has been resized.
|
234 |
+
* Please redraw with its new size.
|
235 |
+
*/
|
236 |
+
APP_CMD_WINDOW_RESIZED,
|
237 |
+
|
238 |
+
/**
|
239 |
+
* Command from main thread: the system needs that the current ANativeWindow
|
240 |
+
* be redrawn. You should redraw the window before handing this to
|
241 |
+
* android_app_exec_cmd() in order to avoid transient drawing glitches.
|
242 |
+
*/
|
243 |
+
APP_CMD_WINDOW_REDRAW_NEEDED,
|
244 |
+
|
245 |
+
/**
|
246 |
+
* Command from main thread: the content area of the window has changed,
|
247 |
+
* such as from the soft input window being shown or hidden. You can
|
248 |
+
* find the new content rect in android_app::contentRect.
|
249 |
+
*/
|
250 |
+
APP_CMD_CONTENT_RECT_CHANGED,
|
251 |
+
|
252 |
+
/**
|
253 |
+
* Command from main thread: the app's activity window has gained
|
254 |
+
* input focus.
|
255 |
+
*/
|
256 |
+
APP_CMD_GAINED_FOCUS,
|
257 |
+
|
258 |
+
/**
|
259 |
+
* Command from main thread: the app's activity window has lost
|
260 |
+
* input focus.
|
261 |
+
*/
|
262 |
+
APP_CMD_LOST_FOCUS,
|
263 |
+
|
264 |
+
/**
|
265 |
+
* Command from main thread: the current device configuration has changed.
|
266 |
+
*/
|
267 |
+
APP_CMD_CONFIG_CHANGED,
|
268 |
+
|
269 |
+
/**
|
270 |
+
* Command from main thread: the system is running low on memory.
|
271 |
+
* Try to reduce your memory use.
|
272 |
+
*/
|
273 |
+
APP_CMD_LOW_MEMORY,
|
274 |
+
|
275 |
+
/**
|
276 |
+
* Command from main thread: the app's activity has been started.
|
277 |
+
*/
|
278 |
+
APP_CMD_START,
|
279 |
+
|
280 |
+
/**
|
281 |
+
* Command from main thread: the app's activity has been resumed.
|
282 |
+
*/
|
283 |
+
APP_CMD_RESUME,
|
284 |
+
|
285 |
+
/**
|
286 |
+
* Command from main thread: the app should generate a new saved state
|
287 |
+
* for itself, to restore from later if needed. If you have saved state,
|
288 |
+
* allocate it with malloc and place it in android_app.savedState with
|
289 |
+
* the size in android_app.savedStateSize. The will be freed for you
|
290 |
+
* later.
|
291 |
+
*/
|
292 |
+
APP_CMD_SAVE_STATE,
|
293 |
+
|
294 |
+
/**
|
295 |
+
* Command from main thread: the app's activity has been paused.
|
296 |
+
*/
|
297 |
+
APP_CMD_PAUSE,
|
298 |
+
|
299 |
+
/**
|
300 |
+
* Command from main thread: the app's activity has been stopped.
|
301 |
+
*/
|
302 |
+
APP_CMD_STOP,
|
303 |
+
|
304 |
+
/**
|
305 |
+
* Command from main thread: the app's activity is being destroyed,
|
306 |
+
* and waiting for the app thread to clean up and exit before proceeding.
|
307 |
+
*/
|
308 |
+
APP_CMD_DESTROY,
|
309 |
+
};
|
310 |
+
|
311 |
+
/**
|
312 |
+
* Call when ALooper_pollAll() returns LOOPER_ID_MAIN, reading the next
|
313 |
+
* app command message.
|
314 |
+
*/
|
315 |
+
int8_t android_app_read_cmd(struct android_app* android_app);
|
316 |
+
|
317 |
+
/**
|
318 |
+
* Call with the command returned by android_app_read_cmd() to do the
|
319 |
+
* initial pre-processing of the given command. You can perform your own
|
320 |
+
* actions for the command after calling this function.
|
321 |
+
*/
|
322 |
+
void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd);
|
323 |
+
|
324 |
+
/**
|
325 |
+
* Call with the command returned by android_app_read_cmd() to do the
|
326 |
+
* final post-processing of the given command. You must have done your own
|
327 |
+
* actions for the command before calling this function.
|
328 |
+
*/
|
329 |
+
void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd);
|
330 |
+
|
331 |
+
#ifdef __cplusplus
|
332 |
+
}
|
333 |
+
#endif
|
third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/handler_bitsets.h
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2013 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#pragma once
|
29 |
+
|
30 |
+
#include <pangolin/utils/bitmask.h>
|
31 |
+
#include <pangolin/windowing/handler_enums.h>
|
32 |
+
|
33 |
+
namespace pangolin
|
34 |
+
{
|
35 |
+
|
36 |
+
using MouseButtonBitmask = bitmask<MouseButton,true>;
|
37 |
+
|
38 |
+
using KeyModifierBitmask = bitmask<KeyModifier,true>;
|
39 |
+
|
40 |
+
}
|
third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/handler_enums.h
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2013 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#pragma once
|
29 |
+
|
30 |
+
namespace pangolin
|
31 |
+
{
|
32 |
+
|
33 |
+
// Supported Key modifiers for GlobalKeyPressCallback.
|
34 |
+
// e.g. PANGO_CTRL + 'r', PANGO_SPECIAL + PANGO_KEY_RIGHT, etc.
|
35 |
+
const int PANGO_SPECIAL = 128;
|
36 |
+
const int PANGO_CTRL = -96;
|
37 |
+
const int PANGO_OPTN = 132;
|
38 |
+
|
39 |
+
// Ordinary keys
|
40 |
+
const int PANGO_KEY_TAB = 9;
|
41 |
+
const int PANGO_KEY_ESCAPE = 27;
|
42 |
+
|
43 |
+
// Special Keys (same as GLUT_ defines)
|
44 |
+
const int PANGO_KEY_F1 = 1;
|
45 |
+
const int PANGO_KEY_F2 = 2;
|
46 |
+
const int PANGO_KEY_F3 = 3;
|
47 |
+
const int PANGO_KEY_F4 = 4;
|
48 |
+
const int PANGO_KEY_F5 = 5;
|
49 |
+
const int PANGO_KEY_F6 = 6;
|
50 |
+
const int PANGO_KEY_F7 = 7;
|
51 |
+
const int PANGO_KEY_F8 = 8;
|
52 |
+
const int PANGO_KEY_F9 = 9;
|
53 |
+
const int PANGO_KEY_F10 = 10;
|
54 |
+
const int PANGO_KEY_F11 = 11;
|
55 |
+
const int PANGO_KEY_F12 = 12;
|
56 |
+
const int PANGO_KEY_LEFT = 100;
|
57 |
+
const int PANGO_KEY_UP = 101;
|
58 |
+
const int PANGO_KEY_RIGHT = 102;
|
59 |
+
const int PANGO_KEY_DOWN = 103;
|
60 |
+
const int PANGO_KEY_PAGE_UP = 104;
|
61 |
+
const int PANGO_KEY_PAGE_DOWN = 105;
|
62 |
+
const int PANGO_KEY_HOME = 106;
|
63 |
+
const int PANGO_KEY_END = 107;
|
64 |
+
const int PANGO_KEY_INSERT = 108;
|
65 |
+
|
66 |
+
enum MouseButton
|
67 |
+
{
|
68 |
+
MouseButtonLeft = 1<<0,
|
69 |
+
MouseButtonMiddle = 1<<1,
|
70 |
+
MouseButtonRight = 1<<2,
|
71 |
+
MouseWheelUp = 1<<3,
|
72 |
+
MouseWheelDown = 1<<4,
|
73 |
+
MouseWheelRight = 1<<5,
|
74 |
+
MouseWheelLeft = 1<<6,
|
75 |
+
};
|
76 |
+
|
77 |
+
enum KeyModifier
|
78 |
+
{
|
79 |
+
KeyModifierShift = 1<<16,
|
80 |
+
KeyModifierCtrl = 1<<17,
|
81 |
+
KeyModifierAlt = 1<<18,
|
82 |
+
KeyModifierCmd = 1<<19,
|
83 |
+
KeyModifierFnc = 1<<20
|
84 |
+
};
|
85 |
+
|
86 |
+
enum InputSpecial
|
87 |
+
{
|
88 |
+
InputSpecialScroll,
|
89 |
+
InputSpecialZoom,
|
90 |
+
InputSpecialRotate,
|
91 |
+
InputSpecialTablet
|
92 |
+
};
|
93 |
+
|
94 |
+
}
|
third-party/DPVO/Pangolin/components/pango_windowing/include/pangolin/windowing/window.h
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2016 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#pragma once
|
29 |
+
#include <exception>
|
30 |
+
#include <functional>
|
31 |
+
#include <array>
|
32 |
+
#include <string>
|
33 |
+
#include <memory>
|
34 |
+
#include <pangolin/platform.h>
|
35 |
+
#include <pangolin/utils/signal_slot.h>
|
36 |
+
#include <pangolin/utils/true_false_toggle.h>
|
37 |
+
#include <pangolin/utils/uri.h>
|
38 |
+
#include <pangolin/windowing/handler_bitsets.h>
|
39 |
+
|
40 |
+
namespace pangolin
|
41 |
+
{
|
42 |
+
|
43 |
+
// CreateWindowAndBind parameter key names.
|
44 |
+
constexpr char PARAM_DISPLAYNAME[] = "DISPLAYNAME\0"; // std::string
|
45 |
+
constexpr char PARAM_DOUBLEBUFFER[] = "DOUBLEBUFFER\0"; // bool
|
46 |
+
constexpr char PARAM_SAMPLE_BUFFERS[] = "SAMPLE_BUFFERS\0"; // int
|
47 |
+
constexpr char PARAM_SAMPLES[] = "SAMPLES\0"; // int
|
48 |
+
constexpr char PARAM_HIGHRES[] = "HIGHRES\0"; // bool
|
49 |
+
|
50 |
+
struct PANGOLIN_EXPORT WindowResizeEvent
|
51 |
+
{
|
52 |
+
int width;
|
53 |
+
int height;
|
54 |
+
};
|
55 |
+
|
56 |
+
struct PANGOLIN_EXPORT WindowInputEvent
|
57 |
+
{
|
58 |
+
float x;
|
59 |
+
float y;
|
60 |
+
KeyModifierBitmask key_modifiers;
|
61 |
+
};
|
62 |
+
|
63 |
+
struct PANGOLIN_EXPORT KeyboardEvent : public WindowInputEvent
|
64 |
+
{
|
65 |
+
unsigned char key;
|
66 |
+
bool pressed;
|
67 |
+
};
|
68 |
+
|
69 |
+
struct PANGOLIN_EXPORT MouseEvent : public WindowInputEvent
|
70 |
+
{
|
71 |
+
int button;
|
72 |
+
bool pressed;
|
73 |
+
};
|
74 |
+
|
75 |
+
struct PANGOLIN_EXPORT MouseMotionEvent : public WindowInputEvent
|
76 |
+
{
|
77 |
+
};
|
78 |
+
|
79 |
+
struct PANGOLIN_EXPORT SpecialInputEvent : public WindowInputEvent
|
80 |
+
{
|
81 |
+
InputSpecial inType;
|
82 |
+
float p[4];
|
83 |
+
};
|
84 |
+
|
85 |
+
class PANGOLIN_EXPORT GlContextInterface
|
86 |
+
{
|
87 |
+
public:
|
88 |
+
virtual ~GlContextInterface() {}
|
89 |
+
};
|
90 |
+
|
91 |
+
class PANGOLIN_EXPORT WindowInterface
|
92 |
+
{
|
93 |
+
public:
|
94 |
+
virtual ~WindowInterface() {}
|
95 |
+
|
96 |
+
/// Move window to (\param x, \param y) on screen
|
97 |
+
/// measured in pixels.
|
98 |
+
virtual void Move(int x, int y) = 0;
|
99 |
+
|
100 |
+
/// Resize window to (\param x, \param y) on screen
|
101 |
+
/// measured in pixels
|
102 |
+
virtual void Resize(unsigned int w, unsigned int h) = 0;
|
103 |
+
|
104 |
+
/// If supported by this windowing system, turn on,
|
105 |
+
/// turn off, or toggle fullscreen mode.
|
106 |
+
virtual void ShowFullscreen(const TrueFalseToggle on_off) = 0;
|
107 |
+
|
108 |
+
/// Make this the active context for graphic rendering.
|
109 |
+
virtual void MakeCurrent() = 0;
|
110 |
+
|
111 |
+
/// Unregister this context from being the active rendering context.
|
112 |
+
virtual void RemoveCurrent() = 0;
|
113 |
+
|
114 |
+
/// Process all windowing events to keep window alive
|
115 |
+
/// and emit interaction callbacks (e.g. mouse, keyboard, resize)
|
116 |
+
virtual void ProcessEvents() = 0;
|
117 |
+
|
118 |
+
/// If double-buffered rendering is enabled, swap the
|
119 |
+
/// front and back buffers revealing the recent renders
|
120 |
+
/// to the back buffer.
|
121 |
+
virtual void SwapBuffers() = 0;
|
122 |
+
|
123 |
+
sigslot::signal<> CloseSignal;
|
124 |
+
sigslot::signal<WindowResizeEvent> ResizeSignal;
|
125 |
+
sigslot::signal<KeyboardEvent> KeyboardSignal;
|
126 |
+
sigslot::signal<MouseEvent> MouseSignal;
|
127 |
+
sigslot::signal<MouseMotionEvent> MouseMotionSignal;
|
128 |
+
sigslot::signal<MouseMotionEvent> PassiveMouseMotionSignal;
|
129 |
+
sigslot::signal<SpecialInputEvent> SpecialInputSignal;
|
130 |
+
};
|
131 |
+
|
132 |
+
//! Open Window Interface from Uri specification
|
133 |
+
PANGOLIN_EXPORT
|
134 |
+
std::unique_ptr<WindowInterface> ConstructWindow(const Uri& uri);
|
135 |
+
|
136 |
+
}
|
third-party/DPVO/Pangolin/components/pango_windowing/src/PangolinNSApplication.mm
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2011 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#include <pangolin/platform.h>
|
29 |
+
#include <pangolin/windowing/PangolinNSApplication.h>
|
30 |
+
|
31 |
+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
|
32 |
+
# define NSAnyEventMask NSEventMaskAny
|
33 |
+
#endif
|
34 |
+
|
35 |
+
////////////////////////////////////////////////////////////////////
|
36 |
+
// PangolinNSApplication
|
37 |
+
////////////////////////////////////////////////////////////////////
|
38 |
+
|
39 |
+
@implementation PangolinNSApplication
|
40 |
+
|
41 |
+
+ (void)run_pre
|
42 |
+
{
|
43 |
+
[[NSNotificationCenter defaultCenter]
|
44 |
+
postNotificationName:NSApplicationWillFinishLaunchingNotification
|
45 |
+
object:NSApp];
|
46 |
+
[[NSNotificationCenter defaultCenter]
|
47 |
+
postNotificationName:NSApplicationDidFinishLaunchingNotification
|
48 |
+
object:NSApp];
|
49 |
+
}
|
50 |
+
|
51 |
+
+ (void)run_step
|
52 |
+
{
|
53 |
+
NSEvent *event;
|
54 |
+
do{
|
55 |
+
event = [NSApp
|
56 |
+
nextEventMatchingMask:NSAnyEventMask
|
57 |
+
untilDate:nil
|
58 |
+
inMode:NSDefaultRunLoopMode
|
59 |
+
dequeue:YES];
|
60 |
+
[NSApp sendEvent:event];
|
61 |
+
[NSApp updateWindows];
|
62 |
+
}while(event != nil);
|
63 |
+
}
|
64 |
+
|
65 |
+
@end
|
66 |
+
|
67 |
+
////////////////////////////////////////////////////////////////////
|
68 |
+
// PangolinWindowDelegate
|
69 |
+
////////////////////////////////////////////////////////////////////
|
70 |
+
|
71 |
+
@implementation PangolinWindowDelegate
|
72 |
+
|
73 |
+
- (BOOL)windowShouldClose:(id)sender {
|
74 |
+
PANGOLIN_UNUSED(sender);
|
75 |
+
osx_window->CloseSignal();
|
76 |
+
return YES;
|
77 |
+
}
|
78 |
+
|
79 |
+
@end
|
80 |
+
|
81 |
+
////////////////////////////////////////////////////////////////////
|
82 |
+
// PangolinAppDelegate
|
83 |
+
////////////////////////////////////////////////////////////////////
|
84 |
+
|
85 |
+
@implementation PangolinAppDelegate
|
86 |
+
|
87 |
+
- (void)dealloc
|
88 |
+
{
|
89 |
+
[super dealloc];
|
90 |
+
}
|
91 |
+
|
92 |
+
@end
|
third-party/DPVO/Pangolin/components/pango_windowing/src/PangolinNSGLView.mm
ADDED
@@ -0,0 +1,318 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/platform.h>
|
2 |
+
#include <pangolin/gl/glinclude.h>
|
3 |
+
#include <pangolin/windowing/PangolinNSGLView.h>
|
4 |
+
#include <pangolin/windowing/handler_enums.h>
|
5 |
+
|
6 |
+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
|
7 |
+
# define NSDeviceIndependentModifierFlagsMask NSEventModifierFlagDeviceIndependentFlagsMask
|
8 |
+
# define NSShiftKeyMask NSEventModifierFlagShift
|
9 |
+
# define NSControlKeyMask NSEventModifierFlagControl
|
10 |
+
# define NSAlternateKeyMask NSEventModifierFlagOption
|
11 |
+
# define NSCommandKeyMask NSEventModifierFlagCommand
|
12 |
+
# define NSFunctionKeyMask NSEventModifierFlagFunction
|
13 |
+
#endif
|
14 |
+
|
15 |
+
////////////////////////////////////////////////////////////////////
|
16 |
+
// Input maps
|
17 |
+
////////////////////////////////////////////////////////////////////
|
18 |
+
|
19 |
+
inline
|
20 |
+
int mapMouseButton(int osx_button )
|
21 |
+
{
|
22 |
+
const int map[] = {0, 2, 1, 3, 4, 5, 6, 7, 8, 9, 10};
|
23 |
+
return map[osx_button];
|
24 |
+
}
|
25 |
+
|
26 |
+
inline
|
27 |
+
int mapKeymap(int osx_key)
|
28 |
+
{
|
29 |
+
if(osx_key == NSUpArrowFunctionKey)
|
30 |
+
return pangolin::PANGO_SPECIAL + pangolin::PANGO_KEY_UP;
|
31 |
+
else if(osx_key == NSDownArrowFunctionKey)
|
32 |
+
return pangolin::PANGO_SPECIAL + pangolin::PANGO_KEY_DOWN;
|
33 |
+
else if(osx_key == NSLeftArrowFunctionKey)
|
34 |
+
return pangolin::PANGO_SPECIAL + pangolin::PANGO_KEY_LEFT;
|
35 |
+
else if(osx_key == NSRightArrowFunctionKey)
|
36 |
+
return pangolin::PANGO_SPECIAL + pangolin::PANGO_KEY_RIGHT;
|
37 |
+
else if(osx_key == NSPageUpFunctionKey)
|
38 |
+
return pangolin::PANGO_SPECIAL + pangolin::PANGO_KEY_PAGE_UP;
|
39 |
+
else if(osx_key == NSPageDownFunctionKey)
|
40 |
+
return pangolin::PANGO_SPECIAL + pangolin::PANGO_KEY_PAGE_DOWN;
|
41 |
+
else if(osx_key == NSHomeFunctionKey)
|
42 |
+
return pangolin::PANGO_SPECIAL + pangolin::PANGO_KEY_HOME;
|
43 |
+
else if(osx_key == NSEndFunctionKey)
|
44 |
+
return pangolin::PANGO_SPECIAL + pangolin::PANGO_KEY_END;
|
45 |
+
else if(osx_key == NSInsertFunctionKey)
|
46 |
+
return pangolin::PANGO_SPECIAL + pangolin::PANGO_KEY_INSERT;
|
47 |
+
else if(osx_key == NSDeleteCharacter )
|
48 |
+
return NSBackspaceCharacter;
|
49 |
+
else if(osx_key == NSDeleteFunctionKey)
|
50 |
+
return NSDeleteCharacter;
|
51 |
+
else {
|
52 |
+
return osx_key;
|
53 |
+
}
|
54 |
+
}
|
55 |
+
|
56 |
+
inline
|
57 |
+
pangolin::KeyModifierBitmask GetKeyModifierBitmask(NSEvent *event)
|
58 |
+
{
|
59 |
+
unsigned int flags = [event modifierFlags] & NSDeviceIndependentModifierFlagsMask;
|
60 |
+
pangolin::KeyModifierBitmask mask;
|
61 |
+
if(flags&NSShiftKeyMask) mask |= pangolin::KeyModifierShift;
|
62 |
+
if(flags&NSControlKeyMask) mask |= pangolin::KeyModifierCtrl;
|
63 |
+
if(flags&NSAlternateKeyMask) mask |= pangolin::KeyModifierAlt;
|
64 |
+
if(flags&NSCommandKeyMask) mask |= pangolin::KeyModifierCmd;
|
65 |
+
if(flags&NSFunctionKeyMask) mask |= pangolin::KeyModifierFnc;
|
66 |
+
return mask;
|
67 |
+
}
|
68 |
+
|
69 |
+
////////////////////////////////////////////////////////////////////
|
70 |
+
// PangolinNSGLView
|
71 |
+
////////////////////////////////////////////////////////////////////
|
72 |
+
|
73 |
+
@implementation PangolinNSGLView
|
74 |
+
|
75 |
+
-(id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat *)format
|
76 |
+
{
|
77 |
+
self = [super initWithFrame:frameRect pixelFormat:format];
|
78 |
+
return(self);
|
79 |
+
}
|
80 |
+
|
81 |
+
- (void)prepareOpenGL
|
82 |
+
{
|
83 |
+
[super prepareOpenGL];
|
84 |
+
}
|
85 |
+
|
86 |
+
-(void)reshape
|
87 |
+
{
|
88 |
+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
|
89 |
+
if ( [self wantsBestResolutionOpenGLSurface] && [ self.window respondsToSelector:@selector(backingScaleFactor) ] )
|
90 |
+
backing_scale = [self.window backingScaleFactor];
|
91 |
+
else
|
92 |
+
#endif
|
93 |
+
backing_scale = 1.0;
|
94 |
+
|
95 |
+
const int width = self.bounds.size.width * backing_scale;
|
96 |
+
const int height = self.bounds.size.height * backing_scale;
|
97 |
+
osx_window->ResizeSignal(pangolin::WindowResizeEvent({width, height}));
|
98 |
+
|
99 |
+
[[self openGLContext] update];
|
100 |
+
[super reshape];
|
101 |
+
}
|
102 |
+
|
103 |
+
-(BOOL)acceptsFirstResponder
|
104 |
+
{
|
105 |
+
return(YES);
|
106 |
+
}
|
107 |
+
|
108 |
+
-(BOOL)becomeFirstResponder
|
109 |
+
{
|
110 |
+
return(YES);
|
111 |
+
}
|
112 |
+
|
113 |
+
-(BOOL)resignFirstResponder
|
114 |
+
{
|
115 |
+
return(YES);
|
116 |
+
}
|
117 |
+
|
118 |
+
-(BOOL)isFlipped
|
119 |
+
{
|
120 |
+
return(YES);
|
121 |
+
}
|
122 |
+
|
123 |
+
-(NSPoint)_Location:(NSEvent *)event
|
124 |
+
{
|
125 |
+
NSPoint location = [self convertPoint: [event locationInWindow] fromView: nil];
|
126 |
+
location.x *= backing_scale;
|
127 |
+
location.y *= backing_scale;
|
128 |
+
return location;
|
129 |
+
}
|
130 |
+
|
131 |
+
////////////////////////////////////////////////////////////////////
|
132 |
+
// Keyboard
|
133 |
+
////////////////////////////////////////////////////////////////////
|
134 |
+
|
135 |
+
-(void)keyDown:(NSEvent *)event
|
136 |
+
{
|
137 |
+
const NSPoint location = [self _Location: event];
|
138 |
+
NSString *str = [event characters];
|
139 |
+
int len = (int)[str length];
|
140 |
+
for(int i = 0; i < len; i++)
|
141 |
+
{
|
142 |
+
const int osx_key = [str characterAtIndex:i];
|
143 |
+
osx_window->KeyboardSignal(pangolin::KeyboardEvent(
|
144 |
+
{(float)location.x, (float)location.y,
|
145 |
+
GetKeyModifierBitmask(event),
|
146 |
+
(unsigned char)mapKeymap(osx_key), true}
|
147 |
+
));
|
148 |
+
}
|
149 |
+
unsigned int flags = [event modifierFlags] & NSDeviceIndependentModifierFlagsMask;
|
150 |
+
}
|
151 |
+
|
152 |
+
-(void)keyUp:(NSEvent *)event
|
153 |
+
{
|
154 |
+
const NSPoint location = [self _Location: event];
|
155 |
+
NSString *str = [event characters];
|
156 |
+
int len = (int)[str length];
|
157 |
+
for(int i = 0; i < len; i++)
|
158 |
+
{
|
159 |
+
const int osx_key = [str characterAtIndex:i];
|
160 |
+
osx_window->KeyboardSignal(pangolin::KeyboardEvent(
|
161 |
+
{(float)location.x, (float)location.y,
|
162 |
+
GetKeyModifierBitmask(event),
|
163 |
+
(unsigned char)mapKeymap(osx_key), false}
|
164 |
+
));
|
165 |
+
}
|
166 |
+
}
|
167 |
+
|
168 |
+
////////////////////////////////////////////////////////////////////
|
169 |
+
// Mouse Input
|
170 |
+
////////////////////////////////////////////////////////////////////
|
171 |
+
|
172 |
+
-(void)mouseDownCommon:(NSEvent *)event
|
173 |
+
{
|
174 |
+
const int button = (int)[event buttonNumber];
|
175 |
+
const NSPoint location = [self _Location: event];
|
176 |
+
osx_window->MouseSignal(pangolin::MouseEvent({
|
177 |
+
(float)location.x, (float)location.y,
|
178 |
+
GetKeyModifierBitmask(event),
|
179 |
+
mapMouseButton(button), true
|
180 |
+
}));
|
181 |
+
}
|
182 |
+
|
183 |
+
-(void)mouseUpCommon:(NSEvent *)event
|
184 |
+
{
|
185 |
+
const int button = (int)[event buttonNumber];
|
186 |
+
const NSPoint location = [self _Location: event];
|
187 |
+
osx_window->MouseSignal(pangolin::MouseEvent({
|
188 |
+
(float)location.x, (float)location.y,
|
189 |
+
GetKeyModifierBitmask(event),
|
190 |
+
mapMouseButton(button), false
|
191 |
+
}));
|
192 |
+
}
|
193 |
+
|
194 |
+
- (void)mouseDraggedCommon: (NSEvent *)event
|
195 |
+
{
|
196 |
+
const NSPoint location = [self _Location: event];
|
197 |
+
osx_window->MouseMotionSignal(pangolin::MouseMotionEvent({
|
198 |
+
(float)location.x, (float)location.y,
|
199 |
+
GetKeyModifierBitmask(event)
|
200 |
+
}));
|
201 |
+
}
|
202 |
+
|
203 |
+
-(void)mouseDown:(NSEvent *)event
|
204 |
+
{
|
205 |
+
[self mouseDownCommon:event];
|
206 |
+
}
|
207 |
+
|
208 |
+
-(void)mouseUp:(NSEvent *)event
|
209 |
+
{
|
210 |
+
[self mouseUpCommon:event];
|
211 |
+
}
|
212 |
+
|
213 |
+
- (void)mouseDragged: (NSEvent *)event
|
214 |
+
{
|
215 |
+
[self mouseDraggedCommon:event];
|
216 |
+
}
|
217 |
+
|
218 |
+
-(void)rightMouseDown:(NSEvent *)event
|
219 |
+
{
|
220 |
+
[self mouseDownCommon:event];
|
221 |
+
}
|
222 |
+
|
223 |
+
-(void)rightMouseUp:(NSEvent *)event
|
224 |
+
{
|
225 |
+
[self mouseUpCommon:event];
|
226 |
+
}
|
227 |
+
|
228 |
+
- (void)rightMouseDragged: (NSEvent *)event
|
229 |
+
{
|
230 |
+
[self mouseDraggedCommon:event];
|
231 |
+
}
|
232 |
+
|
233 |
+
-(void)otherMouseDown:(NSEvent *)event
|
234 |
+
{
|
235 |
+
[self mouseDownCommon:event];
|
236 |
+
}
|
237 |
+
|
238 |
+
-(void)otherMouseUp:(NSEvent *)event
|
239 |
+
{
|
240 |
+
[self mouseUpCommon:event];
|
241 |
+
}
|
242 |
+
|
243 |
+
- (void)otherMouseDragged: (NSEvent *)event
|
244 |
+
{
|
245 |
+
[self mouseDraggedCommon:event];
|
246 |
+
}
|
247 |
+
|
248 |
+
- (void)mouseMoved: (NSEvent *)event
|
249 |
+
{
|
250 |
+
const NSPoint location = [self _Location: event];
|
251 |
+
osx_window->PassiveMouseMotionSignal(pangolin::MouseMotionEvent({(float)location.x, (float)location.y}));
|
252 |
+
}
|
253 |
+
|
254 |
+
- (void)scrollWheel:(NSEvent *)event
|
255 |
+
{
|
256 |
+
const NSPoint location = [self _Location: event];
|
257 |
+
|
258 |
+
float dx, dy;
|
259 |
+
if([event respondsToSelector:@selector(scrollingDeltaX)]) {
|
260 |
+
dx = event.scrollingDeltaX; dy = event.scrollingDeltaY;
|
261 |
+
} else {
|
262 |
+
dx = event.deltaX; dy = event.deltaY;
|
263 |
+
}
|
264 |
+
|
265 |
+
if(dx != 0.0f || dy != 0.0f) {
|
266 |
+
osx_window->SpecialInputSignal(pangolin::SpecialInputEvent({
|
267 |
+
(float)location.x, (float)location.y,
|
268 |
+
GetKeyModifierBitmask(event),
|
269 |
+
pangolin::InputSpecialScroll,
|
270 |
+
{dx, dy, 0.0f, 0.0f}
|
271 |
+
}));
|
272 |
+
}
|
273 |
+
}
|
274 |
+
|
275 |
+
- (void)magnifyWithEvent: (NSEvent *)event
|
276 |
+
{
|
277 |
+
const NSPoint location = [self _Location: event];
|
278 |
+
const float dm = event.magnification;
|
279 |
+
if(dm != 0.0f) {
|
280 |
+
osx_window->SpecialInputSignal(pangolin::SpecialInputEvent({
|
281 |
+
(float)location.x, (float)location.y,
|
282 |
+
GetKeyModifierBitmask(event),
|
283 |
+
pangolin::InputSpecialZoom,
|
284 |
+
{dm, 0.0f, 0.0f, 0.0f}
|
285 |
+
}));
|
286 |
+
}
|
287 |
+
}
|
288 |
+
|
289 |
+
- (void)rotateWithEvent: (NSEvent *)event
|
290 |
+
{
|
291 |
+
const NSPoint location = [self _Location: event];
|
292 |
+
const float dr = event.rotation;
|
293 |
+
if(dr != 0.0f) {
|
294 |
+
osx_window->SpecialInputSignal(pangolin::SpecialInputEvent({
|
295 |
+
(float)location.x, (float)location.y,
|
296 |
+
GetKeyModifierBitmask(event),
|
297 |
+
pangolin::InputSpecialRotate,
|
298 |
+
{dr, 0.0f, 0.0f, 0.0f}
|
299 |
+
}));
|
300 |
+
}
|
301 |
+
}
|
302 |
+
|
303 |
+
- (void)mouseEntered: (NSEvent *)event
|
304 |
+
{
|
305 |
+
PANGOLIN_UNUSED(event);
|
306 |
+
}
|
307 |
+
|
308 |
+
- (void)mouseExited: (NSEvent *)event
|
309 |
+
{
|
310 |
+
PANGOLIN_UNUSED(event);
|
311 |
+
}
|
312 |
+
|
313 |
+
-(void)dealloc
|
314 |
+
{
|
315 |
+
[super dealloc];
|
316 |
+
}
|
317 |
+
|
318 |
+
@end
|
third-party/DPVO/Pangolin/components/pango_windowing/src/display_android.cpp
ADDED
@@ -0,0 +1,1026 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2013 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Based largely on android_native_app_glue.c from Android NDK
|
7 |
+
*
|
8 |
+
* Permission is hereby granted, free of charge, to any person
|
9 |
+
* obtaining a copy of this software and associated documentation
|
10 |
+
* files (the "Software"), to deal in the Software without
|
11 |
+
* restriction, including without limitation the rights to use,
|
12 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
13 |
+
* copies of the Software, and to permit persons to whom the
|
14 |
+
* Software is furnished to do so, subject to the following
|
15 |
+
* conditions:
|
16 |
+
*
|
17 |
+
* The above copyright notice and this permission notice shall be
|
18 |
+
* included in all copies or substantial portions of the Software.
|
19 |
+
*
|
20 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
21 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
22 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
23 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
24 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
25 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
26 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
27 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
28 |
+
*/
|
29 |
+
|
30 |
+
#include <pangolin/factory/factory_registry.h>
|
31 |
+
#include <pangolin/gl/glinclude.h>
|
32 |
+
#include <pangolin/display/display.h>
|
33 |
+
#include <pangolin/display/display_internal.h>
|
34 |
+
#include <pangolin/display/device/display_android.h>
|
35 |
+
|
36 |
+
#include <poll.h>
|
37 |
+
#include <pthread.h>
|
38 |
+
#include <sched.h>
|
39 |
+
#include <errno.h>
|
40 |
+
#include <dlfcn.h>
|
41 |
+
|
42 |
+
#include <android/configuration.h>
|
43 |
+
#include <android/looper.h>
|
44 |
+
#include <android/native_activity.h>
|
45 |
+
#include <android/sensor.h>
|
46 |
+
#include <android/log.h>
|
47 |
+
#include <android/window.h>
|
48 |
+
|
49 |
+
#include <iostream>
|
50 |
+
#include <string>
|
51 |
+
#include <sstream>
|
52 |
+
#include <algorithm>
|
53 |
+
#include <iterator>
|
54 |
+
#include <stdexcept>
|
55 |
+
|
56 |
+
#include <jni.h>
|
57 |
+
|
58 |
+
#include <errno.h>
|
59 |
+
#include <string.h>
|
60 |
+
#include <unistd.h>
|
61 |
+
#include <sys/resource.h>
|
62 |
+
|
63 |
+
namespace pangolin
|
64 |
+
{
|
65 |
+
|
66 |
+
extern __thread PangolinGl* context;
|
67 |
+
|
68 |
+
/**
|
69 |
+
* Shared state for our app.
|
70 |
+
*/
|
71 |
+
struct engine {
|
72 |
+
android_app* app;
|
73 |
+
ANativeActivity* activity;
|
74 |
+
|
75 |
+
int has_focus;
|
76 |
+
EGLDisplay display;
|
77 |
+
EGLSurface surface;
|
78 |
+
EGLContext context;
|
79 |
+
int32_t width;
|
80 |
+
int32_t height;
|
81 |
+
// struct saved_state state;
|
82 |
+
};
|
83 |
+
|
84 |
+
/**
|
85 |
+
* Initialize an EGL context for the current display.
|
86 |
+
*/
|
87 |
+
static int engine_init_display(struct engine* engine) {
|
88 |
+
// initialize OpenGL ES and EGL
|
89 |
+
|
90 |
+
/*
|
91 |
+
* Here specify the attributes of the desired configuration.
|
92 |
+
* Below, we select an EGLConfig with at least 8 bits per color
|
93 |
+
* component compatible with on-screen windows
|
94 |
+
*/
|
95 |
+
const EGLint attribs[] = {
|
96 |
+
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
97 |
+
EGL_BLUE_SIZE, 8,
|
98 |
+
EGL_GREEN_SIZE, 8,
|
99 |
+
EGL_RED_SIZE, 8,
|
100 |
+
EGL_NONE
|
101 |
+
};
|
102 |
+
EGLint w, h, format;
|
103 |
+
EGLint numConfigs;
|
104 |
+
EGLConfig config;
|
105 |
+
EGLSurface surface;
|
106 |
+
EGLContext context;
|
107 |
+
|
108 |
+
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
109 |
+
|
110 |
+
eglInitialize(display, 0, 0);
|
111 |
+
|
112 |
+
/* Here, the application chooses the configuration it desires. In this
|
113 |
+
* sample, we have a very simplified selection process, where we pick
|
114 |
+
* the first EGLConfig that matches our criteria */
|
115 |
+
eglChooseConfig(display, attribs, &config, 1, &numConfigs);
|
116 |
+
|
117 |
+
/* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
|
118 |
+
* guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
|
119 |
+
* As soon as we picked a EGLConfig, we can safely reconfigure the
|
120 |
+
* ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
|
121 |
+
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
|
122 |
+
|
123 |
+
ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);
|
124 |
+
// ANativeActivity_setWindowFlags(engine->app->activity, AWINDOW_FLAG_FULLSCREEN, 0 );
|
125 |
+
|
126 |
+
EGLint const attrib_list[] = {
|
127 |
+
#ifdef HAVE_GLES_2
|
128 |
+
EGL_CONTEXT_CLIENT_VERSION, 2,
|
129 |
+
#endif
|
130 |
+
EGL_NONE
|
131 |
+
};
|
132 |
+
|
133 |
+
surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);
|
134 |
+
context = eglCreateContext(display, config, NULL, attrib_list);
|
135 |
+
|
136 |
+
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
|
137 |
+
LOGW("Unable to eglMakeCurrent");
|
138 |
+
return -1;
|
139 |
+
}
|
140 |
+
|
141 |
+
eglQuerySurface(display, surface, EGL_WIDTH, &w);
|
142 |
+
eglQuerySurface(display, surface, EGL_HEIGHT, &h);
|
143 |
+
|
144 |
+
engine->display = display;
|
145 |
+
engine->context = context;
|
146 |
+
engine->surface = surface;
|
147 |
+
engine->width = w;
|
148 |
+
engine->height = h;
|
149 |
+
|
150 |
+
pangolin::process::Resize(engine->width,engine->height);
|
151 |
+
|
152 |
+
// Initialize GL state.
|
153 |
+
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
|
154 |
+
glEnable(GL_CULL_FACE);
|
155 |
+
glShadeModel(GL_SMOOTH);
|
156 |
+
glDisable(GL_DEPTH_TEST);
|
157 |
+
|
158 |
+
return 0;
|
159 |
+
}
|
160 |
+
|
161 |
+
/**
|
162 |
+
* Just the current frame in the display.
|
163 |
+
*/
|
164 |
+
static void engine_draw_frame(struct engine* engine) {
|
165 |
+
if (engine->display != NULL) {
|
166 |
+
}
|
167 |
+
}
|
168 |
+
|
169 |
+
/**
|
170 |
+
* Tear down the EGL context currently associated with the display.
|
171 |
+
*/
|
172 |
+
static void engine_term_display(struct engine* engine) {
|
173 |
+
if (engine->display != EGL_NO_DISPLAY) {
|
174 |
+
eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
175 |
+
if (engine->context != EGL_NO_CONTEXT) {
|
176 |
+
eglDestroyContext(engine->display, engine->context);
|
177 |
+
}
|
178 |
+
if (engine->surface != EGL_NO_SURFACE) {
|
179 |
+
eglDestroySurface(engine->display, engine->surface);
|
180 |
+
}
|
181 |
+
eglTerminate(engine->display);
|
182 |
+
}
|
183 |
+
engine->has_focus = 0;
|
184 |
+
engine->display = EGL_NO_DISPLAY;
|
185 |
+
engine->context = EGL_NO_CONTEXT;
|
186 |
+
engine->surface = EGL_NO_SURFACE;
|
187 |
+
}
|
188 |
+
|
189 |
+
void UnpressAll(float last_x, float last_y)
|
190 |
+
{
|
191 |
+
if(context->mouse_state & pangolin::MouseButtonLeft) {
|
192 |
+
pangolin::process::Mouse(0, 1, last_x, last_y);
|
193 |
+
}
|
194 |
+
if(context->mouse_state & pangolin::MouseButtonMiddle) {
|
195 |
+
pangolin::process::Mouse(1, 1, last_x, last_y);
|
196 |
+
}
|
197 |
+
if(context->mouse_state & pangolin::MouseButtonRight) {
|
198 |
+
pangolin::process::Mouse(2, 1, last_x, last_y);
|
199 |
+
}
|
200 |
+
if(context->mouse_state & pangolin::MouseWheelUp) {
|
201 |
+
pangolin::process::Mouse(3, 1, last_x, last_y);
|
202 |
+
}
|
203 |
+
if(context->mouse_state & pangolin::MouseWheelDown) {
|
204 |
+
pangolin::process::Mouse(4, 1, last_x, last_y);
|
205 |
+
}
|
206 |
+
context->mouse_state = 0;
|
207 |
+
}
|
208 |
+
|
209 |
+
int PangolinKeyFromAndroidKeycode(int32_t keycode, bool shift)
|
210 |
+
{
|
211 |
+
if( AKEYCODE_0 <= keycode && keycode <= AKEYCODE_9) {
|
212 |
+
return '0' + (keycode - AKEYCODE_0);
|
213 |
+
}
|
214 |
+
|
215 |
+
if( AKEYCODE_A <= keycode && keycode <= AKEYCODE_Z) {
|
216 |
+
return (shift ? 'A' : 'a') + (keycode - AKEYCODE_A);
|
217 |
+
}
|
218 |
+
|
219 |
+
if(shift) {
|
220 |
+
switch (keycode) {
|
221 |
+
case AKEYCODE_GRAVE: return '~';
|
222 |
+
default:
|
223 |
+
LOGI("Unknown keycode (with shift): %d", keycode);
|
224 |
+
return '?';
|
225 |
+
}
|
226 |
+
}else{
|
227 |
+
switch (keycode) {
|
228 |
+
case AKEYCODE_COMMA: return ',';
|
229 |
+
case AKEYCODE_PERIOD: return '.';
|
230 |
+
case AKEYCODE_SPACE: return ' ';
|
231 |
+
case AKEYCODE_ENTER: return '\r';
|
232 |
+
case AKEYCODE_TAB: return '\t';
|
233 |
+
case AKEYCODE_DEL: return '\b';
|
234 |
+
case AKEYCODE_SLASH: return '/';
|
235 |
+
case AKEYCODE_BACKSLASH: return '\\';
|
236 |
+
case AKEYCODE_SEMICOLON: return ';';
|
237 |
+
case AKEYCODE_APOSTROPHE:return '\'';
|
238 |
+
case AKEYCODE_MINUS: return '-';
|
239 |
+
case AKEYCODE_EQUALS: return '=';
|
240 |
+
case AKEYCODE_PLUS: return '+';
|
241 |
+
case AKEYCODE_AT: return '@';
|
242 |
+
case AKEYCODE_GRAVE: return '`';
|
243 |
+
default:
|
244 |
+
LOGI("Unknown keycode: %d", keycode);
|
245 |
+
return '?';
|
246 |
+
}
|
247 |
+
}
|
248 |
+
}
|
249 |
+
|
250 |
+
/**
|
251 |
+
* Process the next input event.
|
252 |
+
*/
|
253 |
+
static int32_t engine_handle_input(struct android_app* app, AInputEvent* event) {
|
254 |
+
struct engine* engine = (struct engine*)app->userData;
|
255 |
+
|
256 |
+
static float last_x = 0;
|
257 |
+
static float last_y = 0;
|
258 |
+
|
259 |
+
const int32_t input_type = AInputEvent_getType(event);
|
260 |
+
|
261 |
+
if (input_type == AINPUT_EVENT_TYPE_MOTION) {
|
262 |
+
engine->has_focus = 1;
|
263 |
+
|
264 |
+
|
265 |
+
const float x = AMotionEvent_getX(event, 0);
|
266 |
+
const float y = AMotionEvent_getY(event, 0);
|
267 |
+
const int32_t actionAndPtr = AMotionEvent_getAction(event);
|
268 |
+
const int32_t action = AMOTION_EVENT_ACTION_MASK & actionAndPtr;
|
269 |
+
// const int32_t ptrindex = (AMOTION_EVENT_ACTION_POINTER_INDEX_MASK & actionAndPtr) >> AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
|
270 |
+
|
271 |
+
const size_t num_ptrs = AMotionEvent_getPointerCount(event);
|
272 |
+
|
273 |
+
switch(action)
|
274 |
+
{
|
275 |
+
case AMOTION_EVENT_ACTION_UP:
|
276 |
+
case AMOTION_EVENT_ACTION_POINTER_UP:
|
277 |
+
UnpressAll(last_x, last_y);
|
278 |
+
break;
|
279 |
+
case AMOTION_EVENT_ACTION_DOWN:
|
280 |
+
case AMOTION_EVENT_ACTION_POINTER_DOWN:
|
281 |
+
UnpressAll(last_x, last_y);
|
282 |
+
if(num_ptrs <=2) {
|
283 |
+
const int button = (num_ptrs==1) ? 0 : 2;
|
284 |
+
pangolin::process::Mouse(button, 0, x, y);
|
285 |
+
}
|
286 |
+
break;
|
287 |
+
case AMOTION_EVENT_ACTION_MOVE:
|
288 |
+
if(num_ptrs == 3) {
|
289 |
+
const double dx = x - last_x;
|
290 |
+
const double dy = y - last_y;
|
291 |
+
pangolin::process::Scroll(dx,dy);
|
292 |
+
}else{
|
293 |
+
pangolin::process::MouseMotion(x,y);
|
294 |
+
}
|
295 |
+
break;
|
296 |
+
default:
|
297 |
+
break;
|
298 |
+
}
|
299 |
+
|
300 |
+
last_x = x;
|
301 |
+
last_y = y;
|
302 |
+
|
303 |
+
return 1;
|
304 |
+
}else if(AINPUT_EVENT_TYPE_KEY) {
|
305 |
+
static bool shift = false;
|
306 |
+
|
307 |
+
const int32_t action = AKeyEvent_getAction(event);
|
308 |
+
const int32_t keycode = AKeyEvent_getKeyCode(event);
|
309 |
+
|
310 |
+
if(keycode == AKEYCODE_SHIFT_LEFT) {
|
311 |
+
shift = (action == AKEY_EVENT_ACTION_DOWN);
|
312 |
+
return 1;
|
313 |
+
}
|
314 |
+
|
315 |
+
unsigned char key = PangolinKeyFromAndroidKeycode(keycode, shift);
|
316 |
+
|
317 |
+
if(action == AKEY_EVENT_ACTION_DOWN) {
|
318 |
+
pangolin::process::Keyboard(key, last_x, last_y);
|
319 |
+
}else{
|
320 |
+
pangolin::process::KeyboardUp(key, last_x, last_y);
|
321 |
+
}
|
322 |
+
}
|
323 |
+
return 1;
|
324 |
+
}
|
325 |
+
|
326 |
+
/**
|
327 |
+
* Process the next main command.
|
328 |
+
*/
|
329 |
+
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
|
330 |
+
struct engine* engine = (struct engine*)app->userData;
|
331 |
+
switch (cmd) {
|
332 |
+
case APP_CMD_SAVE_STATE:
|
333 |
+
// // The system has asked us to save our current state. Do so.
|
334 |
+
// engine->app->savedState = malloc(sizeof(struct saved_state));
|
335 |
+
// *((struct saved_state*)engine->app->savedState) = engine->state;
|
336 |
+
// engine->app->savedStateSize = sizeof(struct saved_state);
|
337 |
+
break;
|
338 |
+
case APP_CMD_INIT_WINDOW:
|
339 |
+
// The window is being shown, get it ready.
|
340 |
+
if (engine->app->window != NULL) {
|
341 |
+
engine_init_display(engine);
|
342 |
+
engine_draw_frame(engine);
|
343 |
+
}
|
344 |
+
break;
|
345 |
+
case APP_CMD_TERM_WINDOW:
|
346 |
+
// The window is being hidden or closed, clean it up.
|
347 |
+
engine_term_display(engine);
|
348 |
+
break;
|
349 |
+
case APP_CMD_GAINED_FOCUS:
|
350 |
+
engine->has_focus = 1;
|
351 |
+
break;
|
352 |
+
case APP_CMD_LOST_FOCUS:
|
353 |
+
engine->has_focus = 0;
|
354 |
+
break;
|
355 |
+
}
|
356 |
+
}
|
357 |
+
|
358 |
+
}
|
359 |
+
|
360 |
+
// Define library entry point.
|
361 |
+
extern "C" {
|
362 |
+
|
363 |
+
JNIEnv* GetEnvAttachThread(JavaVM* vm)
|
364 |
+
{
|
365 |
+
JNIEnv* env;
|
366 |
+
switch (vm->GetEnv((void**)&env, JNI_VERSION_1_6)) {
|
367 |
+
case JNI_OK:
|
368 |
+
break;
|
369 |
+
case JNI_EDETACHED:
|
370 |
+
if (vm->AttachCurrentThread(&env, NULL)!=0) {
|
371 |
+
LOGE("Could not attach current thread");
|
372 |
+
throw std::runtime_error("Could not attach current thread");
|
373 |
+
}
|
374 |
+
break;
|
375 |
+
case JNI_EVERSION:
|
376 |
+
LOGE("Invalid Java version");
|
377 |
+
throw std::runtime_error("Invalid Java version");
|
378 |
+
}
|
379 |
+
return env;
|
380 |
+
}
|
381 |
+
|
382 |
+
// https://groups.google.com/d/msg/android-ndk/Tk3g00wLKhk/TJQucoaE_asJ
|
383 |
+
void displayKeyboard(android_app* app, bool pShow) {
|
384 |
+
jint lFlags = 0;
|
385 |
+
JNIEnv* env = GetEnvAttachThread(app->activity->vm);
|
386 |
+
if(env) {
|
387 |
+
// Retrieves NativeActivity.
|
388 |
+
jobject lNativeActivity = app->activity->clazz;
|
389 |
+
jclass ClassNativeActivity = env->GetObjectClass(lNativeActivity);
|
390 |
+
|
391 |
+
// Retrieves Context.INPUT_METHOD_SERVICE.
|
392 |
+
jclass ClassContext = env->FindClass("android/content/Context");
|
393 |
+
jfieldID FieldINPUT_METHOD_SERVICE = env->GetStaticFieldID(ClassContext, "INPUT_METHOD_SERVICE", "Ljava/lang/String;");
|
394 |
+
jobject INPUT_METHOD_SERVICE = env->GetStaticObjectField(ClassContext, FieldINPUT_METHOD_SERVICE);
|
395 |
+
// jniCheck(INPUT_METHOD_SERVICE);
|
396 |
+
|
397 |
+
// lInputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE).
|
398 |
+
jclass ClassInputMethodManager = env->FindClass( "android/view/inputmethod/InputMethodManager");
|
399 |
+
jmethodID MethodGetSystemService = env->GetMethodID( ClassNativeActivity, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
|
400 |
+
jobject lInputMethodManager = env->CallObjectMethod( lNativeActivity, MethodGetSystemService, INPUT_METHOD_SERVICE);
|
401 |
+
|
402 |
+
// lDecorView = getWindow().getDecorView().
|
403 |
+
jmethodID MethodGetWindow = env->GetMethodID( ClassNativeActivity, "getWindow", "()Landroid/view/Window;");
|
404 |
+
jobject lWindow = env->CallObjectMethod(lNativeActivity, MethodGetWindow);
|
405 |
+
jclass ClassWindow = env->FindClass( "android/view/Window");
|
406 |
+
jmethodID MethodGetDecorView = env->GetMethodID( ClassWindow, "getDecorView", "()Landroid/view/View;");
|
407 |
+
jobject lDecorView = env->CallObjectMethod(lWindow, MethodGetDecorView);
|
408 |
+
|
409 |
+
if (pShow) {
|
410 |
+
// Runs lInputMethodManager.showSoftInput(...).
|
411 |
+
jmethodID MethodShowSoftInput = env->GetMethodID( ClassInputMethodManager, "showSoftInput", "(Landroid/view/View;I)Z");
|
412 |
+
/*jboolean lResult = */env->CallBooleanMethod( lInputMethodManager, MethodShowSoftInput, lDecorView, lFlags);
|
413 |
+
} else {
|
414 |
+
// Runs lWindow.getViewToken()
|
415 |
+
jclass ClassView = env->FindClass( "android/view/View");
|
416 |
+
jmethodID MethodGetWindowToken = env->GetMethodID( ClassView, "getWindowToken", "()Landroid/os/IBinder;");
|
417 |
+
jobject lBinder = env->CallObjectMethod(lDecorView, MethodGetWindowToken);
|
418 |
+
|
419 |
+
// lInputMethodManager.hideSoftInput(...).
|
420 |
+
jmethodID MethodHideSoftInput = env->GetMethodID( ClassInputMethodManager, "hideSoftInputFromWindow", "(Landroid/os/IBinder;I)Z");
|
421 |
+
/*jboolean lRes = */env->CallBooleanMethod( lInputMethodManager, MethodHideSoftInput, lBinder, lFlags);
|
422 |
+
}
|
423 |
+
|
424 |
+
// Finished with the JVM.
|
425 |
+
app->activity->vm->DetachCurrentThread();
|
426 |
+
}
|
427 |
+
}
|
428 |
+
|
429 |
+
pangolin::engine g_engine;
|
430 |
+
|
431 |
+
static void free_saved_state(struct android_app* android_app) {
|
432 |
+
pthread_mutex_lock(&android_app->mutex);
|
433 |
+
if (android_app->savedState != NULL) {
|
434 |
+
free(android_app->savedState);
|
435 |
+
android_app->savedState = NULL;
|
436 |
+
android_app->savedStateSize = 0;
|
437 |
+
}
|
438 |
+
pthread_mutex_unlock(&android_app->mutex);
|
439 |
+
}
|
440 |
+
|
441 |
+
int8_t android_app_read_cmd(struct android_app* android_app) {
|
442 |
+
int8_t cmd;
|
443 |
+
if (read(android_app->msgread, &cmd, sizeof(cmd)) == sizeof(cmd)) {
|
444 |
+
switch (cmd) {
|
445 |
+
case APP_CMD_SAVE_STATE:
|
446 |
+
free_saved_state(android_app);
|
447 |
+
break;
|
448 |
+
}
|
449 |
+
return cmd;
|
450 |
+
} else {
|
451 |
+
LOGE("No data on command pipe!");
|
452 |
+
}
|
453 |
+
return -1;
|
454 |
+
}
|
455 |
+
|
456 |
+
static void print_cur_config(struct android_app* android_app) {
|
457 |
+
char lang[2], country[2];
|
458 |
+
AConfiguration_getLanguage(android_app->config, lang);
|
459 |
+
AConfiguration_getCountry(android_app->config, country);
|
460 |
+
|
461 |
+
LOGV("Config: mcc=%d mnc=%d lang=%c%c cnt=%c%c orien=%d touch=%d dens=%d "
|
462 |
+
"keys=%d nav=%d keysHid=%d navHid=%d sdk=%d size=%d long=%d "
|
463 |
+
"modetype=%d modenight=%d",
|
464 |
+
AConfiguration_getMcc(android_app->config),
|
465 |
+
AConfiguration_getMnc(android_app->config),
|
466 |
+
lang[0], lang[1], country[0], country[1],
|
467 |
+
AConfiguration_getOrientation(android_app->config),
|
468 |
+
AConfiguration_getTouchscreen(android_app->config),
|
469 |
+
AConfiguration_getDensity(android_app->config),
|
470 |
+
AConfiguration_getKeyboard(android_app->config),
|
471 |
+
AConfiguration_getNavigation(android_app->config),
|
472 |
+
AConfiguration_getKeysHidden(android_app->config),
|
473 |
+
AConfiguration_getNavHidden(android_app->config),
|
474 |
+
AConfiguration_getSdkVersion(android_app->config),
|
475 |
+
AConfiguration_getScreenSize(android_app->config),
|
476 |
+
AConfiguration_getScreenLong(android_app->config),
|
477 |
+
AConfiguration_getUiModeType(android_app->config),
|
478 |
+
AConfiguration_getUiModeNight(android_app->config));
|
479 |
+
}
|
480 |
+
|
481 |
+
void android_app_pre_exec_cmd(struct android_app* android_app, int8_t cmd) {
|
482 |
+
switch (cmd) {
|
483 |
+
case APP_CMD_INPUT_CHANGED:
|
484 |
+
LOGV("APP_CMD_INPUT_CHANGED\n");
|
485 |
+
pthread_mutex_lock(&android_app->mutex);
|
486 |
+
if (android_app->inputQueue != NULL) {
|
487 |
+
AInputQueue_detachLooper(android_app->inputQueue);
|
488 |
+
}
|
489 |
+
android_app->inputQueue = android_app->pendingInputQueue;
|
490 |
+
if (android_app->inputQueue != NULL) {
|
491 |
+
LOGV("Attaching input queue to looper");
|
492 |
+
AInputQueue_attachLooper(android_app->inputQueue,
|
493 |
+
android_app->looper, LOOPER_ID_INPUT, NULL,
|
494 |
+
&android_app->inputPollSource);
|
495 |
+
}
|
496 |
+
pthread_cond_broadcast(&android_app->cond);
|
497 |
+
pthread_mutex_unlock(&android_app->mutex);
|
498 |
+
break;
|
499 |
+
|
500 |
+
case APP_CMD_INIT_WINDOW:
|
501 |
+
LOGV("APP_CMD_INIT_WINDOW\n");
|
502 |
+
pthread_mutex_lock(&android_app->mutex);
|
503 |
+
android_app->window = android_app->pendingWindow;
|
504 |
+
pthread_cond_broadcast(&android_app->cond);
|
505 |
+
pthread_mutex_unlock(&android_app->mutex);
|
506 |
+
break;
|
507 |
+
|
508 |
+
case APP_CMD_TERM_WINDOW:
|
509 |
+
LOGV("APP_CMD_TERM_WINDOW\n");
|
510 |
+
pthread_cond_broadcast(&android_app->cond);
|
511 |
+
break;
|
512 |
+
|
513 |
+
case APP_CMD_RESUME:
|
514 |
+
case APP_CMD_START:
|
515 |
+
case APP_CMD_PAUSE:
|
516 |
+
case APP_CMD_STOP:
|
517 |
+
LOGV("activityState=%d\n", cmd);
|
518 |
+
pthread_mutex_lock(&android_app->mutex);
|
519 |
+
android_app->activityState = cmd;
|
520 |
+
pthread_cond_broadcast(&android_app->cond);
|
521 |
+
pthread_mutex_unlock(&android_app->mutex);
|
522 |
+
break;
|
523 |
+
|
524 |
+
case APP_CMD_CONFIG_CHANGED:
|
525 |
+
LOGV("APP_CMD_CONFIG_CHANGED\n");
|
526 |
+
AConfiguration_fromAssetManager(android_app->config,
|
527 |
+
android_app->activity->assetManager);
|
528 |
+
print_cur_config(android_app);
|
529 |
+
break;
|
530 |
+
|
531 |
+
case APP_CMD_DESTROY:
|
532 |
+
LOGV("APP_CMD_DESTROY\n");
|
533 |
+
android_app->destroyRequested = 1;
|
534 |
+
break;
|
535 |
+
}
|
536 |
+
}
|
537 |
+
|
538 |
+
void android_app_post_exec_cmd(struct android_app* android_app, int8_t cmd) {
|
539 |
+
switch (cmd) {
|
540 |
+
case APP_CMD_TERM_WINDOW:
|
541 |
+
LOGV("APP_CMD_TERM_WINDOW\n");
|
542 |
+
pthread_mutex_lock(&android_app->mutex);
|
543 |
+
android_app->window = NULL;
|
544 |
+
pthread_cond_broadcast(&android_app->cond);
|
545 |
+
pthread_mutex_unlock(&android_app->mutex);
|
546 |
+
break;
|
547 |
+
|
548 |
+
case APP_CMD_SAVE_STATE:
|
549 |
+
LOGV("APP_CMD_SAVE_STATE\n");
|
550 |
+
pthread_mutex_lock(&android_app->mutex);
|
551 |
+
android_app->stateSaved = 1;
|
552 |
+
pthread_cond_broadcast(&android_app->cond);
|
553 |
+
pthread_mutex_unlock(&android_app->mutex);
|
554 |
+
break;
|
555 |
+
|
556 |
+
case APP_CMD_RESUME:
|
557 |
+
free_saved_state(android_app);
|
558 |
+
break;
|
559 |
+
}
|
560 |
+
}
|
561 |
+
|
562 |
+
static void android_app_destroy(struct android_app* android_app) {
|
563 |
+
LOGV("+android_app_destroy!");
|
564 |
+
free_saved_state(android_app);
|
565 |
+
pthread_mutex_lock(&android_app->mutex);
|
566 |
+
if (android_app->inputQueue != NULL) {
|
567 |
+
AInputQueue_detachLooper(android_app->inputQueue);
|
568 |
+
}
|
569 |
+
AConfiguration_delete(android_app->config);
|
570 |
+
android_app->destroyed = 1;
|
571 |
+
pthread_cond_broadcast(&android_app->cond);
|
572 |
+
pthread_mutex_unlock(&android_app->mutex);
|
573 |
+
// Can't touch android_app object after this.
|
574 |
+
LOGV("-android_app_destroy!");
|
575 |
+
}
|
576 |
+
|
577 |
+
static void process_input(struct android_app* app, struct android_poll_source* source) {
|
578 |
+
AInputEvent* event = NULL;
|
579 |
+
if (AInputQueue_getEvent(app->inputQueue, &event) >= 0) {
|
580 |
+
|
581 |
+
// HACK: Override back buttom to show / hide keyboard.
|
582 |
+
int type = AInputEvent_getType(event);
|
583 |
+
if(type == AINPUT_EVENT_TYPE_KEY) {
|
584 |
+
if(AKeyEvent_getAction(event) == AKEY_EVENT_ACTION_DOWN) {
|
585 |
+
static bool keyboard_shown = false;
|
586 |
+
if( AKeyEvent_getKeyCode(event) == AKEYCODE_BACK ) {
|
587 |
+
displayKeyboard(app,!keyboard_shown);
|
588 |
+
keyboard_shown = !keyboard_shown;
|
589 |
+
AInputQueue_finishEvent(app->inputQueue, event, 1);
|
590 |
+
return;
|
591 |
+
}
|
592 |
+
}
|
593 |
+
}
|
594 |
+
|
595 |
+
if (AInputQueue_preDispatchEvent(app->inputQueue, event)) {
|
596 |
+
return;
|
597 |
+
}
|
598 |
+
int32_t handled = 0;
|
599 |
+
if (app->onInputEvent != NULL) handled = app->onInputEvent(app, event);
|
600 |
+
AInputQueue_finishEvent(app->inputQueue, event, handled);
|
601 |
+
} else {
|
602 |
+
LOGE("Failure reading next input event: %s\n", strerror(errno));
|
603 |
+
}
|
604 |
+
}
|
605 |
+
|
606 |
+
static void process_cmd(struct android_app* app, struct android_poll_source* source) {
|
607 |
+
int8_t cmd = android_app_read_cmd(app);
|
608 |
+
android_app_pre_exec_cmd(app, cmd);
|
609 |
+
if (app->onAppCmd != NULL) app->onAppCmd(app, cmd);
|
610 |
+
android_app_post_exec_cmd(app, cmd);
|
611 |
+
}
|
612 |
+
|
613 |
+
static void* android_app_entry(void* param) {
|
614 |
+
LOGV("+android_app_entry");
|
615 |
+
struct android_app* android_app = (struct android_app*)param;
|
616 |
+
|
617 |
+
android_app->config = AConfiguration_new();
|
618 |
+
AConfiguration_fromAssetManager(android_app->config, android_app->activity->assetManager);
|
619 |
+
|
620 |
+
print_cur_config(android_app);
|
621 |
+
|
622 |
+
android_app->cmdPollSource.id = LOOPER_ID_MAIN;
|
623 |
+
android_app->cmdPollSource.app = android_app;
|
624 |
+
android_app->cmdPollSource.process = process_cmd;
|
625 |
+
android_app->inputPollSource.id = LOOPER_ID_INPUT;
|
626 |
+
android_app->inputPollSource.app = android_app;
|
627 |
+
android_app->inputPollSource.process = process_input;
|
628 |
+
|
629 |
+
ALooper* looper = ALooper_prepare(ALOOPER_PREPARE_ALLOW_NON_CALLBACKS);
|
630 |
+
ALooper_addFd(looper, android_app->msgread, LOOPER_ID_MAIN, ALOOPER_EVENT_INPUT, NULL,
|
631 |
+
&android_app->cmdPollSource);
|
632 |
+
android_app->looper = looper;
|
633 |
+
|
634 |
+
pthread_mutex_lock(&android_app->mutex);
|
635 |
+
android_app->running = 1;
|
636 |
+
pthread_cond_broadcast(&android_app->cond);
|
637 |
+
pthread_mutex_unlock(&android_app->mutex);
|
638 |
+
|
639 |
+
std::string sargv;
|
640 |
+
|
641 |
+
// Load command line from ARGV parameter
|
642 |
+
JNIEnv *env = GetEnvAttachThread(android_app->activity->vm);
|
643 |
+
if(env) {
|
644 |
+
jobject me = android_app->activity->clazz;
|
645 |
+
|
646 |
+
jclass acl = env->GetObjectClass(me); //class pointer of NativeActivity
|
647 |
+
jmethodID giid = env->GetMethodID(acl, "getIntent", "()Landroid/content/Intent;");
|
648 |
+
jobject intent = env->CallObjectMethod(me, giid); //Got our intent
|
649 |
+
|
650 |
+
jclass icl = env->GetObjectClass(intent); //class pointer of Intent
|
651 |
+
jmethodID gseid = env->GetMethodID(icl, "getStringExtra", "(Ljava/lang/String;)Ljava/lang/String;");
|
652 |
+
|
653 |
+
jstring jsARGV = (jstring)env->CallObjectMethod(intent, gseid, env->NewStringUTF("ARGV"));
|
654 |
+
|
655 |
+
|
656 |
+
if(jsARGV) {
|
657 |
+
const char *chARGV = env->GetStringUTFChars(jsARGV, 0);
|
658 |
+
if(chARGV) {
|
659 |
+
sargv = std::string(chARGV);
|
660 |
+
LOGI("ARGV: pango %s", chARGV);
|
661 |
+
}
|
662 |
+
env->ReleaseStringUTFChars(jsARGV, chARGV);
|
663 |
+
}
|
664 |
+
|
665 |
+
android_app->activity->vm->DetachCurrentThread();
|
666 |
+
}
|
667 |
+
|
668 |
+
// Set up argv/argc to pass to users main
|
669 |
+
std::vector<std::string> vargv;
|
670 |
+
vargv.push_back("pango");
|
671 |
+
|
672 |
+
// Parse parameters from ARGV android intent parameter
|
673 |
+
std::istringstream iss(sargv);
|
674 |
+
std::copy(std::istream_iterator<std::string>(iss),
|
675 |
+
std::istream_iterator<std::string>(),
|
676 |
+
std::back_inserter<std::vector<std::string> >(vargv));
|
677 |
+
|
678 |
+
char* argv[vargv.size()+1];
|
679 |
+
for(size_t ac = 0; ac < vargv.size(); ++ac) {
|
680 |
+
argv[ac] = new char[vargv[ac].size()];
|
681 |
+
strcpy( argv[ac], vargv[ac].c_str() );
|
682 |
+
}
|
683 |
+
argv[vargv.size()] = NULL;
|
684 |
+
|
685 |
+
// Find main symbol
|
686 |
+
void (*main)(int, char**);
|
687 |
+
*(void **) (&main) = dlsym( dlopen(android_app->application_so, RTLD_NOW), "main");
|
688 |
+
if (!main) {
|
689 |
+
LOGE( "undefined symbol main, crap" );
|
690 |
+
exit(1);
|
691 |
+
}
|
692 |
+
// Call users standard main entry point.
|
693 |
+
(*main)(vargv.size(), argv);
|
694 |
+
|
695 |
+
// Clean up parameters
|
696 |
+
for(size_t ac = 0; ac < vargv.size(); ++ac) {
|
697 |
+
delete[] argv[ac];
|
698 |
+
}
|
699 |
+
|
700 |
+
android_app_destroy(android_app);
|
701 |
+
|
702 |
+
LOGV("-android_app_entry");
|
703 |
+
|
704 |
+
return NULL;
|
705 |
+
}
|
706 |
+
|
707 |
+
static void android_app_write_cmd(struct android_app* android_app, int8_t cmd) {
|
708 |
+
if (write(android_app->msgwrite, &cmd, sizeof(cmd)) != sizeof(cmd)) {
|
709 |
+
LOGE("Failure writing android_app cmd: %s\n", strerror(errno));
|
710 |
+
}
|
711 |
+
}
|
712 |
+
|
713 |
+
static void android_app_set_input(struct android_app* android_app, AInputQueue* inputQueue) {
|
714 |
+
pthread_mutex_lock(&android_app->mutex);
|
715 |
+
android_app->pendingInputQueue = inputQueue;
|
716 |
+
android_app_write_cmd(android_app, APP_CMD_INPUT_CHANGED);
|
717 |
+
while (android_app->inputQueue != android_app->pendingInputQueue) {
|
718 |
+
pthread_cond_wait(&android_app->cond, &android_app->mutex);
|
719 |
+
}
|
720 |
+
pthread_mutex_unlock(&android_app->mutex);
|
721 |
+
}
|
722 |
+
|
723 |
+
static void android_app_set_window(struct android_app* android_app, ANativeWindow* window) {
|
724 |
+
pthread_mutex_lock(&android_app->mutex);
|
725 |
+
if (android_app->pendingWindow != NULL) {
|
726 |
+
android_app_write_cmd(android_app, APP_CMD_TERM_WINDOW);
|
727 |
+
}
|
728 |
+
android_app->pendingWindow = window;
|
729 |
+
if (window != NULL) {
|
730 |
+
android_app_write_cmd(android_app, APP_CMD_INIT_WINDOW);
|
731 |
+
}
|
732 |
+
while (android_app->window != android_app->pendingWindow) {
|
733 |
+
pthread_cond_wait(&android_app->cond, &android_app->mutex);
|
734 |
+
}
|
735 |
+
pthread_mutex_unlock(&android_app->mutex);
|
736 |
+
}
|
737 |
+
|
738 |
+
static void android_app_set_activity_state(struct android_app* android_app, int8_t cmd) {
|
739 |
+
pthread_mutex_lock(&android_app->mutex);
|
740 |
+
android_app_write_cmd(android_app, cmd);
|
741 |
+
while (android_app->activityState != cmd) {
|
742 |
+
pthread_cond_wait(&android_app->cond, &android_app->mutex);
|
743 |
+
}
|
744 |
+
pthread_mutex_unlock(&android_app->mutex);
|
745 |
+
}
|
746 |
+
|
747 |
+
static void android_app_free(struct android_app* android_app) {
|
748 |
+
pthread_mutex_lock(&android_app->mutex);
|
749 |
+
android_app_write_cmd(android_app, APP_CMD_DESTROY);
|
750 |
+
while (!android_app->destroyed) {
|
751 |
+
pthread_cond_wait(&android_app->cond, &android_app->mutex);
|
752 |
+
}
|
753 |
+
pthread_mutex_unlock(&android_app->mutex);
|
754 |
+
|
755 |
+
close(android_app->msgread);
|
756 |
+
close(android_app->msgwrite);
|
757 |
+
pthread_cond_destroy(&android_app->cond);
|
758 |
+
pthread_mutex_destroy(&android_app->mutex);
|
759 |
+
free(android_app);
|
760 |
+
}
|
761 |
+
|
762 |
+
static void onDestroy(ANativeActivity* activity) {
|
763 |
+
LOGV("Destroy: %p\n", activity);
|
764 |
+
android_app_free((struct android_app*)activity->instance);
|
765 |
+
}
|
766 |
+
|
767 |
+
static void onStart(ANativeActivity* activity) {
|
768 |
+
LOGV("Start: %p\n", activity);
|
769 |
+
android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_START);
|
770 |
+
}
|
771 |
+
|
772 |
+
static void onResume(ANativeActivity* activity) {
|
773 |
+
LOGV("Resume: %p\n", activity);
|
774 |
+
android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_RESUME);
|
775 |
+
}
|
776 |
+
|
777 |
+
static void* onSaveInstanceState(ANativeActivity* activity, size_t* outLen) {
|
778 |
+
struct android_app* android_app = (struct android_app*)activity->instance;
|
779 |
+
void* savedState = NULL;
|
780 |
+
|
781 |
+
LOGV("SaveInstanceState: %p\n", activity);
|
782 |
+
pthread_mutex_lock(&android_app->mutex);
|
783 |
+
android_app->stateSaved = 0;
|
784 |
+
android_app_write_cmd(android_app, APP_CMD_SAVE_STATE);
|
785 |
+
while (!android_app->stateSaved) {
|
786 |
+
pthread_cond_wait(&android_app->cond, &android_app->mutex);
|
787 |
+
}
|
788 |
+
|
789 |
+
if (android_app->savedState != NULL) {
|
790 |
+
savedState = android_app->savedState;
|
791 |
+
*outLen = android_app->savedStateSize;
|
792 |
+
android_app->savedState = NULL;
|
793 |
+
android_app->savedStateSize = 0;
|
794 |
+
}
|
795 |
+
|
796 |
+
pthread_mutex_unlock(&android_app->mutex);
|
797 |
+
|
798 |
+
return savedState;
|
799 |
+
}
|
800 |
+
|
801 |
+
static void onPause(ANativeActivity* activity) {
|
802 |
+
LOGV("Pause: %p\n", activity);
|
803 |
+
android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_PAUSE);
|
804 |
+
}
|
805 |
+
|
806 |
+
static void onStop(ANativeActivity* activity) {
|
807 |
+
LOGV("Stop: %p\n", activity);
|
808 |
+
android_app_set_activity_state((struct android_app*)activity->instance, APP_CMD_STOP);
|
809 |
+
}
|
810 |
+
|
811 |
+
static void onConfigurationChanged(ANativeActivity* activity) {
|
812 |
+
struct android_app* android_app = (struct android_app*)activity->instance;
|
813 |
+
LOGV("ConfigurationChanged: %p\n", activity);
|
814 |
+
android_app_write_cmd(android_app, APP_CMD_CONFIG_CHANGED);
|
815 |
+
}
|
816 |
+
|
817 |
+
static void onLowMemory(ANativeActivity* activity) {
|
818 |
+
struct android_app* android_app = (struct android_app*)activity->instance;
|
819 |
+
LOGV("LowMemory: %p\n", activity);
|
820 |
+
android_app_write_cmd(android_app, APP_CMD_LOW_MEMORY);
|
821 |
+
}
|
822 |
+
|
823 |
+
static void onWindowFocusChanged(ANativeActivity* activity, int focused) {
|
824 |
+
LOGV("WindowFocusChanged: %p -- %d\n", activity, focused);
|
825 |
+
android_app_write_cmd((struct android_app*)activity->instance,
|
826 |
+
focused ? APP_CMD_GAINED_FOCUS : APP_CMD_LOST_FOCUS);
|
827 |
+
}
|
828 |
+
|
829 |
+
static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) {
|
830 |
+
LOGV("NativeWindowCreated: %p -- %p\n", activity, window);
|
831 |
+
android_app_set_window((struct android_app*)activity->instance, window);
|
832 |
+
}
|
833 |
+
|
834 |
+
static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) {
|
835 |
+
LOGV("NativeWindowDestroyed: %p -- %p\n", activity, window);
|
836 |
+
android_app_set_window((struct android_app*)activity->instance, NULL);
|
837 |
+
}
|
838 |
+
|
839 |
+
static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue) {
|
840 |
+
LOGV("InputQueueCreated: %p -- %p\n", activity, queue);
|
841 |
+
android_app_set_input((struct android_app*)activity->instance, queue);
|
842 |
+
}
|
843 |
+
|
844 |
+
static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue) {
|
845 |
+
LOGV("InputQueueDestroyed: %p -- %p\n", activity, queue);
|
846 |
+
android_app_set_input((struct android_app*)activity->instance, NULL);
|
847 |
+
}
|
848 |
+
|
849 |
+
static void onContentRectChanged(ANativeActivity* activity, const ARect* rect) {
|
850 |
+
LOGV("onContentRectChanged: %p -- (%d, %d), (%d, %d)\n", activity, rect->left, rect->top, rect->right, rect->bottom);
|
851 |
+
}
|
852 |
+
|
853 |
+
void DeferredNativeActivity_onCreate(
|
854 |
+
ANativeActivity* activity,
|
855 |
+
void* savedState,
|
856 |
+
size_t savedStateSize,
|
857 |
+
const char* load_target
|
858 |
+
)
|
859 |
+
{
|
860 |
+
activity->callbacks->onDestroy = onDestroy;
|
861 |
+
activity->callbacks->onStart = onStart;
|
862 |
+
activity->callbacks->onResume = onResume;
|
863 |
+
activity->callbacks->onSaveInstanceState = onSaveInstanceState;
|
864 |
+
activity->callbacks->onPause = onPause;
|
865 |
+
activity->callbacks->onStop = onStop;
|
866 |
+
activity->callbacks->onConfigurationChanged = onConfigurationChanged;
|
867 |
+
activity->callbacks->onLowMemory = onLowMemory;
|
868 |
+
activity->callbacks->onWindowFocusChanged = onWindowFocusChanged;
|
869 |
+
activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
|
870 |
+
activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
|
871 |
+
activity->callbacks->onInputQueueCreated = onInputQueueCreated;
|
872 |
+
activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
|
873 |
+
activity->callbacks->onContentRectChanged = onContentRectChanged;
|
874 |
+
|
875 |
+
// Create threaded android_app
|
876 |
+
android_app* app = (struct android_app*)malloc(sizeof(struct android_app));
|
877 |
+
memset(app, 0, sizeof(struct android_app));
|
878 |
+
app->activity = activity;
|
879 |
+
app->application_so = load_target;
|
880 |
+
|
881 |
+
pthread_mutex_init(&app->mutex, NULL);
|
882 |
+
pthread_cond_init(&app->cond, NULL);
|
883 |
+
|
884 |
+
if (savedState != NULL) {
|
885 |
+
app->savedState = malloc(savedStateSize);
|
886 |
+
app->savedStateSize = savedStateSize;
|
887 |
+
memcpy(app->savedState, savedState, savedStateSize);
|
888 |
+
}
|
889 |
+
|
890 |
+
int msgpipe[2];
|
891 |
+
if (pipe(msgpipe)) {
|
892 |
+
LOGE("could not create pipe: %s", strerror(errno));
|
893 |
+
exit(1);
|
894 |
+
}
|
895 |
+
app->msgread = msgpipe[0];
|
896 |
+
app->msgwrite = msgpipe[1];
|
897 |
+
|
898 |
+
pthread_attr_t attr;
|
899 |
+
pthread_attr_init(&attr);
|
900 |
+
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
|
901 |
+
pthread_create(&app->thread, &attr, android_app_entry, app);
|
902 |
+
|
903 |
+
// Wait for thread to start.
|
904 |
+
pthread_mutex_lock(&app->mutex);
|
905 |
+
while (!app->running) {
|
906 |
+
pthread_cond_wait(&app->cond, &app->mutex);
|
907 |
+
}
|
908 |
+
pthread_mutex_unlock(&app->mutex);
|
909 |
+
|
910 |
+
activity->instance = app;
|
911 |
+
|
912 |
+
// Save global variables for use later
|
913 |
+
memset(&g_engine, 0, sizeof(pangolin::engine));
|
914 |
+
app->userData = &g_engine;
|
915 |
+
app->onAppCmd = pangolin::engine_handle_cmd;
|
916 |
+
app->onInputEvent = pangolin::engine_handle_input;
|
917 |
+
g_engine.app = app;
|
918 |
+
g_engine.activity = activity;
|
919 |
+
|
920 |
+
// // Load existing state if it exists
|
921 |
+
// if (app->savedState != NULL) {
|
922 |
+
// // We are starting with a previous saved state; restore from it.
|
923 |
+
// g_engine.state = *(struct pangolin::saved_state*)app->savedState;
|
924 |
+
// }
|
925 |
+
}
|
926 |
+
}
|
927 |
+
|
928 |
+
namespace pangolin
|
929 |
+
{
|
930 |
+
|
931 |
+
void CreateAndroidWindowAndBind(std::string name)
|
932 |
+
{
|
933 |
+
LOGI("*****************************************************************");
|
934 |
+
LOGV("+CreateAndroidWindowAndBind");
|
935 |
+
// Bind and Wait for GL Context
|
936 |
+
pangolin::BindToContext(name);
|
937 |
+
ProcessAndroidEvents();
|
938 |
+
LOGV("-CreateAndroidWindowAndBind");
|
939 |
+
}
|
940 |
+
|
941 |
+
void ProcessAndroidEvents()
|
942 |
+
{
|
943 |
+
do {
|
944 |
+
// Read all pending events.
|
945 |
+
int ident;
|
946 |
+
int events;
|
947 |
+
struct android_poll_source* source;
|
948 |
+
|
949 |
+
// If not animating, we will block forever waiting for events.
|
950 |
+
// If animating, we loop until all events are read, then continue
|
951 |
+
// to draw the next frame of animation.
|
952 |
+
while ((ident=ALooper_pollAll(g_engine.has_focus ? 0 : -1, NULL, &events,
|
953 |
+
(void**)&source)) >= 0) {
|
954 |
+
|
955 |
+
// Process this event.
|
956 |
+
if (source != NULL) {
|
957 |
+
source->process(g_engine.app, source);
|
958 |
+
}
|
959 |
+
|
960 |
+
// Check if we are exiting.
|
961 |
+
if (g_engine.app->destroyRequested != 0) {
|
962 |
+
engine_term_display(&g_engine);
|
963 |
+
context->quit = true;
|
964 |
+
return;
|
965 |
+
}
|
966 |
+
}
|
967 |
+
} while (g_engine.display == NULL);
|
968 |
+
}
|
969 |
+
|
970 |
+
void FinishAndroidFrame()
|
971 |
+
{
|
972 |
+
ProcessAndroidEvents();
|
973 |
+
RenderViews();
|
974 |
+
PostRender();
|
975 |
+
eglSwapBuffers(g_engine.display, g_engine.surface);
|
976 |
+
}
|
977 |
+
|
978 |
+
// Implement platform agnostic version
|
979 |
+
void CreateWindowAndBind(std::string window_title, int /*w*/, int /*h*/, const Params& /*params*/ )
|
980 |
+
{
|
981 |
+
CreateAndroidWindowAndBind(window_title);
|
982 |
+
|
983 |
+
#ifdef HAVE_GLES_2
|
984 |
+
// Bind default compatibility shader
|
985 |
+
pangolin::glEngine().prog_fixed.Bind();
|
986 |
+
#endif
|
987 |
+
}
|
988 |
+
|
989 |
+
// Implement platform agnostic version
|
990 |
+
void FinishFrame()
|
991 |
+
{
|
992 |
+
FinishAndroidFrame();
|
993 |
+
}
|
994 |
+
|
995 |
+
void SetFullscreen(bool /*fullscreen*/)
|
996 |
+
{
|
997 |
+
// Do nothing
|
998 |
+
}
|
999 |
+
|
1000 |
+
void PangolinPlatformInit(PangolinGl& /*context*/)
|
1001 |
+
{
|
1002 |
+
}
|
1003 |
+
|
1004 |
+
void PangolinPlatformDeinit(PangolinGl& /*context*/)
|
1005 |
+
{
|
1006 |
+
}
|
1007 |
+
|
1008 |
+
PANGOLIN_REGISTER_FACTORY(AndroidWindow)
|
1009 |
+
{
|
1010 |
+
struct AndroidWindowFactory : public TypedFactoryInterface<WindowInterface> {
|
1011 |
+
std::unique_ptr<WindowInterface> Open(const Uri& uri) override {
|
1012 |
+
|
1013 |
+
const std::string window_title = uri.Get<std::string>("window_title", "window");
|
1014 |
+
CreateAndroidWindowAndBind(window_title);
|
1015 |
+
#ifdef HAVE_GLES_2
|
1016 |
+
// Bind default compatibility shader
|
1017 |
+
pangolin::glEngine().prog_fixed.Bind();
|
1018 |
+
#endif
|
1019 |
+
return NULL;
|
1020 |
+
}
|
1021 |
+
};
|
1022 |
+
|
1023 |
+
auto factory = std::make_shared<AndroidWindowFactory>();
|
1024 |
+
FactoryRegistry<WindowInterface>::I().RegisterFactory(factory, 10, "android");
|
1025 |
+
}
|
1026 |
+
}
|
third-party/DPVO/Pangolin/components/pango_windowing/src/display_emscripten.cpp
ADDED
@@ -0,0 +1,324 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2018 Andrey Mnatsakanov
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#include <pangolin/factory/factory_registry.h>
|
29 |
+
#include <pangolin/platform.h>
|
30 |
+
#include <pangolin/gl/glinclude.h>
|
31 |
+
#include <pangolin/windowing/window.h>
|
32 |
+
#include <pangolin/windowing/EmscriptenWindow.h>
|
33 |
+
|
34 |
+
#include <mutex>
|
35 |
+
#include <stdexcept>
|
36 |
+
#include <stdio.h>
|
37 |
+
#include <stdlib.h>
|
38 |
+
#include <string.h>
|
39 |
+
#include <unistd.h>
|
40 |
+
#include <stdint.h>
|
41 |
+
|
42 |
+
namespace pangolin
|
43 |
+
{
|
44 |
+
|
45 |
+
constexpr char em_dom_id[] = "#canvas";
|
46 |
+
|
47 |
+
int spec_key(const char* key_string){
|
48 |
+
if(strlen(key_string)==1){
|
49 |
+
return -1;
|
50 |
+
}
|
51 |
+
if(strcmp(key_string, "F1")==0) return PANGO_SPECIAL + PANGO_KEY_F1;
|
52 |
+
if(strcmp(key_string, "F2")==0) return PANGO_SPECIAL + PANGO_KEY_F2;
|
53 |
+
if(strcmp(key_string, "F3")==0) return PANGO_SPECIAL + PANGO_KEY_F3;
|
54 |
+
if(strcmp(key_string, "F4")==0) return PANGO_SPECIAL + PANGO_KEY_F4;
|
55 |
+
if(strcmp(key_string, "F5")==0) return PANGO_SPECIAL + PANGO_KEY_F5;
|
56 |
+
if(strcmp(key_string, "F6")==0) return PANGO_SPECIAL + PANGO_KEY_F6;
|
57 |
+
if(strcmp(key_string, "F7")==0) return PANGO_SPECIAL + PANGO_KEY_F7;
|
58 |
+
if(strcmp(key_string, "F8")==0) return PANGO_SPECIAL + PANGO_KEY_F8;
|
59 |
+
if(strcmp(key_string, "F9")==0) return PANGO_SPECIAL + PANGO_KEY_F9;
|
60 |
+
if(strcmp(key_string, "F10")==0)return PANGO_SPECIAL + PANGO_KEY_F10;
|
61 |
+
if(strcmp(key_string, "F11")==0)return PANGO_SPECIAL + PANGO_KEY_F11;
|
62 |
+
if(strcmp(key_string, "F12")==0)return PANGO_SPECIAL + PANGO_KEY_F12;
|
63 |
+
if(strcmp(key_string, "ArrowLeft")==0)return PANGO_SPECIAL + PANGO_KEY_LEFT;
|
64 |
+
if(strcmp(key_string, "ArrowUp")==0)return PANGO_SPECIAL + PANGO_KEY_UP;
|
65 |
+
if(strcmp(key_string, "ArrowRight")==0)return PANGO_SPECIAL + PANGO_KEY_RIGHT;
|
66 |
+
if(strcmp(key_string, "ArrowDown")==0)return PANGO_SPECIAL + PANGO_KEY_DOWN;
|
67 |
+
if(strcmp(key_string, "PageUp")==0)return PANGO_SPECIAL + PANGO_KEY_PAGE_UP;
|
68 |
+
if(strcmp(key_string, "PageDown")==0)return PANGO_SPECIAL + PANGO_KEY_PAGE_DOWN;
|
69 |
+
if(strcmp(key_string, "Home")==0)return PANGO_SPECIAL + PANGO_KEY_HOME;
|
70 |
+
if(strcmp(key_string, "End")==0)return PANGO_SPECIAL + PANGO_KEY_END;
|
71 |
+
if(strcmp(key_string, "Insert")==0)return PANGO_SPECIAL + PANGO_KEY_INSERT;
|
72 |
+
return -1;
|
73 |
+
}
|
74 |
+
|
75 |
+
KeyModifier mod_key(const char* key_string){
|
76 |
+
if(strlen(key_string)==1) return KeyModifier(0);
|
77 |
+
if(strcmp(key_string, "Shift")==0) return pangolin::KeyModifierShift;
|
78 |
+
if(strcmp(key_string, "Control")==0) return pangolin::KeyModifierCtrl;
|
79 |
+
if(strcmp(key_string, "Alt")==0) return pangolin::KeyModifierAlt;
|
80 |
+
if(strcmp(key_string, "Meta")==0) return pangolin::KeyModifierCmd;
|
81 |
+
return KeyModifier(0);
|
82 |
+
}
|
83 |
+
|
84 |
+
std::mutex window_mutex;
|
85 |
+
|
86 |
+
pangolin::KeyModifierBitmask GetKeyModifierBitmask(const EmscriptenKeyboardEvent *event)
|
87 |
+
{
|
88 |
+
pangolin::KeyModifierBitmask mask;
|
89 |
+
if(event->shiftKey) mask |= pangolin::KeyModifierShift;
|
90 |
+
if(event->ctrlKey) mask |= pangolin::KeyModifierCtrl;
|
91 |
+
if(event->altKey) mask |= pangolin::KeyModifierAlt;
|
92 |
+
if(event->metaKey) mask |= pangolin::KeyModifierCmd;
|
93 |
+
return mask;
|
94 |
+
}
|
95 |
+
|
96 |
+
pangolin::KeyModifierBitmask GetKeyModifierBitmask(const EmscriptenMouseEvent *event)
|
97 |
+
{
|
98 |
+
pangolin::KeyModifierBitmask mask;
|
99 |
+
if(event->shiftKey) mask |= pangolin::KeyModifierShift;
|
100 |
+
if(event->ctrlKey) mask |= pangolin::KeyModifierCtrl;
|
101 |
+
if(event->altKey) mask |= pangolin::KeyModifierAlt;
|
102 |
+
if(event->metaKey) mask |= pangolin::KeyModifierCmd;
|
103 |
+
return mask;
|
104 |
+
}
|
105 |
+
|
106 |
+
EM_BOOL key_callback(int eventType, const EmscriptenKeyboardEvent *e, void *userData){
|
107 |
+
EmscriptenWindow* w=(EmscriptenWindow*)userData;
|
108 |
+
|
109 |
+
if(eventType==EMSCRIPTEN_EVENT_KEYPRESS)
|
110 |
+
return false;
|
111 |
+
|
112 |
+
const KeyModifier mod = mod_key(e->key);
|
113 |
+
if(mod == 0) {
|
114 |
+
// Not a modifier key
|
115 |
+
int key = spec_key(e->key);
|
116 |
+
if(key != -1){
|
117 |
+
w->KeyboardSignal(KeyboardEvent({
|
118 |
+
(float)w->x, (float)w->y, GetKeyModifierBitmask(e),
|
119 |
+
(uint8_t)key, eventType==EMSCRIPTEN_EVENT_KEYDOWN
|
120 |
+
}));
|
121 |
+
}
|
122 |
+
if(strlen(e->key)==1){
|
123 |
+
w->KeyboardSignal(KeyboardEvent({
|
124 |
+
(float)w->x, (float)w->y, GetKeyModifierBitmask(e),
|
125 |
+
(uint8_t)((e->ctrlKey?PANGO_CTRL:0) + e->key[0]),
|
126 |
+
eventType==EMSCRIPTEN_EVENT_KEYDOWN
|
127 |
+
}));
|
128 |
+
}
|
129 |
+
}else{
|
130 |
+
w->key_modifier_state.set(mod, eventType==EMSCRIPTEN_EVENT_KEYDOWN);
|
131 |
+
}
|
132 |
+
return false;
|
133 |
+
}
|
134 |
+
|
135 |
+
EM_BOOL mouse_callback(int eventType, const EmscriptenMouseEvent *e, void *userData){
|
136 |
+
EmscriptenWindow* w=(EmscriptenWindow*)userData;
|
137 |
+
w->x = e->targetX;
|
138 |
+
w->y = e->targetY;
|
139 |
+
|
140 |
+
switch(eventType) {
|
141 |
+
case EMSCRIPTEN_EVENT_MOUSEDOWN:
|
142 |
+
case EMSCRIPTEN_EVENT_MOUSEUP:
|
143 |
+
w->MouseSignal(MouseEvent({
|
144 |
+
(float)w->x, (float)w->y, GetKeyModifierBitmask(e),
|
145 |
+
e->button, eventType == EMSCRIPTEN_EVENT_MOUSEDOWN
|
146 |
+
}));
|
147 |
+
break;
|
148 |
+
case EMSCRIPTEN_EVENT_MOUSEMOVE:
|
149 |
+
if(e->buttons){
|
150 |
+
w->MouseMotionSignal(MouseMotionEvent({(float)w->x, (float)w->y, GetKeyModifierBitmask(e)}));
|
151 |
+
} else {
|
152 |
+
w->PassiveMouseMotionSignal(MouseMotionEvent({(float)w->x, (float)w->y, GetKeyModifierBitmask(e)}));
|
153 |
+
}
|
154 |
+
break;
|
155 |
+
case EMSCRIPTEN_EVENT_MOUSEOVER:
|
156 |
+
break;
|
157 |
+
case EMSCRIPTEN_EVENT_MOUSEOUT:
|
158 |
+
break;
|
159 |
+
default:
|
160 |
+
break;
|
161 |
+
}
|
162 |
+
return false;
|
163 |
+
}
|
164 |
+
|
165 |
+
EM_BOOL wheel_callback(int eventType, const EmscriptenWheelEvent *e, void *userData){
|
166 |
+
EmscriptenWindow* w=(EmscriptenWindow*)userData;
|
167 |
+
w->SpecialInputSignal(SpecialInputEvent({
|
168 |
+
(float)w->x, (float)w->y, w->key_modifier_state,
|
169 |
+
InputSpecialScroll,
|
170 |
+
(float)e->deltaX, (float)e->deltaY, 0, 0
|
171 |
+
}));
|
172 |
+
return true;
|
173 |
+
}
|
174 |
+
EM_BOOL uievent_callback(int eventType, const EmscriptenUiEvent *e, void *userData){
|
175 |
+
EmscriptenWindow* w=(EmscriptenWindow*)userData;
|
176 |
+
switch(eventType) {
|
177 |
+
case EMSCRIPTEN_EVENT_RESIZE:
|
178 |
+
int width, height;
|
179 |
+
emscripten_get_canvas_element_size(em_dom_id, &width, &height);
|
180 |
+
w->ResizeSignal(WindowResizeEvent({width, height}));
|
181 |
+
break;
|
182 |
+
}
|
183 |
+
return true;
|
184 |
+
}
|
185 |
+
|
186 |
+
#define TEST(error) if(error!=EMSCRIPTEN_RESULT_SUCCESS)std::cerr << "error: " << __FILE__<< ":" << __LINE__ << std::endl
|
187 |
+
|
188 |
+
EmscriptenWindow:: EmscriptenWindow()
|
189 |
+
: done_init_events(false)
|
190 |
+
{
|
191 |
+
EmscriptenWebGLContextAttributes attr;
|
192 |
+
emscripten_webgl_init_context_attributes(&attr);
|
193 |
+
attr.majorVersion = 2;
|
194 |
+
attr.minorVersion = 0;
|
195 |
+
attr.stencil = true;
|
196 |
+
attr.depth = true;
|
197 |
+
ctx = emscripten_webgl_create_context(em_dom_id, &attr);
|
198 |
+
if( ctx < 0 ) {
|
199 |
+
throw std::runtime_error("Pangolin Emscripten: Failed to create window." );
|
200 |
+
}
|
201 |
+
// Try to enable some extensions we'll probably need.
|
202 |
+
emscripten_webgl_enable_extension(ctx, "EXT_float_blend");
|
203 |
+
|
204 |
+
EMSCRIPTEN_RESULT ret;
|
205 |
+
ret = emscripten_set_keypress_callback(em_dom_id, this, 1, key_callback);
|
206 |
+
TEST(ret);
|
207 |
+
ret = emscripten_set_keydown_callback(em_dom_id, this, 1, key_callback);
|
208 |
+
TEST(ret);
|
209 |
+
ret = emscripten_set_keyup_callback(em_dom_id, this, 1, key_callback);
|
210 |
+
TEST(ret);
|
211 |
+
ret = emscripten_set_click_callback(em_dom_id, this, 1, mouse_callback);
|
212 |
+
TEST(ret);
|
213 |
+
ret = emscripten_set_mousedown_callback(em_dom_id, this, 1, mouse_callback);
|
214 |
+
TEST(ret);
|
215 |
+
ret = emscripten_set_mouseup_callback(em_dom_id, this, 1, mouse_callback);
|
216 |
+
TEST(ret);
|
217 |
+
ret = emscripten_set_dblclick_callback(em_dom_id, this, 1, mouse_callback);
|
218 |
+
TEST(ret);
|
219 |
+
ret = emscripten_set_mousemove_callback(em_dom_id, this, 1, mouse_callback);
|
220 |
+
TEST(ret);
|
221 |
+
ret = emscripten_set_mouseenter_callback(em_dom_id, this, 1, mouse_callback);
|
222 |
+
TEST(ret);
|
223 |
+
ret = emscripten_set_mouseleave_callback(em_dom_id, this, 1, mouse_callback);
|
224 |
+
TEST(ret);
|
225 |
+
ret = emscripten_set_mouseover_callback(em_dom_id, this, 1, mouse_callback);
|
226 |
+
TEST(ret);
|
227 |
+
ret = emscripten_set_mouseout_callback(em_dom_id, this, 1, mouse_callback);
|
228 |
+
TEST(ret);
|
229 |
+
ret = emscripten_set_wheel_callback(em_dom_id, this, 1, wheel_callback);
|
230 |
+
TEST(ret);
|
231 |
+
ret = emscripten_set_resize_callback(em_dom_id, this, 1, uievent_callback);
|
232 |
+
TEST(ret);
|
233 |
+
|
234 |
+
EM_ASM(Module['noExitRuntime'] = true);
|
235 |
+
}
|
236 |
+
|
237 |
+
#undef TEST
|
238 |
+
|
239 |
+
EmscriptenWindow::~ EmscriptenWindow()
|
240 |
+
{
|
241 |
+
emscripten_webgl_destroy_context(ctx);
|
242 |
+
}
|
243 |
+
|
244 |
+
void EmscriptenWindow::MakeCurrent()
|
245 |
+
{
|
246 |
+
emscripten_webgl_make_context_current(ctx);
|
247 |
+
pangolin::glEngine().prog_fixed.Bind();
|
248 |
+
}
|
249 |
+
|
250 |
+
void EmscriptenWindow::RemoveCurrent()
|
251 |
+
{
|
252 |
+
pangolin::glEngine().prog_fixed.Unbind();
|
253 |
+
}
|
254 |
+
|
255 |
+
void EmscriptenWindow::ShowFullscreen(const TrueFalseToggle)
|
256 |
+
{
|
257 |
+
// Not implemented
|
258 |
+
}
|
259 |
+
|
260 |
+
void EmscriptenWindow::Move(int x, int y)
|
261 |
+
{
|
262 |
+
// Not implemented
|
263 |
+
}
|
264 |
+
|
265 |
+
void EmscriptenWindow::Resize(unsigned int w, unsigned int h)
|
266 |
+
{
|
267 |
+
emscripten_set_canvas_element_size(em_dom_id, w, h);
|
268 |
+
}
|
269 |
+
|
270 |
+
void EmscriptenWindow::ProcessEvents()
|
271 |
+
{
|
272 |
+
// Emscripten will trigger callbacks.
|
273 |
+
if(!done_init_events) {
|
274 |
+
done_init_events = true;
|
275 |
+
int width, height;
|
276 |
+
emscripten_get_canvas_element_size(em_dom_id, &width, &height);
|
277 |
+
ResizeSignal(WindowResizeEvent({width, height}));
|
278 |
+
}
|
279 |
+
}
|
280 |
+
|
281 |
+
void EmscriptenWindow::SwapBuffers() {
|
282 |
+
emscripten_sleep(10);
|
283 |
+
}
|
284 |
+
|
285 |
+
PANGOLIN_REGISTER_FACTORY(EmscriptenWindow)
|
286 |
+
{
|
287 |
+
struct EmscriptenWindowFactory : public TypedFactoryInterface<WindowInterface> {
|
288 |
+
std::map<std::string,Precedence> Schemes() const override
|
289 |
+
{
|
290 |
+
return {{"emscripten",10},{"default",100}};
|
291 |
+
}
|
292 |
+
const char* Description() const override
|
293 |
+
{
|
294 |
+
return "Use WebGL window";
|
295 |
+
}
|
296 |
+
ParamSet Params() const override
|
297 |
+
{
|
298 |
+
return {{
|
299 |
+
{"window_title","-","Ignored"},
|
300 |
+
{"w","640","Ignored"},
|
301 |
+
{"h","480","Ignored"},
|
302 |
+
}};
|
303 |
+
}
|
304 |
+
|
305 |
+
std::unique_ptr<WindowInterface> Open(const Uri& uri) override {
|
306 |
+
// We're going to be naughty and actually ignore the title, width and height,
|
307 |
+
// but list them as parameters to be compatible with other windowing libs.
|
308 |
+
return std::unique_ptr<WindowInterface>(new EmscriptenWindow());
|
309 |
+
}
|
310 |
+
};
|
311 |
+
return FactoryRegistry::I()->RegisterFactory<WindowInterface>(std::make_shared<EmscriptenWindowFactory>());
|
312 |
+
}
|
313 |
+
|
314 |
+
}
|
315 |
+
|
316 |
+
#include <emscripten/bind.h>
|
317 |
+
|
318 |
+
std::string pango_get_exception_message(intptr_t exceptionPtr) {
|
319 |
+
return std::string(reinterpret_cast<std::exception *>(exceptionPtr)->what());
|
320 |
+
}
|
321 |
+
|
322 |
+
EMSCRIPTEN_BINDINGS(Bindings) {
|
323 |
+
emscripten::function("pango_get_exception_message", &pango_get_exception_message);
|
324 |
+
};
|
third-party/DPVO/Pangolin/components/pango_windowing/src/display_headless.cpp
ADDED
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/windowing/window.h>
|
2 |
+
#include <pangolin/factory/factory_registry.h>
|
3 |
+
#include <EGL/egl.h>
|
4 |
+
|
5 |
+
namespace pangolin {
|
6 |
+
|
7 |
+
namespace headless {
|
8 |
+
|
9 |
+
class EGLDisplayHL {
|
10 |
+
public:
|
11 |
+
EGLDisplayHL(const int width, const int height);
|
12 |
+
|
13 |
+
~EGLDisplayHL();
|
14 |
+
|
15 |
+
void swap();
|
16 |
+
|
17 |
+
void makeCurrent();
|
18 |
+
|
19 |
+
void removeCurrent();
|
20 |
+
|
21 |
+
private:
|
22 |
+
EGLSurface egl_surface;
|
23 |
+
EGLContext egl_context;
|
24 |
+
EGLDisplay egl_display;
|
25 |
+
|
26 |
+
static constexpr EGLint attribs[] = {
|
27 |
+
EGL_SURFACE_TYPE , EGL_PBUFFER_BIT,
|
28 |
+
EGL_RENDERABLE_TYPE , EGL_OPENGL_BIT,
|
29 |
+
EGL_RED_SIZE , 8,
|
30 |
+
EGL_GREEN_SIZE , 8,
|
31 |
+
EGL_BLUE_SIZE , 8,
|
32 |
+
EGL_ALPHA_SIZE , 8,
|
33 |
+
EGL_DEPTH_SIZE , 24,
|
34 |
+
EGL_STENCIL_SIZE , 8,
|
35 |
+
EGL_NONE
|
36 |
+
};
|
37 |
+
};
|
38 |
+
|
39 |
+
constexpr EGLint EGLDisplayHL::attribs[];
|
40 |
+
|
41 |
+
struct HeadlessWindow : public WindowInterface {
|
42 |
+
HeadlessWindow(const int width, const int height);
|
43 |
+
|
44 |
+
~HeadlessWindow() override;
|
45 |
+
|
46 |
+
void ShowFullscreen(const TrueFalseToggle on_off) override;
|
47 |
+
|
48 |
+
void Move(const int x, const int y) override;
|
49 |
+
|
50 |
+
void Resize(const unsigned int w, const unsigned int h) override;
|
51 |
+
|
52 |
+
void MakeCurrent() override;
|
53 |
+
|
54 |
+
void RemoveCurrent() override;
|
55 |
+
|
56 |
+
void SwapBuffers() override;
|
57 |
+
|
58 |
+
void ProcessEvents() override;
|
59 |
+
|
60 |
+
EGLDisplayHL display;
|
61 |
+
};
|
62 |
+
|
63 |
+
EGLDisplayHL::EGLDisplayHL(const int width, const int height) {
|
64 |
+
egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
65 |
+
if(!egl_display) {
|
66 |
+
std::cerr << "Failed to open EGL display" << std::endl;
|
67 |
+
}
|
68 |
+
|
69 |
+
EGLint major, minor;
|
70 |
+
if(eglInitialize(egl_display, &major, &minor)==EGL_FALSE) {
|
71 |
+
std::cerr << "EGL init failed" << std::endl;
|
72 |
+
}
|
73 |
+
|
74 |
+
if(eglBindAPI(EGL_OPENGL_API)==EGL_FALSE) {
|
75 |
+
std::cerr << "EGL bind failed" << std::endl;
|
76 |
+
}
|
77 |
+
|
78 |
+
EGLint count;
|
79 |
+
eglGetConfigs(egl_display, nullptr, 0, &count);
|
80 |
+
|
81 |
+
std::vector<EGLConfig> egl_configs(count);
|
82 |
+
|
83 |
+
EGLint numConfigs;
|
84 |
+
eglChooseConfig(egl_display, attribs, egl_configs.data(), count, &numConfigs);
|
85 |
+
|
86 |
+
egl_context = eglCreateContext(egl_display, egl_configs[0], EGL_NO_CONTEXT, nullptr);
|
87 |
+
|
88 |
+
const EGLint pbufferAttribs[] = {
|
89 |
+
EGL_WIDTH, width,
|
90 |
+
EGL_HEIGHT, height,
|
91 |
+
EGL_NONE,
|
92 |
+
};
|
93 |
+
egl_surface = eglCreatePbufferSurface(egl_display, egl_configs[0], pbufferAttribs);
|
94 |
+
if (egl_surface == EGL_NO_SURFACE) {
|
95 |
+
std::cerr << "Cannot create EGL surface" << std::endl;
|
96 |
+
}
|
97 |
+
}
|
98 |
+
|
99 |
+
EGLDisplayHL::~EGLDisplayHL() {
|
100 |
+
if(egl_context) eglDestroyContext(egl_display, egl_context);
|
101 |
+
if(egl_surface) eglDestroySurface(egl_display, egl_surface);
|
102 |
+
if(egl_display) eglTerminate(egl_display);
|
103 |
+
}
|
104 |
+
|
105 |
+
void EGLDisplayHL::swap() {
|
106 |
+
eglSwapBuffers(egl_display, egl_surface);
|
107 |
+
}
|
108 |
+
|
109 |
+
void EGLDisplayHL::makeCurrent() {
|
110 |
+
eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
|
111 |
+
}
|
112 |
+
|
113 |
+
void EGLDisplayHL::removeCurrent() {
|
114 |
+
eglMakeCurrent(egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
115 |
+
}
|
116 |
+
|
117 |
+
HeadlessWindow::HeadlessWindow(const int w, const int h) : display(w, h) {
|
118 |
+
}
|
119 |
+
|
120 |
+
HeadlessWindow::~HeadlessWindow() { }
|
121 |
+
|
122 |
+
void HeadlessWindow::MakeCurrent() {
|
123 |
+
display.makeCurrent();
|
124 |
+
}
|
125 |
+
|
126 |
+
void HeadlessWindow::RemoveCurrent() {
|
127 |
+
display.removeCurrent();
|
128 |
+
}
|
129 |
+
|
130 |
+
void HeadlessWindow::ShowFullscreen(const TrueFalseToggle) { }
|
131 |
+
|
132 |
+
void HeadlessWindow::Move(const int /*x*/, const int /*y*/) { }
|
133 |
+
|
134 |
+
void HeadlessWindow::Resize(const unsigned int /*w*/, const unsigned int /*h*/) { }
|
135 |
+
|
136 |
+
void HeadlessWindow::ProcessEvents() { }
|
137 |
+
|
138 |
+
void HeadlessWindow::SwapBuffers() {
|
139 |
+
display.swap();
|
140 |
+
MakeCurrent();
|
141 |
+
}
|
142 |
+
|
143 |
+
} // namespace headless
|
144 |
+
|
145 |
+
PANGOLIN_REGISTER_FACTORY(HeadlessWindow) {
|
146 |
+
struct HeadlessWindowFactory : public TypedFactoryInterface<WindowInterface> {
|
147 |
+
std::map<std::string,Precedence> Schemes() const override
|
148 |
+
{
|
149 |
+
return {{"egl",10}, {"nogui",10}, {"headless",10}, {"none",10}};
|
150 |
+
}
|
151 |
+
const char* Description() const override
|
152 |
+
{
|
153 |
+
return "Headless GL Buffer";
|
154 |
+
}
|
155 |
+
ParamSet Params() const override
|
156 |
+
{
|
157 |
+
return {{
|
158 |
+
{"w","640","Requested buffer width"},
|
159 |
+
{"h","480","Requested buffer height"},
|
160 |
+
{"window_title","main","Title (Unused)"},
|
161 |
+
}};
|
162 |
+
}
|
163 |
+
std::unique_ptr<WindowInterface> Open(const Uri& uri) override {
|
164 |
+
return std::unique_ptr<WindowInterface>(new headless::HeadlessWindow(uri.Get<int>("w", 640), uri.Get<int>("h", 480)));
|
165 |
+
}
|
166 |
+
|
167 |
+
virtual ~HeadlessWindowFactory() { }
|
168 |
+
};
|
169 |
+
|
170 |
+
return FactoryRegistry::I()->RegisterFactory<WindowInterface>(std::make_shared<HeadlessWindowFactory>());
|
171 |
+
}
|
172 |
+
|
173 |
+
} // namespace pangolin
|
174 |
+
|
third-party/DPVO/Pangolin/components/pango_windowing/src/display_osx.mm
ADDED
@@ -0,0 +1,213 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2011-2018 Steven Lovegrove, Andrey Mnatsakanov
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
// Silence all the OSX GL deprecation messages.
|
29 |
+
#define GL_SILENCE_DEPRECATION
|
30 |
+
|
31 |
+
#include <pangolin/factory/factory_registry.h>
|
32 |
+
#include <pangolin/platform.h>
|
33 |
+
#include <pangolin/gl/glinclude.h>
|
34 |
+
#include <pangolin/windowing/OsxWindow.h>
|
35 |
+
#include <pangolin/windowing/PangolinNSGLView.h>
|
36 |
+
#include <pangolin/windowing/PangolinNSApplication.h>
|
37 |
+
#include <memory>
|
38 |
+
|
39 |
+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101200
|
40 |
+
# define NSFullScreenWindowMask NSWindowStyleMaskFullScreen
|
41 |
+
# define NSTitledWindowMask NSWindowStyleMaskTitled
|
42 |
+
# define NSMiniaturizableWindowMask NSWindowStyleMaskMiniaturizable
|
43 |
+
# define NSResizableWindowMask NSWindowStyleMaskResizable
|
44 |
+
# define NSClosableWindowMask NSWindowStyleMaskClosable
|
45 |
+
#endif
|
46 |
+
|
47 |
+
// Hack to fix window focus issue
|
48 |
+
// http://www.miscdebris.net/blog/2010/03/30/solution-for-my-mac-os-x-gui-program-doesnt-get-focus-if-its-outside-an-application-bundle/
|
49 |
+
extern "C" { void CPSEnableForegroundOperation(ProcessSerialNumber* psn); }
|
50 |
+
inline void FixOsxFocus()
|
51 |
+
{
|
52 |
+
#pragma clang diagnostic push
|
53 |
+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
54 |
+
ProcessSerialNumber psn;
|
55 |
+
GetCurrentProcess( &psn );
|
56 |
+
CPSEnableForegroundOperation( &psn );
|
57 |
+
SetFrontProcess( &psn );
|
58 |
+
#pragma clang diagnostic pop
|
59 |
+
}
|
60 |
+
|
61 |
+
namespace pangolin
|
62 |
+
{
|
63 |
+
|
64 |
+
std::unique_ptr<WindowInterface> CreateOsxWindowAndBind(std::string window_title, int w, int h, const bool is_highres)
|
65 |
+
{
|
66 |
+
|
67 |
+
OsxWindow* win = new OsxWindow(window_title, w, h, is_highres);
|
68 |
+
|
69 |
+
return std::unique_ptr<WindowInterface>(win);
|
70 |
+
}
|
71 |
+
|
72 |
+
OsxWindow::OsxWindow(
|
73 |
+
const std::string& title, int width, int height, bool USE_RETINA
|
74 |
+
) {
|
75 |
+
///////////////////////////////////////////////////////////////////////
|
76 |
+
// Make sure Application is initialised correctly.
|
77 |
+
// This can be run repeatedly.
|
78 |
+
|
79 |
+
[NSApplication sharedApplication];
|
80 |
+
PangolinAppDelegate *delegate = [[PangolinAppDelegate alloc] init];
|
81 |
+
|
82 |
+
[NSApp setDelegate:delegate];
|
83 |
+
[NSApp setPresentationOptions:NSFullScreenWindowMask];
|
84 |
+
|
85 |
+
[PangolinNSApplication run_pre];
|
86 |
+
[PangolinNSApplication run_step];
|
87 |
+
|
88 |
+
///////////////////////////////////////////////////////////////////////
|
89 |
+
// Create Window
|
90 |
+
|
91 |
+
NSRect viewRect = NSMakeRect( 0.0, 0.0, width, height );
|
92 |
+
|
93 |
+
_window = [[NSWindow alloc] initWithContentRect:viewRect styleMask:NSTitledWindowMask|NSMiniaturizableWindowMask|NSResizableWindowMask|NSClosableWindowMask backing:NSBackingStoreBuffered defer:YES];
|
94 |
+
[_window setTitle:[NSString stringWithUTF8String:title.c_str()]];
|
95 |
+
[_window setOpaque:YES];
|
96 |
+
[_window makeKeyAndOrderFront:NSApp];
|
97 |
+
[_window setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary];
|
98 |
+
|
99 |
+
PangolinWindowDelegate *windelegate = [[PangolinWindowDelegate alloc] init];
|
100 |
+
[_window setDelegate:windelegate];
|
101 |
+
windelegate->osx_window = this;
|
102 |
+
|
103 |
+
///////////////////////////////////////////////////////////////////////
|
104 |
+
// Create OpenGL View for Window
|
105 |
+
|
106 |
+
NSOpenGLPixelFormatAttribute attrs[] =
|
107 |
+
{
|
108 |
+
NSOpenGLPFADoubleBuffer,
|
109 |
+
NSOpenGLPFADepthSize, 32,
|
110 |
+
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersionLegacy,
|
111 |
+
0
|
112 |
+
};
|
113 |
+
|
114 |
+
NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
|
115 |
+
view = [[PangolinNSGLView alloc] initWithFrame:_window.frame pixelFormat:format];
|
116 |
+
view->osx_window = this;
|
117 |
+
|
118 |
+
[format release];
|
119 |
+
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
|
120 |
+
if( USE_RETINA && floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
|
121 |
+
[view setWantsBestResolutionOpenGLSurface:YES];
|
122 |
+
#endif /*MAC_OS_X_VERSION_MAX_ALLOWED*/
|
123 |
+
|
124 |
+
[_window setContentView:view];
|
125 |
+
|
126 |
+
glewInit();
|
127 |
+
|
128 |
+
FixOsxFocus();
|
129 |
+
}
|
130 |
+
|
131 |
+
OsxWindow::~OsxWindow()
|
132 |
+
{
|
133 |
+
// Not sure how to deallocate...
|
134 |
+
}
|
135 |
+
|
136 |
+
|
137 |
+
void OsxWindow::ShowFullscreen(const TrueFalseToggle on_off)
|
138 |
+
{
|
139 |
+
const bool is_fullscreen = ([_window styleMask] & NSFullScreenWindowMask) == NSFullScreenWindowMask;
|
140 |
+
if(should_toggle(on_off, is_fullscreen) ) {
|
141 |
+
[_window toggleFullScreen:nil];
|
142 |
+
}
|
143 |
+
}
|
144 |
+
|
145 |
+
void OsxWindow::Move(int x, int y)
|
146 |
+
{
|
147 |
+
[_window setFrame:CGRectMake(x, y, [_window frame].size.width,
|
148 |
+
[_window frame].size.height) display:NO];
|
149 |
+
}
|
150 |
+
|
151 |
+
void OsxWindow::Resize(unsigned int w, unsigned int h)
|
152 |
+
{
|
153 |
+
const CGFloat title_height = _window.frame.size.height -
|
154 |
+
[_window contentRectForFrameRect: _window.frame].size.height;
|
155 |
+
|
156 |
+
[_window setFrame:CGRectMake([_window frame].origin.x,
|
157 |
+
[_window frame].origin.y, w, h+title_height) display:NO];
|
158 |
+
}
|
159 |
+
|
160 |
+
void OsxWindow::MakeCurrent()
|
161 |
+
{
|
162 |
+
[[view openGLContext] makeCurrentContext];
|
163 |
+
}
|
164 |
+
|
165 |
+
void OsxWindow::RemoveCurrent()
|
166 |
+
{
|
167 |
+
[NSOpenGLContext clearCurrentContext];
|
168 |
+
}
|
169 |
+
|
170 |
+
void OsxWindow::SwapBuffers()
|
171 |
+
{
|
172 |
+
[[view openGLContext] flushBuffer];
|
173 |
+
}
|
174 |
+
|
175 |
+
void OsxWindow::ProcessEvents()
|
176 |
+
{
|
177 |
+
[PangolinNSApplication run_step];
|
178 |
+
}
|
179 |
+
|
180 |
+
PANGOLIN_REGISTER_FACTORY(OsxWindow)
|
181 |
+
{
|
182 |
+
struct OsxWindowFactory : public TypedFactoryInterface<WindowInterface> {
|
183 |
+
std::map<std::string,Precedence> Schemes() const override
|
184 |
+
{
|
185 |
+
return {{"cocoa",10}, {"default",10}};
|
186 |
+
}
|
187 |
+
const char* Description() const override
|
188 |
+
{
|
189 |
+
return "Use MacOS native window toolkit";
|
190 |
+
}
|
191 |
+
ParamSet Params() const override
|
192 |
+
{
|
193 |
+
return {{
|
194 |
+
{"window_title","window","Title of application Window"},
|
195 |
+
{"HIGHRES","true","Use 'retina' resolution"},
|
196 |
+
{"w","640","Requested window width"},
|
197 |
+
{"h","480","Requested window height"},
|
198 |
+
}};
|
199 |
+
}
|
200 |
+
std::unique_ptr<WindowInterface> Open(const Uri& uri) override {
|
201 |
+
|
202 |
+
const std::string window_title = uri.Get<std::string>("window_title", "window");
|
203 |
+
const int w = uri.Get<int>("w", 640);
|
204 |
+
const int h = uri.Get<int>("h", 480);
|
205 |
+
const bool is_highres = uri.Get<bool>(PARAM_HIGHRES, true);
|
206 |
+
return std::unique_ptr<WindowInterface>(CreateOsxWindowAndBind(window_title, w, h, is_highres));
|
207 |
+
}
|
208 |
+
};
|
209 |
+
|
210 |
+
return FactoryRegistry::I()->RegisterFactory<WindowInterface>(std::make_shared<OsxWindowFactory>());
|
211 |
+
}
|
212 |
+
|
213 |
+
}
|
third-party/DPVO/Pangolin/components/pango_windowing/src/display_wayland.cpp
ADDED
@@ -0,0 +1,1074 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/platform.h>
|
2 |
+
#include <pangolin/factory/factory_registry.h>
|
3 |
+
#include <pangolin/gl/colour.h>
|
4 |
+
#include <pangolin/gl/gldraw.h>
|
5 |
+
#include <pangolin/windowing/window.h>
|
6 |
+
|
7 |
+
#include <wayland-client.h>
|
8 |
+
#include <wayland-egl.h>
|
9 |
+
#include <EGL/egl.h>
|
10 |
+
#include <xkbcommon/xkbcommon.h>
|
11 |
+
#include <wayland-cursor.h>
|
12 |
+
#include <linux/input.h>
|
13 |
+
#include <sys/mman.h>
|
14 |
+
|
15 |
+
#include <mutex>
|
16 |
+
#include <string.h>
|
17 |
+
#include <unistd.h>
|
18 |
+
#include <cstdlib>
|
19 |
+
|
20 |
+
#include <xdg-shell-client-protocol.h>
|
21 |
+
|
22 |
+
#define WAYLAND_VERSION_GE(MAJ, MIN) WAYLAND_VERSION_MAJOR >= MAJ && WAYLAND_VERSION_MINOR >= MIN
|
23 |
+
|
24 |
+
// The "wl_array_for_each" C macro for C++
|
25 |
+
// https://github.com/libretro/RetroArch/blob/a9125fffaa981cab811ba6caf4d756fa6ef9a561/input/common/wayland_common.h#L50-L53
|
26 |
+
#define WL_ARRAY_FOR_EACH(pos, array, type) \
|
27 |
+
for (pos = (type)(array)->data; \
|
28 |
+
(const char *) pos < ((const char *) (array)->data + (array)->size); \
|
29 |
+
(pos)++)
|
30 |
+
|
31 |
+
namespace pangolin {
|
32 |
+
|
33 |
+
namespace wayland {
|
34 |
+
|
35 |
+
static const std::map<enum xdg_toplevel_resize_edge, std::string> resize_cursor = {
|
36 |
+
{XDG_TOPLEVEL_RESIZE_EDGE_NONE, "grabbing"},
|
37 |
+
{XDG_TOPLEVEL_RESIZE_EDGE_TOP, "top_side"},
|
38 |
+
{XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM, "bottom_side"},
|
39 |
+
{XDG_TOPLEVEL_RESIZE_EDGE_LEFT, "left_side"},
|
40 |
+
{XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT, "top_left_corner"},
|
41 |
+
{XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT, "bottom_left_corner"},
|
42 |
+
{XDG_TOPLEVEL_RESIZE_EDGE_RIGHT, "right_side"},
|
43 |
+
{XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT, "top_right_corner"},
|
44 |
+
{XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT, "bottom_right_corner"},
|
45 |
+
};
|
46 |
+
|
47 |
+
struct ButtonSurface {
|
48 |
+
struct wl_surface *surface;
|
49 |
+
struct wl_subsurface *subsurface;
|
50 |
+
struct wl_egl_window *egl_window;
|
51 |
+
EGLSurface egl_surface;
|
52 |
+
EGLContext egl_context;
|
53 |
+
EGLDisplay egl_display;
|
54 |
+
const int32_t x, y;
|
55 |
+
const uint width, height;
|
56 |
+
pangolin::Colour colour;
|
57 |
+
|
58 |
+
enum type {
|
59 |
+
CLOSE = 100,
|
60 |
+
MAXIMISE
|
61 |
+
} function;
|
62 |
+
|
63 |
+
ButtonSurface(wl_compositor* compositor, wl_subcompositor* subcompositor,
|
64 |
+
wl_surface* source, EGLDisplay egl_display, EGLConfig config,
|
65 |
+
int32_t x, int32_t y, uint width, uint height,
|
66 |
+
type fnct, pangolin::Colour colour
|
67 |
+
) :
|
68 |
+
egl_display(egl_display),
|
69 |
+
x(x), y(y),
|
70 |
+
width(width), height(height),
|
71 |
+
colour(colour),
|
72 |
+
function(fnct)
|
73 |
+
{
|
74 |
+
surface = wl_compositor_create_surface(compositor);
|
75 |
+
subsurface = wl_subcompositor_get_subsurface(subcompositor, surface, source);
|
76 |
+
egl_context = eglCreateContext (egl_display, config, EGL_NO_CONTEXT, nullptr);
|
77 |
+
egl_window = wl_egl_window_create(surface, width, height);
|
78 |
+
egl_surface = eglCreateWindowSurface(egl_display, config, (EGLNativeWindowType)egl_window, nullptr);
|
79 |
+
}
|
80 |
+
|
81 |
+
~ButtonSurface() {
|
82 |
+
if(egl_surface) eglDestroySurface(egl_display, egl_surface);
|
83 |
+
if(egl_window) wl_egl_window_destroy(egl_window);
|
84 |
+
if(egl_context) eglDestroyContext(egl_display, egl_context);
|
85 |
+
|
86 |
+
if(subsurface) wl_subsurface_destroy(subsurface);
|
87 |
+
if(surface) wl_surface_destroy(surface);
|
88 |
+
}
|
89 |
+
|
90 |
+
void reposition(const int main_w) const {
|
91 |
+
wl_subsurface_set_position(subsurface, main_w-x-width, y);
|
92 |
+
}
|
93 |
+
|
94 |
+
void draw() const {
|
95 |
+
eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
|
96 |
+
glClearColor(colour.r, colour.g, colour.b, colour.a);
|
97 |
+
glClear(GL_COLOR_BUFFER_BIT);
|
98 |
+
switch (function) {
|
99 |
+
case CLOSE:
|
100 |
+
glLineWidth(3);
|
101 |
+
glColor3f(1, 1, 1);
|
102 |
+
glBegin(GL_LINES);
|
103 |
+
glVertex2f(-1, -1);
|
104 |
+
glVertex2f(1, 1);
|
105 |
+
glVertex2f(1, -1);
|
106 |
+
glVertex2f(-1, 1);
|
107 |
+
glEnd();
|
108 |
+
break;
|
109 |
+
case MAXIMISE:
|
110 |
+
glLineWidth(2);
|
111 |
+
glColor3f(0, 0, 0);
|
112 |
+
glBegin(GL_LINE_LOOP);
|
113 |
+
glVertex2f(-0.7f, -0.7f);
|
114 |
+
glVertex2f(0.7f, -0.7f);
|
115 |
+
glVertex2f(0.7f, 0.7f);
|
116 |
+
glVertex2f(-0.7f, 0.7f);
|
117 |
+
glEnd();
|
118 |
+
glLineWidth(3);
|
119 |
+
glBegin(GL_LINES);
|
120 |
+
glVertex2f(+0.7f, +0.7f);
|
121 |
+
glVertex2f(-0.7f, +0.7f);
|
122 |
+
glEnd();
|
123 |
+
break;
|
124 |
+
}
|
125 |
+
eglSwapInterval(egl_display, 0);
|
126 |
+
eglSwapBuffers(egl_display, egl_surface);
|
127 |
+
}
|
128 |
+
};
|
129 |
+
|
130 |
+
struct DecorationSurface {
|
131 |
+
struct wl_surface *surface;
|
132 |
+
struct wl_subsurface *subsurface;
|
133 |
+
struct wl_egl_window *egl_window;
|
134 |
+
EGLSurface egl_surface;
|
135 |
+
EGLContext egl_context;
|
136 |
+
EGLDisplay egl_display;
|
137 |
+
pangolin::Colour colour;
|
138 |
+
uint border_size;
|
139 |
+
uint title_bar_size;
|
140 |
+
|
141 |
+
enum xdg_toplevel_resize_edge function;
|
142 |
+
|
143 |
+
DecorationSurface(wl_compositor* compositor, wl_subcompositor* subcompositor,
|
144 |
+
wl_surface* source, EGLDisplay egl_display, EGLConfig config,
|
145 |
+
const uint _border_size, const uint _title_bar_size,
|
146 |
+
enum xdg_toplevel_resize_edge type, pangolin::Colour _colour
|
147 |
+
) :
|
148 |
+
egl_display(egl_display),
|
149 |
+
colour(_colour),
|
150 |
+
border_size(_border_size),
|
151 |
+
title_bar_size(_title_bar_size),
|
152 |
+
function(type)
|
153 |
+
{
|
154 |
+
surface = wl_compositor_create_surface(compositor);
|
155 |
+
subsurface = wl_subcompositor_get_subsurface(subcompositor, surface, source);
|
156 |
+
egl_context = eglCreateContext(egl_display, config, EGL_NO_CONTEXT, nullptr);
|
157 |
+
egl_window = wl_egl_window_create(surface, 50, 50);
|
158 |
+
egl_surface = eglCreateWindowSurface(egl_display, config, (EGLNativeWindowType)egl_window, nullptr);
|
159 |
+
}
|
160 |
+
|
161 |
+
~DecorationSurface() {
|
162 |
+
if(egl_surface) eglDestroySurface(egl_display, egl_surface);
|
163 |
+
if(egl_window) wl_egl_window_destroy(egl_window);
|
164 |
+
if(egl_context) eglDestroyContext(egl_display, egl_context);
|
165 |
+
|
166 |
+
if(subsurface) wl_subsurface_destroy(subsurface);
|
167 |
+
if(surface) wl_surface_destroy(surface);
|
168 |
+
}
|
169 |
+
|
170 |
+
void calc_dim(const int main_w, const int main_h, int &x, int &y, int &w, int &h) const {
|
171 |
+
// get position and dimension from type and main surface
|
172 |
+
switch (function) {
|
173 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_NONE:
|
174 |
+
x=0; y=-title_bar_size;
|
175 |
+
w=main_w; h=title_bar_size;
|
176 |
+
break;
|
177 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_TOP:
|
178 |
+
x=0; y=-title_bar_size-border_size;
|
179 |
+
w=main_w; h=border_size;
|
180 |
+
break;
|
181 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM:
|
182 |
+
x=0; y=main_h;
|
183 |
+
w=main_w; h=border_size;
|
184 |
+
break;
|
185 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_LEFT:
|
186 |
+
x=-border_size; y=-title_bar_size;
|
187 |
+
w=border_size; h=main_h+title_bar_size;
|
188 |
+
break;
|
189 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT:
|
190 |
+
x=-border_size; y=-border_size-title_bar_size;
|
191 |
+
w=border_size; h=border_size;
|
192 |
+
break;
|
193 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT:
|
194 |
+
x=-border_size; y=main_h;
|
195 |
+
w=border_size; h=border_size;
|
196 |
+
break;
|
197 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_RIGHT:
|
198 |
+
x=main_w; y=-title_bar_size;
|
199 |
+
w=border_size; h=main_h+title_bar_size;
|
200 |
+
break;
|
201 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT:
|
202 |
+
x=main_w; y=-border_size-title_bar_size;
|
203 |
+
w=border_size; h=border_size;
|
204 |
+
break;
|
205 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT:
|
206 |
+
x=main_w; y=main_h;
|
207 |
+
w=border_size; h=border_size;
|
208 |
+
break;
|
209 |
+
}
|
210 |
+
}
|
211 |
+
|
212 |
+
void resize(const int main_w, const int main_h) const {
|
213 |
+
int x=0, y=0, w=0, h=0;
|
214 |
+
calc_dim(main_w, main_h, x, y, w, h);
|
215 |
+
wl_subsurface_set_position(subsurface, x, y);
|
216 |
+
wl_egl_window_resize(egl_window, w, h, 0, 0);
|
217 |
+
}
|
218 |
+
|
219 |
+
void draw() const {
|
220 |
+
eglMakeCurrent(egl_display, egl_surface, egl_surface, egl_context);
|
221 |
+
glClearColor(colour.r, colour.g, colour.b, colour.a);
|
222 |
+
glClear(GL_COLOR_BUFFER_BIT);
|
223 |
+
eglSwapInterval(egl_display, 0);
|
224 |
+
eglSwapBuffers(egl_display, egl_surface);
|
225 |
+
}
|
226 |
+
};
|
227 |
+
|
228 |
+
struct Decoration {
|
229 |
+
Decoration(const uint border_size,
|
230 |
+
const uint title_size,
|
231 |
+
const pangolin::Colour colour,
|
232 |
+
wl_compositor* compositor,
|
233 |
+
wl_subcompositor* subcompositor,
|
234 |
+
wl_surface* surface,
|
235 |
+
EGLDisplay egl_display,
|
236 |
+
EGLConfig config
|
237 |
+
) :
|
238 |
+
border_size(border_size),
|
239 |
+
title_size(title_size),
|
240 |
+
colour(colour),
|
241 |
+
egl_display(egl_display),
|
242 |
+
compositor(compositor),
|
243 |
+
subcompositor(subcompositor),
|
244 |
+
surface(surface),
|
245 |
+
config(config)
|
246 |
+
{ }
|
247 |
+
|
248 |
+
void create() {
|
249 |
+
// reserve memory to prevent that DecorationSurface's destructor gets
|
250 |
+
// called by 'emplace_back'
|
251 |
+
decorations.reserve(9);
|
252 |
+
|
253 |
+
// title bar, 2D movement
|
254 |
+
decorations.emplace_back(compositor, subcompositor, surface, egl_display, config, border_size, title_size, XDG_TOPLEVEL_RESIZE_EDGE_NONE, colour);
|
255 |
+
|
256 |
+
// sides, 1D resizing
|
257 |
+
decorations.emplace_back(compositor, subcompositor, surface, egl_display, config, border_size, title_size, XDG_TOPLEVEL_RESIZE_EDGE_LEFT, colour);
|
258 |
+
decorations.emplace_back(compositor, subcompositor, surface, egl_display, config, border_size, title_size, XDG_TOPLEVEL_RESIZE_EDGE_RIGHT, colour);
|
259 |
+
decorations.emplace_back(compositor, subcompositor, surface, egl_display, config, border_size, title_size, XDG_TOPLEVEL_RESIZE_EDGE_TOP, colour);
|
260 |
+
decorations.emplace_back(compositor, subcompositor, surface, egl_display, config, border_size, title_size, XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM, colour);
|
261 |
+
|
262 |
+
// corners, 2D resizing
|
263 |
+
decorations.emplace_back(compositor, subcompositor, surface, egl_display, config, border_size, title_size, XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT, colour);
|
264 |
+
decorations.emplace_back(compositor, subcompositor, surface, egl_display, config, border_size, title_size, XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT, colour);
|
265 |
+
decorations.emplace_back(compositor, subcompositor, surface, egl_display, config, border_size, title_size, XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT, colour);
|
266 |
+
decorations.emplace_back(compositor, subcompositor, surface, egl_display, config, border_size, title_size, XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT, colour);
|
267 |
+
|
268 |
+
// buttons
|
269 |
+
buttons.reserve(2);
|
270 |
+
buttons.emplace_back(compositor, subcompositor, decorations[0].surface, egl_display, config, 5, 1, button_width, button_height, ButtonSurface::type::CLOSE, pangolin::Colour(0, 110/255.0, 182/255.0));
|
271 |
+
buttons.emplace_back(compositor, subcompositor, decorations[0].surface, egl_display, config, 10+button_width, 1, button_width, button_height, ButtonSurface::type::MAXIMISE, pangolin::Colour(1.0, 204/255.0f, 0));
|
272 |
+
}
|
273 |
+
|
274 |
+
void destroy() {
|
275 |
+
decorations.clear();
|
276 |
+
buttons.clear();
|
277 |
+
}
|
278 |
+
|
279 |
+
void resize(const int32_t width, const int32_t height) {
|
280 |
+
for(const DecorationSurface &d : decorations) { d.resize(width, height); }
|
281 |
+
for(const ButtonSurface &b : buttons) { b.reposition(width); }
|
282 |
+
}
|
283 |
+
|
284 |
+
void draw() {
|
285 |
+
for(const DecorationSurface &d : decorations) { d.draw(); }
|
286 |
+
for(const ButtonSurface &b : buttons) { b.draw(); }
|
287 |
+
}
|
288 |
+
|
289 |
+
void setTypeFromSurface(const wl_surface *surface) {
|
290 |
+
for(const DecorationSurface &d : decorations) {
|
291 |
+
if(d.surface==surface) {
|
292 |
+
last_type = d.function;
|
293 |
+
return;
|
294 |
+
}
|
295 |
+
}
|
296 |
+
for(const ButtonSurface &b : buttons) {
|
297 |
+
if(b.surface==surface) {
|
298 |
+
last_type = b.function;
|
299 |
+
return;
|
300 |
+
}
|
301 |
+
}
|
302 |
+
// surface is not part of the window decoration
|
303 |
+
last_type = -1;
|
304 |
+
}
|
305 |
+
|
306 |
+
const std::string getCursorForCurrentSurface() const {
|
307 |
+
return resize_cursor.count((enum xdg_toplevel_resize_edge)last_type) ? resize_cursor.at((enum xdg_toplevel_resize_edge)last_type) : "left_ptr";
|
308 |
+
}
|
309 |
+
|
310 |
+
void toContentSize(const int window_w, const int window_h, int &content_w, int &content_h) {
|
311 |
+
content_w = window_w - (2*border_size);
|
312 |
+
content_h = window_h - (2*border_size + title_size);
|
313 |
+
}
|
314 |
+
|
315 |
+
void toWindowSize(const int content_w, const int content_h, int &window_w, int &window_h) {
|
316 |
+
window_w = content_w + (2*border_size);
|
317 |
+
window_h = content_h + (2*border_size + title_size);
|
318 |
+
}
|
319 |
+
|
320 |
+
std::vector<DecorationSurface> decorations;
|
321 |
+
int last_type;
|
322 |
+
|
323 |
+
std::vector<ButtonSurface> buttons;
|
324 |
+
|
325 |
+
const uint border_size;
|
326 |
+
const uint title_size;
|
327 |
+
const pangolin::Colour colour;
|
328 |
+
EGLDisplay egl_display;
|
329 |
+
wl_compositor* compositor;
|
330 |
+
wl_subcompositor* subcompositor;
|
331 |
+
wl_surface* surface;
|
332 |
+
EGLConfig config;
|
333 |
+
|
334 |
+
static const uint button_width;
|
335 |
+
static const uint button_height;
|
336 |
+
};
|
337 |
+
|
338 |
+
const uint Decoration::button_width = 25;
|
339 |
+
const uint Decoration::button_height = 15;
|
340 |
+
|
341 |
+
struct WaylandWindow;
|
342 |
+
|
343 |
+
struct WaylandDisplay {
|
344 |
+
WaylandDisplay();
|
345 |
+
|
346 |
+
~WaylandDisplay();
|
347 |
+
|
348 |
+
struct wl_display *wdisplay = nullptr;
|
349 |
+
struct wl_registry *wregistry = nullptr;
|
350 |
+
struct wl_compositor *wcompositor = nullptr;
|
351 |
+
struct wl_subcompositor *wsubcompositor = nullptr;
|
352 |
+
struct xdg_wm_base *xshell = nullptr;
|
353 |
+
|
354 |
+
struct wl_seat *wseat = nullptr;
|
355 |
+
struct wl_keyboard *wkeyboard = nullptr;
|
356 |
+
struct wl_pointer *pointer = nullptr;
|
357 |
+
|
358 |
+
// for cursor
|
359 |
+
struct wl_shm *shm = nullptr;
|
360 |
+
struct wl_cursor_theme *cursor_theme = nullptr;
|
361 |
+
struct wl_surface *cursor_surface = nullptr;
|
362 |
+
|
363 |
+
// xkbcommon
|
364 |
+
struct xkb_context *xkb_context = nullptr;
|
365 |
+
struct xkb_keymap *keymap = nullptr;
|
366 |
+
struct xkb_state *xkb_state = nullptr;
|
367 |
+
KeyModifierBitmask flags;
|
368 |
+
|
369 |
+
std::vector<EGLConfig> egl_configs;
|
370 |
+
EGLContext egl_context = nullptr;
|
371 |
+
EGLDisplay egl_display = nullptr;
|
372 |
+
|
373 |
+
static constexpr EGLint attribs[] = {
|
374 |
+
EGL_RENDERABLE_TYPE , EGL_OPENGL_BIT,
|
375 |
+
EGL_RED_SIZE , 8,
|
376 |
+
EGL_GREEN_SIZE , 8,
|
377 |
+
EGL_BLUE_SIZE , 8,
|
378 |
+
EGL_DEPTH_SIZE , 24,
|
379 |
+
EGL_STENCIL_SIZE , 8,
|
380 |
+
EGL_NONE
|
381 |
+
};
|
382 |
+
|
383 |
+
// assume a single window for now
|
384 |
+
WaylandWindow* window;
|
385 |
+
};
|
386 |
+
|
387 |
+
constexpr EGLint WaylandDisplay::attribs[];
|
388 |
+
|
389 |
+
struct WaylandWindow : public WindowInterface
|
390 |
+
{
|
391 |
+
public:
|
392 |
+
WaylandWindow(const int width, const int height, const std::string& title, std::shared_ptr<WaylandDisplay> display);
|
393 |
+
|
394 |
+
~WaylandWindow() override;
|
395 |
+
|
396 |
+
void ShowFullscreen(const TrueFalseToggle on_off) override;
|
397 |
+
|
398 |
+
void Move(const int x, const int y) override;
|
399 |
+
|
400 |
+
void Resize(const unsigned int w, const unsigned int h) override;
|
401 |
+
|
402 |
+
void MakeCurrent() override;
|
403 |
+
|
404 |
+
void RemoveCurrent() override;
|
405 |
+
|
406 |
+
void SwapBuffers() override;
|
407 |
+
|
408 |
+
void ProcessEvents() override;
|
409 |
+
|
410 |
+
//private:
|
411 |
+
std::shared_ptr<WaylandDisplay> display;
|
412 |
+
|
413 |
+
EGLint width, height;
|
414 |
+
bool is_fullscreen;
|
415 |
+
bool is_maximised;
|
416 |
+
// store floating window dimensions to restore to,
|
417 |
+
// when returning from maximised or fullscreen
|
418 |
+
int floating_width = 0, floating_height = 0;
|
419 |
+
|
420 |
+
bool pressed = false;
|
421 |
+
int lastx=0;
|
422 |
+
int lasty=0;
|
423 |
+
|
424 |
+
struct wl_surface *wsurface = nullptr;
|
425 |
+
struct wl_egl_window *egl_window = nullptr;
|
426 |
+
struct xdg_surface *xshell_surface = nullptr;
|
427 |
+
struct xdg_toplevel *xshell_toplevel = nullptr;
|
428 |
+
|
429 |
+
// we can only attach a buffer to a surface after it has been configured at least once
|
430 |
+
bool configured = false;
|
431 |
+
|
432 |
+
EGLSurface egl_surface = nullptr;
|
433 |
+
|
434 |
+
std::shared_ptr<Decoration> decoration;
|
435 |
+
};
|
436 |
+
|
437 |
+
// map wayland ids to pangolin ids
|
438 |
+
static const std::map<uint,int> wl_button_ids = {
|
439 |
+
{BTN_LEFT, 0},
|
440 |
+
{BTN_MIDDLE, 1},
|
441 |
+
{BTN_RIGHT, 2},
|
442 |
+
};
|
443 |
+
|
444 |
+
static const std::map<uint,KeyModifier> wl_key_mod_ids = {
|
445 |
+
{KEY_LEFTSHIFT, KeyModifierShift},
|
446 |
+
{KEY_RIGHTSHIFT, KeyModifierShift},
|
447 |
+
{KEY_LEFTCTRL, KeyModifierCtrl},
|
448 |
+
{KEY_RIGHTCTRL, KeyModifierCtrl},
|
449 |
+
{KEY_LEFTALT, KeyModifierAlt},
|
450 |
+
{KEY_RIGHTALT, KeyModifierAlt},
|
451 |
+
};
|
452 |
+
|
453 |
+
static const std::map<uint,int> wl_key_special_ids = {
|
454 |
+
{KEY_F1, PANGO_KEY_F1},
|
455 |
+
{KEY_F2, PANGO_KEY_F2},
|
456 |
+
{KEY_F3, PANGO_KEY_F3},
|
457 |
+
{KEY_F4, PANGO_KEY_F4},
|
458 |
+
{KEY_F5, PANGO_KEY_F5},
|
459 |
+
{KEY_F6, PANGO_KEY_F6},
|
460 |
+
{KEY_F7, PANGO_KEY_F7},
|
461 |
+
{KEY_F8, PANGO_KEY_F8},
|
462 |
+
{KEY_F9, PANGO_KEY_F9},
|
463 |
+
{KEY_F10, PANGO_KEY_F10},
|
464 |
+
{KEY_F11, PANGO_KEY_F11},
|
465 |
+
{KEY_F12, PANGO_KEY_F12},
|
466 |
+
|
467 |
+
{KEY_LEFT, PANGO_KEY_LEFT},
|
468 |
+
{KEY_UP, PANGO_KEY_UP},
|
469 |
+
{KEY_RIGHT, PANGO_KEY_RIGHT},
|
470 |
+
{KEY_DOWN, PANGO_KEY_DOWN},
|
471 |
+
|
472 |
+
{KEY_PAGEUP, PANGO_KEY_PAGE_UP},
|
473 |
+
{KEY_PAGEDOWN, PANGO_KEY_PAGE_DOWN},
|
474 |
+
{KEY_HOME, PANGO_KEY_HOME},
|
475 |
+
{KEY_END, PANGO_KEY_END},
|
476 |
+
{KEY_INSERT, PANGO_KEY_INSERT},
|
477 |
+
};
|
478 |
+
|
479 |
+
static void handle_configure_toplevel(void *data, struct xdg_toplevel */*xdg_toplevel*/, int32_t width, int32_t height, struct wl_array *states) {
|
480 |
+
|
481 |
+
const static uint min_width = 70;
|
482 |
+
const static uint min_height = 70;
|
483 |
+
|
484 |
+
WaylandWindow* const w = static_cast<WaylandWindow*>(data);
|
485 |
+
|
486 |
+
// reset window states
|
487 |
+
w->is_maximised = w->is_fullscreen = false;
|
488 |
+
|
489 |
+
// set new window states
|
490 |
+
const enum xdg_toplevel_state *state;
|
491 |
+
WL_ARRAY_FOR_EACH(state, states, const enum xdg_toplevel_state*) {
|
492 |
+
switch (*state) {
|
493 |
+
case XDG_TOPLEVEL_STATE_MAXIMIZED:
|
494 |
+
w->is_maximised = true;
|
495 |
+
break;
|
496 |
+
case XDG_TOPLEVEL_STATE_FULLSCREEN:
|
497 |
+
w->is_fullscreen = true;
|
498 |
+
break;
|
499 |
+
default:
|
500 |
+
break;
|
501 |
+
}
|
502 |
+
}
|
503 |
+
|
504 |
+
// set proposed dimensions, this includes the entire window with decorations
|
505 |
+
int restore_w = width;
|
506 |
+
int restore_h = height;
|
507 |
+
|
508 |
+
// restore from stored floating state if not specified
|
509 |
+
if (restore_w==0) restore_w = w->floating_width;
|
510 |
+
if (restore_h==0) restore_h = w->floating_height;
|
511 |
+
|
512 |
+
// use initially provided dimensions if floating was never set,
|
513 |
+
// these are in the "content" dimensions
|
514 |
+
if (restore_w==0) restore_w = w->width;
|
515 |
+
if (restore_h==0) restore_h = w->height;
|
516 |
+
|
517 |
+
if(!w->is_fullscreen && (width!=0 && height!=0)) {
|
518 |
+
// has decoration
|
519 |
+
w->decoration->toContentSize(restore_w, restore_h, w->width, w->height);
|
520 |
+
w->width = std::max(w->width, int(min_width));
|
521 |
+
w->height = std::max(w->height, int(min_height));
|
522 |
+
}
|
523 |
+
else {
|
524 |
+
w->width = restore_w;
|
525 |
+
w->height = restore_h;
|
526 |
+
}
|
527 |
+
|
528 |
+
// store the current state as floating state,
|
529 |
+
// if we are neither maximised nor fullscreen
|
530 |
+
if (!w->is_maximised && !w->is_fullscreen) {
|
531 |
+
w->floating_width = w->width;
|
532 |
+
w->floating_height = w->height;
|
533 |
+
}
|
534 |
+
}
|
535 |
+
|
536 |
+
static void handle_configure(void *data, struct xdg_surface *xdg_surface, uint32_t serial) {
|
537 |
+
WaylandWindow* const w = static_cast<WaylandWindow*>(data);
|
538 |
+
|
539 |
+
// resize main surface
|
540 |
+
wl_egl_window_resize(w->egl_window, w->width, w->height, 0, 0);
|
541 |
+
|
542 |
+
// resize all decoration elements
|
543 |
+
w->decoration->resize(w->width, w->height);
|
544 |
+
|
545 |
+
// notify Panglin views about resized area
|
546 |
+
w->ResizeSignal(WindowResizeEvent{w->width, w->height});
|
547 |
+
|
548 |
+
xdg_surface_ack_configure(xdg_surface, serial);
|
549 |
+
|
550 |
+
w->configured = true;
|
551 |
+
}
|
552 |
+
|
553 |
+
static const struct xdg_surface_listener shell_surface_listener = {
|
554 |
+
handle_configure,
|
555 |
+
};
|
556 |
+
|
557 |
+
static void handle_toplevel_close(void *data, struct xdg_toplevel */*xdg_toplevel*/) {
|
558 |
+
static_cast<WaylandWindow*>(data)->CloseSignal();
|
559 |
+
}
|
560 |
+
|
561 |
+
static const struct xdg_toplevel_listener toplevel_listener = {
|
562 |
+
.configure = handle_configure_toplevel,
|
563 |
+
.close = handle_toplevel_close,
|
564 |
+
};
|
565 |
+
|
566 |
+
static void xdg_wm_base_ping(void */*data*/, struct xdg_wm_base *xdg_wm_base, uint32_t serial) {
|
567 |
+
xdg_wm_base_pong(xdg_wm_base, serial);
|
568 |
+
}
|
569 |
+
|
570 |
+
static const struct xdg_wm_base_listener shell_listener = {
|
571 |
+
.ping = xdg_wm_base_ping
|
572 |
+
};
|
573 |
+
|
574 |
+
static void pointer_handle_enter(void *data, struct wl_pointer *pointer, uint32_t serial, struct wl_surface *surface, wl_fixed_t /*sx*/, wl_fixed_t /*sy*/) {
|
575 |
+
WaylandDisplay* const d = static_cast<WaylandDisplay*>(data);
|
576 |
+
WaylandWindow* const w = d->window;
|
577 |
+
w->decoration->setTypeFromSurface(surface);
|
578 |
+
|
579 |
+
const std::string cursor = w->decoration->getCursorForCurrentSurface();
|
580 |
+
|
581 |
+
const auto image = wl_cursor_theme_get_cursor(d->cursor_theme, cursor.c_str())->images[0];
|
582 |
+
wl_pointer_set_cursor(pointer, serial, d->cursor_surface, image->hotspot_x, image->hotspot_y);
|
583 |
+
wl_surface_attach(d->cursor_surface, wl_cursor_image_get_buffer(image), 0, 0);
|
584 |
+
wl_surface_damage(d->cursor_surface, 0, 0, image->width, image->height);
|
585 |
+
wl_surface_commit(d->cursor_surface);
|
586 |
+
}
|
587 |
+
|
588 |
+
static void pointer_handle_leave(void *data, struct wl_pointer */*pointer*/, uint32_t /*serial*/, struct wl_surface */*surface*/) {
|
589 |
+
WaylandDisplay* const d = static_cast<WaylandDisplay*>(data);
|
590 |
+
d->window->pressed = false;
|
591 |
+
}
|
592 |
+
|
593 |
+
static void pointer_handle_motion(void *data, struct wl_pointer */*pointer*/, uint32_t /*time*/, wl_fixed_t sx, wl_fixed_t sy) {
|
594 |
+
WaylandDisplay* const d = static_cast<WaylandDisplay*>(data);
|
595 |
+
WaylandWindow* const w = d->window;
|
596 |
+
|
597 |
+
w->lastx=wl_fixed_to_int(sx);
|
598 |
+
w->lasty=wl_fixed_to_int(sy);
|
599 |
+
if(w->pressed) {
|
600 |
+
w->MouseMotionSignal(MouseMotionEvent{{float(w->lastx), float(w->lasty), d->flags}});
|
601 |
+
}
|
602 |
+
else {
|
603 |
+
w->PassiveMouseMotionSignal(MouseMotionEvent{{float(w->lastx), float(w->lasty), d->flags}});
|
604 |
+
}
|
605 |
+
}
|
606 |
+
|
607 |
+
static void pointer_handle_button(void *data, struct wl_pointer */*wl_pointer*/, uint32_t serial, uint32_t /*time*/, uint32_t button, uint32_t state) {
|
608 |
+
WaylandDisplay* const d = static_cast<WaylandDisplay*>(data);
|
609 |
+
WaylandWindow* const w = d->window;
|
610 |
+
|
611 |
+
if(w->decoration->last_type<0) {
|
612 |
+
// input goes to pangoling view
|
613 |
+
if(!wl_button_ids.count(button))
|
614 |
+
return;
|
615 |
+
|
616 |
+
w->pressed = (state==WL_POINTER_BUTTON_STATE_PRESSED);
|
617 |
+
w->MouseSignal(MouseEvent{{float(w->lastx), float(w->lasty), d->flags}, wl_button_ids.at(button), w->pressed});
|
618 |
+
}
|
619 |
+
else {
|
620 |
+
// input goes to window decoration
|
621 |
+
// resizing using window decoration
|
622 |
+
if((button==BTN_LEFT) && (state==WL_POINTER_BUTTON_STATE_PRESSED)) {
|
623 |
+
switch (w->decoration->last_type) {
|
624 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_NONE:
|
625 |
+
xdg_toplevel_move(w->xshell_toplevel, d->wseat, serial);
|
626 |
+
break;
|
627 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_TOP:
|
628 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM:
|
629 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_LEFT:
|
630 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT:
|
631 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT:
|
632 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_RIGHT:
|
633 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT:
|
634 |
+
case XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT:
|
635 |
+
xdg_toplevel_resize(w->xshell_toplevel, d->wseat, serial, w->decoration->last_type);
|
636 |
+
break;
|
637 |
+
case ButtonSurface::type::CLOSE:
|
638 |
+
w->CloseSignal();
|
639 |
+
break;
|
640 |
+
case ButtonSurface::type::MAXIMISE:
|
641 |
+
if(w->is_maximised) {
|
642 |
+
xdg_toplevel_unset_maximized(w->xshell_toplevel);
|
643 |
+
}
|
644 |
+
else {
|
645 |
+
xdg_toplevel_set_maximized(w->xshell_toplevel);
|
646 |
+
}
|
647 |
+
|
648 |
+
break;
|
649 |
+
}
|
650 |
+
}
|
651 |
+
}
|
652 |
+
}
|
653 |
+
|
654 |
+
static void pointer_handle_axis(void *data, struct wl_pointer */*wl_pointer*/, uint32_t /*time*/, uint32_t axis, wl_fixed_t value) {
|
655 |
+
WaylandDisplay* const d = static_cast<WaylandDisplay*>(data);
|
656 |
+
WaylandWindow* const w = d->window;
|
657 |
+
|
658 |
+
const float v = wl_fixed_to_double(value);
|
659 |
+
float dx = 0, dy = 0;
|
660 |
+
|
661 |
+
switch (axis) {
|
662 |
+
case REL_X: dy = v; break; // up, down
|
663 |
+
case REL_Y: dx = v; break; // left, right
|
664 |
+
}
|
665 |
+
|
666 |
+
w->SpecialInputSignal(SpecialInputEvent({
|
667 |
+
{float(w->lastx), float(w->lasty), d->flags},
|
668 |
+
InputSpecialScroll, {dx, dy, 0, 0}
|
669 |
+
}));
|
670 |
+
}
|
671 |
+
|
672 |
+
#if WAYLAND_VERSION_GE(1,12)
|
673 |
+
|
674 |
+
static void pointer_handle_frame(void */*data*/, struct wl_pointer */*wl_pointer*/) { }
|
675 |
+
|
676 |
+
static void pointer_handle_axis_source(void */*data*/, struct wl_pointer */*wl_pointer*/, uint32_t /*axis_source*/) { }
|
677 |
+
|
678 |
+
static void pointer_handle_axis_stop(void */*data*/, struct wl_pointer */*wl_pointer*/, uint32_t /*time*/, uint32_t /*axis*/) { }
|
679 |
+
|
680 |
+
static void pointer_handle_axis_discrete(void */*data*/, struct wl_pointer */*wl_pointer*/, uint32_t /*axis*/, int32_t /*discrete*/) { }
|
681 |
+
|
682 |
+
#endif
|
683 |
+
|
684 |
+
static const struct wl_pointer_listener pointer_listener = {
|
685 |
+
pointer_handle_enter,
|
686 |
+
pointer_handle_leave,
|
687 |
+
pointer_handle_motion,
|
688 |
+
pointer_handle_button,
|
689 |
+
pointer_handle_axis,
|
690 |
+
#if WAYLAND_VERSION_GE(1,12)
|
691 |
+
pointer_handle_frame,
|
692 |
+
pointer_handle_axis_source,
|
693 |
+
pointer_handle_axis_stop,
|
694 |
+
pointer_handle_axis_discrete,
|
695 |
+
#endif
|
696 |
+
};
|
697 |
+
|
698 |
+
static void keyboard_handle_keymap(void *data, struct wl_keyboard */*keyboard*/, uint32_t format, int fd, uint32_t size) {
|
699 |
+
if ((!data) || (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)) {
|
700 |
+
std::cerr << "wrong keymap format, got " << format << ", expected WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1" << std::endl;
|
701 |
+
close(fd);
|
702 |
+
return;
|
703 |
+
}
|
704 |
+
|
705 |
+
WaylandDisplay* const d = static_cast<WaylandDisplay*>(data);
|
706 |
+
|
707 |
+
char *keymap_string = static_cast<char*>(mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0));
|
708 |
+
if (keymap_string == MAP_FAILED) {
|
709 |
+
std::cerr << "keymap mmap failed: " << std::string(std::strerror(errno)) << std::endl;
|
710 |
+
close(fd);
|
711 |
+
return;
|
712 |
+
}
|
713 |
+
xkb_keymap_unref(d->keymap);
|
714 |
+
d->keymap = xkb_keymap_new_from_string(d->xkb_context, keymap_string, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
|
715 |
+
munmap(keymap_string, size);
|
716 |
+
close(fd);
|
717 |
+
xkb_state_unref(d->xkb_state);
|
718 |
+
d->xkb_state = xkb_state_new(d->keymap);
|
719 |
+
}
|
720 |
+
|
721 |
+
static void keyboard_handle_enter(void *data, struct wl_keyboard */*keyboard*/, uint32_t /*serial*/, struct wl_surface */*surface*/, struct wl_array */*keys*/) {
|
722 |
+
static_cast<WaylandDisplay*>(data)->flags = {};
|
723 |
+
}
|
724 |
+
|
725 |
+
static void keyboard_handle_leave(void */*data*/, struct wl_keyboard */*keyboard*/, uint32_t /*serial*/, struct wl_surface */*surface*/) { }
|
726 |
+
|
727 |
+
static void keyboard_handle_key(void *data, struct wl_keyboard */*keyboard*/, uint32_t /*serial*/, uint32_t /*time*/, uint32_t key, uint32_t state) {
|
728 |
+
WaylandDisplay* const d = static_cast<WaylandDisplay*>(data);
|
729 |
+
WaylandWindow* const w = d->window;
|
730 |
+
|
731 |
+
// modifier keys
|
732 |
+
if(wl_key_mod_ids.count(key)) {
|
733 |
+
if(state==WL_KEYBOARD_KEY_STATE_PRESSED) {
|
734 |
+
d->flags |= wl_key_mod_ids.at(key);
|
735 |
+
}
|
736 |
+
else if (state==WL_KEYBOARD_KEY_STATE_RELEASED) {
|
737 |
+
// NOTE: "d->flags &= ~wl_key_mod_ids.at(key);" does not work here
|
738 |
+
d->flags.set(wl_key_mod_ids.at(key), false);
|
739 |
+
}
|
740 |
+
return;
|
741 |
+
}
|
742 |
+
|
743 |
+
// character and special keys
|
744 |
+
int pango_key = -1;
|
745 |
+
if(wl_key_special_ids.count(key)) {
|
746 |
+
// special keys
|
747 |
+
pango_key = PANGO_SPECIAL + wl_key_special_ids.at(key);
|
748 |
+
}
|
749 |
+
else {
|
750 |
+
// character keys
|
751 |
+
const uint32_t utf32 = xkb_state_key_get_utf32(d->xkb_state, key+8);
|
752 |
+
// filter non-ASCII
|
753 |
+
if(utf32>0 && utf32<=127) {
|
754 |
+
pango_key = int(utf32);
|
755 |
+
}
|
756 |
+
}
|
757 |
+
|
758 |
+
if(pango_key>0) {
|
759 |
+
w->KeyboardSignal(KeyboardEvent{{float(w->lastx), float(w->lasty), d->flags}, uint8_t(pango_key), state==WL_KEYBOARD_KEY_STATE_PRESSED});
|
760 |
+
}
|
761 |
+
}
|
762 |
+
|
763 |
+
static void keyboard_handle_modifiers(void *data, struct wl_keyboard */*keyboard*/, uint32_t /*serial*/, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) {
|
764 |
+
WaylandDisplay* const d = static_cast<WaylandDisplay*>(data);
|
765 |
+
|
766 |
+
xkb_state_update_mask(d->xkb_state, mods_depressed, mods_latched, mods_locked, 0, 0, group);
|
767 |
+
}
|
768 |
+
|
769 |
+
#if WAYLAND_VERSION_GE(1,12)
|
770 |
+
|
771 |
+
static void keyboard_handle_repeat_info(void */*data*/, struct wl_keyboard */*wl_keyboard*/, int32_t /*rate*/, int32_t /*delay*/) { }
|
772 |
+
|
773 |
+
#endif
|
774 |
+
|
775 |
+
static const struct wl_keyboard_listener keyboard_listener = {
|
776 |
+
keyboard_handle_keymap,
|
777 |
+
keyboard_handle_enter,
|
778 |
+
keyboard_handle_leave,
|
779 |
+
keyboard_handle_key,
|
780 |
+
keyboard_handle_modifiers,
|
781 |
+
#if WAYLAND_VERSION_GE(1,12)
|
782 |
+
keyboard_handle_repeat_info,
|
783 |
+
#endif
|
784 |
+
};
|
785 |
+
|
786 |
+
static void seat_handle_capabilities(void *data, struct wl_seat *seat, uint32_t caps1) {
|
787 |
+
WaylandDisplay* const w = static_cast<WaylandDisplay*>(data);
|
788 |
+
|
789 |
+
enum wl_seat_capability caps;
|
790 |
+
caps = (enum wl_seat_capability)caps1;
|
791 |
+
if (caps & WL_SEAT_CAPABILITY_KEYBOARD) {
|
792 |
+
w->wkeyboard = wl_seat_get_keyboard(seat);
|
793 |
+
wl_keyboard_add_listener(w->wkeyboard, &keyboard_listener, data);
|
794 |
+
} else {
|
795 |
+
wl_keyboard_destroy(w->wkeyboard);
|
796 |
+
w->wkeyboard = nullptr;
|
797 |
+
}
|
798 |
+
if (caps & WL_SEAT_CAPABILITY_POINTER) {
|
799 |
+
w->pointer = wl_seat_get_pointer(seat);
|
800 |
+
w->cursor_surface = wl_compositor_create_surface(w->wcompositor);
|
801 |
+
wl_pointer_add_listener(w->pointer, &pointer_listener, data);
|
802 |
+
} else {
|
803 |
+
wl_pointer_destroy(w->pointer);
|
804 |
+
w->pointer = nullptr;
|
805 |
+
}
|
806 |
+
}
|
807 |
+
|
808 |
+
static void seat_handle_name(void */*data*/, struct wl_seat */*wl_seat*/, const char */*name*/) { }
|
809 |
+
|
810 |
+
static const struct wl_seat_listener seat_listener = {
|
811 |
+
seat_handle_capabilities,
|
812 |
+
seat_handle_name,
|
813 |
+
};
|
814 |
+
|
815 |
+
static void global_registry_handler(void *data, struct wl_registry *registry, uint32_t id, const char *interface, uint32_t version) {
|
816 |
+
WaylandDisplay* const w = static_cast<WaylandDisplay*>(data);
|
817 |
+
|
818 |
+
if (strcmp(interface, wl_compositor_interface.name) == 0) {
|
819 |
+
w->wcompositor = reinterpret_cast<wl_compositor*> (wl_registry_bind(registry, id, &wl_compositor_interface, std::min<uint32_t>(version,4)));
|
820 |
+
}
|
821 |
+
else if (strcmp(interface, wl_subcompositor_interface.name) == 0) {
|
822 |
+
w->wsubcompositor = static_cast<wl_subcompositor*>(wl_registry_bind(registry, id, &wl_subcompositor_interface, std::min<uint32_t>(version,1)));
|
823 |
+
}
|
824 |
+
else if (strcmp(interface, xdg_wm_base_interface.name) == 0) {
|
825 |
+
w->xshell = reinterpret_cast<xdg_wm_base*> (wl_registry_bind(registry, id, &xdg_wm_base_interface, std::min<uint32_t>(version,3)));
|
826 |
+
}
|
827 |
+
else if (strcmp(interface, wl_seat_interface.name) == 0) {
|
828 |
+
w->wseat = reinterpret_cast<wl_seat*>(wl_registry_bind(registry, id, &wl_seat_interface, std::min<uint32_t>(version,5)));
|
829 |
+
wl_seat_add_listener(w->wseat, &seat_listener, data);
|
830 |
+
}
|
831 |
+
else if (strcmp(interface, wl_shm_interface.name) == 0) {
|
832 |
+
w->shm = static_cast<wl_shm*>(wl_registry_bind(registry, id, &wl_shm_interface, std::min<uint32_t>(version,1)));
|
833 |
+
w->cursor_theme = wl_cursor_theme_load(nullptr, 16, w->shm);
|
834 |
+
}
|
835 |
+
}
|
836 |
+
|
837 |
+
static void global_registry_remover(void */*data*/, struct wl_registry */*registry*/, uint32_t /*id*/) { }
|
838 |
+
|
839 |
+
static const struct wl_registry_listener wregistry_listener = {
|
840 |
+
global_registry_handler,
|
841 |
+
global_registry_remover
|
842 |
+
};
|
843 |
+
|
844 |
+
WaylandDisplay::WaylandDisplay() {
|
845 |
+
xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
|
846 |
+
|
847 |
+
wdisplay = wl_display_connect(nullptr);
|
848 |
+
if (wdisplay == nullptr) {
|
849 |
+
throw std::runtime_error("Cannot connect to Wayland compositor!");
|
850 |
+
}
|
851 |
+
|
852 |
+
wregistry = wl_display_get_registry(wdisplay);
|
853 |
+
wl_registry_add_listener(wregistry, &wregistry_listener, this);
|
854 |
+
|
855 |
+
wl_display_roundtrip(wdisplay);
|
856 |
+
|
857 |
+
egl_display = eglGetDisplay((EGLNativeDisplayType)wdisplay);
|
858 |
+
if(!egl_display) {
|
859 |
+
std::cerr << "Failed to open EGL display" << std::endl;
|
860 |
+
}
|
861 |
+
|
862 |
+
EGLint major, minor;
|
863 |
+
if(eglInitialize(egl_display, &major, &minor)==EGL_FALSE) {
|
864 |
+
std::cerr << "EGL init failed" << std::endl;
|
865 |
+
}
|
866 |
+
|
867 |
+
if(eglBindAPI(EGL_OPENGL_API)==EGL_FALSE) {
|
868 |
+
std::cerr << "EGL bind failed" << std::endl;
|
869 |
+
}
|
870 |
+
|
871 |
+
EGLint count;
|
872 |
+
eglGetConfigs(egl_display, nullptr, 0, &count);
|
873 |
+
|
874 |
+
egl_configs.resize(count);
|
875 |
+
|
876 |
+
EGLint numConfigs;
|
877 |
+
eglChooseConfig(egl_display, attribs, egl_configs.data(), count, &numConfigs);
|
878 |
+
|
879 |
+
egl_context = eglCreateContext(egl_display, egl_configs[0], EGL_NO_CONTEXT, nullptr);
|
880 |
+
|
881 |
+
if(xshell==nullptr) {
|
882 |
+
throw std::runtime_error("No Wayland shell available!");
|
883 |
+
}
|
884 |
+
|
885 |
+
xdg_wm_base_add_listener(xshell, &shell_listener, this);
|
886 |
+
|
887 |
+
wl_display_roundtrip(wdisplay);
|
888 |
+
}
|
889 |
+
|
890 |
+
WaylandDisplay::~WaylandDisplay() {
|
891 |
+
// cleanup EGL
|
892 |
+
if(egl_context) eglDestroyContext(egl_display, egl_context);
|
893 |
+
if(egl_display) eglTerminate(egl_display);
|
894 |
+
|
895 |
+
// cleanup Wayland
|
896 |
+
if(wkeyboard) wl_keyboard_destroy(wkeyboard);
|
897 |
+
if(pointer) wl_pointer_destroy(pointer);
|
898 |
+
if(cursor_surface) wl_surface_destroy(cursor_surface);
|
899 |
+
if(cursor_theme) wl_cursor_theme_destroy(cursor_theme);
|
900 |
+
if(shm) wl_shm_destroy(shm);
|
901 |
+
if(wseat) wl_seat_destroy(wseat);
|
902 |
+
if(xshell) xdg_wm_base_destroy(xshell);
|
903 |
+
if(wsubcompositor) wl_subcompositor_destroy(wsubcompositor);
|
904 |
+
if(wcompositor) wl_compositor_destroy(wcompositor);
|
905 |
+
if(wregistry) wl_registry_destroy(wregistry);
|
906 |
+
if(wdisplay) wl_display_disconnect(wdisplay);
|
907 |
+
|
908 |
+
if(xkb_context) xkb_context_unref(xkb_context);
|
909 |
+
if(xkb_state) xkb_state_unref(xkb_state);
|
910 |
+
if(keymap) xkb_keymap_unref(keymap);
|
911 |
+
}
|
912 |
+
|
913 |
+
WaylandWindow::WaylandWindow(const int w, const int h,
|
914 |
+
const std::string& title,
|
915 |
+
std::shared_ptr<WaylandDisplay> display)
|
916 |
+
: display(display)
|
917 |
+
{
|
918 |
+
wsurface = wl_compositor_create_surface(display->wcompositor);
|
919 |
+
|
920 |
+
display->window = this;
|
921 |
+
|
922 |
+
width = w;
|
923 |
+
height = h;
|
924 |
+
|
925 |
+
egl_window = wl_egl_window_create(wsurface, w, h);
|
926 |
+
if(!egl_window) {
|
927 |
+
std::cerr << "Cannot create EGL window" << std::endl;
|
928 |
+
}
|
929 |
+
|
930 |
+
egl_surface = eglCreateWindowSurface(display->egl_display, display->egl_configs[0], (EGLNativeWindowType)egl_window, nullptr);
|
931 |
+
if (egl_surface == EGL_NO_SURFACE) {
|
932 |
+
std::cerr << "Cannot create EGL surface" << std::endl;
|
933 |
+
}
|
934 |
+
|
935 |
+
xshell_surface = xdg_wm_base_get_xdg_surface(display->xshell, wsurface);
|
936 |
+
xdg_surface_add_listener(xshell_surface, &shell_surface_listener, this);
|
937 |
+
xshell_toplevel = xdg_surface_get_toplevel(xshell_surface);
|
938 |
+
xdg_toplevel_add_listener(xshell_toplevel, &toplevel_listener, this);
|
939 |
+
xdg_toplevel_set_title(xshell_toplevel, title.c_str());
|
940 |
+
xdg_toplevel_set_app_id(xshell_toplevel, title.c_str());
|
941 |
+
|
942 |
+
// construct window decoration
|
943 |
+
const pangolin::Colour grey(0.5f, 0.5f, 0.5f);
|
944 |
+
decoration = std::unique_ptr<Decoration>(new Decoration(5, 20, grey, display->wcompositor, display->wsubcompositor, wsurface, display->egl_display, display->egl_configs[0]));
|
945 |
+
decoration->create();
|
946 |
+
decoration->resize(width, height);
|
947 |
+
|
948 |
+
wl_surface_commit(wsurface);
|
949 |
+
|
950 |
+
wl_display_roundtrip(display->wdisplay);
|
951 |
+
wl_display_roundtrip(display->wdisplay);
|
952 |
+
|
953 |
+
// wait for the first configure event
|
954 |
+
while (!configured) {
|
955 |
+
wl_display_dispatch(display->wdisplay);
|
956 |
+
}
|
957 |
+
}
|
958 |
+
|
959 |
+
WaylandWindow::~WaylandWindow() {
|
960 |
+
if(decoration) decoration->destroy();
|
961 |
+
|
962 |
+
// cleanup EGL
|
963 |
+
if(egl_surface) eglDestroySurface(display->egl_display, egl_surface);
|
964 |
+
if(egl_window) wl_egl_window_destroy(egl_window);
|
965 |
+
|
966 |
+
// cleanup Wayland
|
967 |
+
if(xshell_surface) xdg_surface_destroy(xshell_surface);
|
968 |
+
if(xshell_toplevel) xdg_toplevel_destroy(xshell_toplevel);
|
969 |
+
|
970 |
+
if(wsurface) wl_surface_destroy(wsurface);
|
971 |
+
}
|
972 |
+
|
973 |
+
void WaylandWindow::MakeCurrent() {
|
974 |
+
eglMakeCurrent(display->egl_display, egl_surface, egl_surface, display->egl_context);
|
975 |
+
}
|
976 |
+
|
977 |
+
void WaylandWindow::RemoveCurrent() {
|
978 |
+
eglMakeCurrent(display->egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
979 |
+
}
|
980 |
+
|
981 |
+
void WaylandWindow::ShowFullscreen(const TrueFalseToggle on_off) {
|
982 |
+
switch (on_off) {
|
983 |
+
case TrueFalseToggle::False:
|
984 |
+
decoration->create();
|
985 |
+
xdg_toplevel_unset_fullscreen(xshell_toplevel);
|
986 |
+
break;
|
987 |
+
case TrueFalseToggle::True:
|
988 |
+
decoration->destroy();
|
989 |
+
xdg_toplevel_set_fullscreen(xshell_toplevel, nullptr);
|
990 |
+
break;
|
991 |
+
case TrueFalseToggle::Toggle:
|
992 |
+
ShowFullscreen(TrueFalseToggle(!is_fullscreen));
|
993 |
+
break;
|
994 |
+
}
|
995 |
+
|
996 |
+
wl_display_sync(display->wdisplay);
|
997 |
+
}
|
998 |
+
|
999 |
+
void WaylandWindow::Move(const int /*x*/, const int /*y*/) { }
|
1000 |
+
|
1001 |
+
void WaylandWindow::Resize(const unsigned int /*w*/, const unsigned int /*h*/) { }
|
1002 |
+
|
1003 |
+
void WaylandWindow::ProcessEvents() { }
|
1004 |
+
|
1005 |
+
void WaylandWindow::SwapBuffers() {
|
1006 |
+
eglSwapBuffers(display->egl_display, egl_surface);
|
1007 |
+
|
1008 |
+
// draw all decoration elements
|
1009 |
+
decoration->draw();
|
1010 |
+
|
1011 |
+
MakeCurrent();
|
1012 |
+
|
1013 |
+
wl_display_dispatch(display->wdisplay);
|
1014 |
+
}
|
1015 |
+
|
1016 |
+
|
1017 |
+
std::unique_ptr<WindowInterface> CreateWaylandWindowAndBind(const std::string window_title, const int w, const int h, const std::string /*display_name*/, const bool /*double_buffered*/, const int /*sample_buffers*/, const int /*samples*/) {
|
1018 |
+
|
1019 |
+
try{
|
1020 |
+
return std::make_unique<WaylandWindow>(w, h, window_title, std::make_shared<WaylandDisplay>());
|
1021 |
+
}
|
1022 |
+
catch(const std::runtime_error&) {
|
1023 |
+
// return null pointer for fallback to X11
|
1024 |
+
return nullptr;
|
1025 |
+
}
|
1026 |
+
}
|
1027 |
+
|
1028 |
+
} // namespace wayland
|
1029 |
+
|
1030 |
+
PANGOLIN_REGISTER_FACTORY(WaylandWindow)
|
1031 |
+
{
|
1032 |
+
struct WaylandWindowFactory : public TypedFactoryInterface<WindowInterface> {
|
1033 |
+
std::map<std::string,Precedence> Schemes() const override
|
1034 |
+
{
|
1035 |
+
return {{"wayland",10}, {"linux",9}, {"default",90}};
|
1036 |
+
}
|
1037 |
+
|
1038 |
+
const char* Description() const override
|
1039 |
+
{
|
1040 |
+
return "Use X11 native window";
|
1041 |
+
}
|
1042 |
+
|
1043 |
+
ParamSet Params() const override
|
1044 |
+
{
|
1045 |
+
return {{
|
1046 |
+
{"window_title","window","Title of application Window"},
|
1047 |
+
{"w","640","Requested window width"},
|
1048 |
+
{"h","480","Requested window height"},
|
1049 |
+
{"display_name","","The display name to open the window on"},
|
1050 |
+
{"double_buffered","true","Whether the window should be double buffered"},
|
1051 |
+
{"sample_buffers","1",""},
|
1052 |
+
{"samples","1",""},
|
1053 |
+
}};
|
1054 |
+
}
|
1055 |
+
|
1056 |
+
std::unique_ptr<WindowInterface> Open(const Uri& uri) override {
|
1057 |
+
|
1058 |
+
const std::string window_title = uri.Get<std::string>("window_title", "window");
|
1059 |
+
const int w = uri.Get<int>("w", 640);
|
1060 |
+
const int h = uri.Get<int>("h", 480);
|
1061 |
+
const std::string display_name = uri.Get<std::string>("display_name", "");
|
1062 |
+
const bool double_buffered = uri.Get<bool>("double_buffered", true);
|
1063 |
+
const int sample_buffers = uri.Get<int>("sample_buffers", 1);
|
1064 |
+
const int samples = uri.Get<int>("samples", 1);
|
1065 |
+
return std::unique_ptr<WindowInterface>(wayland::CreateWaylandWindowAndBind(window_title, w, h, display_name, double_buffered, sample_buffers, samples));
|
1066 |
+
}
|
1067 |
+
|
1068 |
+
virtual ~WaylandWindowFactory() { }
|
1069 |
+
};
|
1070 |
+
|
1071 |
+
return FactoryRegistry::I()->RegisterFactory<WindowInterface>(std::make_shared<WaylandWindowFactory>());
|
1072 |
+
}
|
1073 |
+
|
1074 |
+
} // namespace pangolin
|
third-party/DPVO/Pangolin/components/pango_windowing/src/display_win.cpp
ADDED
@@ -0,0 +1,631 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2011-2018 Steven Lovegrove, Andrey Mnatsakanov
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#include <pangolin/factory/factory_registry.h>
|
29 |
+
#include <pangolin/platform.h>
|
30 |
+
#include <pangolin/gl/glinclude.h>
|
31 |
+
#include <pangolin/windowing/WinWindow.h>
|
32 |
+
#include <memory>
|
33 |
+
|
34 |
+
#define CheckWGLDieOnError() pangolin::_CheckWLDieOnError( __FILE__, __LINE__ );
|
35 |
+
namespace pangolin {
|
36 |
+
inline void _CheckWLDieOnError( const char *sFile, const int nLine )
|
37 |
+
{
|
38 |
+
DWORD errorCode = GetLastError();
|
39 |
+
if(errorCode!=0) {
|
40 |
+
LPVOID lpMsgBuf;
|
41 |
+
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
42 |
+
NULL, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR) &lpMsgBuf, 0, NULL);
|
43 |
+
// MessageBox( NULL, (LPCTSTR)lpMsgBuf, ("Error "+std::to_string(errorCode)).c_str(), MB_OK | MB_ICONINFORMATION );
|
44 |
+
pango_print_error("Error %i: %ws", errorCode, (WCHAR *)lpMsgBuf);
|
45 |
+
pango_print_error("In: %s, line %d\n", sFile, nLine);
|
46 |
+
// exit(EXIT_FAILURE);
|
47 |
+
}
|
48 |
+
}
|
49 |
+
}
|
50 |
+
|
51 |
+
namespace pangolin
|
52 |
+
{
|
53 |
+
|
54 |
+
const char *className = "Pangolin";
|
55 |
+
|
56 |
+
////////////////////////////////////////////////////////////////////////
|
57 |
+
// Utils
|
58 |
+
////////////////////////////////////////////////////////////////////////
|
59 |
+
|
60 |
+
unsigned char GetPangoKey(WPARAM wParam, LPARAM lParam)
|
61 |
+
{
|
62 |
+
switch (wParam)
|
63 |
+
{
|
64 |
+
case VK_F1: return PANGO_SPECIAL + PANGO_KEY_F1;
|
65 |
+
case VK_F2: return PANGO_SPECIAL + PANGO_KEY_F2;
|
66 |
+
case VK_F3: return PANGO_SPECIAL + PANGO_KEY_F3;
|
67 |
+
case VK_F4: return PANGO_SPECIAL + PANGO_KEY_F4;
|
68 |
+
case VK_F5: return PANGO_SPECIAL + PANGO_KEY_F5;
|
69 |
+
case VK_F6: return PANGO_SPECIAL + PANGO_KEY_F6;
|
70 |
+
case VK_F7: return PANGO_SPECIAL + PANGO_KEY_F7;
|
71 |
+
case VK_F8: return PANGO_SPECIAL + PANGO_KEY_F8;
|
72 |
+
case VK_F9: return PANGO_SPECIAL + PANGO_KEY_F9;
|
73 |
+
case VK_F10: return PANGO_SPECIAL + PANGO_KEY_F10;
|
74 |
+
case VK_F11: return PANGO_SPECIAL + PANGO_KEY_F11;
|
75 |
+
case VK_F12: return PANGO_SPECIAL + PANGO_KEY_F12;
|
76 |
+
case VK_LEFT: return PANGO_SPECIAL + PANGO_KEY_LEFT;
|
77 |
+
case VK_UP: return PANGO_SPECIAL + PANGO_KEY_UP;
|
78 |
+
case VK_RIGHT: return PANGO_SPECIAL + PANGO_KEY_RIGHT;
|
79 |
+
case VK_DOWN: return PANGO_SPECIAL + PANGO_KEY_DOWN;
|
80 |
+
case VK_HOME: return PANGO_SPECIAL + PANGO_KEY_HOME;
|
81 |
+
case VK_END: return PANGO_SPECIAL + PANGO_KEY_END;
|
82 |
+
case VK_INSERT: return PANGO_SPECIAL + PANGO_KEY_INSERT;
|
83 |
+
case VK_DELETE: return 127;
|
84 |
+
default:
|
85 |
+
const int lBufferSize = 2;
|
86 |
+
WCHAR lBuffer[lBufferSize];
|
87 |
+
|
88 |
+
BYTE State[256];
|
89 |
+
GetKeyboardState(State);
|
90 |
+
|
91 |
+
const UINT scanCode = (lParam >> 8) & 0xFFFFFF00;
|
92 |
+
if( ToUnicode((UINT)wParam, scanCode, State, lBuffer, lBufferSize, 0) >=1 ) {
|
93 |
+
return (unsigned char)lBuffer[0];
|
94 |
+
}
|
95 |
+
}
|
96 |
+
return 0;
|
97 |
+
}
|
98 |
+
|
99 |
+
KeyModifierBitmask GetMouseModifierKey(WPARAM wParam)
|
100 |
+
{
|
101 |
+
KeyModifierBitmask mask;
|
102 |
+
if (wParam & MK_SHIFT) mask |= KeyModifierShift;
|
103 |
+
if (wParam & MK_CONTROL) mask |= KeyModifierCtrl;
|
104 |
+
if (HIBYTE(GetKeyState(VK_MENU))) mask |= KeyModifierCmd;
|
105 |
+
return mask;
|
106 |
+
}
|
107 |
+
|
108 |
+
////////////////////////////////////////////////////////////////////////
|
109 |
+
// WinWindow Implementation
|
110 |
+
////////////////////////////////////////////////////////////////////////
|
111 |
+
|
112 |
+
void WinWindow::SetupPixelFormat(HDC hDC)
|
113 |
+
{
|
114 |
+
PIXELFORMATDESCRIPTOR pfd = {
|
115 |
+
sizeof(PIXELFORMATDESCRIPTOR), /* size */
|
116 |
+
1, /* version */
|
117 |
+
PFD_SUPPORT_OPENGL |
|
118 |
+
PFD_DRAW_TO_WINDOW |
|
119 |
+
PFD_DOUBLEBUFFER, /* support double-buffering */
|
120 |
+
PFD_TYPE_RGBA, /* color type */
|
121 |
+
24, /* prefered color depth */
|
122 |
+
0, 0, 0, 0, 0, 0, /* color bits (ignored) */
|
123 |
+
8, /* alpha bits */
|
124 |
+
0, /* alpha shift (ignored) */
|
125 |
+
0, /* no accumulation buffer */
|
126 |
+
0, 0, 0, 0, /* accum bits (ignored) */
|
127 |
+
32, /* depth buffer */
|
128 |
+
0, /* no stencil buffer */
|
129 |
+
0, /* no auxiliary buffers */
|
130 |
+
PFD_MAIN_PLANE, /* main layer */
|
131 |
+
0, /* reserved */
|
132 |
+
0, 0, 0, /* no layer, visible, damage masks */
|
133 |
+
};
|
134 |
+
int pixelFormat;
|
135 |
+
|
136 |
+
pixelFormat = ChoosePixelFormat(hDC, &pfd);
|
137 |
+
if (pixelFormat == 0) {
|
138 |
+
MessageBoxA(WindowFromDC(hDC), "ChoosePixelFormat failed.", "Error", MB_ICONERROR | MB_OK);
|
139 |
+
CheckWGLDieOnError();
|
140 |
+
exit(1);
|
141 |
+
}
|
142 |
+
|
143 |
+
if (SetPixelFormat(hDC, pixelFormat, &pfd) != TRUE) {
|
144 |
+
MessageBoxA(WindowFromDC(hDC), "SetPixelFormat failed.", "Error", MB_ICONERROR | MB_OK);
|
145 |
+
CheckWGLDieOnError();
|
146 |
+
exit(1);
|
147 |
+
}
|
148 |
+
}
|
149 |
+
|
150 |
+
void WinWindow::SetupPalette(HDC hDC)
|
151 |
+
{
|
152 |
+
int pixelFormat = GetPixelFormat(hDC);
|
153 |
+
if(!pixelFormat) {
|
154 |
+
std::cerr << "GetPixelFormat() failed" << std::endl;
|
155 |
+
CheckWGLDieOnError();
|
156 |
+
}
|
157 |
+
|
158 |
+
PIXELFORMATDESCRIPTOR pfd;
|
159 |
+
LOGPALETTE* pPal;
|
160 |
+
int paletteSize;
|
161 |
+
|
162 |
+
if(!DescribePixelFormat(hDC, pixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd)) {
|
163 |
+
std::cerr << "DescribePixelFormat() failed" << std::endl;
|
164 |
+
CheckWGLDieOnError();
|
165 |
+
}
|
166 |
+
|
167 |
+
if (pfd.dwFlags & PFD_NEED_PALETTE) {
|
168 |
+
paletteSize = 1 << pfd.cColorBits;
|
169 |
+
}
|
170 |
+
else {
|
171 |
+
return;
|
172 |
+
}
|
173 |
+
|
174 |
+
pPal = (LOGPALETTE*) malloc(sizeof(LOGPALETTE) + paletteSize * sizeof(PALETTEENTRY));
|
175 |
+
pPal->palVersion = 0x300;
|
176 |
+
pPal->palNumEntries = paletteSize;
|
177 |
+
|
178 |
+
/* build a simple RGB color palette */
|
179 |
+
{
|
180 |
+
int redMask = (1 << pfd.cRedBits) - 1;
|
181 |
+
int greenMask = (1 << pfd.cGreenBits) - 1;
|
182 |
+
int blueMask = (1 << pfd.cBlueBits) - 1;
|
183 |
+
int i;
|
184 |
+
|
185 |
+
for (i = 0; i<paletteSize; ++i) {
|
186 |
+
pPal->palPalEntry[i].peRed =
|
187 |
+
(((i >> pfd.cRedShift) & redMask) * 255) / redMask;
|
188 |
+
pPal->palPalEntry[i].peGreen =
|
189 |
+
(((i >> pfd.cGreenShift) & greenMask) * 255) / greenMask;
|
190 |
+
pPal->palPalEntry[i].peBlue =
|
191 |
+
(((i >> pfd.cBlueShift) & blueMask) * 255) / blueMask;
|
192 |
+
pPal->palPalEntry[i].peFlags = 0;
|
193 |
+
}
|
194 |
+
}
|
195 |
+
|
196 |
+
hPalette = CreatePalette(pPal);
|
197 |
+
free(pPal);
|
198 |
+
|
199 |
+
if (hPalette) {
|
200 |
+
SelectPalette(hDC, hPalette, FALSE);
|
201 |
+
RealizePalette(hDC);
|
202 |
+
}
|
203 |
+
else {
|
204 |
+
std::cerr << "CreatePalette() failed" << std::endl;
|
205 |
+
}
|
206 |
+
}
|
207 |
+
|
208 |
+
WinWindow::WinWindow(
|
209 |
+
const std::string& window_title, int width, int height
|
210 |
+
) : hWnd(0), bIsFullscreen(false)
|
211 |
+
{
|
212 |
+
const HMODULE hCurrentInst = GetModuleHandleA(nullptr);
|
213 |
+
if(hCurrentInst==NULL) {
|
214 |
+
std::cerr << "GetModuleHandle() failed" << std::endl;
|
215 |
+
CheckWGLDieOnError();
|
216 |
+
}
|
217 |
+
RegisterThisClass(hCurrentInst);
|
218 |
+
|
219 |
+
HWND thishwnd = CreateWindowA(
|
220 |
+
className, window_title.c_str(),
|
221 |
+
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
|
222 |
+
0, 0, width, height,
|
223 |
+
NULL, NULL, hCurrentInst, this);
|
224 |
+
if(thishwnd==NULL) {
|
225 |
+
std::cerr << "CreateWindow() failed" << std::endl;
|
226 |
+
CheckWGLDieOnError();
|
227 |
+
}
|
228 |
+
|
229 |
+
if( thishwnd != hWnd ) {
|
230 |
+
throw std::runtime_error("Pangolin Window Creation Failed.");
|
231 |
+
}
|
232 |
+
|
233 |
+
// Display Window
|
234 |
+
ShowWindow(hWnd, SW_SHOW);
|
235 |
+
}
|
236 |
+
|
237 |
+
WinWindow::~WinWindow()
|
238 |
+
{
|
239 |
+
if(IsWindow(hWnd) && !DestroyWindow(hWnd)) {
|
240 |
+
std::cerr << "DestroyWindow() failed" << std::endl;
|
241 |
+
CheckWGLDieOnError();
|
242 |
+
}
|
243 |
+
}
|
244 |
+
|
245 |
+
void WinWindow::RegisterThisClass(HMODULE hCurrentInst)
|
246 |
+
{
|
247 |
+
WNDCLASSA wndClass;
|
248 |
+
wndClass.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
|
249 |
+
wndClass.lpfnWndProc = WinWindow::WndProc;
|
250 |
+
wndClass.cbClsExtra = 0;
|
251 |
+
wndClass.cbWndExtra = 0;
|
252 |
+
wndClass.hInstance = hCurrentInst;
|
253 |
+
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
254 |
+
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
|
255 |
+
wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
|
256 |
+
wndClass.lpszMenuName = NULL;
|
257 |
+
wndClass.lpszClassName = className;
|
258 |
+
if(!RegisterClassA(&wndClass)) {
|
259 |
+
std::cerr << "RegisterClass() failed" << std::endl;
|
260 |
+
CheckWGLDieOnError();
|
261 |
+
}
|
262 |
+
}
|
263 |
+
|
264 |
+
LRESULT APIENTRY
|
265 |
+
WinWindow::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
266 |
+
{
|
267 |
+
WinWindow* self = 0;
|
268 |
+
|
269 |
+
if (uMsg == WM_NCCREATE) {
|
270 |
+
auto lpcs = reinterpret_cast<LPCREATESTRUCTA>(lParam);
|
271 |
+
self = reinterpret_cast<WinWindow*>(lpcs->lpCreateParams);
|
272 |
+
if(self) {
|
273 |
+
self->hWnd = hwnd;
|
274 |
+
SetWindowLongPtrA(hwnd, GWLP_USERDATA, reinterpret_cast<LPARAM>(self));
|
275 |
+
}
|
276 |
+
} else {
|
277 |
+
self = reinterpret_cast<WinWindow*>(GetWindowLongPtrA(hwnd, GWLP_USERDATA));
|
278 |
+
}
|
279 |
+
|
280 |
+
if (self) {
|
281 |
+
return self->HandleWinMessages(uMsg, wParam, lParam);
|
282 |
+
} else {
|
283 |
+
return DefWindowProcA(hwnd, uMsg, wParam, lParam);
|
284 |
+
}
|
285 |
+
}
|
286 |
+
|
287 |
+
LRESULT WinWindow::HandleWinMessages(UINT message, WPARAM wParam, LPARAM lParam)
|
288 |
+
{
|
289 |
+
switch (message) {
|
290 |
+
case WM_CREATE:
|
291 |
+
/* initialize OpenGL rendering */
|
292 |
+
hDC = GetDC(hWnd);
|
293 |
+
if(hDC==NULL) {
|
294 |
+
std::cerr << "WM_CREATE GetDC() failed" << std::endl;
|
295 |
+
}
|
296 |
+
SetupPixelFormat(hDC);
|
297 |
+
SetupPalette(hDC);
|
298 |
+
hGLRC = wglCreateContext(hDC);
|
299 |
+
if(!hGLRC) {
|
300 |
+
std::cerr << "WM_CREATE wglCreateContext() failed" << std::endl;
|
301 |
+
CheckWGLDieOnError();
|
302 |
+
}
|
303 |
+
if(!wglMakeCurrent(hDC, hGLRC)) {
|
304 |
+
std::cerr << "WM_CREATE wglMakeCurrent() failed" << std::endl;
|
305 |
+
CheckWGLDieOnError();
|
306 |
+
}
|
307 |
+
return 0;
|
308 |
+
case WM_CLOSE:
|
309 |
+
case WM_QUIT:
|
310 |
+
if(!DestroyWindow(hWnd)) {
|
311 |
+
std::cerr << "DestroyWindow() failed" << std::endl;
|
312 |
+
CheckWGLDieOnError();
|
313 |
+
}
|
314 |
+
return 0;
|
315 |
+
case WM_DESTROY:
|
316 |
+
/* finish OpenGL rendering */
|
317 |
+
if (hGLRC) {
|
318 |
+
if(!wglMakeCurrent(NULL, NULL)) {
|
319 |
+
std::cerr << "WM_DESTROY wglMakeCurrent() failed" << std::endl;
|
320 |
+
CheckWGLDieOnError();
|
321 |
+
}
|
322 |
+
if(!wglDeleteContext(hGLRC)) {
|
323 |
+
std::cerr << "WM_DESTROY wglDeleteContext() failed" << std::endl;
|
324 |
+
CheckWGLDieOnError();
|
325 |
+
}
|
326 |
+
}
|
327 |
+
if (hPalette) {
|
328 |
+
DeleteObject(hPalette);
|
329 |
+
}
|
330 |
+
ReleaseDC(hWnd, hDC);
|
331 |
+
PostQuitMessage(0);
|
332 |
+
return 0;
|
333 |
+
case WM_SIZE:
|
334 |
+
ResizeSignal(WindowResizeEvent({(int)LOWORD(lParam), (int)HIWORD(lParam)}));
|
335 |
+
return 0;
|
336 |
+
case WM_PALETTECHANGED:
|
337 |
+
/* realize palette if this is *not* the current window */
|
338 |
+
if (hGLRC && hPalette && (HWND)wParam != hWnd) {
|
339 |
+
if(!UnrealizeObject(hPalette)) {
|
340 |
+
std::cerr << "WM_PALETTECHANGED UnrealizeObject() failed" << std::endl;
|
341 |
+
}
|
342 |
+
if(!SelectPalette(hDC, hPalette, FALSE)) {
|
343 |
+
std::cerr << "WM_PALETTECHANGED SelectPalette() failed" << std::endl;
|
344 |
+
}
|
345 |
+
if(RealizePalette(hDC)==GDI_ERROR) {
|
346 |
+
std::cerr << "WM_PALETTECHANGED RealizePalette() failed" << std::endl;
|
347 |
+
}
|
348 |
+
//redraw();
|
349 |
+
break;
|
350 |
+
}
|
351 |
+
break;
|
352 |
+
case WM_QUERYNEWPALETTE:
|
353 |
+
/* realize palette if this is the current window */
|
354 |
+
if (hGLRC && hPalette) {
|
355 |
+
if(!UnrealizeObject(hPalette)) {
|
356 |
+
std::cerr << "WM_QUERYNEWPALETTE UnrealizeObject() failed" << std::endl;
|
357 |
+
}
|
358 |
+
if(!SelectPalette(hDC, hPalette, FALSE)) {
|
359 |
+
std::cerr << "WM_QUERYNEWPALETTE SelectPalette() failed" << std::endl;
|
360 |
+
}
|
361 |
+
if(RealizePalette(hDC)==GDI_ERROR) {
|
362 |
+
std::cerr << "WM_QUERYNEWPALETTE RealizePalette() failed" << std::endl;
|
363 |
+
}
|
364 |
+
return TRUE;
|
365 |
+
}
|
366 |
+
break;
|
367 |
+
case WM_PAINT:
|
368 |
+
break;
|
369 |
+
case WM_KEYDOWN:
|
370 |
+
case WM_KEYUP:
|
371 |
+
{
|
372 |
+
unsigned char key = GetPangoKey(wParam, lParam);
|
373 |
+
if(key > 0) {
|
374 |
+
KeyboardSignal(KeyboardEvent({
|
375 |
+
afLastMousePos[0], afLastMousePos[1],
|
376 |
+
KeyModifierBitmask(),
|
377 |
+
(unsigned char)key, message == WM_KEYDOWN
|
378 |
+
}));
|
379 |
+
}
|
380 |
+
return 0;
|
381 |
+
}
|
382 |
+
case WM_LBUTTONDOWN:
|
383 |
+
MouseSignal(MouseEvent({
|
384 |
+
(float)GET_X_LPARAM(lParam), (float)GET_Y_LPARAM(lParam),
|
385 |
+
GetMouseModifierKey(wParam), 0, true
|
386 |
+
}));
|
387 |
+
return 0;
|
388 |
+
case WM_MBUTTONDOWN:
|
389 |
+
MouseSignal(MouseEvent({
|
390 |
+
(float)GET_X_LPARAM(lParam), (float)GET_Y_LPARAM(lParam),
|
391 |
+
GetMouseModifierKey(wParam), 1, true
|
392 |
+
}));
|
393 |
+
return 0;
|
394 |
+
case WM_RBUTTONDOWN:
|
395 |
+
MouseSignal(MouseEvent({
|
396 |
+
(float)GET_X_LPARAM(lParam), (float)GET_Y_LPARAM(lParam),
|
397 |
+
GetMouseModifierKey(wParam), 2, true
|
398 |
+
}));
|
399 |
+
return 0;
|
400 |
+
case WM_LBUTTONUP:
|
401 |
+
MouseSignal(MouseEvent({
|
402 |
+
(float)GET_X_LPARAM(lParam), (float)GET_Y_LPARAM(lParam),
|
403 |
+
GetMouseModifierKey(wParam), 0, false
|
404 |
+
}));
|
405 |
+
return 0;
|
406 |
+
case WM_MBUTTONUP:
|
407 |
+
MouseSignal(MouseEvent({
|
408 |
+
(float)GET_X_LPARAM(lParam), (float)GET_Y_LPARAM(lParam),
|
409 |
+
GetMouseModifierKey(wParam), 1, false
|
410 |
+
}));
|
411 |
+
return 0;
|
412 |
+
case WM_RBUTTONUP:
|
413 |
+
MouseSignal(MouseEvent({
|
414 |
+
(float)GET_X_LPARAM(lParam), (float)GET_Y_LPARAM(lParam),
|
415 |
+
GetMouseModifierKey(wParam), 2, false
|
416 |
+
}));
|
417 |
+
return 0;
|
418 |
+
case WM_MOUSEMOVE:
|
419 |
+
if (wParam & (MK_LBUTTON | MK_MBUTTON | MK_RBUTTON) ) {
|
420 |
+
MouseMotionSignal(MouseMotionEvent({
|
421 |
+
(float)GET_X_LPARAM(lParam), (float)GET_Y_LPARAM(lParam),
|
422 |
+
GetMouseModifierKey(wParam),
|
423 |
+
}));
|
424 |
+
} else{
|
425 |
+
PassiveMouseMotionSignal(MouseMotionEvent({
|
426 |
+
(float)GET_X_LPARAM(lParam), (float)GET_Y_LPARAM(lParam),
|
427 |
+
GetMouseModifierKey(wParam),
|
428 |
+
}));
|
429 |
+
}
|
430 |
+
afLastMousePos[0] = (float)GET_X_LPARAM(lParam);
|
431 |
+
afLastMousePos[1] = (float)GET_Y_LPARAM(lParam);
|
432 |
+
return 0;
|
433 |
+
case WM_MOUSEWHEEL:
|
434 |
+
SpecialInputSignal(SpecialInputEvent({
|
435 |
+
afLastMousePos[0], afLastMousePos[1],
|
436 |
+
GetMouseModifierKey(wParam), InputSpecialScroll,
|
437 |
+
{0.0f, GET_WHEEL_DELTA_WPARAM(wParam) / 5.0f, 0.0f, 0.0f }
|
438 |
+
}));
|
439 |
+
return 0;
|
440 |
+
case WM_MOUSEHWHEEL:
|
441 |
+
SpecialInputSignal(SpecialInputEvent({
|
442 |
+
afLastMousePos[0], afLastMousePos[1],
|
443 |
+
GetMouseModifierKey(wParam), InputSpecialScroll,
|
444 |
+
{GET_WHEEL_DELTA_WPARAM(wParam) / 5.0f, 0.0f, 0.0f, 0.0f }
|
445 |
+
}));
|
446 |
+
return 0;
|
447 |
+
default:
|
448 |
+
break;
|
449 |
+
}
|
450 |
+
return DefWindowProcA(hWnd, message, wParam, lParam);
|
451 |
+
}
|
452 |
+
|
453 |
+
void WinWindow::StartFullScreen() {
|
454 |
+
LONG dwExStyle = GetWindowLongA(hWnd, GWL_EXSTYLE)
|
455 |
+
& ~(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
|
456 |
+
if(dwExStyle==0) {
|
457 |
+
std::cerr << "GetWindowLongA() failed" << std::endl;
|
458 |
+
CheckWGLDieOnError();
|
459 |
+
}
|
460 |
+
LONG dwStyle = GetWindowLongA(hWnd, GWL_STYLE)
|
461 |
+
& ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZE | WS_MAXIMIZE | WS_SYSMENU);
|
462 |
+
if(dwStyle==0) {
|
463 |
+
std::cerr << "GetWindowLongA() failed" << std::endl;
|
464 |
+
CheckWGLDieOnError();
|
465 |
+
}
|
466 |
+
|
467 |
+
if(!SetWindowLongA(hWnd, GWL_EXSTYLE, dwExStyle)) {
|
468 |
+
std::cerr << "SetWindowLongA() failed" << std::endl;
|
469 |
+
CheckWGLDieOnError();
|
470 |
+
}
|
471 |
+
if(!SetWindowLongA(hWnd, GWL_STYLE, dwStyle)) {
|
472 |
+
std::cerr << "SetWindowLongA() failed" << std::endl;
|
473 |
+
CheckWGLDieOnError();
|
474 |
+
}
|
475 |
+
|
476 |
+
// Save windowed size so that we can un-fullscreen to
|
477 |
+
// to this dimension
|
478 |
+
GetWindowRect(hWnd, &cWindowedRect);
|
479 |
+
ShowWindow(hWnd, SW_SHOWMAXIMIZED);
|
480 |
+
}
|
481 |
+
|
482 |
+
void WinWindow::StopFullScreen() {
|
483 |
+
ChangeDisplaySettings(NULL, 0);
|
484 |
+
ShowCursor(TRUE);
|
485 |
+
|
486 |
+
LONG dwExStyle = GetWindowLongA(hWnd, GWL_EXSTYLE) | WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
|
487 |
+
LONG dwStyle = GetWindowLongA(hWnd, GWL_STYLE) | WS_OVERLAPPEDWINDOW;
|
488 |
+
|
489 |
+
if(dwExStyle==0) {
|
490 |
+
std::cerr << "GetWindowLongA() failed" << std::endl;
|
491 |
+
CheckWGLDieOnError();
|
492 |
+
}
|
493 |
+
if(dwStyle==0) {
|
494 |
+
std::cerr << "GetWindowLongA() failed" << std::endl;
|
495 |
+
CheckWGLDieOnError();
|
496 |
+
}
|
497 |
+
|
498 |
+
if(!SetWindowLongA(hWnd, GWL_EXSTYLE, dwExStyle)) {
|
499 |
+
std::cerr << "SetWindowLongA() failed" << std::endl;
|
500 |
+
CheckWGLDieOnError();
|
501 |
+
}
|
502 |
+
if(!SetWindowLongA(hWnd, GWL_STYLE, dwStyle)) {
|
503 |
+
std::cerr << "SetWindowLongA() failed" << std::endl;
|
504 |
+
CheckWGLDieOnError();
|
505 |
+
}
|
506 |
+
|
507 |
+
// Restore previous size
|
508 |
+
Resize(cWindowedRect.right-cWindowedRect.left, cWindowedRect.bottom-cWindowedRect.top);
|
509 |
+
Move(cWindowedRect.left, cWindowedRect.top);
|
510 |
+
}
|
511 |
+
|
512 |
+
void WinWindow::ShowFullscreen(const TrueFalseToggle on_off)
|
513 |
+
{
|
514 |
+
const bool target = to_bool(on_off, bIsFullscreen);
|
515 |
+
if(target != bIsFullscreen) {
|
516 |
+
if(target) {
|
517 |
+
StartFullScreen();
|
518 |
+
}else{
|
519 |
+
StopFullScreen();
|
520 |
+
}
|
521 |
+
}
|
522 |
+
bIsFullscreen = target;
|
523 |
+
}
|
524 |
+
|
525 |
+
void WinWindow::Move(int x, int y)
|
526 |
+
{
|
527 |
+
if( !SetWindowPos(hWnd, 0, x, y, 0, 0, SWP_NOSIZE) ) {
|
528 |
+
std::cerr << "WinWindow::Move failed" << std::endl;
|
529 |
+
CheckWGLDieOnError();
|
530 |
+
}
|
531 |
+
}
|
532 |
+
|
533 |
+
void WinWindow::Resize(unsigned int w, unsigned int h)
|
534 |
+
{
|
535 |
+
if( !SetWindowPos(hWnd, 0, 0, 0, w, h, SWP_NOMOVE) ) {
|
536 |
+
std::cerr << "WinWindow::Resize failed" << std::endl;
|
537 |
+
CheckWGLDieOnError();
|
538 |
+
}
|
539 |
+
}
|
540 |
+
|
541 |
+
void WinWindow::MakeCurrent()
|
542 |
+
{
|
543 |
+
if(wglMakeCurrent(hDC, hGLRC)==FALSE) {
|
544 |
+
std::cerr << "wglMakeCurrent() failed" << std::endl;
|
545 |
+
CheckWGLDieOnError();
|
546 |
+
}
|
547 |
+
|
548 |
+
RECT rect;
|
549 |
+
if(!GetWindowRect(hWnd, &rect)) {
|
550 |
+
std::cerr << "GetWindowRect() failed" << std::endl;
|
551 |
+
CheckWGLDieOnError();
|
552 |
+
}
|
553 |
+
Resize(rect.right - rect.left, rect.bottom - rect.top);
|
554 |
+
}
|
555 |
+
|
556 |
+
void WinWindow::RemoveCurrent()
|
557 |
+
{
|
558 |
+
if(wglMakeCurrent(NULL, NULL)==0) {
|
559 |
+
std::cerr << "wglMakeCurrent() failed" << std::endl;
|
560 |
+
CheckWGLDieOnError();
|
561 |
+
}
|
562 |
+
}
|
563 |
+
|
564 |
+
void WinWindow::SwapBuffers()
|
565 |
+
{
|
566 |
+
if(!::SwapBuffers(hDC)) {
|
567 |
+
std::cerr << "SwapBuffers() failed" << std::endl;
|
568 |
+
CheckWGLDieOnError();
|
569 |
+
}
|
570 |
+
}
|
571 |
+
|
572 |
+
void WinWindow::ProcessEvents()
|
573 |
+
{
|
574 |
+
static bool needs_init = true;
|
575 |
+
if(needs_init) {
|
576 |
+
RECT cRect;
|
577 |
+
GetClientRect(hWnd, &cRect);
|
578 |
+
ResizeSignal(WindowResizeEvent({cRect.right, cRect.bottom}));
|
579 |
+
needs_init = false;
|
580 |
+
}
|
581 |
+
|
582 |
+
MSG msg;
|
583 |
+
while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) {
|
584 |
+
if (msg.message == WM_QUIT) {
|
585 |
+
CloseSignal();
|
586 |
+
break;
|
587 |
+
}
|
588 |
+
TranslateMessage(&msg);
|
589 |
+
DispatchMessageA(&msg);
|
590 |
+
}
|
591 |
+
}
|
592 |
+
|
593 |
+
std::unique_ptr<WindowInterface> CreateWinWindowAndBind(std::string window_title, int w, int h)
|
594 |
+
{
|
595 |
+
WinWindow* win = new WinWindow(window_title, w, h);
|
596 |
+
|
597 |
+
return std::unique_ptr<WindowInterface>(win);
|
598 |
+
}
|
599 |
+
|
600 |
+
PANGOLIN_REGISTER_FACTORY(WinWindow)
|
601 |
+
{
|
602 |
+
struct WinWindowFactory : public TypedFactoryInterface<WindowInterface> {
|
603 |
+
std::map<std::string,Precedence> Schemes() const override
|
604 |
+
{
|
605 |
+
return {{"winapi",10}, {"default",100}};
|
606 |
+
}
|
607 |
+
const char* Description() const override
|
608 |
+
{
|
609 |
+
return "Use Windows native window";
|
610 |
+
}
|
611 |
+
ParamSet Params() const override
|
612 |
+
{
|
613 |
+
return {{
|
614 |
+
{"window_title","window","Title of application Window"},
|
615 |
+
{"w","640","Requested window width"},
|
616 |
+
{"h","480","Requested window height"}
|
617 |
+
}};
|
618 |
+
}
|
619 |
+
std::unique_ptr<WindowInterface> Open(const Uri& uri) override {
|
620 |
+
|
621 |
+
const std::string window_title = uri.Get<std::string>("window_title", "window");
|
622 |
+
const int w = uri.Get<int>("w", 640);
|
623 |
+
const int h = uri.Get<int>("h", 480);
|
624 |
+
return std::unique_ptr<WindowInterface>(CreateWinWindowAndBind(window_title, w, h));
|
625 |
+
}
|
626 |
+
};
|
627 |
+
|
628 |
+
return FactoryRegistry::I()->RegisterFactory<WindowInterface>(std::make_shared<WinWindowFactory>());
|
629 |
+
}
|
630 |
+
|
631 |
+
}
|
third-party/DPVO/Pangolin/components/pango_windowing/src/display_x11.cpp
ADDED
@@ -0,0 +1,548 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2011-2018 Steven Lovegrove, Andrey Mnatsakanov
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
|
29 |
+
// Code based on public domain sample at
|
30 |
+
// https://www.opengl.org/wiki/Tutorial:_OpenGL_3.0_Context_Creation_%28GLX%29
|
31 |
+
|
32 |
+
#include <pangolin/factory/factory_registry.h>
|
33 |
+
#include <pangolin/platform.h>
|
34 |
+
#include <pangolin/gl/glinclude.h>
|
35 |
+
#include <pangolin/windowing/X11Window.h>
|
36 |
+
|
37 |
+
#include <mutex>
|
38 |
+
#include <stdexcept>
|
39 |
+
#include <stdio.h>
|
40 |
+
#include <stdlib.h>
|
41 |
+
#include <string.h>
|
42 |
+
#include <unistd.h>
|
43 |
+
|
44 |
+
#include <GL/glx.h>
|
45 |
+
|
46 |
+
namespace pangolin
|
47 |
+
{
|
48 |
+
|
49 |
+
std::mutex window_mutex;
|
50 |
+
std::weak_ptr<X11GlContext> global_gl_context;
|
51 |
+
|
52 |
+
const long EVENT_MASKS = ButtonPressMask|ButtonReleaseMask|StructureNotifyMask|ButtonMotionMask|PointerMotionMask|KeyPressMask|KeyReleaseMask|FocusChangeMask;
|
53 |
+
|
54 |
+
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
|
55 |
+
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
|
56 |
+
typedef GLXContext (*glXCreateContextAttribsARBProc)(::Display*, ::GLXFBConfig, ::GLXContext, Bool, const int*);
|
57 |
+
|
58 |
+
// Adapted from: http://www.opengl.org/resources/features/OGLextensions/
|
59 |
+
bool isExtensionSupported(const char *extList, const char *extension)
|
60 |
+
{
|
61 |
+
/* Extension names should not have spaces. */
|
62 |
+
const char* where = strchr(extension, ' ');
|
63 |
+
if (where || *extension == '\0') {
|
64 |
+
return false;
|
65 |
+
}
|
66 |
+
|
67 |
+
for(const char* start=extList;;) {
|
68 |
+
where = strstr(start, extension);
|
69 |
+
if (!where) {
|
70 |
+
break;
|
71 |
+
}
|
72 |
+
|
73 |
+
const char *terminator = where + strlen(extension);
|
74 |
+
|
75 |
+
if ( where == start || *(where - 1) == ' ' ) {
|
76 |
+
if ( *terminator == ' ' || *terminator == '\0' ) {
|
77 |
+
return true;
|
78 |
+
}
|
79 |
+
}
|
80 |
+
|
81 |
+
start = terminator;
|
82 |
+
}
|
83 |
+
|
84 |
+
return false;
|
85 |
+
}
|
86 |
+
|
87 |
+
::GLXFBConfig ChooseFrameBuffer(
|
88 |
+
::Display *display, bool glx_doublebuffer,
|
89 |
+
int glx_sample_buffers, int glx_samples
|
90 |
+
) {
|
91 |
+
// Desired attributes
|
92 |
+
int visual_attribs[] =
|
93 |
+
{
|
94 |
+
GLX_X_RENDERABLE , True,
|
95 |
+
GLX_DRAWABLE_TYPE , GLX_WINDOW_BIT,
|
96 |
+
GLX_RENDER_TYPE , GLX_RGBA_BIT,
|
97 |
+
GLX_X_VISUAL_TYPE , GLX_TRUE_COLOR,
|
98 |
+
GLX_RED_SIZE , 8,
|
99 |
+
GLX_GREEN_SIZE , 8,
|
100 |
+
GLX_BLUE_SIZE , 8,
|
101 |
+
GLX_ALPHA_SIZE , 8,
|
102 |
+
GLX_DEPTH_SIZE , 24,
|
103 |
+
GLX_STENCIL_SIZE , 8,
|
104 |
+
GLX_DOUBLEBUFFER , glx_doublebuffer ? True : False,
|
105 |
+
None
|
106 |
+
};
|
107 |
+
|
108 |
+
int fbcount;
|
109 |
+
GLXFBConfig* fbc = glXChooseFBConfig(display, DefaultScreen(display), visual_attribs, &fbcount);
|
110 |
+
if (!fbc) {
|
111 |
+
throw std::runtime_error("Pangolin X11: Unable to retrieve framebuffer options");
|
112 |
+
}
|
113 |
+
|
114 |
+
int best_fbc = -1;
|
115 |
+
int worst_fbc = -1;
|
116 |
+
int best_num_samp = -1;
|
117 |
+
int worst_num_samp = 999;
|
118 |
+
|
119 |
+
// Enumerate framebuffer options, storing the best and worst that match our attribs
|
120 |
+
for (int i=0; i<fbcount; ++i)
|
121 |
+
{
|
122 |
+
XVisualInfo *vi = glXGetVisualFromFBConfig( display, fbc[i] );
|
123 |
+
if ( vi )
|
124 |
+
{
|
125 |
+
int samp_buf, samples;
|
126 |
+
glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLE_BUFFERS, &samp_buf );
|
127 |
+
glXGetFBConfigAttrib( display, fbc[i], GLX_SAMPLES , &samples );
|
128 |
+
|
129 |
+
// Filter for the best available.
|
130 |
+
if ( samples > best_num_samp ) {
|
131 |
+
best_fbc = i;
|
132 |
+
best_num_samp = samples;
|
133 |
+
}
|
134 |
+
|
135 |
+
// Filter lowest settings which match minimum user requirement.
|
136 |
+
if ( samp_buf >= glx_sample_buffers && samples >= glx_samples && samples < worst_num_samp ) {
|
137 |
+
worst_fbc = i;
|
138 |
+
worst_num_samp = samples;
|
139 |
+
}
|
140 |
+
}
|
141 |
+
XFree( vi );
|
142 |
+
}
|
143 |
+
|
144 |
+
// Select the minimum suitable option. The 'best' is often too slow.
|
145 |
+
int chosen_fbc_id = worst_fbc;
|
146 |
+
|
147 |
+
// If minimum requested isn't available, return the best that is.
|
148 |
+
if(chosen_fbc_id < 0) {
|
149 |
+
pango_print_warn("Framebuffer with requested attributes not available. Using available framebuffer. You may see visual artifacts.");
|
150 |
+
chosen_fbc_id = best_fbc;
|
151 |
+
}
|
152 |
+
|
153 |
+
::GLXFBConfig chosenFbc = fbc[ chosen_fbc_id ];
|
154 |
+
XFree( fbc );
|
155 |
+
return chosenFbc;
|
156 |
+
}
|
157 |
+
|
158 |
+
static bool ctxErrorOccurred = false;
|
159 |
+
static int ctxErrorHandler( ::Display * /*dpy*/, ::XErrorEvent * ev )
|
160 |
+
{
|
161 |
+
const int buffer_size = 10240;
|
162 |
+
char buffer[buffer_size];
|
163 |
+
XGetErrorText(ev->display, ev->error_code, buffer, buffer_size );
|
164 |
+
pango_print_error("X11 Error: %s\n", buffer);
|
165 |
+
ctxErrorOccurred = true;
|
166 |
+
return 0;
|
167 |
+
}
|
168 |
+
|
169 |
+
GLXContext CreateGlContext(::Display *display, ::GLXFBConfig chosenFbc, GLXContext share_context = 0)
|
170 |
+
{
|
171 |
+
int glx_major, glx_minor;
|
172 |
+
if ( !glXQueryVersion( display, &glx_major, &glx_minor ) ||
|
173 |
+
( ( glx_major == 1 ) && ( glx_minor < 3 ) ) || ( glx_major < 1 ) )
|
174 |
+
{
|
175 |
+
throw std::runtime_error("Pangolin X11: Invalid GLX version. Require GLX >= 1.3");
|
176 |
+
}
|
177 |
+
|
178 |
+
GLXContext new_ctx;
|
179 |
+
|
180 |
+
// Get the default screen's GLX extension list
|
181 |
+
const char *glxExts = glXQueryExtensionsString( display, DefaultScreen( display ) );
|
182 |
+
|
183 |
+
glXCreateContextAttribsARBProc glXCreateContextAttribsARB =
|
184 |
+
(glXCreateContextAttribsARBProc) glXGetProcAddressARB(
|
185 |
+
(const GLubyte *) "glXCreateContextAttribsARB"
|
186 |
+
);
|
187 |
+
|
188 |
+
// Install an X error handler so the application won't exit if GL 3.0
|
189 |
+
// context allocation fails. Handler is global and shared across all threads.
|
190 |
+
ctxErrorOccurred = false;
|
191 |
+
int (*oldHandler)(::Display*, ::XErrorEvent*) = XSetErrorHandler(&ctxErrorHandler);
|
192 |
+
|
193 |
+
if ( isExtensionSupported( glxExts, "GLX_ARB_create_context" ) && glXCreateContextAttribsARB )
|
194 |
+
{
|
195 |
+
int context_attribs[] = {
|
196 |
+
GLX_CONTEXT_MAJOR_VERSION_ARB, 3,
|
197 |
+
GLX_CONTEXT_MINOR_VERSION_ARB, 0,
|
198 |
+
//GLX_CONTEXT_FLAGS_ARB , GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
|
199 |
+
None
|
200 |
+
};
|
201 |
+
|
202 |
+
new_ctx = glXCreateContextAttribsARB( display, chosenFbc, share_context, True, context_attribs );
|
203 |
+
|
204 |
+
// Sync to ensure any errors generated are processed.
|
205 |
+
XSync( display, False );
|
206 |
+
if ( ctxErrorOccurred || !new_ctx ) {
|
207 |
+
ctxErrorOccurred = false;
|
208 |
+
// Fall back to old-style 2.x context. Implementations will return the newest
|
209 |
+
// context version compatible with OpenGL versions less than version 3.0.
|
210 |
+
context_attribs[1] = 1; // GLX_CONTEXT_MAJOR_VERSION_ARB = 1
|
211 |
+
context_attribs[3] = 0; // GLX_CONTEXT_MINOR_VERSION_ARB = 0
|
212 |
+
new_ctx = glXCreateContextAttribsARB( display, chosenFbc, share_context, True, context_attribs );
|
213 |
+
}
|
214 |
+
} else {
|
215 |
+
// Fallback to GLX 1.3 Context
|
216 |
+
new_ctx = glXCreateNewContext( display, chosenFbc, GLX_RGBA_TYPE, share_context, True );
|
217 |
+
}
|
218 |
+
|
219 |
+
// Sync to ensure any errors generated are processed.
|
220 |
+
XSync( display, False );
|
221 |
+
|
222 |
+
// Restore the original error handler
|
223 |
+
XSetErrorHandler( oldHandler );
|
224 |
+
|
225 |
+
if ( ctxErrorOccurred || !new_ctx ) {
|
226 |
+
throw std::runtime_error("Pangolin X11: Failed to create an OpenGL context");
|
227 |
+
}
|
228 |
+
|
229 |
+
// Verifying that context is a direct context
|
230 |
+
if ( ! glXIsDirect ( display, new_ctx ) ) {
|
231 |
+
pango_print_warn("Pangolin X11: Indirect GLX rendering context obtained\n");
|
232 |
+
}
|
233 |
+
|
234 |
+
return new_ctx;
|
235 |
+
}
|
236 |
+
|
237 |
+
X11GlContext::X11GlContext(std::shared_ptr<X11Display>& d, ::GLXFBConfig chosenFbc, std::shared_ptr<X11GlContext> shared_context)
|
238 |
+
: display(d), shared_context(shared_context)
|
239 |
+
{
|
240 |
+
// prevent chained sharing
|
241 |
+
while(shared_context && shared_context->shared_context) {
|
242 |
+
shared_context = shared_context->shared_context;
|
243 |
+
}
|
244 |
+
|
245 |
+
// Contexts can't be shared across different displays.
|
246 |
+
if(shared_context && shared_context->display != d) {
|
247 |
+
shared_context.reset();
|
248 |
+
}
|
249 |
+
|
250 |
+
glcontext = CreateGlContext(display->display, chosenFbc, shared_context ? shared_context->glcontext : 0);
|
251 |
+
}
|
252 |
+
|
253 |
+
X11GlContext::~X11GlContext()
|
254 |
+
{
|
255 |
+
glXDestroyContext( display->display, glcontext );
|
256 |
+
}
|
257 |
+
|
258 |
+
X11Window::X11Window(
|
259 |
+
const std::string& title, int width, int height,
|
260 |
+
std::shared_ptr<X11Display>& display, ::GLXFBConfig chosenFbc
|
261 |
+
) : display(display), glcontext(0), win(0), cmap(0)
|
262 |
+
{
|
263 |
+
// Get a visual
|
264 |
+
XVisualInfo *vi = glXGetVisualFromFBConfig( display->display, chosenFbc );
|
265 |
+
|
266 |
+
// Create colourmap
|
267 |
+
XSetWindowAttributes swa;
|
268 |
+
swa.background_pixmap = None;
|
269 |
+
swa.border_pixel = 0;
|
270 |
+
swa.event_mask = StructureNotifyMask;
|
271 |
+
swa.colormap = cmap = XCreateColormap( display->display,
|
272 |
+
RootWindow( display->display, vi->screen ),
|
273 |
+
vi->visual, AllocNone );
|
274 |
+
|
275 |
+
// Create window
|
276 |
+
win = XCreateWindow( display->display, RootWindow( display->display, vi->screen ),
|
277 |
+
0, 0, width, height, 0, vi->depth, InputOutput,
|
278 |
+
vi->visual,
|
279 |
+
CWBorderPixel|CWColormap|CWEventMask, &swa );
|
280 |
+
|
281 |
+
XFree( vi );
|
282 |
+
|
283 |
+
if ( !win ) {
|
284 |
+
throw std::runtime_error("Pangolin X11: Failed to create window." );
|
285 |
+
}
|
286 |
+
|
287 |
+
// set name in window switching (alt-tab) list
|
288 |
+
{
|
289 |
+
XClassHint class_hint;
|
290 |
+
class_hint.res_class = const_cast<char*>(title.c_str());
|
291 |
+
class_hint.res_name = const_cast<char*>("");
|
292 |
+
XSetClassHint( display->display, win, &class_hint );
|
293 |
+
}
|
294 |
+
|
295 |
+
// set window title
|
296 |
+
XStoreName( display->display, win, title.c_str() );
|
297 |
+
XMapWindow( display->display, win );
|
298 |
+
|
299 |
+
// Request to be notified of these events
|
300 |
+
XSelectInput(display->display, win, EVENT_MASKS );
|
301 |
+
|
302 |
+
delete_message = XInternAtom(display->display, "WM_DELETE_WINDOW", False);
|
303 |
+
XSetWMProtocols(display->display, win, &delete_message, 1);
|
304 |
+
}
|
305 |
+
|
306 |
+
X11Window::~X11Window()
|
307 |
+
{
|
308 |
+
glXMakeCurrent( display->display, 0, 0 );
|
309 |
+
XDestroyWindow( display->display, win );
|
310 |
+
XFreeColormap( display->display, cmap );
|
311 |
+
}
|
312 |
+
|
313 |
+
void X11Window::MakeCurrent(GLXContext ctx)
|
314 |
+
{
|
315 |
+
glXMakeCurrent( display->display, win, ctx );
|
316 |
+
}
|
317 |
+
|
318 |
+
void X11Window::MakeCurrent()
|
319 |
+
{
|
320 |
+
MakeCurrent(glcontext ? glcontext->glcontext : global_gl_context.lock()->glcontext);
|
321 |
+
}
|
322 |
+
|
323 |
+
void X11Window::RemoveCurrent()
|
324 |
+
{
|
325 |
+
glXMakeCurrent(display->display, 0, nullptr);
|
326 |
+
}
|
327 |
+
|
328 |
+
void X11Window::ShowFullscreen(const TrueFalseToggle true_false)
|
329 |
+
{
|
330 |
+
const Atom _NET_WM_STATE_FULLSCREEN = XInternAtom(display->display, "_NET_WM_STATE_FULLSCREEN", True);
|
331 |
+
const Atom _NET_WM_STATE = XInternAtom(display->display, "_NET_WM_STATE", True);
|
332 |
+
XEvent e;
|
333 |
+
e.xclient.type = ClientMessage;
|
334 |
+
e.xclient.window = win;
|
335 |
+
e.xclient.message_type = _NET_WM_STATE;
|
336 |
+
e.xclient.format = 32;
|
337 |
+
e.xclient.data.l[0] = (int)true_false; // Toggle
|
338 |
+
e.xclient.data.l[1] = _NET_WM_STATE_FULLSCREEN;
|
339 |
+
e.xclient.data.l[2] = 0;
|
340 |
+
e.xclient.data.l[3] = 1;
|
341 |
+
e.xclient.data.l[4] = 0;
|
342 |
+
|
343 |
+
XSendEvent(display->display, DefaultRootWindow(display->display), False, SubstructureRedirectMask | SubstructureNotifyMask, &e);
|
344 |
+
// XMoveResizeWindow(display->display, win, 0, 0, windowed_size[0], windowed_size[1]);
|
345 |
+
}
|
346 |
+
|
347 |
+
void X11Window::Move(int x, int y)
|
348 |
+
{
|
349 |
+
XMoveWindow(display->display, win, x, y);
|
350 |
+
}
|
351 |
+
|
352 |
+
void X11Window::Resize(unsigned int w, unsigned int h)
|
353 |
+
{
|
354 |
+
XResizeWindow(display->display, win, w, h);
|
355 |
+
}
|
356 |
+
|
357 |
+
KeyModifierBitmask GetEventFlagsFromXState(unsigned int state) {
|
358 |
+
KeyModifierBitmask flags;
|
359 |
+
|
360 |
+
if (state & ShiftMask)
|
361 |
+
flags |= KeyModifierShift;
|
362 |
+
if (state & ControlMask)
|
363 |
+
flags |= KeyModifierCtrl;
|
364 |
+
if (state & Mod1Mask)
|
365 |
+
flags |= KeyModifierAlt;
|
366 |
+
if (state & Mod4Mask)
|
367 |
+
flags |= KeyModifierCmd;
|
368 |
+
if (state & Mod5Mask) // altgr
|
369 |
+
flags |= KeyModifierAlt;
|
370 |
+
// if (state & LockMask) // capslock
|
371 |
+
// if (state & Mod2Mask) // numlock
|
372 |
+
// if (state & Mod3Mask) // mod3
|
373 |
+
// if (state & Button1Mask) // LEFT_MOUSE_BUTTON;
|
374 |
+
// if (state & Button2Mask) // MIDDLE_MOUSE_BUTTON;
|
375 |
+
// if (state & Button3Mask) // RIGHT_MOUSE_BUTTON;
|
376 |
+
return flags;
|
377 |
+
}
|
378 |
+
|
379 |
+
void X11Window::ProcessEvents()
|
380 |
+
{
|
381 |
+
XEvent ev;
|
382 |
+
while(XPending(display->display) > 0)
|
383 |
+
{
|
384 |
+
XNextEvent(display->display, &ev);
|
385 |
+
|
386 |
+
|
387 |
+
switch(ev.type){
|
388 |
+
case ConfigureNotify:
|
389 |
+
ResizeSignal(WindowResizeEvent{ev.xconfigure.width, ev.xconfigure.height});
|
390 |
+
break;
|
391 |
+
case ClientMessage:
|
392 |
+
// We've only registered to receive WM_DELETE_WINDOW, so no further checks needed.
|
393 |
+
CloseSignal();
|
394 |
+
break;
|
395 |
+
case ButtonPress:
|
396 |
+
case ButtonRelease:
|
397 |
+
{
|
398 |
+
const int button = ev.xbutton.button-1;
|
399 |
+
MouseSignal(MouseEvent{
|
400 |
+
(float)ev.xbutton.x, (float)ev.xbutton.y,
|
401 |
+
GetEventFlagsFromXState(ev.xkey.state),
|
402 |
+
button, ev.xbutton.type == ButtonPress
|
403 |
+
});
|
404 |
+
break;
|
405 |
+
}
|
406 |
+
case FocusOut:
|
407 |
+
break;
|
408 |
+
case MotionNotify:
|
409 |
+
if(ev.xmotion.state & (Button1Mask|Button2Mask|Button3Mask) ) {
|
410 |
+
MouseMotionSignal(MouseMotionEvent{
|
411 |
+
(float)ev.xbutton.x, (float)ev.xbutton.y,
|
412 |
+
GetEventFlagsFromXState(ev.xkey.state),
|
413 |
+
});
|
414 |
+
}else{
|
415 |
+
PassiveMouseMotionSignal(MouseMotionEvent{
|
416 |
+
(float)ev.xbutton.x, (float)ev.xbutton.y,
|
417 |
+
GetEventFlagsFromXState(ev.xkey.state)
|
418 |
+
});
|
419 |
+
}
|
420 |
+
break;
|
421 |
+
case KeyPress:
|
422 |
+
case KeyRelease:
|
423 |
+
int key;
|
424 |
+
char ch;
|
425 |
+
KeySym sym;
|
426 |
+
|
427 |
+
if( XLookupString(&ev.xkey,&ch,1,&sym,0) == 0) {
|
428 |
+
switch (sym) {
|
429 |
+
case XK_F1: key = PANGO_SPECIAL + PANGO_KEY_F1 ; break;
|
430 |
+
case XK_F2: key = PANGO_SPECIAL + PANGO_KEY_F2 ; break;
|
431 |
+
case XK_F3: key = PANGO_SPECIAL + PANGO_KEY_F3 ; break;
|
432 |
+
case XK_F4: key = PANGO_SPECIAL + PANGO_KEY_F4 ; break;
|
433 |
+
case XK_F5: key = PANGO_SPECIAL + PANGO_KEY_F5 ; break;
|
434 |
+
case XK_F6: key = PANGO_SPECIAL + PANGO_KEY_F6 ; break;
|
435 |
+
case XK_F7: key = PANGO_SPECIAL + PANGO_KEY_F7 ; break;
|
436 |
+
case XK_F8: key = PANGO_SPECIAL + PANGO_KEY_F8 ; break;
|
437 |
+
case XK_F9: key = PANGO_SPECIAL + PANGO_KEY_F9 ; break;
|
438 |
+
case XK_F10: key = PANGO_SPECIAL + PANGO_KEY_F10 ; break;
|
439 |
+
case XK_F11: key = PANGO_SPECIAL + PANGO_KEY_F11 ; break;
|
440 |
+
case XK_F12: key = PANGO_SPECIAL + PANGO_KEY_F12 ; break;
|
441 |
+
case XK_Left: key = PANGO_SPECIAL + PANGO_KEY_LEFT ; break;
|
442 |
+
case XK_Up: key = PANGO_SPECIAL + PANGO_KEY_UP ; break;
|
443 |
+
case XK_Right: key = PANGO_SPECIAL + PANGO_KEY_RIGHT ; break;
|
444 |
+
case XK_Down: key = PANGO_SPECIAL + PANGO_KEY_DOWN ; break;
|
445 |
+
case XK_Page_Up: key = PANGO_SPECIAL + PANGO_KEY_PAGE_UP ; break;
|
446 |
+
case XK_Page_Down: key = PANGO_SPECIAL + PANGO_KEY_PAGE_DOWN ; break;
|
447 |
+
case XK_Home: key = PANGO_SPECIAL + PANGO_KEY_HOME ; break;
|
448 |
+
case XK_End: key = PANGO_SPECIAL + PANGO_KEY_END ; break;
|
449 |
+
case XK_Insert: key = PANGO_SPECIAL + PANGO_KEY_INSERT ; break;
|
450 |
+
case XK_Shift_L:
|
451 |
+
case XK_Shift_R:
|
452 |
+
case XK_Control_L:
|
453 |
+
case XK_Control_R:
|
454 |
+
case XK_Alt_L:
|
455 |
+
case XK_Alt_R:
|
456 |
+
case XK_Super_L:
|
457 |
+
case XK_Super_R:
|
458 |
+
default:
|
459 |
+
key = -1;
|
460 |
+
break;
|
461 |
+
}
|
462 |
+
}else{
|
463 |
+
key = ch;
|
464 |
+
}
|
465 |
+
|
466 |
+
if(key >=0) {
|
467 |
+
KeyboardSignal(KeyboardEvent{
|
468 |
+
(float)ev.xkey.x, (float)ev.xkey.y,
|
469 |
+
GetEventFlagsFromXState(ev.xkey.state),
|
470 |
+
(unsigned char)key, ev.type == KeyPress
|
471 |
+
});
|
472 |
+
}
|
473 |
+
|
474 |
+
break;
|
475 |
+
}
|
476 |
+
}
|
477 |
+
}
|
478 |
+
|
479 |
+
void X11Window::SwapBuffers() {
|
480 |
+
glXSwapBuffers(display->display, win);
|
481 |
+
}
|
482 |
+
|
483 |
+
std::unique_ptr<WindowInterface> CreateX11WindowAndBind(const std::string& window_title, const int w, const int h, const std::string& display_name, const bool double_buffered, const int sample_buffers, const int samples)
|
484 |
+
{
|
485 |
+
std::shared_ptr<X11Display> newdisplay = std::make_shared<X11Display>(display_name.empty() ? NULL : display_name.c_str() );
|
486 |
+
if (!newdisplay) {
|
487 |
+
throw std::runtime_error("Pangolin X11: Failed to open X display");
|
488 |
+
}
|
489 |
+
::GLXFBConfig newfbc = ChooseFrameBuffer(newdisplay->display, double_buffered, sample_buffers, samples);
|
490 |
+
|
491 |
+
window_mutex.lock();
|
492 |
+
std::shared_ptr<X11GlContext> newglcontext = std::make_shared<X11GlContext>(
|
493 |
+
newdisplay, newfbc, global_gl_context.lock()
|
494 |
+
);
|
495 |
+
|
496 |
+
if(!global_gl_context.lock()) {
|
497 |
+
global_gl_context = newglcontext;
|
498 |
+
}
|
499 |
+
window_mutex.unlock();
|
500 |
+
|
501 |
+
X11Window* win = new X11Window(window_title, w, h, newdisplay, newfbc);
|
502 |
+
win->glcontext = newglcontext;
|
503 |
+
|
504 |
+
return std::unique_ptr<WindowInterface>(win);
|
505 |
+
}
|
506 |
+
|
507 |
+
PANGOLIN_REGISTER_FACTORY(X11Window)
|
508 |
+
{
|
509 |
+
struct X11WindowFactory : public TypedFactoryInterface<WindowInterface> {
|
510 |
+
std::map<std::string,Precedence> Schemes() const override
|
511 |
+
{
|
512 |
+
return {{"x11",10}, {"linux",10}, {"default",100}};
|
513 |
+
}
|
514 |
+
const char* Description() const override
|
515 |
+
{
|
516 |
+
return "Use X11 native window";
|
517 |
+
}
|
518 |
+
ParamSet Params() const override
|
519 |
+
{
|
520 |
+
return {{
|
521 |
+
{"window_title","window","Title of application Window"},
|
522 |
+
{"w","640","Requested window width"},
|
523 |
+
{"h","480","Requested window height"},
|
524 |
+
{"display_name","","The display name to open the window on"},
|
525 |
+
{"double_buffered","true","Whether the window should be double buffered"},
|
526 |
+
{"sample_buffers","1",""},
|
527 |
+
{"samples","1",""},
|
528 |
+
}};
|
529 |
+
}
|
530 |
+
|
531 |
+
std::unique_ptr<WindowInterface> Open(const Uri& uri) override {
|
532 |
+
|
533 |
+
const std::string window_title = uri.Get<std::string>("window_title", "window");
|
534 |
+
const int w = uri.Get<int>("w", 640);
|
535 |
+
const int h = uri.Get<int>("h", 480);
|
536 |
+
const std::string display_name = uri.Get<std::string>("display_name", "");
|
537 |
+
const bool double_buffered = uri.Get<bool>("double_buffered", true);
|
538 |
+
const int sample_buffers = uri.Get<int>("sample_buffers", 1);
|
539 |
+
const int samples = uri.Get<int>("samples", 1);
|
540 |
+
return std::unique_ptr<WindowInterface>(CreateX11WindowAndBind(window_title, w, h, display_name, double_buffered, sample_buffers, samples));
|
541 |
+
}
|
542 |
+
};
|
543 |
+
|
544 |
+
return FactoryRegistry::I()->RegisterFactory<WindowInterface>(std::make_shared<X11WindowFactory>());
|
545 |
+
}
|
546 |
+
|
547 |
+
}
|
548 |
+
|
third-party/DPVO/Pangolin/components/pango_windowing/src/window.cpp
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/windowing/window.h>
|
2 |
+
#include <pangolin/factory/factory_registry.h>
|
3 |
+
#include <pangolin/factory/RegisterFactoriesWindowInterface.h>
|
4 |
+
|
5 |
+
namespace pangolin
|
6 |
+
{
|
7 |
+
|
8 |
+
std::unique_ptr<WindowInterface> ConstructWindow(const Uri& uri)
|
9 |
+
{
|
10 |
+
RegisterFactoriesWindowInterface();
|
11 |
+
return FactoryRegistry::I()->Construct<WindowInterface>(uri);
|
12 |
+
}
|
13 |
+
|
14 |
+
}
|
third-party/DPVO/Pangolin/components/tinyobj/CMakeLists.txt
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
set(COMPONENT tinyobj)
|
2 |
+
|
3 |
+
target_sources( ${COMPONENT} PRIVATE "${CMAKE_CURRENT_LIST_DIR}/src/tinyobj.cpp")
|
4 |
+
set_property(TARGET ${COMPONENT} PROPERTY CXX_STANDARD 11)
|
5 |
+
target_include_directories(${COMPONENT} PUBLIC
|
6 |
+
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
|
7 |
+
$<INSTALL_INTERFACE:include>
|
8 |
+
)
|
9 |
+
install(DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/include"
|
10 |
+
DESTINATION ${CMAKE_INSTALL_PREFIX}
|
11 |
+
)
|
third-party/DPVO/Pangolin/components/tinyobj/include/tinyobj/tiny_obj_loader.h
ADDED
@@ -0,0 +1,2547 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/*
|
2 |
+
The MIT License (MIT)
|
3 |
+
|
4 |
+
Copyright (c) 2012-2018 Syoyo Fujita and many contributors.
|
5 |
+
|
6 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 |
+
of this software and associated documentation files (the "Software"), to deal
|
8 |
+
in the Software without restriction, including without limitation the rights
|
9 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 |
+
copies of the Software, and to permit persons to whom the Software is
|
11 |
+
furnished to do so, subject to the following conditions:
|
12 |
+
|
13 |
+
The above copyright notice and this permission notice shall be included in
|
14 |
+
all copies or substantial portions of the Software.
|
15 |
+
|
16 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 |
+
THE SOFTWARE.
|
23 |
+
*/
|
24 |
+
|
25 |
+
//
|
26 |
+
// version 1.3.1 : Make ParseTextureNameAndOption API public
|
27 |
+
// version 1.3.0 : Separate warning and error message(breaking API of LoadObj)
|
28 |
+
// version 1.2.3 : Added color space extension('-colorspace') to tex opts.
|
29 |
+
// version 1.2.2 : Parse multiple group names.
|
30 |
+
// version 1.2.1 : Added initial support for line('l') primitive(PR #178)
|
31 |
+
// version 1.2.0 : Hardened implementation(#175)
|
32 |
+
// version 1.1.1 : Support smoothing groups(#162)
|
33 |
+
// version 1.1.0 : Support parsing vertex color(#144)
|
34 |
+
// version 1.0.8 : Fix parsing `g` tag just after `usemtl`(#138)
|
35 |
+
// version 1.0.7 : Support multiple tex options(#126)
|
36 |
+
// version 1.0.6 : Add TINYOBJLOADER_USE_DOUBLE option(#124)
|
37 |
+
// version 1.0.5 : Ignore `Tr` when `d` exists in MTL(#43)
|
38 |
+
// version 1.0.4 : Support multiple filenames for 'mtllib'(#112)
|
39 |
+
// version 1.0.3 : Support parsing texture options(#85)
|
40 |
+
// version 1.0.2 : Improve parsing speed by about a factor of 2 for large
|
41 |
+
// files(#105)
|
42 |
+
// version 1.0.1 : Fixes a shape is lost if obj ends with a 'usemtl'(#104)
|
43 |
+
// version 1.0.0 : Change data structure. Change license from BSD to MIT.
|
44 |
+
//
|
45 |
+
|
46 |
+
//
|
47 |
+
// Use this in *one* .cc
|
48 |
+
// #define TINYOBJLOADER_IMPLEMENTATION
|
49 |
+
// #include "tiny_obj_loader.h"
|
50 |
+
//
|
51 |
+
|
52 |
+
#ifndef TINY_OBJ_LOADER_H_
|
53 |
+
#define TINY_OBJ_LOADER_H_
|
54 |
+
|
55 |
+
#include <map>
|
56 |
+
#include <string>
|
57 |
+
#include <vector>
|
58 |
+
|
59 |
+
namespace tinyobj {
|
60 |
+
|
61 |
+
#ifdef __clang__
|
62 |
+
#pragma clang diagnostic push
|
63 |
+
#if __has_warning("-Wzero-as-null-pointer-constant")
|
64 |
+
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
|
65 |
+
#endif
|
66 |
+
|
67 |
+
#pragma clang diagnostic ignored "-Wpadded"
|
68 |
+
|
69 |
+
#endif
|
70 |
+
|
71 |
+
// https://en.wikipedia.org/wiki/Wavefront_.obj_file says ...
|
72 |
+
//
|
73 |
+
// -blendu on | off # set horizontal texture blending
|
74 |
+
// (default on)
|
75 |
+
// -blendv on | off # set vertical texture blending
|
76 |
+
// (default on)
|
77 |
+
// -boost real_value # boost mip-map sharpness
|
78 |
+
// -mm base_value gain_value # modify texture map values (default
|
79 |
+
// 0 1)
|
80 |
+
// # base_value = brightness,
|
81 |
+
// gain_value = contrast
|
82 |
+
// -o u [v [w]] # Origin offset (default
|
83 |
+
// 0 0 0)
|
84 |
+
// -s u [v [w]] # Scale (default
|
85 |
+
// 1 1 1)
|
86 |
+
// -t u [v [w]] # Turbulence (default
|
87 |
+
// 0 0 0)
|
88 |
+
// -texres resolution # texture resolution to create
|
89 |
+
// -clamp on | off # only render texels in the clamped
|
90 |
+
// 0-1 range (default off)
|
91 |
+
// # When unclamped, textures are
|
92 |
+
// repeated across a surface,
|
93 |
+
// # when clamped, only texels which
|
94 |
+
// fall within the 0-1
|
95 |
+
// # range are rendered.
|
96 |
+
// -bm mult_value # bump multiplier (for bump maps
|
97 |
+
// only)
|
98 |
+
//
|
99 |
+
// -imfchan r | g | b | m | l | z # specifies which channel of the file
|
100 |
+
// is used to
|
101 |
+
// # create a scalar or bump texture.
|
102 |
+
// r:red, g:green,
|
103 |
+
// # b:blue, m:matte, l:luminance,
|
104 |
+
// z:z-depth..
|
105 |
+
// # (the default for bump is 'l' and
|
106 |
+
// for decal is 'm')
|
107 |
+
// bump -imfchan r bumpmap.tga # says to use the red channel of
|
108 |
+
// bumpmap.tga as the bumpmap
|
109 |
+
//
|
110 |
+
// For reflection maps...
|
111 |
+
//
|
112 |
+
// -type sphere # specifies a sphere for a "refl"
|
113 |
+
// reflection map
|
114 |
+
// -type cube_top | cube_bottom | # when using a cube map, the texture
|
115 |
+
// file for each
|
116 |
+
// cube_front | cube_back | # side of the cube is specified
|
117 |
+
// separately
|
118 |
+
// cube_left | cube_right
|
119 |
+
//
|
120 |
+
// TinyObjLoader extension.
|
121 |
+
//
|
122 |
+
// -colorspace SPACE # Color space of the texture. e.g.
|
123 |
+
// 'sRGB` or 'linear'
|
124 |
+
//
|
125 |
+
|
126 |
+
#ifdef TINYOBJLOADER_USE_DOUBLE
|
127 |
+
//#pragma message "using double"
|
128 |
+
typedef double real_t;
|
129 |
+
#else
|
130 |
+
//#pragma message "using float"
|
131 |
+
typedef float real_t;
|
132 |
+
#endif
|
133 |
+
|
134 |
+
typedef enum {
|
135 |
+
TEXTURE_TYPE_NONE, // default
|
136 |
+
TEXTURE_TYPE_SPHERE,
|
137 |
+
TEXTURE_TYPE_CUBE_TOP,
|
138 |
+
TEXTURE_TYPE_CUBE_BOTTOM,
|
139 |
+
TEXTURE_TYPE_CUBE_FRONT,
|
140 |
+
TEXTURE_TYPE_CUBE_BACK,
|
141 |
+
TEXTURE_TYPE_CUBE_LEFT,
|
142 |
+
TEXTURE_TYPE_CUBE_RIGHT
|
143 |
+
} texture_type_t;
|
144 |
+
|
145 |
+
typedef struct {
|
146 |
+
texture_type_t type; // -type (default TEXTURE_TYPE_NONE)
|
147 |
+
real_t sharpness; // -boost (default 1.0?)
|
148 |
+
real_t brightness; // base_value in -mm option (default 0)
|
149 |
+
real_t contrast; // gain_value in -mm option (default 1)
|
150 |
+
real_t origin_offset[3]; // -o u [v [w]] (default 0 0 0)
|
151 |
+
real_t scale[3]; // -s u [v [w]] (default 1 1 1)
|
152 |
+
real_t turbulence[3]; // -t u [v [w]] (default 0 0 0)
|
153 |
+
// int texture_resolution; // -texres resolution (default = ?) TODO
|
154 |
+
bool clamp; // -clamp (default false)
|
155 |
+
char imfchan; // -imfchan (the default for bump is 'l' and for decal is 'm')
|
156 |
+
bool blendu; // -blendu (default on)
|
157 |
+
bool blendv; // -blendv (default on)
|
158 |
+
real_t bump_multiplier; // -bm (for bump maps only, default 1.0)
|
159 |
+
|
160 |
+
// extension
|
161 |
+
std::string colorspace; // Explicitly specify color space of stored value.
|
162 |
+
// Usually `sRGB` or `linear` (default empty).
|
163 |
+
} texture_option_t;
|
164 |
+
|
165 |
+
typedef struct {
|
166 |
+
std::string name;
|
167 |
+
|
168 |
+
real_t ambient[3];
|
169 |
+
real_t diffuse[3];
|
170 |
+
real_t specular[3];
|
171 |
+
real_t transmittance[3];
|
172 |
+
real_t emission[3];
|
173 |
+
real_t shininess;
|
174 |
+
real_t ior; // index of refraction
|
175 |
+
real_t dissolve; // 1 == opaque; 0 == fully transparent
|
176 |
+
// illumination model (see http://www.fileformat.info/format/material/)
|
177 |
+
int illum;
|
178 |
+
|
179 |
+
int dummy; // Suppress padding warning.
|
180 |
+
|
181 |
+
std::string ambient_texname; // map_Ka
|
182 |
+
std::string diffuse_texname; // map_Kd
|
183 |
+
std::string specular_texname; // map_Ks
|
184 |
+
std::string specular_highlight_texname; // map_Ns
|
185 |
+
std::string bump_texname; // map_bump, map_Bump, bump
|
186 |
+
std::string displacement_texname; // disp
|
187 |
+
std::string alpha_texname; // map_d
|
188 |
+
std::string reflection_texname; // refl
|
189 |
+
|
190 |
+
texture_option_t ambient_texopt;
|
191 |
+
texture_option_t diffuse_texopt;
|
192 |
+
texture_option_t specular_texopt;
|
193 |
+
texture_option_t specular_highlight_texopt;
|
194 |
+
texture_option_t bump_texopt;
|
195 |
+
texture_option_t displacement_texopt;
|
196 |
+
texture_option_t alpha_texopt;
|
197 |
+
texture_option_t reflection_texopt;
|
198 |
+
|
199 |
+
// PBR extension
|
200 |
+
// http://exocortex.com/blog/extending_wavefront_mtl_to_support_pbr
|
201 |
+
real_t roughness; // [0, 1] default 0
|
202 |
+
real_t metallic; // [0, 1] default 0
|
203 |
+
real_t sheen; // [0, 1] default 0
|
204 |
+
real_t clearcoat_thickness; // [0, 1] default 0
|
205 |
+
real_t clearcoat_roughness; // [0, 1] default 0
|
206 |
+
real_t anisotropy; // aniso. [0, 1] default 0
|
207 |
+
real_t anisotropy_rotation; // anisor. [0, 1] default 0
|
208 |
+
real_t pad0;
|
209 |
+
std::string roughness_texname; // map_Pr
|
210 |
+
std::string metallic_texname; // map_Pm
|
211 |
+
std::string sheen_texname; // map_Ps
|
212 |
+
std::string emissive_texname; // map_Ke
|
213 |
+
std::string normal_texname; // norm. For normal mapping.
|
214 |
+
|
215 |
+
texture_option_t roughness_texopt;
|
216 |
+
texture_option_t metallic_texopt;
|
217 |
+
texture_option_t sheen_texopt;
|
218 |
+
texture_option_t emissive_texopt;
|
219 |
+
texture_option_t normal_texopt;
|
220 |
+
|
221 |
+
int pad2;
|
222 |
+
|
223 |
+
std::map<std::string, std::string> unknown_parameter;
|
224 |
+
} material_t;
|
225 |
+
|
226 |
+
typedef struct {
|
227 |
+
std::string name;
|
228 |
+
|
229 |
+
std::vector<int> intValues;
|
230 |
+
std::vector<real_t> floatValues;
|
231 |
+
std::vector<std::string> stringValues;
|
232 |
+
} tag_t;
|
233 |
+
|
234 |
+
// Index struct to support different indices for vtx/normal/texcoord.
|
235 |
+
// -1 means not used.
|
236 |
+
typedef struct {
|
237 |
+
int vertex_index;
|
238 |
+
int normal_index;
|
239 |
+
int texcoord_index;
|
240 |
+
} index_t;
|
241 |
+
|
242 |
+
typedef struct {
|
243 |
+
std::vector<index_t> indices;
|
244 |
+
std::vector<unsigned char> num_face_vertices; // The number of vertices per
|
245 |
+
// face. 3 = polygon, 4 = quad,
|
246 |
+
// ... Up to 255.
|
247 |
+
std::vector<int> material_ids; // per-face material ID
|
248 |
+
std::vector<unsigned int> smoothing_group_ids; // per-face smoothing group
|
249 |
+
// ID(0 = off. positive value
|
250 |
+
// = group id)
|
251 |
+
std::vector<tag_t> tags; // SubD tag
|
252 |
+
} mesh_t;
|
253 |
+
|
254 |
+
typedef struct {
|
255 |
+
std::vector<int> indices; // pairs of indices for lines
|
256 |
+
} path_t;
|
257 |
+
|
258 |
+
typedef struct {
|
259 |
+
std::string name;
|
260 |
+
mesh_t mesh;
|
261 |
+
path_t path;
|
262 |
+
} shape_t;
|
263 |
+
|
264 |
+
// Vertex attributes
|
265 |
+
typedef struct {
|
266 |
+
std::vector<real_t> vertices; // 'v'
|
267 |
+
std::vector<real_t> normals; // 'vn'
|
268 |
+
std::vector<real_t> texcoords; // 'vt'
|
269 |
+
std::vector<real_t> colors; // extension: vertex colors
|
270 |
+
} attrib_t;
|
271 |
+
|
272 |
+
typedef struct callback_t_ {
|
273 |
+
// W is optional and set to 1 if there is no `w` item in `v` line
|
274 |
+
void (*vertex_cb)(void *user_data, real_t x, real_t y, real_t z, real_t w);
|
275 |
+
void (*normal_cb)(void *user_data, real_t x, real_t y, real_t z);
|
276 |
+
|
277 |
+
// y and z are optional and set to 0 if there is no `y` and/or `z` item(s) in
|
278 |
+
// `vt` line.
|
279 |
+
void (*texcoord_cb)(void *user_data, real_t x, real_t y, real_t z);
|
280 |
+
|
281 |
+
// called per 'f' line. num_indices is the number of face indices(e.g. 3 for
|
282 |
+
// triangle, 4 for quad)
|
283 |
+
// 0 will be passed for undefined index in index_t members.
|
284 |
+
void (*index_cb)(void *user_data, index_t *indices, int num_indices);
|
285 |
+
// `name` material name, `material_id` = the array index of material_t[]. -1
|
286 |
+
// if
|
287 |
+
// a material not found in .mtl
|
288 |
+
void (*usemtl_cb)(void *user_data, const char *name, int material_id);
|
289 |
+
// `materials` = parsed material data.
|
290 |
+
void (*mtllib_cb)(void *user_data, const material_t *materials,
|
291 |
+
int num_materials);
|
292 |
+
// There may be multiple group names
|
293 |
+
void (*group_cb)(void *user_data, const char **names, int num_names);
|
294 |
+
void (*object_cb)(void *user_data, const char *name);
|
295 |
+
|
296 |
+
callback_t_()
|
297 |
+
: vertex_cb(NULL),
|
298 |
+
normal_cb(NULL),
|
299 |
+
texcoord_cb(NULL),
|
300 |
+
index_cb(NULL),
|
301 |
+
usemtl_cb(NULL),
|
302 |
+
mtllib_cb(NULL),
|
303 |
+
group_cb(NULL),
|
304 |
+
object_cb(NULL) {}
|
305 |
+
} callback_t;
|
306 |
+
|
307 |
+
class MaterialReader {
|
308 |
+
public:
|
309 |
+
MaterialReader() {}
|
310 |
+
virtual ~MaterialReader();
|
311 |
+
|
312 |
+
virtual bool operator()(const std::string &matId,
|
313 |
+
std::vector<material_t> *materials,
|
314 |
+
std::map<std::string, int> *matMap, std::string *warn,
|
315 |
+
std::string *err) = 0;
|
316 |
+
};
|
317 |
+
|
318 |
+
class MaterialFileReader : public MaterialReader {
|
319 |
+
public:
|
320 |
+
explicit MaterialFileReader(const std::string &mtl_basedir)
|
321 |
+
: m_mtlBaseDir(mtl_basedir) {}
|
322 |
+
virtual ~MaterialFileReader() {}
|
323 |
+
virtual bool operator()(const std::string &matId,
|
324 |
+
std::vector<material_t> *materials,
|
325 |
+
std::map<std::string, int> *matMap, std::string *warn,
|
326 |
+
std::string *err);
|
327 |
+
|
328 |
+
private:
|
329 |
+
std::string m_mtlBaseDir;
|
330 |
+
};
|
331 |
+
|
332 |
+
class MaterialStreamReader : public MaterialReader {
|
333 |
+
public:
|
334 |
+
explicit MaterialStreamReader(std::istream &inStream)
|
335 |
+
: m_inStream(inStream) {}
|
336 |
+
virtual ~MaterialStreamReader() {}
|
337 |
+
virtual bool operator()(const std::string &matId,
|
338 |
+
std::vector<material_t> *materials,
|
339 |
+
std::map<std::string, int> *matMap, std::string *warn,
|
340 |
+
std::string *err);
|
341 |
+
|
342 |
+
private:
|
343 |
+
std::istream &m_inStream;
|
344 |
+
};
|
345 |
+
|
346 |
+
/// Loads .obj from a file.
|
347 |
+
/// 'attrib', 'shapes' and 'materials' will be filled with parsed shape data
|
348 |
+
/// 'shapes' will be filled with parsed shape data
|
349 |
+
/// Returns true when loading .obj become success.
|
350 |
+
/// Returns warning message into `warn`, and error message into `err`
|
351 |
+
/// 'mtl_basedir' is optional, and used for base directory for .mtl file.
|
352 |
+
/// In default(`NULL'), .mtl file is searched from an application's working
|
353 |
+
/// directory.
|
354 |
+
/// 'triangulate' is optional, and used whether triangulate polygon face in .obj
|
355 |
+
/// or not.
|
356 |
+
/// Option 'default_vcols_fallback' specifies whether vertex colors should
|
357 |
+
/// always be defined, even if no colors are given (fallback to white).
|
358 |
+
bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
359 |
+
std::vector<material_t> *materials, std::string *warn,
|
360 |
+
std::string *err, const char *filename,
|
361 |
+
const char *mtl_basedir = NULL, bool triangulate = true,
|
362 |
+
bool default_vcols_fallback = true);
|
363 |
+
|
364 |
+
/// Loads .obj from a file with custom user callback.
|
365 |
+
/// .mtl is loaded as usual and parsed material_t data will be passed to
|
366 |
+
/// `callback.mtllib_cb`.
|
367 |
+
/// Returns true when loading .obj/.mtl become success.
|
368 |
+
/// Returns warning message into `warn`, and error message into `err`
|
369 |
+
/// See `examples/callback_api/` for how to use this function.
|
370 |
+
bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
|
371 |
+
void *user_data = NULL,
|
372 |
+
MaterialReader *readMatFn = NULL,
|
373 |
+
std::string *warn = NULL, std::string *err = NULL);
|
374 |
+
|
375 |
+
/// Loads object from a std::istream, uses GetMtlIStreamFn to retrieve
|
376 |
+
/// std::istream for materials.
|
377 |
+
/// Returns true when loading .obj become success.
|
378 |
+
/// Returns warning and error message into `err`
|
379 |
+
bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
380 |
+
std::vector<material_t> *materials, std::string *warn,
|
381 |
+
std::string *err, std::istream *inStream,
|
382 |
+
MaterialReader *readMatFn = NULL, bool triangulate = true,
|
383 |
+
bool default_vcols_fallback = true);
|
384 |
+
|
385 |
+
/// Loads materials into std::map
|
386 |
+
void LoadMtl(std::map<std::string, int> *material_map,
|
387 |
+
std::vector<material_t> *materials, std::istream *inStream,
|
388 |
+
std::string *warning, std::string *err);
|
389 |
+
|
390 |
+
///
|
391 |
+
/// Parse texture name and texture option for custom texture parameter through material::unknown_parameter
|
392 |
+
///
|
393 |
+
/// @param[out] texname Parsed texture name
|
394 |
+
/// @param[out] texopt Parsed texopt
|
395 |
+
/// @param[in] linebuf Input string
|
396 |
+
/// @param[in] is_bump Is this texture bump/normal?
|
397 |
+
///
|
398 |
+
bool ParseTextureNameAndOption(std::string *texname,
|
399 |
+
texture_option_t *texopt,
|
400 |
+
const char *linebuf,
|
401 |
+
const bool is_bump);
|
402 |
+
} // namespace tinyobj
|
403 |
+
|
404 |
+
#endif // TINY_OBJ_LOADER_H_
|
405 |
+
|
406 |
+
#ifdef TINYOBJLOADER_IMPLEMENTATION
|
407 |
+
#include <cassert>
|
408 |
+
#include <cctype>
|
409 |
+
#include <cmath>
|
410 |
+
#include <cstddef>
|
411 |
+
#include <cstdlib>
|
412 |
+
#include <cstring>
|
413 |
+
#include <limits>
|
414 |
+
#include <utility>
|
415 |
+
|
416 |
+
#include <fstream>
|
417 |
+
#include <sstream>
|
418 |
+
|
419 |
+
namespace tinyobj {
|
420 |
+
|
421 |
+
MaterialReader::~MaterialReader() {}
|
422 |
+
|
423 |
+
struct vertex_index_t {
|
424 |
+
int v_idx, vt_idx, vn_idx;
|
425 |
+
vertex_index_t() : v_idx(-1), vt_idx(-1), vn_idx(-1) {}
|
426 |
+
explicit vertex_index_t(int idx) : v_idx(idx), vt_idx(idx), vn_idx(idx) {}
|
427 |
+
vertex_index_t(int vidx, int vtidx, int vnidx)
|
428 |
+
: v_idx(vidx), vt_idx(vtidx), vn_idx(vnidx) {}
|
429 |
+
};
|
430 |
+
|
431 |
+
// Internal data structure for face representation
|
432 |
+
// index + smoothing group.
|
433 |
+
struct face_t {
|
434 |
+
unsigned int
|
435 |
+
smoothing_group_id; // smoothing group id. 0 = smoothing groupd is off.
|
436 |
+
int pad_;
|
437 |
+
std::vector<vertex_index_t> vertex_indices; // face vertex indices.
|
438 |
+
|
439 |
+
face_t() : smoothing_group_id(0) {}
|
440 |
+
};
|
441 |
+
|
442 |
+
struct line_t {
|
443 |
+
int idx0;
|
444 |
+
int idx1;
|
445 |
+
};
|
446 |
+
|
447 |
+
struct tag_sizes {
|
448 |
+
tag_sizes() : num_ints(0), num_reals(0), num_strings(0) {}
|
449 |
+
int num_ints;
|
450 |
+
int num_reals;
|
451 |
+
int num_strings;
|
452 |
+
};
|
453 |
+
|
454 |
+
struct obj_shape {
|
455 |
+
std::vector<real_t> v;
|
456 |
+
std::vector<real_t> vn;
|
457 |
+
std::vector<real_t> vt;
|
458 |
+
};
|
459 |
+
|
460 |
+
// See
|
461 |
+
// http://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf
|
462 |
+
static std::istream &safeGetline(std::istream &is, std::string &t) {
|
463 |
+
t.clear();
|
464 |
+
|
465 |
+
// The characters in the stream are read one-by-one using a std::streambuf.
|
466 |
+
// That is faster than reading them one-by-one using the std::istream.
|
467 |
+
// Code that uses streambuf this way must be guarded by a sentry object.
|
468 |
+
// The sentry object performs various tasks,
|
469 |
+
// such as thread synchronization and updating the stream state.
|
470 |
+
|
471 |
+
std::istream::sentry se(is, true);
|
472 |
+
std::streambuf *sb = is.rdbuf();
|
473 |
+
|
474 |
+
if (se) {
|
475 |
+
for (;;) {
|
476 |
+
int c = sb->sbumpc();
|
477 |
+
switch (c) {
|
478 |
+
case '\n':
|
479 |
+
return is;
|
480 |
+
case '\r':
|
481 |
+
if (sb->sgetc() == '\n') sb->sbumpc();
|
482 |
+
return is;
|
483 |
+
case EOF:
|
484 |
+
// Also handle the case when the last line has no line ending
|
485 |
+
if (t.empty()) is.setstate(std::ios::eofbit);
|
486 |
+
return is;
|
487 |
+
default:
|
488 |
+
t += static_cast<char>(c);
|
489 |
+
}
|
490 |
+
}
|
491 |
+
}
|
492 |
+
|
493 |
+
return is;
|
494 |
+
}
|
495 |
+
|
496 |
+
#define IS_SPACE(x) (((x) == ' ') || ((x) == '\t'))
|
497 |
+
#define IS_DIGIT(x) \
|
498 |
+
(static_cast<unsigned int>((x) - '0') < static_cast<unsigned int>(10))
|
499 |
+
#define IS_NEW_LINE(x) (((x) == '\r') || ((x) == '\n') || ((x) == '\0'))
|
500 |
+
|
501 |
+
// Make index zero-base, and also support relative index.
|
502 |
+
static inline bool fixIndex(int idx, int n, int *ret) {
|
503 |
+
if (!ret) {
|
504 |
+
return false;
|
505 |
+
}
|
506 |
+
|
507 |
+
if (idx > 0) {
|
508 |
+
(*ret) = idx - 1;
|
509 |
+
return true;
|
510 |
+
}
|
511 |
+
|
512 |
+
if (idx == 0) {
|
513 |
+
// zero is not allowed according to the spec.
|
514 |
+
return false;
|
515 |
+
}
|
516 |
+
|
517 |
+
if (idx < 0) {
|
518 |
+
(*ret) = n + idx; // negative value = relative
|
519 |
+
return true;
|
520 |
+
}
|
521 |
+
|
522 |
+
return false; // never reach here.
|
523 |
+
}
|
524 |
+
|
525 |
+
static inline std::string parseString(const char **token) {
|
526 |
+
std::string s;
|
527 |
+
(*token) += strspn((*token), " \t");
|
528 |
+
size_t e = strcspn((*token), " \t\r");
|
529 |
+
s = std::string((*token), &(*token)[e]);
|
530 |
+
(*token) += e;
|
531 |
+
return s;
|
532 |
+
}
|
533 |
+
|
534 |
+
static inline int parseInt(const char **token) {
|
535 |
+
(*token) += strspn((*token), " \t");
|
536 |
+
int i = atoi((*token));
|
537 |
+
(*token) += strcspn((*token), " \t\r");
|
538 |
+
return i;
|
539 |
+
}
|
540 |
+
|
541 |
+
// Tries to parse a floating point number located at s.
|
542 |
+
//
|
543 |
+
// s_end should be a location in the string where reading should absolutely
|
544 |
+
// stop. For example at the end of the string, to prevent buffer overflows.
|
545 |
+
//
|
546 |
+
// Parses the following EBNF grammar:
|
547 |
+
// sign = "+" | "-" ;
|
548 |
+
// END = ? anything not in digit ?
|
549 |
+
// digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
|
550 |
+
// integer = [sign] , digit , {digit} ;
|
551 |
+
// decimal = integer , ["." , integer] ;
|
552 |
+
// float = ( decimal , END ) | ( decimal , ("E" | "e") , integer , END ) ;
|
553 |
+
//
|
554 |
+
// Valid strings are for example:
|
555 |
+
// -0 +3.1417e+2 -0.0E-3 1.0324 -1.41 11e2
|
556 |
+
//
|
557 |
+
// If the parsing is a success, result is set to the parsed value and true
|
558 |
+
// is returned.
|
559 |
+
//
|
560 |
+
// The function is greedy and will parse until any of the following happens:
|
561 |
+
// - a non-conforming character is encountered.
|
562 |
+
// - s_end is reached.
|
563 |
+
//
|
564 |
+
// The following situations triggers a failure:
|
565 |
+
// - s >= s_end.
|
566 |
+
// - parse failure.
|
567 |
+
//
|
568 |
+
static bool tryParseDouble(const char *s, const char *s_end, double *result) {
|
569 |
+
if (s >= s_end) {
|
570 |
+
return false;
|
571 |
+
}
|
572 |
+
|
573 |
+
double mantissa = 0.0;
|
574 |
+
// This exponent is base 2 rather than 10.
|
575 |
+
// However the exponent we parse is supposed to be one of ten,
|
576 |
+
// thus we must take care to convert the exponent/and or the
|
577 |
+
// mantissa to a * 2^E, where a is the mantissa and E is the
|
578 |
+
// exponent.
|
579 |
+
// To get the final double we will use ldexp, it requires the
|
580 |
+
// exponent to be in base 2.
|
581 |
+
int exponent = 0;
|
582 |
+
|
583 |
+
// NOTE: THESE MUST BE DECLARED HERE SINCE WE ARE NOT ALLOWED
|
584 |
+
// TO JUMP OVER DEFINITIONS.
|
585 |
+
char sign = '+';
|
586 |
+
char exp_sign = '+';
|
587 |
+
char const *curr = s;
|
588 |
+
|
589 |
+
// How many characters were read in a loop.
|
590 |
+
int read = 0;
|
591 |
+
// Tells whether a loop terminated due to reaching s_end.
|
592 |
+
bool end_not_reached = false;
|
593 |
+
|
594 |
+
/*
|
595 |
+
BEGIN PARSING.
|
596 |
+
*/
|
597 |
+
|
598 |
+
// Find out what sign we've got.
|
599 |
+
if (*curr == '+' || *curr == '-') {
|
600 |
+
sign = *curr;
|
601 |
+
curr++;
|
602 |
+
} else if (IS_DIGIT(*curr)) { /* Pass through. */
|
603 |
+
} else {
|
604 |
+
goto fail;
|
605 |
+
}
|
606 |
+
|
607 |
+
// Read the integer part.
|
608 |
+
end_not_reached = (curr != s_end);
|
609 |
+
while (end_not_reached && IS_DIGIT(*curr)) {
|
610 |
+
mantissa *= 10;
|
611 |
+
mantissa += static_cast<int>(*curr - 0x30);
|
612 |
+
curr++;
|
613 |
+
read++;
|
614 |
+
end_not_reached = (curr != s_end);
|
615 |
+
}
|
616 |
+
|
617 |
+
// We must make sure we actually got something.
|
618 |
+
if (read == 0) goto fail;
|
619 |
+
// We allow numbers of form "#", "###" etc.
|
620 |
+
if (!end_not_reached) goto assemble;
|
621 |
+
|
622 |
+
// Read the decimal part.
|
623 |
+
if (*curr == '.') {
|
624 |
+
curr++;
|
625 |
+
read = 1;
|
626 |
+
end_not_reached = (curr != s_end);
|
627 |
+
while (end_not_reached && IS_DIGIT(*curr)) {
|
628 |
+
static const double pow_lut[] = {
|
629 |
+
1.0, 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001,
|
630 |
+
};
|
631 |
+
const int lut_entries = sizeof pow_lut / sizeof pow_lut[0];
|
632 |
+
|
633 |
+
// NOTE: Don't use powf here, it will absolutely murder precision.
|
634 |
+
mantissa += static_cast<int>(*curr - 0x30) *
|
635 |
+
(read < lut_entries ? pow_lut[read] : std::pow(10.0, -read));
|
636 |
+
read++;
|
637 |
+
curr++;
|
638 |
+
end_not_reached = (curr != s_end);
|
639 |
+
}
|
640 |
+
} else if (*curr == 'e' || *curr == 'E') {
|
641 |
+
} else {
|
642 |
+
goto assemble;
|
643 |
+
}
|
644 |
+
|
645 |
+
if (!end_not_reached) goto assemble;
|
646 |
+
|
647 |
+
// Read the exponent part.
|
648 |
+
if (*curr == 'e' || *curr == 'E') {
|
649 |
+
curr++;
|
650 |
+
// Figure out if a sign is present and if it is.
|
651 |
+
end_not_reached = (curr != s_end);
|
652 |
+
if (end_not_reached && (*curr == '+' || *curr == '-')) {
|
653 |
+
exp_sign = *curr;
|
654 |
+
curr++;
|
655 |
+
} else if (IS_DIGIT(*curr)) { /* Pass through. */
|
656 |
+
} else {
|
657 |
+
// Empty E is not allowed.
|
658 |
+
goto fail;
|
659 |
+
}
|
660 |
+
|
661 |
+
read = 0;
|
662 |
+
end_not_reached = (curr != s_end);
|
663 |
+
while (end_not_reached && IS_DIGIT(*curr)) {
|
664 |
+
exponent *= 10;
|
665 |
+
exponent += static_cast<int>(*curr - 0x30);
|
666 |
+
curr++;
|
667 |
+
read++;
|
668 |
+
end_not_reached = (curr != s_end);
|
669 |
+
}
|
670 |
+
exponent *= (exp_sign == '+' ? 1 : -1);
|
671 |
+
if (read == 0) goto fail;
|
672 |
+
}
|
673 |
+
|
674 |
+
assemble:
|
675 |
+
*result = (sign == '+' ? 1 : -1) *
|
676 |
+
(exponent ? std::ldexp(mantissa * std::pow(5.0, exponent), exponent)
|
677 |
+
: mantissa);
|
678 |
+
return true;
|
679 |
+
fail:
|
680 |
+
return false;
|
681 |
+
}
|
682 |
+
|
683 |
+
static inline real_t parseReal(const char **token, double default_value = 0.0) {
|
684 |
+
(*token) += strspn((*token), " \t");
|
685 |
+
const char *end = (*token) + strcspn((*token), " \t\r");
|
686 |
+
double val = default_value;
|
687 |
+
tryParseDouble((*token), end, &val);
|
688 |
+
real_t f = static_cast<real_t>(val);
|
689 |
+
(*token) = end;
|
690 |
+
return f;
|
691 |
+
}
|
692 |
+
|
693 |
+
static inline bool parseReal(const char **token, real_t *out) {
|
694 |
+
(*token) += strspn((*token), " \t");
|
695 |
+
const char *end = (*token) + strcspn((*token), " \t\r");
|
696 |
+
double val;
|
697 |
+
bool ret = tryParseDouble((*token), end, &val);
|
698 |
+
if (ret) {
|
699 |
+
real_t f = static_cast<real_t>(val);
|
700 |
+
(*out) = f;
|
701 |
+
}
|
702 |
+
(*token) = end;
|
703 |
+
return ret;
|
704 |
+
}
|
705 |
+
|
706 |
+
static inline void parseReal2(real_t *x, real_t *y, const char **token,
|
707 |
+
const double default_x = 0.0,
|
708 |
+
const double default_y = 0.0) {
|
709 |
+
(*x) = parseReal(token, default_x);
|
710 |
+
(*y) = parseReal(token, default_y);
|
711 |
+
}
|
712 |
+
|
713 |
+
static inline void parseReal3(real_t *x, real_t *y, real_t *z,
|
714 |
+
const char **token, const double default_x = 0.0,
|
715 |
+
const double default_y = 0.0,
|
716 |
+
const double default_z = 0.0) {
|
717 |
+
(*x) = parseReal(token, default_x);
|
718 |
+
(*y) = parseReal(token, default_y);
|
719 |
+
(*z) = parseReal(token, default_z);
|
720 |
+
}
|
721 |
+
|
722 |
+
static inline void parseV(real_t *x, real_t *y, real_t *z, real_t *w,
|
723 |
+
const char **token, const double default_x = 0.0,
|
724 |
+
const double default_y = 0.0,
|
725 |
+
const double default_z = 0.0,
|
726 |
+
const double default_w = 1.0) {
|
727 |
+
(*x) = parseReal(token, default_x);
|
728 |
+
(*y) = parseReal(token, default_y);
|
729 |
+
(*z) = parseReal(token, default_z);
|
730 |
+
(*w) = parseReal(token, default_w);
|
731 |
+
}
|
732 |
+
|
733 |
+
// Extension: parse vertex with colors(6 items)
|
734 |
+
static inline bool parseVertexWithColor(real_t *x, real_t *y, real_t *z,
|
735 |
+
real_t *r, real_t *g, real_t *b,
|
736 |
+
const char **token,
|
737 |
+
const double default_x = 0.0,
|
738 |
+
const double default_y = 0.0,
|
739 |
+
const double default_z = 0.0) {
|
740 |
+
(*x) = parseReal(token, default_x);
|
741 |
+
(*y) = parseReal(token, default_y);
|
742 |
+
(*z) = parseReal(token, default_z);
|
743 |
+
|
744 |
+
const bool found_color =
|
745 |
+
parseReal(token, r) && parseReal(token, g) && parseReal(token, b);
|
746 |
+
|
747 |
+
if (!found_color) {
|
748 |
+
(*r) = (*g) = (*b) = 1.0;
|
749 |
+
}
|
750 |
+
|
751 |
+
return found_color;
|
752 |
+
}
|
753 |
+
|
754 |
+
static inline bool parseOnOff(const char **token, bool default_value = true) {
|
755 |
+
(*token) += strspn((*token), " \t");
|
756 |
+
const char *end = (*token) + strcspn((*token), " \t\r");
|
757 |
+
|
758 |
+
bool ret = default_value;
|
759 |
+
if ((0 == strncmp((*token), "on", 2))) {
|
760 |
+
ret = true;
|
761 |
+
} else if ((0 == strncmp((*token), "off", 3))) {
|
762 |
+
ret = false;
|
763 |
+
}
|
764 |
+
|
765 |
+
(*token) = end;
|
766 |
+
return ret;
|
767 |
+
}
|
768 |
+
|
769 |
+
static inline texture_type_t parseTextureType(
|
770 |
+
const char **token, texture_type_t default_value = TEXTURE_TYPE_NONE) {
|
771 |
+
(*token) += strspn((*token), " \t");
|
772 |
+
const char *end = (*token) + strcspn((*token), " \t\r");
|
773 |
+
texture_type_t ty = default_value;
|
774 |
+
|
775 |
+
if ((0 == strncmp((*token), "cube_top", strlen("cube_top")))) {
|
776 |
+
ty = TEXTURE_TYPE_CUBE_TOP;
|
777 |
+
} else if ((0 == strncmp((*token), "cube_bottom", strlen("cube_bottom")))) {
|
778 |
+
ty = TEXTURE_TYPE_CUBE_BOTTOM;
|
779 |
+
} else if ((0 == strncmp((*token), "cube_left", strlen("cube_left")))) {
|
780 |
+
ty = TEXTURE_TYPE_CUBE_LEFT;
|
781 |
+
} else if ((0 == strncmp((*token), "cube_right", strlen("cube_right")))) {
|
782 |
+
ty = TEXTURE_TYPE_CUBE_RIGHT;
|
783 |
+
} else if ((0 == strncmp((*token), "cube_front", strlen("cube_front")))) {
|
784 |
+
ty = TEXTURE_TYPE_CUBE_FRONT;
|
785 |
+
} else if ((0 == strncmp((*token), "cube_back", strlen("cube_back")))) {
|
786 |
+
ty = TEXTURE_TYPE_CUBE_BACK;
|
787 |
+
} else if ((0 == strncmp((*token), "sphere", strlen("sphere")))) {
|
788 |
+
ty = TEXTURE_TYPE_SPHERE;
|
789 |
+
}
|
790 |
+
|
791 |
+
(*token) = end;
|
792 |
+
return ty;
|
793 |
+
}
|
794 |
+
|
795 |
+
static tag_sizes parseTagTriple(const char **token) {
|
796 |
+
tag_sizes ts;
|
797 |
+
|
798 |
+
(*token) += strspn((*token), " \t");
|
799 |
+
ts.num_ints = atoi((*token));
|
800 |
+
(*token) += strcspn((*token), "/ \t\r");
|
801 |
+
if ((*token)[0] != '/') {
|
802 |
+
return ts;
|
803 |
+
}
|
804 |
+
|
805 |
+
(*token)++; // Skip '/'
|
806 |
+
|
807 |
+
(*token) += strspn((*token), " \t");
|
808 |
+
ts.num_reals = atoi((*token));
|
809 |
+
(*token) += strcspn((*token), "/ \t\r");
|
810 |
+
if ((*token)[0] != '/') {
|
811 |
+
return ts;
|
812 |
+
}
|
813 |
+
(*token)++; // Skip '/'
|
814 |
+
|
815 |
+
ts.num_strings = parseInt(token);
|
816 |
+
|
817 |
+
return ts;
|
818 |
+
}
|
819 |
+
|
820 |
+
// Parse triples with index offsets: i, i/j/k, i//k, i/j
|
821 |
+
static bool parseTriple(const char **token, int vsize, int vnsize, int vtsize,
|
822 |
+
vertex_index_t *ret) {
|
823 |
+
if (!ret) {
|
824 |
+
return false;
|
825 |
+
}
|
826 |
+
|
827 |
+
vertex_index_t vi(-1);
|
828 |
+
|
829 |
+
if (!fixIndex(atoi((*token)), vsize, &(vi.v_idx))) {
|
830 |
+
return false;
|
831 |
+
}
|
832 |
+
|
833 |
+
(*token) += strcspn((*token), "/ \t\r");
|
834 |
+
if ((*token)[0] != '/') {
|
835 |
+
(*ret) = vi;
|
836 |
+
return true;
|
837 |
+
}
|
838 |
+
(*token)++;
|
839 |
+
|
840 |
+
// i//k
|
841 |
+
if ((*token)[0] == '/') {
|
842 |
+
(*token)++;
|
843 |
+
if (!fixIndex(atoi((*token)), vnsize, &(vi.vn_idx))) {
|
844 |
+
return false;
|
845 |
+
}
|
846 |
+
(*token) += strcspn((*token), "/ \t\r");
|
847 |
+
(*ret) = vi;
|
848 |
+
return true;
|
849 |
+
}
|
850 |
+
|
851 |
+
// i/j/k or i/j
|
852 |
+
if (!fixIndex(atoi((*token)), vtsize, &(vi.vt_idx))) {
|
853 |
+
return false;
|
854 |
+
}
|
855 |
+
|
856 |
+
(*token) += strcspn((*token), "/ \t\r");
|
857 |
+
if ((*token)[0] != '/') {
|
858 |
+
(*ret) = vi;
|
859 |
+
return true;
|
860 |
+
}
|
861 |
+
|
862 |
+
// i/j/k
|
863 |
+
(*token)++; // skip '/'
|
864 |
+
if (!fixIndex(atoi((*token)), vnsize, &(vi.vn_idx))) {
|
865 |
+
return false;
|
866 |
+
}
|
867 |
+
(*token) += strcspn((*token), "/ \t\r");
|
868 |
+
|
869 |
+
(*ret) = vi;
|
870 |
+
|
871 |
+
return true;
|
872 |
+
}
|
873 |
+
|
874 |
+
// Parse raw triples: i, i/j/k, i//k, i/j
|
875 |
+
static vertex_index_t parseRawTriple(const char **token) {
|
876 |
+
vertex_index_t vi(static_cast<int>(0)); // 0 is an invalid index in OBJ
|
877 |
+
|
878 |
+
vi.v_idx = atoi((*token));
|
879 |
+
(*token) += strcspn((*token), "/ \t\r");
|
880 |
+
if ((*token)[0] != '/') {
|
881 |
+
return vi;
|
882 |
+
}
|
883 |
+
(*token)++;
|
884 |
+
|
885 |
+
// i//k
|
886 |
+
if ((*token)[0] == '/') {
|
887 |
+
(*token)++;
|
888 |
+
vi.vn_idx = atoi((*token));
|
889 |
+
(*token) += strcspn((*token), "/ \t\r");
|
890 |
+
return vi;
|
891 |
+
}
|
892 |
+
|
893 |
+
// i/j/k or i/j
|
894 |
+
vi.vt_idx = atoi((*token));
|
895 |
+
(*token) += strcspn((*token), "/ \t\r");
|
896 |
+
if ((*token)[0] != '/') {
|
897 |
+
return vi;
|
898 |
+
}
|
899 |
+
|
900 |
+
// i/j/k
|
901 |
+
(*token)++; // skip '/'
|
902 |
+
vi.vn_idx = atoi((*token));
|
903 |
+
(*token) += strcspn((*token), "/ \t\r");
|
904 |
+
return vi;
|
905 |
+
}
|
906 |
+
|
907 |
+
bool ParseTextureNameAndOption(std::string *texname,
|
908 |
+
texture_option_t *texopt,
|
909 |
+
const char *linebuf, const bool is_bump) {
|
910 |
+
// @todo { write more robust lexer and parser. }
|
911 |
+
bool found_texname = false;
|
912 |
+
std::string texture_name;
|
913 |
+
|
914 |
+
// Fill with default value for texopt.
|
915 |
+
if (is_bump) {
|
916 |
+
texopt->imfchan = 'l';
|
917 |
+
} else {
|
918 |
+
texopt->imfchan = 'm';
|
919 |
+
}
|
920 |
+
texopt->bump_multiplier = static_cast<real_t>(1.0);
|
921 |
+
texopt->clamp = false;
|
922 |
+
texopt->blendu = true;
|
923 |
+
texopt->blendv = true;
|
924 |
+
texopt->sharpness = static_cast<real_t>(1.0);
|
925 |
+
texopt->brightness = static_cast<real_t>(0.0);
|
926 |
+
texopt->contrast = static_cast<real_t>(1.0);
|
927 |
+
texopt->origin_offset[0] = static_cast<real_t>(0.0);
|
928 |
+
texopt->origin_offset[1] = static_cast<real_t>(0.0);
|
929 |
+
texopt->origin_offset[2] = static_cast<real_t>(0.0);
|
930 |
+
texopt->scale[0] = static_cast<real_t>(1.0);
|
931 |
+
texopt->scale[1] = static_cast<real_t>(1.0);
|
932 |
+
texopt->scale[2] = static_cast<real_t>(1.0);
|
933 |
+
texopt->turbulence[0] = static_cast<real_t>(0.0);
|
934 |
+
texopt->turbulence[1] = static_cast<real_t>(0.0);
|
935 |
+
texopt->turbulence[2] = static_cast<real_t>(0.0);
|
936 |
+
texopt->type = TEXTURE_TYPE_NONE;
|
937 |
+
|
938 |
+
const char *token = linebuf; // Assume line ends with NULL
|
939 |
+
|
940 |
+
while (!IS_NEW_LINE((*token))) {
|
941 |
+
token += strspn(token, " \t"); // skip space
|
942 |
+
if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) {
|
943 |
+
token += 8;
|
944 |
+
texopt->blendu = parseOnOff(&token, /* default */ true);
|
945 |
+
} else if ((0 == strncmp(token, "-blendv", 7)) && IS_SPACE((token[7]))) {
|
946 |
+
token += 8;
|
947 |
+
texopt->blendv = parseOnOff(&token, /* default */ true);
|
948 |
+
} else if ((0 == strncmp(token, "-clamp", 6)) && IS_SPACE((token[6]))) {
|
949 |
+
token += 7;
|
950 |
+
texopt->clamp = parseOnOff(&token, /* default */ true);
|
951 |
+
} else if ((0 == strncmp(token, "-boost", 6)) && IS_SPACE((token[6]))) {
|
952 |
+
token += 7;
|
953 |
+
texopt->sharpness = parseReal(&token, 1.0);
|
954 |
+
} else if ((0 == strncmp(token, "-bm", 3)) && IS_SPACE((token[3]))) {
|
955 |
+
token += 4;
|
956 |
+
texopt->bump_multiplier = parseReal(&token, 1.0);
|
957 |
+
} else if ((0 == strncmp(token, "-o", 2)) && IS_SPACE((token[2]))) {
|
958 |
+
token += 3;
|
959 |
+
parseReal3(&(texopt->origin_offset[0]), &(texopt->origin_offset[1]),
|
960 |
+
&(texopt->origin_offset[2]), &token);
|
961 |
+
} else if ((0 == strncmp(token, "-s", 2)) && IS_SPACE((token[2]))) {
|
962 |
+
token += 3;
|
963 |
+
parseReal3(&(texopt->scale[0]), &(texopt->scale[1]), &(texopt->scale[2]),
|
964 |
+
&token, 1.0, 1.0, 1.0);
|
965 |
+
} else if ((0 == strncmp(token, "-t", 2)) && IS_SPACE((token[2]))) {
|
966 |
+
token += 3;
|
967 |
+
parseReal3(&(texopt->turbulence[0]), &(texopt->turbulence[1]),
|
968 |
+
&(texopt->turbulence[2]), &token);
|
969 |
+
} else if ((0 == strncmp(token, "-type", 5)) && IS_SPACE((token[5]))) {
|
970 |
+
token += 5;
|
971 |
+
texopt->type = parseTextureType((&token), TEXTURE_TYPE_NONE);
|
972 |
+
} else if ((0 == strncmp(token, "-imfchan", 8)) && IS_SPACE((token[8]))) {
|
973 |
+
token += 9;
|
974 |
+
token += strspn(token, " \t");
|
975 |
+
const char *end = token + strcspn(token, " \t\r");
|
976 |
+
if ((end - token) == 1) { // Assume one char for -imfchan
|
977 |
+
texopt->imfchan = (*token);
|
978 |
+
}
|
979 |
+
token = end;
|
980 |
+
} else if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) {
|
981 |
+
token += 4;
|
982 |
+
parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0);
|
983 |
+
} else if ((0 == strncmp(token, "-colorspace", 11)) &&
|
984 |
+
IS_SPACE((token[11]))) {
|
985 |
+
token += 12;
|
986 |
+
texopt->colorspace = parseString(&token);
|
987 |
+
} else {
|
988 |
+
// Assume texture filename
|
989 |
+
#if 0
|
990 |
+
size_t len = strcspn(token, " \t\r"); // untile next space
|
991 |
+
texture_name = std::string(token, token + len);
|
992 |
+
token += len;
|
993 |
+
|
994 |
+
token += strspn(token, " \t"); // skip space
|
995 |
+
#else
|
996 |
+
// Read filename until line end to parse filename containing whitespace
|
997 |
+
// TODO(syoyo): Support parsing texture option flag after the filename.
|
998 |
+
texture_name = std::string(token);
|
999 |
+
token += texture_name.length();
|
1000 |
+
#endif
|
1001 |
+
|
1002 |
+
found_texname = true;
|
1003 |
+
}
|
1004 |
+
}
|
1005 |
+
|
1006 |
+
if (found_texname) {
|
1007 |
+
(*texname) = texture_name;
|
1008 |
+
return true;
|
1009 |
+
} else {
|
1010 |
+
return false;
|
1011 |
+
}
|
1012 |
+
}
|
1013 |
+
|
1014 |
+
static void InitMaterial(material_t *material) {
|
1015 |
+
material->name = "";
|
1016 |
+
material->ambient_texname = "";
|
1017 |
+
material->diffuse_texname = "";
|
1018 |
+
material->specular_texname = "";
|
1019 |
+
material->specular_highlight_texname = "";
|
1020 |
+
material->bump_texname = "";
|
1021 |
+
material->displacement_texname = "";
|
1022 |
+
material->reflection_texname = "";
|
1023 |
+
material->alpha_texname = "";
|
1024 |
+
for (int i = 0; i < 3; i++) {
|
1025 |
+
material->ambient[i] = static_cast<real_t>(0.0);
|
1026 |
+
material->diffuse[i] = static_cast<real_t>(0.0);
|
1027 |
+
material->specular[i] = static_cast<real_t>(0.0);
|
1028 |
+
material->transmittance[i] = static_cast<real_t>(0.0);
|
1029 |
+
material->emission[i] = static_cast<real_t>(0.0);
|
1030 |
+
}
|
1031 |
+
material->illum = 0;
|
1032 |
+
material->dissolve = static_cast<real_t>(1.0);
|
1033 |
+
material->shininess = static_cast<real_t>(1.0);
|
1034 |
+
material->ior = static_cast<real_t>(1.0);
|
1035 |
+
|
1036 |
+
material->roughness = static_cast<real_t>(0.0);
|
1037 |
+
material->metallic = static_cast<real_t>(0.0);
|
1038 |
+
material->sheen = static_cast<real_t>(0.0);
|
1039 |
+
material->clearcoat_thickness = static_cast<real_t>(0.0);
|
1040 |
+
material->clearcoat_roughness = static_cast<real_t>(0.0);
|
1041 |
+
material->anisotropy_rotation = static_cast<real_t>(0.0);
|
1042 |
+
material->anisotropy = static_cast<real_t>(0.0);
|
1043 |
+
material->roughness_texname = "";
|
1044 |
+
material->metallic_texname = "";
|
1045 |
+
material->sheen_texname = "";
|
1046 |
+
material->emissive_texname = "";
|
1047 |
+
material->normal_texname = "";
|
1048 |
+
|
1049 |
+
material->unknown_parameter.clear();
|
1050 |
+
}
|
1051 |
+
|
1052 |
+
// code from https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html
|
1053 |
+
template <typename T>
|
1054 |
+
static int pnpoly(int nvert, T *vertx, T *verty, T testx, T testy) {
|
1055 |
+
int i, j, c = 0;
|
1056 |
+
for (i = 0, j = nvert - 1; i < nvert; j = i++) {
|
1057 |
+
if (((verty[i] > testy) != (verty[j] > testy)) &&
|
1058 |
+
(testx <
|
1059 |
+
(vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) +
|
1060 |
+
vertx[i]))
|
1061 |
+
c = !c;
|
1062 |
+
}
|
1063 |
+
return c;
|
1064 |
+
}
|
1065 |
+
|
1066 |
+
// TODO(syoyo): refactor function.
|
1067 |
+
static bool exportGroupsToShape(shape_t *shape,
|
1068 |
+
const std::vector<face_t> &faceGroup,
|
1069 |
+
std::vector<int> &lineGroup,
|
1070 |
+
const std::vector<tag_t> &tags,
|
1071 |
+
const int material_id, const std::string &name,
|
1072 |
+
bool triangulate,
|
1073 |
+
const std::vector<real_t> &v) {
|
1074 |
+
if (faceGroup.empty() && lineGroup.empty()) {
|
1075 |
+
return false;
|
1076 |
+
}
|
1077 |
+
|
1078 |
+
if (!faceGroup.empty()) {
|
1079 |
+
// Flatten vertices and indices
|
1080 |
+
for (size_t i = 0; i < faceGroup.size(); i++) {
|
1081 |
+
const face_t &face = faceGroup[i];
|
1082 |
+
|
1083 |
+
size_t npolys = face.vertex_indices.size();
|
1084 |
+
|
1085 |
+
if (npolys < 3) {
|
1086 |
+
// Face must have 3+ vertices.
|
1087 |
+
continue;
|
1088 |
+
}
|
1089 |
+
|
1090 |
+
vertex_index_t i0 = face.vertex_indices[0];
|
1091 |
+
vertex_index_t i1(-1);
|
1092 |
+
vertex_index_t i2 = face.vertex_indices[1];
|
1093 |
+
|
1094 |
+
if (triangulate) {
|
1095 |
+
// find the two axes to work in
|
1096 |
+
size_t axes[2] = {1, 2};
|
1097 |
+
for (size_t k = 0; k < npolys; ++k) {
|
1098 |
+
i0 = face.vertex_indices[(k + 0) % npolys];
|
1099 |
+
i1 = face.vertex_indices[(k + 1) % npolys];
|
1100 |
+
i2 = face.vertex_indices[(k + 2) % npolys];
|
1101 |
+
size_t vi0 = size_t(i0.v_idx);
|
1102 |
+
size_t vi1 = size_t(i1.v_idx);
|
1103 |
+
size_t vi2 = size_t(i2.v_idx);
|
1104 |
+
|
1105 |
+
if (((3 * vi0 + 2) >= v.size()) || ((3 * vi1 + 2) >= v.size()) ||
|
1106 |
+
((3 * vi2 + 2) >= v.size())) {
|
1107 |
+
// Invalid triangle.
|
1108 |
+
// FIXME(syoyo): Is it ok to simply skip this invalid triangle?
|
1109 |
+
continue;
|
1110 |
+
}
|
1111 |
+
real_t v0x = v[vi0 * 3 + 0];
|
1112 |
+
real_t v0y = v[vi0 * 3 + 1];
|
1113 |
+
real_t v0z = v[vi0 * 3 + 2];
|
1114 |
+
real_t v1x = v[vi1 * 3 + 0];
|
1115 |
+
real_t v1y = v[vi1 * 3 + 1];
|
1116 |
+
real_t v1z = v[vi1 * 3 + 2];
|
1117 |
+
real_t v2x = v[vi2 * 3 + 0];
|
1118 |
+
real_t v2y = v[vi2 * 3 + 1];
|
1119 |
+
real_t v2z = v[vi2 * 3 + 2];
|
1120 |
+
real_t e0x = v1x - v0x;
|
1121 |
+
real_t e0y = v1y - v0y;
|
1122 |
+
real_t e0z = v1z - v0z;
|
1123 |
+
real_t e1x = v2x - v1x;
|
1124 |
+
real_t e1y = v2y - v1y;
|
1125 |
+
real_t e1z = v2z - v1z;
|
1126 |
+
real_t cx = std::fabs(e0y * e1z - e0z * e1y);
|
1127 |
+
real_t cy = std::fabs(e0z * e1x - e0x * e1z);
|
1128 |
+
real_t cz = std::fabs(e0x * e1y - e0y * e1x);
|
1129 |
+
const real_t epsilon = std::numeric_limits<real_t>::epsilon();
|
1130 |
+
if (cx > epsilon || cy > epsilon || cz > epsilon) {
|
1131 |
+
// found a corner
|
1132 |
+
if (cx > cy && cx > cz) {
|
1133 |
+
} else {
|
1134 |
+
axes[0] = 0;
|
1135 |
+
if (cz > cx && cz > cy) axes[1] = 1;
|
1136 |
+
}
|
1137 |
+
break;
|
1138 |
+
}
|
1139 |
+
}
|
1140 |
+
|
1141 |
+
real_t area = 0;
|
1142 |
+
for (size_t k = 0; k < npolys; ++k) {
|
1143 |
+
i0 = face.vertex_indices[(k + 0) % npolys];
|
1144 |
+
i1 = face.vertex_indices[(k + 1) % npolys];
|
1145 |
+
size_t vi0 = size_t(i0.v_idx);
|
1146 |
+
size_t vi1 = size_t(i1.v_idx);
|
1147 |
+
if (((vi0 * 3 + axes[0]) >= v.size()) ||
|
1148 |
+
((vi0 * 3 + axes[1]) >= v.size()) ||
|
1149 |
+
((vi1 * 3 + axes[0]) >= v.size()) ||
|
1150 |
+
((vi1 * 3 + axes[1]) >= v.size())) {
|
1151 |
+
// Invalid index.
|
1152 |
+
continue;
|
1153 |
+
}
|
1154 |
+
real_t v0x = v[vi0 * 3 + axes[0]];
|
1155 |
+
real_t v0y = v[vi0 * 3 + axes[1]];
|
1156 |
+
real_t v1x = v[vi1 * 3 + axes[0]];
|
1157 |
+
real_t v1y = v[vi1 * 3 + axes[1]];
|
1158 |
+
area += (v0x * v1y - v0y * v1x) * static_cast<real_t>(0.5);
|
1159 |
+
}
|
1160 |
+
|
1161 |
+
int maxRounds = 10; // arbitrary max loop count to protect against
|
1162 |
+
// unexpected errors
|
1163 |
+
|
1164 |
+
face_t remainingFace = face; // copy
|
1165 |
+
size_t guess_vert = 0;
|
1166 |
+
vertex_index_t ind[3];
|
1167 |
+
real_t vx[3];
|
1168 |
+
real_t vy[3];
|
1169 |
+
while (remainingFace.vertex_indices.size() > 3 && maxRounds > 0) {
|
1170 |
+
npolys = remainingFace.vertex_indices.size();
|
1171 |
+
if (guess_vert >= npolys) {
|
1172 |
+
maxRounds -= 1;
|
1173 |
+
guess_vert -= npolys;
|
1174 |
+
}
|
1175 |
+
for (size_t k = 0; k < 3; k++) {
|
1176 |
+
ind[k] = remainingFace.vertex_indices[(guess_vert + k) % npolys];
|
1177 |
+
size_t vi = size_t(ind[k].v_idx);
|
1178 |
+
if (((vi * 3 + axes[0]) >= v.size()) ||
|
1179 |
+
((vi * 3 + axes[1]) >= v.size())) {
|
1180 |
+
// ???
|
1181 |
+
vx[k] = static_cast<real_t>(0.0);
|
1182 |
+
vy[k] = static_cast<real_t>(0.0);
|
1183 |
+
} else {
|
1184 |
+
vx[k] = v[vi * 3 + axes[0]];
|
1185 |
+
vy[k] = v[vi * 3 + axes[1]];
|
1186 |
+
}
|
1187 |
+
}
|
1188 |
+
real_t e0x = vx[1] - vx[0];
|
1189 |
+
real_t e0y = vy[1] - vy[0];
|
1190 |
+
real_t e1x = vx[2] - vx[1];
|
1191 |
+
real_t e1y = vy[2] - vy[1];
|
1192 |
+
real_t cross = e0x * e1y - e0y * e1x;
|
1193 |
+
// if an internal angle
|
1194 |
+
if (cross * area < static_cast<real_t>(0.0)) {
|
1195 |
+
guess_vert += 1;
|
1196 |
+
continue;
|
1197 |
+
}
|
1198 |
+
|
1199 |
+
// check all other verts in case they are inside this triangle
|
1200 |
+
bool overlap = false;
|
1201 |
+
for (size_t otherVert = 3; otherVert < npolys; ++otherVert) {
|
1202 |
+
size_t idx = (guess_vert + otherVert) % npolys;
|
1203 |
+
|
1204 |
+
if (idx >= remainingFace.vertex_indices.size()) {
|
1205 |
+
// ???
|
1206 |
+
continue;
|
1207 |
+
}
|
1208 |
+
|
1209 |
+
size_t ovi = size_t(remainingFace.vertex_indices[idx].v_idx);
|
1210 |
+
|
1211 |
+
if (((ovi * 3 + axes[0]) >= v.size()) ||
|
1212 |
+
((ovi * 3 + axes[1]) >= v.size())) {
|
1213 |
+
// ???
|
1214 |
+
continue;
|
1215 |
+
}
|
1216 |
+
real_t tx = v[ovi * 3 + axes[0]];
|
1217 |
+
real_t ty = v[ovi * 3 + axes[1]];
|
1218 |
+
if (pnpoly(3, vx, vy, tx, ty)) {
|
1219 |
+
overlap = true;
|
1220 |
+
break;
|
1221 |
+
}
|
1222 |
+
}
|
1223 |
+
|
1224 |
+
if (overlap) {
|
1225 |
+
guess_vert += 1;
|
1226 |
+
continue;
|
1227 |
+
}
|
1228 |
+
|
1229 |
+
// this triangle is an ear
|
1230 |
+
{
|
1231 |
+
index_t idx0, idx1, idx2;
|
1232 |
+
idx0.vertex_index = ind[0].v_idx;
|
1233 |
+
idx0.normal_index = ind[0].vn_idx;
|
1234 |
+
idx0.texcoord_index = ind[0].vt_idx;
|
1235 |
+
idx1.vertex_index = ind[1].v_idx;
|
1236 |
+
idx1.normal_index = ind[1].vn_idx;
|
1237 |
+
idx1.texcoord_index = ind[1].vt_idx;
|
1238 |
+
idx2.vertex_index = ind[2].v_idx;
|
1239 |
+
idx2.normal_index = ind[2].vn_idx;
|
1240 |
+
idx2.texcoord_index = ind[2].vt_idx;
|
1241 |
+
|
1242 |
+
shape->mesh.indices.push_back(idx0);
|
1243 |
+
shape->mesh.indices.push_back(idx1);
|
1244 |
+
shape->mesh.indices.push_back(idx2);
|
1245 |
+
|
1246 |
+
shape->mesh.num_face_vertices.push_back(3);
|
1247 |
+
shape->mesh.material_ids.push_back(material_id);
|
1248 |
+
shape->mesh.smoothing_group_ids.push_back(face.smoothing_group_id);
|
1249 |
+
}
|
1250 |
+
|
1251 |
+
// remove v1 from the list
|
1252 |
+
size_t removed_vert_index = (guess_vert + 1) % npolys;
|
1253 |
+
while (removed_vert_index + 1 < npolys) {
|
1254 |
+
remainingFace.vertex_indices[removed_vert_index] =
|
1255 |
+
remainingFace.vertex_indices[removed_vert_index + 1];
|
1256 |
+
removed_vert_index += 1;
|
1257 |
+
}
|
1258 |
+
remainingFace.vertex_indices.pop_back();
|
1259 |
+
}
|
1260 |
+
|
1261 |
+
if (remainingFace.vertex_indices.size() == 3) {
|
1262 |
+
i0 = remainingFace.vertex_indices[0];
|
1263 |
+
i1 = remainingFace.vertex_indices[1];
|
1264 |
+
i2 = remainingFace.vertex_indices[2];
|
1265 |
+
{
|
1266 |
+
index_t idx0, idx1, idx2;
|
1267 |
+
idx0.vertex_index = i0.v_idx;
|
1268 |
+
idx0.normal_index = i0.vn_idx;
|
1269 |
+
idx0.texcoord_index = i0.vt_idx;
|
1270 |
+
idx1.vertex_index = i1.v_idx;
|
1271 |
+
idx1.normal_index = i1.vn_idx;
|
1272 |
+
idx1.texcoord_index = i1.vt_idx;
|
1273 |
+
idx2.vertex_index = i2.v_idx;
|
1274 |
+
idx2.normal_index = i2.vn_idx;
|
1275 |
+
idx2.texcoord_index = i2.vt_idx;
|
1276 |
+
|
1277 |
+
shape->mesh.indices.push_back(idx0);
|
1278 |
+
shape->mesh.indices.push_back(idx1);
|
1279 |
+
shape->mesh.indices.push_back(idx2);
|
1280 |
+
|
1281 |
+
shape->mesh.num_face_vertices.push_back(3);
|
1282 |
+
shape->mesh.material_ids.push_back(material_id);
|
1283 |
+
shape->mesh.smoothing_group_ids.push_back(face.smoothing_group_id);
|
1284 |
+
}
|
1285 |
+
}
|
1286 |
+
} else {
|
1287 |
+
for (size_t k = 0; k < npolys; k++) {
|
1288 |
+
index_t idx;
|
1289 |
+
idx.vertex_index = face.vertex_indices[k].v_idx;
|
1290 |
+
idx.normal_index = face.vertex_indices[k].vn_idx;
|
1291 |
+
idx.texcoord_index = face.vertex_indices[k].vt_idx;
|
1292 |
+
shape->mesh.indices.push_back(idx);
|
1293 |
+
}
|
1294 |
+
|
1295 |
+
shape->mesh.num_face_vertices.push_back(
|
1296 |
+
static_cast<unsigned char>(npolys));
|
1297 |
+
shape->mesh.material_ids.push_back(material_id); // per face
|
1298 |
+
shape->mesh.smoothing_group_ids.push_back(
|
1299 |
+
face.smoothing_group_id); // per face
|
1300 |
+
}
|
1301 |
+
}
|
1302 |
+
|
1303 |
+
shape->name = name;
|
1304 |
+
shape->mesh.tags = tags;
|
1305 |
+
}
|
1306 |
+
|
1307 |
+
if (!lineGroup.empty()) {
|
1308 |
+
shape->path.indices.swap(lineGroup);
|
1309 |
+
}
|
1310 |
+
|
1311 |
+
return true;
|
1312 |
+
}
|
1313 |
+
|
1314 |
+
// Split a string with specified delimiter character.
|
1315 |
+
// http://stackoverflow.com/questions/236129/split-a-string-in-c
|
1316 |
+
static void SplitString(const std::string &s, char delim,
|
1317 |
+
std::vector<std::string> &elems) {
|
1318 |
+
std::stringstream ss;
|
1319 |
+
ss.str(s);
|
1320 |
+
std::string item;
|
1321 |
+
while (std::getline(ss, item, delim)) {
|
1322 |
+
elems.push_back(item);
|
1323 |
+
}
|
1324 |
+
}
|
1325 |
+
|
1326 |
+
void LoadMtl(std::map<std::string, int> *material_map,
|
1327 |
+
std::vector<material_t> *materials, std::istream *inStream,
|
1328 |
+
std::string *warning, std::string *err) {
|
1329 |
+
(void)err;
|
1330 |
+
|
1331 |
+
// Create a default material anyway.
|
1332 |
+
material_t material;
|
1333 |
+
InitMaterial(&material);
|
1334 |
+
|
1335 |
+
// Issue 43. `d` wins against `Tr` since `Tr` is not in the MTL specification.
|
1336 |
+
bool has_d = false;
|
1337 |
+
bool has_tr = false;
|
1338 |
+
|
1339 |
+
std::stringstream warn_ss;
|
1340 |
+
|
1341 |
+
size_t line_no = 0;
|
1342 |
+
std::string linebuf;
|
1343 |
+
while (inStream->peek() != -1) {
|
1344 |
+
safeGetline(*inStream, linebuf);
|
1345 |
+
line_no++;
|
1346 |
+
|
1347 |
+
// Trim trailing whitespace.
|
1348 |
+
if (linebuf.size() > 0) {
|
1349 |
+
linebuf = linebuf.substr(0, linebuf.find_last_not_of(" \t") + 1);
|
1350 |
+
}
|
1351 |
+
|
1352 |
+
// Trim newline '\r\n' or '\n'
|
1353 |
+
if (linebuf.size() > 0) {
|
1354 |
+
if (linebuf[linebuf.size() - 1] == '\n')
|
1355 |
+
linebuf.erase(linebuf.size() - 1);
|
1356 |
+
}
|
1357 |
+
if (linebuf.size() > 0) {
|
1358 |
+
if (linebuf[linebuf.size() - 1] == '\r')
|
1359 |
+
linebuf.erase(linebuf.size() - 1);
|
1360 |
+
}
|
1361 |
+
|
1362 |
+
// Skip if empty line.
|
1363 |
+
if (linebuf.empty()) {
|
1364 |
+
continue;
|
1365 |
+
}
|
1366 |
+
|
1367 |
+
// Skip leading space.
|
1368 |
+
const char *token = linebuf.c_str();
|
1369 |
+
token += strspn(token, " \t");
|
1370 |
+
|
1371 |
+
assert(token);
|
1372 |
+
if (token[0] == '\0') continue; // empty line
|
1373 |
+
|
1374 |
+
if (token[0] == '#') continue; // comment line
|
1375 |
+
|
1376 |
+
// new mtl
|
1377 |
+
if ((0 == strncmp(token, "newmtl", 6)) && IS_SPACE((token[6]))) {
|
1378 |
+
// flush previous material.
|
1379 |
+
if (!material.name.empty()) {
|
1380 |
+
material_map->insert(std::pair<std::string, int>(
|
1381 |
+
material.name, static_cast<int>(materials->size())));
|
1382 |
+
materials->push_back(material);
|
1383 |
+
}
|
1384 |
+
|
1385 |
+
// initial temporary material
|
1386 |
+
InitMaterial(&material);
|
1387 |
+
|
1388 |
+
has_d = false;
|
1389 |
+
has_tr = false;
|
1390 |
+
|
1391 |
+
// set new mtl name
|
1392 |
+
token += 7;
|
1393 |
+
{
|
1394 |
+
std::stringstream sstr;
|
1395 |
+
sstr << token;
|
1396 |
+
material.name = sstr.str();
|
1397 |
+
}
|
1398 |
+
continue;
|
1399 |
+
}
|
1400 |
+
|
1401 |
+
// ambient
|
1402 |
+
if (token[0] == 'K' && token[1] == 'a' && IS_SPACE((token[2]))) {
|
1403 |
+
token += 2;
|
1404 |
+
real_t r, g, b;
|
1405 |
+
parseReal3(&r, &g, &b, &token);
|
1406 |
+
material.ambient[0] = r;
|
1407 |
+
material.ambient[1] = g;
|
1408 |
+
material.ambient[2] = b;
|
1409 |
+
continue;
|
1410 |
+
}
|
1411 |
+
|
1412 |
+
// diffuse
|
1413 |
+
if (token[0] == 'K' && token[1] == 'd' && IS_SPACE((token[2]))) {
|
1414 |
+
token += 2;
|
1415 |
+
real_t r, g, b;
|
1416 |
+
parseReal3(&r, &g, &b, &token);
|
1417 |
+
material.diffuse[0] = r;
|
1418 |
+
material.diffuse[1] = g;
|
1419 |
+
material.diffuse[2] = b;
|
1420 |
+
continue;
|
1421 |
+
}
|
1422 |
+
|
1423 |
+
// specular
|
1424 |
+
if (token[0] == 'K' && token[1] == 's' && IS_SPACE((token[2]))) {
|
1425 |
+
token += 2;
|
1426 |
+
real_t r, g, b;
|
1427 |
+
parseReal3(&r, &g, &b, &token);
|
1428 |
+
material.specular[0] = r;
|
1429 |
+
material.specular[1] = g;
|
1430 |
+
material.specular[2] = b;
|
1431 |
+
continue;
|
1432 |
+
}
|
1433 |
+
|
1434 |
+
// transmittance
|
1435 |
+
if ((token[0] == 'K' && token[1] == 't' && IS_SPACE((token[2]))) ||
|
1436 |
+
(token[0] == 'T' && token[1] == 'f' && IS_SPACE((token[2])))) {
|
1437 |
+
token += 2;
|
1438 |
+
real_t r, g, b;
|
1439 |
+
parseReal3(&r, &g, &b, &token);
|
1440 |
+
material.transmittance[0] = r;
|
1441 |
+
material.transmittance[1] = g;
|
1442 |
+
material.transmittance[2] = b;
|
1443 |
+
continue;
|
1444 |
+
}
|
1445 |
+
|
1446 |
+
// ior(index of refraction)
|
1447 |
+
if (token[0] == 'N' && token[1] == 'i' && IS_SPACE((token[2]))) {
|
1448 |
+
token += 2;
|
1449 |
+
material.ior = parseReal(&token);
|
1450 |
+
continue;
|
1451 |
+
}
|
1452 |
+
|
1453 |
+
// emission
|
1454 |
+
if (token[0] == 'K' && token[1] == 'e' && IS_SPACE(token[2])) {
|
1455 |
+
token += 2;
|
1456 |
+
real_t r, g, b;
|
1457 |
+
parseReal3(&r, &g, &b, &token);
|
1458 |
+
material.emission[0] = r;
|
1459 |
+
material.emission[1] = g;
|
1460 |
+
material.emission[2] = b;
|
1461 |
+
continue;
|
1462 |
+
}
|
1463 |
+
|
1464 |
+
// shininess
|
1465 |
+
if (token[0] == 'N' && token[1] == 's' && IS_SPACE(token[2])) {
|
1466 |
+
token += 2;
|
1467 |
+
material.shininess = parseReal(&token);
|
1468 |
+
continue;
|
1469 |
+
}
|
1470 |
+
|
1471 |
+
// illum model
|
1472 |
+
if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5])) {
|
1473 |
+
token += 6;
|
1474 |
+
material.illum = parseInt(&token);
|
1475 |
+
continue;
|
1476 |
+
}
|
1477 |
+
|
1478 |
+
// dissolve
|
1479 |
+
if ((token[0] == 'd' && IS_SPACE(token[1]))) {
|
1480 |
+
token += 1;
|
1481 |
+
material.dissolve = parseReal(&token);
|
1482 |
+
|
1483 |
+
if (has_tr) {
|
1484 |
+
warn_ss << "Both `d` and `Tr` parameters defined for \""
|
1485 |
+
<< material.name << "\". Use the value of `d` for dissolve (line "
|
1486 |
+
<< line_no << " in .mtl.)"
|
1487 |
+
<< std::endl;
|
1488 |
+
}
|
1489 |
+
has_d = true;
|
1490 |
+
continue;
|
1491 |
+
}
|
1492 |
+
if (token[0] == 'T' && token[1] == 'r' && IS_SPACE(token[2])) {
|
1493 |
+
token += 2;
|
1494 |
+
if (has_d) {
|
1495 |
+
// `d` wins. Ignore `Tr` value.
|
1496 |
+
warn_ss << "Both `d` and `Tr` parameters defined for \""
|
1497 |
+
<< material.name << "\". Use the value of `d` for dissolve (line "
|
1498 |
+
<< line_no << " in .mtl.)"
|
1499 |
+
<< std::endl;
|
1500 |
+
} else {
|
1501 |
+
// We invert value of Tr(assume Tr is in range [0, 1])
|
1502 |
+
// NOTE: Interpretation of Tr is application(exporter) dependent. For
|
1503 |
+
// some application(e.g. 3ds max obj exporter), Tr = d(Issue 43)
|
1504 |
+
material.dissolve = static_cast<real_t>(1.0) - parseReal(&token);
|
1505 |
+
}
|
1506 |
+
has_tr = true;
|
1507 |
+
continue;
|
1508 |
+
}
|
1509 |
+
|
1510 |
+
// PBR: roughness
|
1511 |
+
if (token[0] == 'P' && token[1] == 'r' && IS_SPACE(token[2])) {
|
1512 |
+
token += 2;
|
1513 |
+
material.roughness = parseReal(&token);
|
1514 |
+
continue;
|
1515 |
+
}
|
1516 |
+
|
1517 |
+
// PBR: metallic
|
1518 |
+
if (token[0] == 'P' && token[1] == 'm' && IS_SPACE(token[2])) {
|
1519 |
+
token += 2;
|
1520 |
+
material.metallic = parseReal(&token);
|
1521 |
+
continue;
|
1522 |
+
}
|
1523 |
+
|
1524 |
+
// PBR: sheen
|
1525 |
+
if (token[0] == 'P' && token[1] == 's' && IS_SPACE(token[2])) {
|
1526 |
+
token += 2;
|
1527 |
+
material.sheen = parseReal(&token);
|
1528 |
+
continue;
|
1529 |
+
}
|
1530 |
+
|
1531 |
+
// PBR: clearcoat thickness
|
1532 |
+
if (token[0] == 'P' && token[1] == 'c' && IS_SPACE(token[2])) {
|
1533 |
+
token += 2;
|
1534 |
+
material.clearcoat_thickness = parseReal(&token);
|
1535 |
+
continue;
|
1536 |
+
}
|
1537 |
+
|
1538 |
+
// PBR: clearcoat roughness
|
1539 |
+
if ((0 == strncmp(token, "Pcr", 3)) && IS_SPACE(token[3])) {
|
1540 |
+
token += 4;
|
1541 |
+
material.clearcoat_roughness = parseReal(&token);
|
1542 |
+
continue;
|
1543 |
+
}
|
1544 |
+
|
1545 |
+
// PBR: anisotropy
|
1546 |
+
if ((0 == strncmp(token, "aniso", 5)) && IS_SPACE(token[5])) {
|
1547 |
+
token += 6;
|
1548 |
+
material.anisotropy = parseReal(&token);
|
1549 |
+
continue;
|
1550 |
+
}
|
1551 |
+
|
1552 |
+
// PBR: anisotropy rotation
|
1553 |
+
if ((0 == strncmp(token, "anisor", 6)) && IS_SPACE(token[6])) {
|
1554 |
+
token += 7;
|
1555 |
+
material.anisotropy_rotation = parseReal(&token);
|
1556 |
+
continue;
|
1557 |
+
}
|
1558 |
+
|
1559 |
+
// ambient texture
|
1560 |
+
if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) {
|
1561 |
+
token += 7;
|
1562 |
+
ParseTextureNameAndOption(&(material.ambient_texname),
|
1563 |
+
&(material.ambient_texopt), token,
|
1564 |
+
/* is_bump */ false);
|
1565 |
+
continue;
|
1566 |
+
}
|
1567 |
+
|
1568 |
+
// diffuse texture
|
1569 |
+
if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) {
|
1570 |
+
token += 7;
|
1571 |
+
ParseTextureNameAndOption(&(material.diffuse_texname),
|
1572 |
+
&(material.diffuse_texopt), token,
|
1573 |
+
/* is_bump */ false);
|
1574 |
+
continue;
|
1575 |
+
}
|
1576 |
+
|
1577 |
+
// specular texture
|
1578 |
+
if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) {
|
1579 |
+
token += 7;
|
1580 |
+
ParseTextureNameAndOption(&(material.specular_texname),
|
1581 |
+
&(material.specular_texopt), token,
|
1582 |
+
/* is_bump */ false);
|
1583 |
+
continue;
|
1584 |
+
}
|
1585 |
+
|
1586 |
+
// specular highlight texture
|
1587 |
+
if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) {
|
1588 |
+
token += 7;
|
1589 |
+
ParseTextureNameAndOption(&(material.specular_highlight_texname),
|
1590 |
+
&(material.specular_highlight_texopt), token,
|
1591 |
+
/* is_bump */ false);
|
1592 |
+
continue;
|
1593 |
+
}
|
1594 |
+
|
1595 |
+
// bump texture
|
1596 |
+
if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) {
|
1597 |
+
token += 9;
|
1598 |
+
ParseTextureNameAndOption(&(material.bump_texname),
|
1599 |
+
&(material.bump_texopt), token,
|
1600 |
+
/* is_bump */ true);
|
1601 |
+
continue;
|
1602 |
+
}
|
1603 |
+
|
1604 |
+
// bump texture
|
1605 |
+
if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8])) {
|
1606 |
+
token += 9;
|
1607 |
+
ParseTextureNameAndOption(&(material.bump_texname),
|
1608 |
+
&(material.bump_texopt), token,
|
1609 |
+
/* is_bump */ true);
|
1610 |
+
continue;
|
1611 |
+
}
|
1612 |
+
|
1613 |
+
// bump texture
|
1614 |
+
if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) {
|
1615 |
+
token += 5;
|
1616 |
+
ParseTextureNameAndOption(&(material.bump_texname),
|
1617 |
+
&(material.bump_texopt), token,
|
1618 |
+
/* is_bump */ true);
|
1619 |
+
continue;
|
1620 |
+
}
|
1621 |
+
|
1622 |
+
// alpha texture
|
1623 |
+
if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) {
|
1624 |
+
token += 6;
|
1625 |
+
material.alpha_texname = token;
|
1626 |
+
ParseTextureNameAndOption(&(material.alpha_texname),
|
1627 |
+
&(material.alpha_texopt), token,
|
1628 |
+
/* is_bump */ false);
|
1629 |
+
continue;
|
1630 |
+
}
|
1631 |
+
|
1632 |
+
// displacement texture
|
1633 |
+
if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) {
|
1634 |
+
token += 5;
|
1635 |
+
ParseTextureNameAndOption(&(material.displacement_texname),
|
1636 |
+
&(material.displacement_texopt), token,
|
1637 |
+
/* is_bump */ false);
|
1638 |
+
continue;
|
1639 |
+
}
|
1640 |
+
|
1641 |
+
// reflection map
|
1642 |
+
if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4])) {
|
1643 |
+
token += 5;
|
1644 |
+
ParseTextureNameAndOption(&(material.reflection_texname),
|
1645 |
+
&(material.reflection_texopt), token,
|
1646 |
+
/* is_bump */ false);
|
1647 |
+
continue;
|
1648 |
+
}
|
1649 |
+
|
1650 |
+
// PBR: roughness texture
|
1651 |
+
if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) {
|
1652 |
+
token += 7;
|
1653 |
+
ParseTextureNameAndOption(&(material.roughness_texname),
|
1654 |
+
&(material.roughness_texopt), token,
|
1655 |
+
/* is_bump */ false);
|
1656 |
+
continue;
|
1657 |
+
}
|
1658 |
+
|
1659 |
+
// PBR: metallic texture
|
1660 |
+
if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) {
|
1661 |
+
token += 7;
|
1662 |
+
ParseTextureNameAndOption(&(material.metallic_texname),
|
1663 |
+
&(material.metallic_texopt), token,
|
1664 |
+
/* is_bump */ false);
|
1665 |
+
continue;
|
1666 |
+
}
|
1667 |
+
|
1668 |
+
// PBR: sheen texture
|
1669 |
+
if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) {
|
1670 |
+
token += 7;
|
1671 |
+
ParseTextureNameAndOption(&(material.sheen_texname),
|
1672 |
+
&(material.sheen_texopt), token,
|
1673 |
+
/* is_bump */ false);
|
1674 |
+
continue;
|
1675 |
+
}
|
1676 |
+
|
1677 |
+
// PBR: emissive texture
|
1678 |
+
if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) {
|
1679 |
+
token += 7;
|
1680 |
+
ParseTextureNameAndOption(&(material.emissive_texname),
|
1681 |
+
&(material.emissive_texopt), token,
|
1682 |
+
/* is_bump */ false);
|
1683 |
+
continue;
|
1684 |
+
}
|
1685 |
+
|
1686 |
+
// PBR: normal map texture
|
1687 |
+
if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) {
|
1688 |
+
token += 5;
|
1689 |
+
ParseTextureNameAndOption(
|
1690 |
+
&(material.normal_texname), &(material.normal_texopt), token,
|
1691 |
+
/* is_bump */ false); // @fixme { is_bump will be true? }
|
1692 |
+
continue;
|
1693 |
+
}
|
1694 |
+
|
1695 |
+
// unknown parameter
|
1696 |
+
const char *_space = strchr(token, ' ');
|
1697 |
+
if (!_space) {
|
1698 |
+
_space = strchr(token, '\t');
|
1699 |
+
}
|
1700 |
+
if (_space) {
|
1701 |
+
std::ptrdiff_t len = _space - token;
|
1702 |
+
std::string key(token, static_cast<size_t>(len));
|
1703 |
+
std::string value = _space + 1;
|
1704 |
+
material.unknown_parameter.insert(
|
1705 |
+
std::pair<std::string, std::string>(key, value));
|
1706 |
+
}
|
1707 |
+
}
|
1708 |
+
// flush last material.
|
1709 |
+
material_map->insert(std::pair<std::string, int>(
|
1710 |
+
material.name, static_cast<int>(materials->size())));
|
1711 |
+
materials->push_back(material);
|
1712 |
+
|
1713 |
+
if (warning) {
|
1714 |
+
(*warning) = warn_ss.str();
|
1715 |
+
}
|
1716 |
+
}
|
1717 |
+
|
1718 |
+
bool MaterialFileReader::operator()(const std::string &matId,
|
1719 |
+
std::vector<material_t> *materials,
|
1720 |
+
std::map<std::string, int> *matMap,
|
1721 |
+
std::string *warn, std::string *err) {
|
1722 |
+
std::string filepath;
|
1723 |
+
|
1724 |
+
if (!m_mtlBaseDir.empty()) {
|
1725 |
+
filepath = std::string(m_mtlBaseDir) + matId;
|
1726 |
+
} else {
|
1727 |
+
filepath = matId;
|
1728 |
+
}
|
1729 |
+
|
1730 |
+
std::ifstream matIStream(filepath.c_str());
|
1731 |
+
if (!matIStream) {
|
1732 |
+
std::stringstream ss;
|
1733 |
+
ss << "Material file [ " << filepath << " ] not found." << std::endl;
|
1734 |
+
if (warn) {
|
1735 |
+
(*warn) += ss.str();
|
1736 |
+
}
|
1737 |
+
return false;
|
1738 |
+
}
|
1739 |
+
|
1740 |
+
LoadMtl(matMap, materials, &matIStream, warn, err);
|
1741 |
+
|
1742 |
+
return true;
|
1743 |
+
}
|
1744 |
+
|
1745 |
+
bool MaterialStreamReader::operator()(const std::string &matId,
|
1746 |
+
std::vector<material_t> *materials,
|
1747 |
+
std::map<std::string, int> *matMap,
|
1748 |
+
std::string *warn, std::string *err) {
|
1749 |
+
(void)err;
|
1750 |
+
(void)matId;
|
1751 |
+
if (!m_inStream) {
|
1752 |
+
std::stringstream ss;
|
1753 |
+
ss << "Material stream in error state. " << std::endl;
|
1754 |
+
if (warn) {
|
1755 |
+
(*warn) += ss.str();
|
1756 |
+
}
|
1757 |
+
return false;
|
1758 |
+
}
|
1759 |
+
|
1760 |
+
LoadMtl(matMap, materials, &m_inStream, warn, err);
|
1761 |
+
|
1762 |
+
return true;
|
1763 |
+
}
|
1764 |
+
|
1765 |
+
bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
1766 |
+
std::vector<material_t> *materials, std::string *warn,
|
1767 |
+
std::string *err, const char *filename, const char *mtl_basedir,
|
1768 |
+
bool trianglulate, bool default_vcols_fallback) {
|
1769 |
+
attrib->vertices.clear();
|
1770 |
+
attrib->normals.clear();
|
1771 |
+
attrib->texcoords.clear();
|
1772 |
+
attrib->colors.clear();
|
1773 |
+
shapes->clear();
|
1774 |
+
|
1775 |
+
std::stringstream errss;
|
1776 |
+
|
1777 |
+
std::ifstream ifs(filename);
|
1778 |
+
if (!ifs) {
|
1779 |
+
errss << "Cannot open file [" << filename << "]" << std::endl;
|
1780 |
+
if (err) {
|
1781 |
+
(*err) = errss.str();
|
1782 |
+
}
|
1783 |
+
return false;
|
1784 |
+
}
|
1785 |
+
|
1786 |
+
std::string baseDir = mtl_basedir ? mtl_basedir : "";
|
1787 |
+
if (!baseDir.empty()) {
|
1788 |
+
#ifndef _WIN32
|
1789 |
+
const char dirsep = '/';
|
1790 |
+
#else
|
1791 |
+
const char dirsep = '\\';
|
1792 |
+
#endif
|
1793 |
+
if (baseDir[baseDir.length() - 1] != dirsep) baseDir += dirsep;
|
1794 |
+
}
|
1795 |
+
MaterialFileReader matFileReader(baseDir);
|
1796 |
+
|
1797 |
+
return LoadObj(attrib, shapes, materials, warn, err, &ifs, &matFileReader,
|
1798 |
+
trianglulate, default_vcols_fallback);
|
1799 |
+
}
|
1800 |
+
|
1801 |
+
bool LoadObj(attrib_t *attrib, std::vector<shape_t> *shapes,
|
1802 |
+
std::vector<material_t> *materials, std::string *warn,
|
1803 |
+
std::string *err, std::istream *inStream,
|
1804 |
+
MaterialReader *readMatFn /*= NULL*/, bool triangulate,
|
1805 |
+
bool default_vcols_fallback) {
|
1806 |
+
std::stringstream errss;
|
1807 |
+
|
1808 |
+
std::vector<real_t> v;
|
1809 |
+
std::vector<real_t> vn;
|
1810 |
+
std::vector<real_t> vt;
|
1811 |
+
std::vector<real_t> vc;
|
1812 |
+
std::vector<tag_t> tags;
|
1813 |
+
std::vector<face_t> faceGroup;
|
1814 |
+
std::vector<int> lineGroup;
|
1815 |
+
std::string name;
|
1816 |
+
|
1817 |
+
// material
|
1818 |
+
std::map<std::string, int> material_map;
|
1819 |
+
int material = -1;
|
1820 |
+
|
1821 |
+
// smoothing group id
|
1822 |
+
unsigned int current_smoothing_id =
|
1823 |
+
0; // Initial value. 0 means no smoothing.
|
1824 |
+
|
1825 |
+
int greatest_v_idx = -1;
|
1826 |
+
int greatest_vn_idx = -1;
|
1827 |
+
int greatest_vt_idx = -1;
|
1828 |
+
|
1829 |
+
shape_t shape;
|
1830 |
+
|
1831 |
+
bool found_all_colors = true;
|
1832 |
+
|
1833 |
+
size_t line_num = 0;
|
1834 |
+
std::string linebuf;
|
1835 |
+
while (inStream->peek() != -1) {
|
1836 |
+
safeGetline(*inStream, linebuf);
|
1837 |
+
|
1838 |
+
line_num++;
|
1839 |
+
|
1840 |
+
// Trim newline '\r\n' or '\n'
|
1841 |
+
if (linebuf.size() > 0) {
|
1842 |
+
if (linebuf[linebuf.size() - 1] == '\n')
|
1843 |
+
linebuf.erase(linebuf.size() - 1);
|
1844 |
+
}
|
1845 |
+
if (linebuf.size() > 0) {
|
1846 |
+
if (linebuf[linebuf.size() - 1] == '\r')
|
1847 |
+
linebuf.erase(linebuf.size() - 1);
|
1848 |
+
}
|
1849 |
+
|
1850 |
+
// Skip if empty line.
|
1851 |
+
if (linebuf.empty()) {
|
1852 |
+
continue;
|
1853 |
+
}
|
1854 |
+
|
1855 |
+
// Skip leading space.
|
1856 |
+
const char *token = linebuf.c_str();
|
1857 |
+
token += strspn(token, " \t");
|
1858 |
+
|
1859 |
+
assert(token);
|
1860 |
+
if (token[0] == '\0') continue; // empty line
|
1861 |
+
|
1862 |
+
if (token[0] == '#') continue; // comment line
|
1863 |
+
|
1864 |
+
// vertex
|
1865 |
+
if (token[0] == 'v' && IS_SPACE((token[1]))) {
|
1866 |
+
token += 2;
|
1867 |
+
real_t x, y, z;
|
1868 |
+
real_t r, g, b;
|
1869 |
+
|
1870 |
+
found_all_colors &= parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token);
|
1871 |
+
|
1872 |
+
v.push_back(x);
|
1873 |
+
v.push_back(y);
|
1874 |
+
v.push_back(z);
|
1875 |
+
|
1876 |
+
if (found_all_colors || default_vcols_fallback) {
|
1877 |
+
vc.push_back(r);
|
1878 |
+
vc.push_back(g);
|
1879 |
+
vc.push_back(b);
|
1880 |
+
}
|
1881 |
+
|
1882 |
+
continue;
|
1883 |
+
}
|
1884 |
+
|
1885 |
+
// normal
|
1886 |
+
if (token[0] == 'v' && token[1] == 'n' && IS_SPACE((token[2]))) {
|
1887 |
+
token += 3;
|
1888 |
+
real_t x, y, z;
|
1889 |
+
parseReal3(&x, &y, &z, &token);
|
1890 |
+
vn.push_back(x);
|
1891 |
+
vn.push_back(y);
|
1892 |
+
vn.push_back(z);
|
1893 |
+
continue;
|
1894 |
+
}
|
1895 |
+
|
1896 |
+
// texcoord
|
1897 |
+
if (token[0] == 'v' && token[1] == 't' && IS_SPACE((token[2]))) {
|
1898 |
+
token += 3;
|
1899 |
+
real_t x, y;
|
1900 |
+
parseReal2(&x, &y, &token);
|
1901 |
+
vt.push_back(x);
|
1902 |
+
vt.push_back(y);
|
1903 |
+
continue;
|
1904 |
+
}
|
1905 |
+
|
1906 |
+
// line
|
1907 |
+
if (token[0] == 'l' && IS_SPACE((token[1]))) {
|
1908 |
+
token += 2;
|
1909 |
+
|
1910 |
+
line_t line_cache;
|
1911 |
+
bool end_line_bit = 0;
|
1912 |
+
while (!IS_NEW_LINE(token[0])) {
|
1913 |
+
// get index from string
|
1914 |
+
int idx = 0;
|
1915 |
+
fixIndex(parseInt(&token), 0, &idx);
|
1916 |
+
|
1917 |
+
size_t n = strspn(token, " \t\r");
|
1918 |
+
token += n;
|
1919 |
+
|
1920 |
+
if (!end_line_bit) {
|
1921 |
+
line_cache.idx0 = idx;
|
1922 |
+
} else {
|
1923 |
+
line_cache.idx1 = idx;
|
1924 |
+
lineGroup.push_back(line_cache.idx0);
|
1925 |
+
lineGroup.push_back(line_cache.idx1);
|
1926 |
+
line_cache = line_t();
|
1927 |
+
}
|
1928 |
+
end_line_bit = !end_line_bit;
|
1929 |
+
}
|
1930 |
+
|
1931 |
+
continue;
|
1932 |
+
}
|
1933 |
+
// face
|
1934 |
+
if (token[0] == 'f' && IS_SPACE((token[1]))) {
|
1935 |
+
token += 2;
|
1936 |
+
token += strspn(token, " \t");
|
1937 |
+
|
1938 |
+
face_t face;
|
1939 |
+
|
1940 |
+
face.smoothing_group_id = current_smoothing_id;
|
1941 |
+
face.vertex_indices.reserve(3);
|
1942 |
+
|
1943 |
+
while (!IS_NEW_LINE(token[0])) {
|
1944 |
+
vertex_index_t vi;
|
1945 |
+
if (!parseTriple(&token, static_cast<int>(v.size() / 3),
|
1946 |
+
static_cast<int>(vn.size() / 3),
|
1947 |
+
static_cast<int>(vt.size() / 2), &vi)) {
|
1948 |
+
if (err) {
|
1949 |
+
std::stringstream ss;
|
1950 |
+
ss << "Failed parse `f' line(e.g. zero value for face index. line " << line_num << ".)\n";
|
1951 |
+
(*err) += ss.str();
|
1952 |
+
}
|
1953 |
+
return false;
|
1954 |
+
}
|
1955 |
+
|
1956 |
+
greatest_v_idx = greatest_v_idx > vi.v_idx ? greatest_v_idx : vi.v_idx;
|
1957 |
+
greatest_vn_idx =
|
1958 |
+
greatest_vn_idx > vi.vn_idx ? greatest_vn_idx : vi.vn_idx;
|
1959 |
+
greatest_vt_idx =
|
1960 |
+
greatest_vt_idx > vi.vt_idx ? greatest_vt_idx : vi.vt_idx;
|
1961 |
+
|
1962 |
+
face.vertex_indices.push_back(vi);
|
1963 |
+
size_t n = strspn(token, " \t\r");
|
1964 |
+
token += n;
|
1965 |
+
}
|
1966 |
+
|
1967 |
+
// replace with emplace_back + std::move on C++11
|
1968 |
+
faceGroup.push_back(face);
|
1969 |
+
|
1970 |
+
continue;
|
1971 |
+
}
|
1972 |
+
|
1973 |
+
// use mtl
|
1974 |
+
if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) {
|
1975 |
+
token += 7;
|
1976 |
+
std::stringstream ss;
|
1977 |
+
ss << token;
|
1978 |
+
std::string namebuf = ss.str();
|
1979 |
+
|
1980 |
+
int newMaterialId = -1;
|
1981 |
+
if (material_map.find(namebuf) != material_map.end()) {
|
1982 |
+
newMaterialId = material_map[namebuf];
|
1983 |
+
} else {
|
1984 |
+
// { error!! material not found }
|
1985 |
+
}
|
1986 |
+
|
1987 |
+
if (newMaterialId != material) {
|
1988 |
+
// Create per-face material. Thus we don't add `shape` to `shapes` at
|
1989 |
+
// this time.
|
1990 |
+
// just clear `faceGroup` after `exportGroupsToShape()` call.
|
1991 |
+
exportGroupsToShape(&shape, faceGroup, lineGroup, tags, material, name,
|
1992 |
+
triangulate, v);
|
1993 |
+
faceGroup.clear();
|
1994 |
+
material = newMaterialId;
|
1995 |
+
}
|
1996 |
+
|
1997 |
+
continue;
|
1998 |
+
}
|
1999 |
+
|
2000 |
+
// load mtl
|
2001 |
+
if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) {
|
2002 |
+
if (readMatFn) {
|
2003 |
+
token += 7;
|
2004 |
+
|
2005 |
+
std::vector<std::string> filenames;
|
2006 |
+
SplitString(std::string(token), ' ', filenames);
|
2007 |
+
|
2008 |
+
if (filenames.empty()) {
|
2009 |
+
if (warn) {
|
2010 |
+
std::stringstream ss;
|
2011 |
+
ss << "Looks like empty filename for mtllib. Use default "
|
2012 |
+
"material (line " << line_num << ".)\n";
|
2013 |
+
|
2014 |
+
(*warn) += ss.str();
|
2015 |
+
}
|
2016 |
+
} else {
|
2017 |
+
bool found = false;
|
2018 |
+
for (size_t s = 0; s < filenames.size(); s++) {
|
2019 |
+
std::string warn_mtl;
|
2020 |
+
std::string err_mtl;
|
2021 |
+
bool ok = (*readMatFn)(filenames[s].c_str(), materials,
|
2022 |
+
&material_map, &warn_mtl, &err_mtl);
|
2023 |
+
if (warn && (!warn_mtl.empty())) {
|
2024 |
+
(*warn) += warn_mtl;
|
2025 |
+
}
|
2026 |
+
|
2027 |
+
if (err && (!err_mtl.empty())) {
|
2028 |
+
(*err) += err_mtl;
|
2029 |
+
}
|
2030 |
+
|
2031 |
+
if (ok) {
|
2032 |
+
found = true;
|
2033 |
+
break;
|
2034 |
+
}
|
2035 |
+
}
|
2036 |
+
|
2037 |
+
if (!found) {
|
2038 |
+
if (warn) {
|
2039 |
+
(*warn) +=
|
2040 |
+
"Failed to load material file(s). Use default "
|
2041 |
+
"material.\n";
|
2042 |
+
}
|
2043 |
+
}
|
2044 |
+
}
|
2045 |
+
}
|
2046 |
+
|
2047 |
+
continue;
|
2048 |
+
}
|
2049 |
+
|
2050 |
+
// group name
|
2051 |
+
if (token[0] == 'g' && IS_SPACE((token[1]))) {
|
2052 |
+
// flush previous face group.
|
2053 |
+
bool ret = exportGroupsToShape(&shape, faceGroup, lineGroup, tags,
|
2054 |
+
material, name, triangulate, v);
|
2055 |
+
(void)ret; // return value not used.
|
2056 |
+
|
2057 |
+
if (shape.mesh.indices.size() > 0) {
|
2058 |
+
shapes->push_back(shape);
|
2059 |
+
}
|
2060 |
+
|
2061 |
+
shape = shape_t();
|
2062 |
+
|
2063 |
+
// material = -1;
|
2064 |
+
faceGroup.clear();
|
2065 |
+
|
2066 |
+
std::vector<std::string> names;
|
2067 |
+
|
2068 |
+
while (!IS_NEW_LINE(token[0])) {
|
2069 |
+
std::string str = parseString(&token);
|
2070 |
+
names.push_back(str);
|
2071 |
+
token += strspn(token, " \t\r"); // skip tag
|
2072 |
+
}
|
2073 |
+
|
2074 |
+
// names[0] must be 'g'
|
2075 |
+
|
2076 |
+
if (names.size() < 2) {
|
2077 |
+
// 'g' with empty names
|
2078 |
+
if (warn) {
|
2079 |
+
std::stringstream ss;
|
2080 |
+
ss << "Empty group name. line: " << line_num << "\n";
|
2081 |
+
(*warn) += ss.str();
|
2082 |
+
name = "";
|
2083 |
+
}
|
2084 |
+
} else {
|
2085 |
+
std::stringstream ss;
|
2086 |
+
ss << names[1];
|
2087 |
+
|
2088 |
+
// tinyobjloader does not support multiple groups for a primitive.
|
2089 |
+
// Currently we concatinate multiple group names with a space to get
|
2090 |
+
// single group name.
|
2091 |
+
|
2092 |
+
for (size_t i = 2; i < names.size(); i++) {
|
2093 |
+
ss << " " << names[i];
|
2094 |
+
}
|
2095 |
+
|
2096 |
+
name = ss.str();
|
2097 |
+
}
|
2098 |
+
|
2099 |
+
continue;
|
2100 |
+
}
|
2101 |
+
|
2102 |
+
// object name
|
2103 |
+
if (token[0] == 'o' && IS_SPACE((token[1]))) {
|
2104 |
+
// flush previous face group.
|
2105 |
+
bool ret = exportGroupsToShape(&shape, faceGroup, lineGroup, tags,
|
2106 |
+
material, name, triangulate, v);
|
2107 |
+
if (ret) {
|
2108 |
+
shapes->push_back(shape);
|
2109 |
+
}
|
2110 |
+
|
2111 |
+
// material = -1;
|
2112 |
+
faceGroup.clear();
|
2113 |
+
shape = shape_t();
|
2114 |
+
|
2115 |
+
// @todo { multiple object name? }
|
2116 |
+
token += 2;
|
2117 |
+
std::stringstream ss;
|
2118 |
+
ss << token;
|
2119 |
+
name = ss.str();
|
2120 |
+
|
2121 |
+
continue;
|
2122 |
+
}
|
2123 |
+
|
2124 |
+
if (token[0] == 't' && IS_SPACE(token[1])) {
|
2125 |
+
const int max_tag_nums = 8192; // FIXME(syoyo): Parameterize.
|
2126 |
+
tag_t tag;
|
2127 |
+
|
2128 |
+
token += 2;
|
2129 |
+
|
2130 |
+
tag.name = parseString(&token);
|
2131 |
+
|
2132 |
+
tag_sizes ts = parseTagTriple(&token);
|
2133 |
+
|
2134 |
+
if (ts.num_ints < 0) {
|
2135 |
+
ts.num_ints = 0;
|
2136 |
+
}
|
2137 |
+
if (ts.num_ints > max_tag_nums) {
|
2138 |
+
ts.num_ints = max_tag_nums;
|
2139 |
+
}
|
2140 |
+
|
2141 |
+
if (ts.num_reals < 0) {
|
2142 |
+
ts.num_reals = 0;
|
2143 |
+
}
|
2144 |
+
if (ts.num_reals > max_tag_nums) {
|
2145 |
+
ts.num_reals = max_tag_nums;
|
2146 |
+
}
|
2147 |
+
|
2148 |
+
if (ts.num_strings < 0) {
|
2149 |
+
ts.num_strings = 0;
|
2150 |
+
}
|
2151 |
+
if (ts.num_strings > max_tag_nums) {
|
2152 |
+
ts.num_strings = max_tag_nums;
|
2153 |
+
}
|
2154 |
+
|
2155 |
+
tag.intValues.resize(static_cast<size_t>(ts.num_ints));
|
2156 |
+
|
2157 |
+
for (size_t i = 0; i < static_cast<size_t>(ts.num_ints); ++i) {
|
2158 |
+
tag.intValues[i] = parseInt(&token);
|
2159 |
+
}
|
2160 |
+
|
2161 |
+
tag.floatValues.resize(static_cast<size_t>(ts.num_reals));
|
2162 |
+
for (size_t i = 0; i < static_cast<size_t>(ts.num_reals); ++i) {
|
2163 |
+
tag.floatValues[i] = parseReal(&token);
|
2164 |
+
}
|
2165 |
+
|
2166 |
+
tag.stringValues.resize(static_cast<size_t>(ts.num_strings));
|
2167 |
+
for (size_t i = 0; i < static_cast<size_t>(ts.num_strings); ++i) {
|
2168 |
+
tag.stringValues[i] = parseString(&token);
|
2169 |
+
}
|
2170 |
+
|
2171 |
+
tags.push_back(tag);
|
2172 |
+
|
2173 |
+
continue;
|
2174 |
+
}
|
2175 |
+
|
2176 |
+
if (token[0] == 's' && IS_SPACE(token[1])) {
|
2177 |
+
// smoothing group id
|
2178 |
+
token += 2;
|
2179 |
+
|
2180 |
+
// skip space.
|
2181 |
+
token += strspn(token, " \t"); // skip space
|
2182 |
+
|
2183 |
+
if (token[0] == '\0') {
|
2184 |
+
continue;
|
2185 |
+
}
|
2186 |
+
|
2187 |
+
if (token[0] == '\r' || token[1] == '\n') {
|
2188 |
+
continue;
|
2189 |
+
}
|
2190 |
+
|
2191 |
+
if (strlen(token) >= 3) {
|
2192 |
+
if (token[0] == 'o' && token[1] == 'f' && token[2] == 'f') {
|
2193 |
+
current_smoothing_id = 0;
|
2194 |
+
}
|
2195 |
+
} else {
|
2196 |
+
// assume number
|
2197 |
+
int smGroupId = parseInt(&token);
|
2198 |
+
if (smGroupId < 0) {
|
2199 |
+
// parse error. force set to 0.
|
2200 |
+
// FIXME(syoyo): Report warning.
|
2201 |
+
current_smoothing_id = 0;
|
2202 |
+
} else {
|
2203 |
+
current_smoothing_id = static_cast<unsigned int>(smGroupId);
|
2204 |
+
}
|
2205 |
+
}
|
2206 |
+
|
2207 |
+
continue;
|
2208 |
+
} // smoothing group id
|
2209 |
+
|
2210 |
+
// Ignore unknown command.
|
2211 |
+
}
|
2212 |
+
|
2213 |
+
// not all vertices have colors, no default colors desired? -> clear colors
|
2214 |
+
if (!found_all_colors && !default_vcols_fallback) {
|
2215 |
+
vc.clear();
|
2216 |
+
}
|
2217 |
+
|
2218 |
+
if (greatest_v_idx >= static_cast<int>(v.size() / 3)) {
|
2219 |
+
if (warn) {
|
2220 |
+
std::stringstream ss;
|
2221 |
+
ss << "Vertex indices out of bounds (line " << line_num << ".)\n" << std::endl;
|
2222 |
+
(*warn) += ss.str();
|
2223 |
+
}
|
2224 |
+
}
|
2225 |
+
if (greatest_vn_idx >= static_cast<int>(vn.size() / 3)) {
|
2226 |
+
if (warn) {
|
2227 |
+
std::stringstream ss;
|
2228 |
+
ss << "Vertex normal indices out of bounds (line " << line_num << ".)\n" << std::endl;
|
2229 |
+
(*warn) += ss.str();
|
2230 |
+
}
|
2231 |
+
}
|
2232 |
+
if (greatest_vt_idx >= static_cast<int>(vt.size() / 2)) {
|
2233 |
+
if (warn) {
|
2234 |
+
std::stringstream ss;
|
2235 |
+
ss << "Vertex texcoord indices out of bounds (line " << line_num << ".)\n" << std::endl;
|
2236 |
+
(*warn) += ss.str();
|
2237 |
+
}
|
2238 |
+
}
|
2239 |
+
|
2240 |
+
bool ret = exportGroupsToShape(&shape, faceGroup, lineGroup, tags, material,
|
2241 |
+
name, triangulate, v);
|
2242 |
+
// exportGroupsToShape return false when `usemtl` is called in the last
|
2243 |
+
// line.
|
2244 |
+
// we also add `shape` to `shapes` when `shape.mesh` has already some
|
2245 |
+
// faces(indices)
|
2246 |
+
if (ret || shape.mesh.indices.size()) {
|
2247 |
+
shapes->push_back(shape);
|
2248 |
+
}
|
2249 |
+
faceGroup.clear(); // for safety
|
2250 |
+
|
2251 |
+
if (err) {
|
2252 |
+
(*err) += errss.str();
|
2253 |
+
}
|
2254 |
+
|
2255 |
+
attrib->vertices.swap(v);
|
2256 |
+
attrib->normals.swap(vn);
|
2257 |
+
attrib->texcoords.swap(vt);
|
2258 |
+
attrib->colors.swap(vc);
|
2259 |
+
|
2260 |
+
return true;
|
2261 |
+
}
|
2262 |
+
|
2263 |
+
bool LoadObjWithCallback(std::istream &inStream, const callback_t &callback,
|
2264 |
+
void *user_data /*= NULL*/,
|
2265 |
+
MaterialReader *readMatFn /*= NULL*/,
|
2266 |
+
std::string *warn, /* = NULL*/
|
2267 |
+
std::string *err /*= NULL*/) {
|
2268 |
+
std::stringstream errss;
|
2269 |
+
|
2270 |
+
// material
|
2271 |
+
std::map<std::string, int> material_map;
|
2272 |
+
int material_id = -1; // -1 = invalid
|
2273 |
+
|
2274 |
+
std::vector<index_t> indices;
|
2275 |
+
std::vector<material_t> materials;
|
2276 |
+
std::vector<std::string> names;
|
2277 |
+
names.reserve(2);
|
2278 |
+
std::vector<const char *> names_out;
|
2279 |
+
|
2280 |
+
std::string linebuf;
|
2281 |
+
while (inStream.peek() != -1) {
|
2282 |
+
safeGetline(inStream, linebuf);
|
2283 |
+
|
2284 |
+
// Trim newline '\r\n' or '\n'
|
2285 |
+
if (linebuf.size() > 0) {
|
2286 |
+
if (linebuf[linebuf.size() - 1] == '\n')
|
2287 |
+
linebuf.erase(linebuf.size() - 1);
|
2288 |
+
}
|
2289 |
+
if (linebuf.size() > 0) {
|
2290 |
+
if (linebuf[linebuf.size() - 1] == '\r')
|
2291 |
+
linebuf.erase(linebuf.size() - 1);
|
2292 |
+
}
|
2293 |
+
|
2294 |
+
// Skip if empty line.
|
2295 |
+
if (linebuf.empty()) {
|
2296 |
+
continue;
|
2297 |
+
}
|
2298 |
+
|
2299 |
+
// Skip leading space.
|
2300 |
+
const char *token = linebuf.c_str();
|
2301 |
+
token += strspn(token, " \t");
|
2302 |
+
|
2303 |
+
assert(token);
|
2304 |
+
if (token[0] == '\0') continue; // empty line
|
2305 |
+
|
2306 |
+
if (token[0] == '#') continue; // comment line
|
2307 |
+
|
2308 |
+
// vertex
|
2309 |
+
if (token[0] == 'v' && IS_SPACE((token[1]))) {
|
2310 |
+
token += 2;
|
2311 |
+
// TODO(syoyo): Support parsing vertex color extension.
|
2312 |
+
real_t x, y, z, w; // w is optional. default = 1.0
|
2313 |
+
parseV(&x, &y, &z, &w, &token);
|
2314 |
+
if (callback.vertex_cb) {
|
2315 |
+
callback.vertex_cb(user_data, x, y, z, w);
|
2316 |
+
}
|
2317 |
+
continue;
|
2318 |
+
}
|
2319 |
+
|
2320 |
+
// normal
|
2321 |
+
if (token[0] == 'v' && token[1] == 'n' && IS_SPACE((token[2]))) {
|
2322 |
+
token += 3;
|
2323 |
+
real_t x, y, z;
|
2324 |
+
parseReal3(&x, &y, &z, &token);
|
2325 |
+
if (callback.normal_cb) {
|
2326 |
+
callback.normal_cb(user_data, x, y, z);
|
2327 |
+
}
|
2328 |
+
continue;
|
2329 |
+
}
|
2330 |
+
|
2331 |
+
// texcoord
|
2332 |
+
if (token[0] == 'v' && token[1] == 't' && IS_SPACE((token[2]))) {
|
2333 |
+
token += 3;
|
2334 |
+
real_t x, y, z; // y and z are optional. default = 0.0
|
2335 |
+
parseReal3(&x, &y, &z, &token);
|
2336 |
+
if (callback.texcoord_cb) {
|
2337 |
+
callback.texcoord_cb(user_data, x, y, z);
|
2338 |
+
}
|
2339 |
+
continue;
|
2340 |
+
}
|
2341 |
+
|
2342 |
+
// face
|
2343 |
+
if (token[0] == 'f' && IS_SPACE((token[1]))) {
|
2344 |
+
token += 2;
|
2345 |
+
token += strspn(token, " \t");
|
2346 |
+
|
2347 |
+
indices.clear();
|
2348 |
+
while (!IS_NEW_LINE(token[0])) {
|
2349 |
+
vertex_index_t vi = parseRawTriple(&token);
|
2350 |
+
|
2351 |
+
index_t idx;
|
2352 |
+
idx.vertex_index = vi.v_idx;
|
2353 |
+
idx.normal_index = vi.vn_idx;
|
2354 |
+
idx.texcoord_index = vi.vt_idx;
|
2355 |
+
|
2356 |
+
indices.push_back(idx);
|
2357 |
+
size_t n = strspn(token, " \t\r");
|
2358 |
+
token += n;
|
2359 |
+
}
|
2360 |
+
|
2361 |
+
if (callback.index_cb && indices.size() > 0) {
|
2362 |
+
callback.index_cb(user_data, &indices.at(0),
|
2363 |
+
static_cast<int>(indices.size()));
|
2364 |
+
}
|
2365 |
+
|
2366 |
+
continue;
|
2367 |
+
}
|
2368 |
+
|
2369 |
+
// use mtl
|
2370 |
+
if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) {
|
2371 |
+
token += 7;
|
2372 |
+
std::stringstream ss;
|
2373 |
+
ss << token;
|
2374 |
+
std::string namebuf = ss.str();
|
2375 |
+
|
2376 |
+
int newMaterialId = -1;
|
2377 |
+
if (material_map.find(namebuf) != material_map.end()) {
|
2378 |
+
newMaterialId = material_map[namebuf];
|
2379 |
+
} else {
|
2380 |
+
// { error!! material not found }
|
2381 |
+
}
|
2382 |
+
|
2383 |
+
if (newMaterialId != material_id) {
|
2384 |
+
material_id = newMaterialId;
|
2385 |
+
}
|
2386 |
+
|
2387 |
+
if (callback.usemtl_cb) {
|
2388 |
+
callback.usemtl_cb(user_data, namebuf.c_str(), material_id);
|
2389 |
+
}
|
2390 |
+
|
2391 |
+
continue;
|
2392 |
+
}
|
2393 |
+
|
2394 |
+
// load mtl
|
2395 |
+
if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) {
|
2396 |
+
if (readMatFn) {
|
2397 |
+
token += 7;
|
2398 |
+
|
2399 |
+
std::vector<std::string> filenames;
|
2400 |
+
SplitString(std::string(token), ' ', filenames);
|
2401 |
+
|
2402 |
+
if (filenames.empty()) {
|
2403 |
+
if (warn) {
|
2404 |
+
(*warn) +=
|
2405 |
+
"Looks like empty filename for mtllib. Use default "
|
2406 |
+
"material. \n";
|
2407 |
+
}
|
2408 |
+
} else {
|
2409 |
+
bool found = false;
|
2410 |
+
for (size_t s = 0; s < filenames.size(); s++) {
|
2411 |
+
std::string warn_mtl;
|
2412 |
+
std::string err_mtl;
|
2413 |
+
bool ok = (*readMatFn)(filenames[s].c_str(), &materials,
|
2414 |
+
&material_map, &warn_mtl, &err_mtl);
|
2415 |
+
|
2416 |
+
if (warn && (!warn_mtl.empty())) {
|
2417 |
+
(*warn) += warn_mtl; // This should be warn message.
|
2418 |
+
}
|
2419 |
+
|
2420 |
+
if (err && (!err_mtl.empty())) {
|
2421 |
+
(*err) += err_mtl;
|
2422 |
+
}
|
2423 |
+
|
2424 |
+
if (ok) {
|
2425 |
+
found = true;
|
2426 |
+
break;
|
2427 |
+
}
|
2428 |
+
}
|
2429 |
+
|
2430 |
+
if (!found) {
|
2431 |
+
if (warn) {
|
2432 |
+
(*warn) +=
|
2433 |
+
"Failed to load material file(s). Use default "
|
2434 |
+
"material.\n";
|
2435 |
+
}
|
2436 |
+
} else {
|
2437 |
+
if (callback.mtllib_cb) {
|
2438 |
+
callback.mtllib_cb(user_data, &materials.at(0),
|
2439 |
+
static_cast<int>(materials.size()));
|
2440 |
+
}
|
2441 |
+
}
|
2442 |
+
}
|
2443 |
+
}
|
2444 |
+
|
2445 |
+
continue;
|
2446 |
+
}
|
2447 |
+
|
2448 |
+
// group name
|
2449 |
+
if (token[0] == 'g' && IS_SPACE((token[1]))) {
|
2450 |
+
names.clear();
|
2451 |
+
|
2452 |
+
while (!IS_NEW_LINE(token[0])) {
|
2453 |
+
std::string str = parseString(&token);
|
2454 |
+
names.push_back(str);
|
2455 |
+
token += strspn(token, " \t\r"); // skip tag
|
2456 |
+
}
|
2457 |
+
|
2458 |
+
assert(names.size() > 0);
|
2459 |
+
|
2460 |
+
if (callback.group_cb) {
|
2461 |
+
if (names.size() > 1) {
|
2462 |
+
// create const char* array.
|
2463 |
+
names_out.resize(names.size() - 1);
|
2464 |
+
for (size_t j = 0; j < names_out.size(); j++) {
|
2465 |
+
names_out[j] = names[j + 1].c_str();
|
2466 |
+
}
|
2467 |
+
callback.group_cb(user_data, &names_out.at(0),
|
2468 |
+
static_cast<int>(names_out.size()));
|
2469 |
+
|
2470 |
+
} else {
|
2471 |
+
callback.group_cb(user_data, NULL, 0);
|
2472 |
+
}
|
2473 |
+
}
|
2474 |
+
|
2475 |
+
continue;
|
2476 |
+
}
|
2477 |
+
|
2478 |
+
// object name
|
2479 |
+
if (token[0] == 'o' && IS_SPACE((token[1]))) {
|
2480 |
+
// @todo { multiple object name? }
|
2481 |
+
token += 2;
|
2482 |
+
|
2483 |
+
std::stringstream ss;
|
2484 |
+
ss << token;
|
2485 |
+
std::string object_name = ss.str();
|
2486 |
+
|
2487 |
+
if (callback.object_cb) {
|
2488 |
+
callback.object_cb(user_data, object_name.c_str());
|
2489 |
+
}
|
2490 |
+
|
2491 |
+
continue;
|
2492 |
+
}
|
2493 |
+
|
2494 |
+
#if 0 // @todo
|
2495 |
+
if (token[0] == 't' && IS_SPACE(token[1])) {
|
2496 |
+
tag_t tag;
|
2497 |
+
|
2498 |
+
token += 2;
|
2499 |
+
std::stringstream ss;
|
2500 |
+
ss << token;
|
2501 |
+
tag.name = ss.str();
|
2502 |
+
|
2503 |
+
token += tag.name.size() + 1;
|
2504 |
+
|
2505 |
+
tag_sizes ts = parseTagTriple(&token);
|
2506 |
+
|
2507 |
+
tag.intValues.resize(static_cast<size_t>(ts.num_ints));
|
2508 |
+
|
2509 |
+
for (size_t i = 0; i < static_cast<size_t>(ts.num_ints); ++i) {
|
2510 |
+
tag.intValues[i] = atoi(token);
|
2511 |
+
token += strcspn(token, "/ \t\r") + 1;
|
2512 |
+
}
|
2513 |
+
|
2514 |
+
tag.floatValues.resize(static_cast<size_t>(ts.num_reals));
|
2515 |
+
for (size_t i = 0; i < static_cast<size_t>(ts.num_reals); ++i) {
|
2516 |
+
tag.floatValues[i] = parseReal(&token);
|
2517 |
+
token += strcspn(token, "/ \t\r") + 1;
|
2518 |
+
}
|
2519 |
+
|
2520 |
+
tag.stringValues.resize(static_cast<size_t>(ts.num_strings));
|
2521 |
+
for (size_t i = 0; i < static_cast<size_t>(ts.num_strings); ++i) {
|
2522 |
+
std::stringstream ss;
|
2523 |
+
ss << token;
|
2524 |
+
tag.stringValues[i] = ss.str();
|
2525 |
+
token += tag.stringValues[i].size() + 1;
|
2526 |
+
}
|
2527 |
+
|
2528 |
+
tags.push_back(tag);
|
2529 |
+
}
|
2530 |
+
#endif
|
2531 |
+
|
2532 |
+
// Ignore unknown command.
|
2533 |
+
}
|
2534 |
+
|
2535 |
+
if (err) {
|
2536 |
+
(*err) += errss.str();
|
2537 |
+
}
|
2538 |
+
|
2539 |
+
return true;
|
2540 |
+
}
|
2541 |
+
|
2542 |
+
#ifdef __clang__
|
2543 |
+
#pragma clang diagnostic pop
|
2544 |
+
#endif
|
2545 |
+
} // namespace tinyobj
|
2546 |
+
|
2547 |
+
#endif
|
third-party/DPVO/Pangolin/components/tinyobj/src/tinyobj.cpp
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
/* This file is part of the Pangolin Project.
|
2 |
+
* http://github.com/stevenlovegrove/Pangolin
|
3 |
+
*
|
4 |
+
* Copyright (c) 2011 Steven Lovegrove
|
5 |
+
*
|
6 |
+
* Permission is hereby granted, free of charge, to any person
|
7 |
+
* obtaining a copy of this software and associated documentation
|
8 |
+
* files (the "Software"), to deal in the Software without
|
9 |
+
* restriction, including without limitation the rights to use,
|
10 |
+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
|
11 |
+
* copies of the Software, and to permit persons to whom the
|
12 |
+
* Software is furnished to do so, subject to the following
|
13 |
+
* conditions:
|
14 |
+
*
|
15 |
+
* The above copyright notice and this permission notice shall be
|
16 |
+
* included in all copies or substantial portions of the Software.
|
17 |
+
*
|
18 |
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19 |
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
20 |
+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21 |
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
22 |
+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
23 |
+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
24 |
+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
25 |
+
* OTHER DEALINGS IN THE SOFTWARE.
|
26 |
+
*/
|
27 |
+
|
28 |
+
#define TINYOBJLOADER_IMPLEMENTATION
|
29 |
+
#include <tinyobj/tiny_obj_loader.h>
|
third-party/DPVO/Pangolin/examples/BasicOpenGL/1_gl_intro_classic_triangle.cpp
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/display/display.h>
|
2 |
+
#include <pangolin/gl/gl.h>
|
3 |
+
|
4 |
+
void sample()
|
5 |
+
{
|
6 |
+
// Create a 500x500 pixel application window with an OpenGL Context
|
7 |
+
// By default the window is double buffered if available.
|
8 |
+
// Load any available OpenGL Extensions (through GLEW)
|
9 |
+
pangolin::CreateWindowAndBind("Classic GL Triangle", 500, 500);
|
10 |
+
|
11 |
+
// List coordinates of a triangle
|
12 |
+
// These vertices will be relative to the coordinates of the window
|
13 |
+
// which default in OpenGL to +/- 1.0 in X and Y (first two vertex ordinates)
|
14 |
+
const float vertices[] = {
|
15 |
+
-0.5f, -0.5f, 0.0f,
|
16 |
+
0.5f, -0.5f, 0.0f,
|
17 |
+
0.0f, 0.5f, 0.0f
|
18 |
+
};
|
19 |
+
|
20 |
+
// We want the background to be purple
|
21 |
+
glClearColor(0.64f, 0.5f, 0.81f, 0.0f);
|
22 |
+
|
23 |
+
// We want our triangle to be a pleasant shade of blue!
|
24 |
+
glColor3f(0.29f, 0.71f, 1.0f);
|
25 |
+
|
26 |
+
// We keep rendering in a loop so that the triangle will keep showing
|
27 |
+
// and get adjusted as the window is resized. Press Escape or close the
|
28 |
+
// window to exit the Pangolin loop.
|
29 |
+
while( !pangolin::ShouldQuit() )
|
30 |
+
{
|
31 |
+
// Clear the window
|
32 |
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
33 |
+
|
34 |
+
// This buffer contains floating point vertices with 3 dimensions.
|
35 |
+
// They starts from the 0th element and are packed without padding.
|
36 |
+
glVertexPointer(3, GL_FLOAT, 0, vertices);
|
37 |
+
|
38 |
+
// Use Them!
|
39 |
+
glEnableClientState(GL_VERTEX_ARRAY);
|
40 |
+
|
41 |
+
// Connect the first 3 of these vertices to form a triangle!
|
42 |
+
glDrawArrays(GL_TRIANGLES, 0, 3);
|
43 |
+
|
44 |
+
// Disable the stuff we enabled...
|
45 |
+
glDisableClientState(GL_VERTEX_ARRAY);
|
46 |
+
|
47 |
+
// Process any windowing events and swap the back and front
|
48 |
+
// OpenGL buffers if available.
|
49 |
+
pangolin::FinishFrame();
|
50 |
+
}
|
51 |
+
}
|
52 |
+
|
53 |
+
int main( int /*argc*/, char** /*argv*/ )
|
54 |
+
{
|
55 |
+
sample();
|
56 |
+
return 0;
|
57 |
+
}
|
third-party/DPVO/Pangolin/examples/BasicOpenGL/1_gl_intro_pango_triangle.cpp
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/display/display.h>
|
2 |
+
#include <pangolin/gl/gl.h>
|
3 |
+
#include <pangolin/gl/gldraw.h>
|
4 |
+
|
5 |
+
void sample()
|
6 |
+
{
|
7 |
+
// Create a 500x500 pixel application window with an OpenGL Context
|
8 |
+
// By default the window is double buffered if available.
|
9 |
+
// Load any available OpenGL Extensions (through GLEW)
|
10 |
+
pangolin::CreateWindowAndBind("Pango GL Triangle", 500, 500);
|
11 |
+
|
12 |
+
// List coordinates of a triangle
|
13 |
+
// These vertices will be relative to the coordinates of the window
|
14 |
+
// which default in OpenGL to +/- 1.0 in X and Y (first two vertex ordinates)
|
15 |
+
std::vector<Eigen::Vector3f> vertices = {
|
16 |
+
{-0.5f, -0.5f, 0.0f},
|
17 |
+
{0.5f, -0.5f, 0.0f },
|
18 |
+
{0.0f, 0.5f, 0.0f }
|
19 |
+
};
|
20 |
+
|
21 |
+
// We want the background to be purple
|
22 |
+
glClearColor(0.64f, 0.5f, 0.81f, 0.0f);
|
23 |
+
|
24 |
+
// We want our triangle to be a pleasant shade of blue!
|
25 |
+
glColor3f(0.29f, 0.71f, 1.0f);
|
26 |
+
|
27 |
+
// We keep rendering in a loop so that the triangle will keep showing
|
28 |
+
// and get adjusted as the window is resized. Press Escape or close the
|
29 |
+
// window to exit the Pangolin loop.
|
30 |
+
while( !pangolin::ShouldQuit() )
|
31 |
+
{
|
32 |
+
// Clear the window
|
33 |
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
34 |
+
|
35 |
+
// Connect the first 3 vertices to form a triangle!
|
36 |
+
pangolin::glDrawVertices(vertices, GL_TRIANGLES);
|
37 |
+
|
38 |
+
// Process any windowing events and swap the back and front
|
39 |
+
// OpenGL buffers if available.
|
40 |
+
pangolin::FinishFrame();
|
41 |
+
}
|
42 |
+
}
|
43 |
+
|
44 |
+
int main( int /*argc*/, char** /*argv*/ )
|
45 |
+
{
|
46 |
+
sample();
|
47 |
+
return 0;
|
48 |
+
}
|
third-party/DPVO/Pangolin/examples/BasicOpenGL/2_gl_intro_classic_triangle_vbo.cpp
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/display/display.h>
|
2 |
+
|
3 |
+
// Here is an example of Pangolin being used *just* for windowing.
|
4 |
+
// We're using
|
5 |
+
void sample()
|
6 |
+
{
|
7 |
+
pangolin::CreateWindowAndBind("Classic GL Triangle with VBO", 500, 500);
|
8 |
+
|
9 |
+
// List coordinates of a triangle
|
10 |
+
const float vertices[] = {
|
11 |
+
-0.5f, -0.5f, 0.0f,
|
12 |
+
0.5f, -0.5f, 0.0f,
|
13 |
+
0.0f, 0.5f, 0.0f
|
14 |
+
};
|
15 |
+
|
16 |
+
// Create an OpenGL Buffer to store these coordinates
|
17 |
+
unsigned int VBO;
|
18 |
+
glGenBuffers(1, &VBO);
|
19 |
+
|
20 |
+
// Set that buffer as the current GL buffer
|
21 |
+
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
22 |
+
|
23 |
+
// Copy our host data into the currently bound OpenGL buffer
|
24 |
+
// GL_STATIC_DRAW is a hint about how we'll use the buffer
|
25 |
+
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
26 |
+
|
27 |
+
glClearColor(0.64f, 0.5f, 0.81f, 0.0f);
|
28 |
+
glColor3f(0.29f, 0.71f, 1.0f);
|
29 |
+
|
30 |
+
while( !pangolin::ShouldQuit() )
|
31 |
+
{
|
32 |
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
33 |
+
|
34 |
+
// Set our buffer as the current one for OpenGL
|
35 |
+
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
36 |
+
|
37 |
+
// This buffer contains floating point vertices with 3 dimensions.
|
38 |
+
// They starts from the 0th element and are packed without padding.
|
39 |
+
glVertexPointer(3, GL_FLOAT, 0, 0);
|
40 |
+
|
41 |
+
// Use Them!
|
42 |
+
glEnableClientState(GL_VERTEX_ARRAY);
|
43 |
+
|
44 |
+
// Connect the first 3 of these vertices to form a triangle!
|
45 |
+
glDrawArrays(GL_TRIANGLES, 0, 3);
|
46 |
+
|
47 |
+
// Disable the stuff we enabled...
|
48 |
+
glDisableClientState(GL_VERTEX_ARRAY);
|
49 |
+
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
50 |
+
|
51 |
+
pangolin::FinishFrame();
|
52 |
+
}
|
53 |
+
|
54 |
+
// Deallocate the OpenGL buffer we made
|
55 |
+
glDeleteBuffers(1, &VBO);
|
56 |
+
}
|
57 |
+
|
58 |
+
int main( int /*argc*/, char** /*argv*/ )
|
59 |
+
{
|
60 |
+
sample();
|
61 |
+
return 0;
|
62 |
+
}
|
third-party/DPVO/Pangolin/examples/BasicOpenGL/2_gl_intro_pango_triangle_vbo.cpp
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/display/display.h>
|
2 |
+
#include <pangolin/gl/glvbo.h>
|
3 |
+
|
4 |
+
void sample()
|
5 |
+
{
|
6 |
+
pangolin::CreateWindowAndBind("Pango GL Triangle with VBO", 500, 500);
|
7 |
+
|
8 |
+
// Create an OpenGL Buffer containing the vertices of a triangle
|
9 |
+
pangolin::GlBuffer vbo(pangolin::GlArrayBuffer,
|
10 |
+
std::vector<Eigen::Vector3f>{
|
11 |
+
{-0.5f, -0.5f, 0.0f},
|
12 |
+
{ 0.5f, -0.5f, 0.0f },
|
13 |
+
{ 0.0f, 0.5f, 0.0f }
|
14 |
+
}
|
15 |
+
);
|
16 |
+
|
17 |
+
glClearColor(0.64f, 0.5f, 0.81f, 0.0f);
|
18 |
+
glColor3f(0.29f, 0.71f, 1.0f);
|
19 |
+
|
20 |
+
while( !pangolin::ShouldQuit() )
|
21 |
+
{
|
22 |
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
23 |
+
|
24 |
+
// Connect the first 3 vertices in the GL Buffer to form a triangle!
|
25 |
+
pangolin::RenderVbo(vbo, GL_TRIANGLES);
|
26 |
+
|
27 |
+
pangolin::FinishFrame();
|
28 |
+
}
|
29 |
+
}
|
30 |
+
|
31 |
+
int main( int /*argc*/, char** /*argv*/ )
|
32 |
+
{
|
33 |
+
sample();
|
34 |
+
return 0;
|
35 |
+
}
|
third-party/DPVO/Pangolin/examples/BasicOpenGL/3_gl_intro_classic_triangle_vbo_shader.cpp
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/display/display.h>
|
2 |
+
|
3 |
+
const char* vertex_shader = R"Shader(
|
4 |
+
#version 120
|
5 |
+
attribute vec3 a_position;
|
6 |
+
varying vec2 v_pos;
|
7 |
+
|
8 |
+
void main() {
|
9 |
+
gl_Position = vec4(a_position, 1.0);
|
10 |
+
v_pos = a_position.xy;
|
11 |
+
}
|
12 |
+
)Shader";
|
13 |
+
|
14 |
+
const char* fragment_shader = R"Shader(
|
15 |
+
#version 120
|
16 |
+
varying vec2 v_pos;
|
17 |
+
uniform float u_time;
|
18 |
+
|
19 |
+
vec3 colorA = vec3(0.905,0.045,0.045);
|
20 |
+
vec3 colorB = vec3(0.995,0.705,0.051);
|
21 |
+
|
22 |
+
void main() {
|
23 |
+
float pattern = sin(10*v_pos.y + u_time) * sin(10*v_pos.x + u_time) * 0.5 + 0.5;
|
24 |
+
vec3 color = mix(colorA, colorB, pattern);
|
25 |
+
gl_FragColor = vec4(color, 1.0);
|
26 |
+
}
|
27 |
+
)Shader";
|
28 |
+
|
29 |
+
// Here is an example of Pangolin being used *just* for windowing.
|
30 |
+
// Influenced by https://learnopengl.com/Getting-started/Hello-Triangle
|
31 |
+
void sample()
|
32 |
+
{
|
33 |
+
pangolin::CreateWindowAndBind("Classic GL Triangle With VBO and Shader", 500, 500);
|
34 |
+
|
35 |
+
// Build and compile our shader program
|
36 |
+
// ------------------------------------
|
37 |
+
|
38 |
+
// Create a vertex shader GL object
|
39 |
+
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
40 |
+
|
41 |
+
// Tell GL the source code to use
|
42 |
+
glShaderSource(vertexShader, 1, &vertex_shader, NULL);
|
43 |
+
|
44 |
+
// Actually compile the program
|
45 |
+
glCompileShader(vertexShader);
|
46 |
+
|
47 |
+
// Check if the compilation was successfull, and print anyn errors
|
48 |
+
int success;
|
49 |
+
char infoLog[512];
|
50 |
+
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
|
51 |
+
if (!success)
|
52 |
+
{
|
53 |
+
glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
|
54 |
+
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
|
55 |
+
}
|
56 |
+
|
57 |
+
// Repeat for the Fragment shader
|
58 |
+
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
59 |
+
glShaderSource(fragmentShader, 1, &fragment_shader, NULL);
|
60 |
+
glCompileShader(fragmentShader);
|
61 |
+
// check for shader compile errors
|
62 |
+
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
|
63 |
+
if (!success)
|
64 |
+
{
|
65 |
+
glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
|
66 |
+
std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
|
67 |
+
}
|
68 |
+
|
69 |
+
// Link the vertex and fragment shaders into one complete program
|
70 |
+
unsigned int shaderProgram = glCreateProgram();
|
71 |
+
glAttachShader(shaderProgram, vertexShader);
|
72 |
+
glAttachShader(shaderProgram, fragmentShader);
|
73 |
+
glLinkProgram(shaderProgram);
|
74 |
+
|
75 |
+
// Check for linking errors
|
76 |
+
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
|
77 |
+
if (!success) {
|
78 |
+
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
|
79 |
+
std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
|
80 |
+
}
|
81 |
+
|
82 |
+
// Delete the now unused shader objects
|
83 |
+
glDeleteShader(vertexShader);
|
84 |
+
glDeleteShader(fragmentShader);
|
85 |
+
|
86 |
+
// Setup the Triangle VBO
|
87 |
+
// ------------------------------------
|
88 |
+
|
89 |
+
// List coordinates of a triangle
|
90 |
+
const float vertices[] = {
|
91 |
+
-0.5f, -0.5f, 0.0f,
|
92 |
+
0.5f, -0.5f, 0.0f,
|
93 |
+
0.0f, 0.5f, 0.0f
|
94 |
+
};
|
95 |
+
|
96 |
+
// Create an OpenGL Buffer to store these coordinates
|
97 |
+
unsigned int VBO;
|
98 |
+
glGenBuffers(1, &VBO);
|
99 |
+
|
100 |
+
// Set that buffer as the current GL buffer
|
101 |
+
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
102 |
+
|
103 |
+
// Copy our host data into the currently bound OpenGL buffer
|
104 |
+
// GL_STATIC_DRAW is a hint about how we'll use the buffer
|
105 |
+
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
106 |
+
|
107 |
+
// Enable use of the shader that we created for future rendering commands
|
108 |
+
glUseProgram(shaderProgram);
|
109 |
+
|
110 |
+
glClearColor(0.64f, 0.5f, 0.81f, 0.0f);
|
111 |
+
|
112 |
+
// Setup a variable to progress a simple animation as a function of time
|
113 |
+
float time = 0.01f;
|
114 |
+
|
115 |
+
while( !pangolin::ShouldQuit() )
|
116 |
+
{
|
117 |
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
118 |
+
|
119 |
+
// Set our buffer as the current one for OpenGL
|
120 |
+
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
121 |
+
|
122 |
+
// This buffer contains floating point vertices with 3 dimensions.
|
123 |
+
// They starts from the 0th element and are packed without padding.
|
124 |
+
glVertexPointer(3, GL_FLOAT, 0, 0);
|
125 |
+
|
126 |
+
// Use Them!
|
127 |
+
glEnableClientState(GL_VERTEX_ARRAY);
|
128 |
+
|
129 |
+
GLint u_time = glGetUniformLocation(shaderProgram, "u_time");
|
130 |
+
glUniform1f( u_time, time);
|
131 |
+
|
132 |
+
// Connect the first 3 of these vertices to form a triangle!
|
133 |
+
glDrawArrays(GL_TRIANGLES, 0, 3);
|
134 |
+
|
135 |
+
// Disable the stuff we enabled...
|
136 |
+
glDisableClientState(GL_VERTEX_ARRAY);
|
137 |
+
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
138 |
+
|
139 |
+
time += 0.01;
|
140 |
+
|
141 |
+
pangolin::FinishFrame();
|
142 |
+
}
|
143 |
+
|
144 |
+
// Deallocate the OpenGL buffer we made
|
145 |
+
glDeleteBuffers(1, &VBO);
|
146 |
+
|
147 |
+
// Deallocate the GlSl Shader program
|
148 |
+
glDeleteProgram(shaderProgram);
|
149 |
+
}
|
150 |
+
|
151 |
+
int main( int /*argc*/, char** /*argv*/ )
|
152 |
+
{
|
153 |
+
sample();
|
154 |
+
return 0;
|
155 |
+
}
|
third-party/DPVO/Pangolin/examples/BasicOpenGL/3_gl_intro_pango_triangle_vbo_shader.cpp
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/display/display.h>
|
2 |
+
#include <pangolin/gl/gldraw.h>
|
3 |
+
#include <pangolin/gl/glvbo.h>
|
4 |
+
#include <pangolin/gl/glsl.h>
|
5 |
+
|
6 |
+
const std::string my_shader = R"Shader(
|
7 |
+
@start vertex
|
8 |
+
#version 120
|
9 |
+
attribute vec3 a_position;
|
10 |
+
varying vec2 v_pos;
|
11 |
+
|
12 |
+
void main() {
|
13 |
+
gl_Position = vec4(a_position, 1.0);
|
14 |
+
v_pos = a_position.xy;
|
15 |
+
}
|
16 |
+
|
17 |
+
@start fragment
|
18 |
+
#version 120
|
19 |
+
varying vec2 v_pos;
|
20 |
+
uniform float u_time;
|
21 |
+
|
22 |
+
vec3 colorA = vec3(0.905,0.045,0.045);
|
23 |
+
vec3 colorB = vec3(0.995,0.705,0.051);
|
24 |
+
|
25 |
+
void main() {
|
26 |
+
float pattern = sin(10*v_pos.y + u_time) * sin(10*v_pos.x + u_time) * 0.5 + 0.5;
|
27 |
+
vec3 color = mix(colorA, colorB, pattern);
|
28 |
+
gl_FragColor = vec4(color, 1.0);
|
29 |
+
}
|
30 |
+
)Shader";
|
31 |
+
|
32 |
+
void sample()
|
33 |
+
{
|
34 |
+
pangolin::CreateWindowAndBind("Pango GL Triangle With VBO and Shader", 500, 500);
|
35 |
+
|
36 |
+
pangolin::GlBuffer vbo(pangolin::GlArrayBuffer,
|
37 |
+
std::vector<Eigen::Vector3f>{
|
38 |
+
{-0.5f, -0.5f, 0.0f},
|
39 |
+
{ 0.5f, -0.5f, 0.0f },
|
40 |
+
{ 0.0f, 0.5f, 0.0f }
|
41 |
+
}
|
42 |
+
);
|
43 |
+
|
44 |
+
// Encapsulate a GlSl shader program to define how to render OpenGL primitives
|
45 |
+
// The Pangolin GlSlProgram has a preprocessor which can seperate different kinds of
|
46 |
+
// shaders and manage compile-time defines.
|
47 |
+
pangolin::GlSlProgram prog;
|
48 |
+
prog.AddShader( pangolin::GlSlAnnotatedShader, my_shader );
|
49 |
+
prog.Link();
|
50 |
+
prog.Bind();
|
51 |
+
|
52 |
+
glClearColor(0.64f, 0.5f, 0.81f, 0.0f);
|
53 |
+
|
54 |
+
// Setup a variable to progress a simple animation as a function of time
|
55 |
+
float time = 0.01f;
|
56 |
+
|
57 |
+
while( !pangolin::ShouldQuit() )
|
58 |
+
{
|
59 |
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
60 |
+
|
61 |
+
// Update the shader u_time parameter and render the triangles
|
62 |
+
// whilst this shader is active
|
63 |
+
prog.SetUniform("u_time", time);
|
64 |
+
pangolin::RenderVbo(vbo, GL_TRIANGLES);
|
65 |
+
time += 0.01;
|
66 |
+
|
67 |
+
pangolin::FinishFrame();
|
68 |
+
}
|
69 |
+
}
|
70 |
+
|
71 |
+
int main( int /*argc*/, char** /*argv*/ )
|
72 |
+
{
|
73 |
+
sample();
|
74 |
+
return 0;
|
75 |
+
}
|
third-party/DPVO/Pangolin/examples/BasicOpenGL/4_gl_intro_viewport.cpp
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/display/display.h>
|
2 |
+
#include <pangolin/display/view.h>
|
3 |
+
#include <pangolin/gl/glvbo.h>
|
4 |
+
|
5 |
+
void sample()
|
6 |
+
{
|
7 |
+
pangolin::CreateWindowAndBind("Pango GL Triangle with VBO", 1000, 500);
|
8 |
+
|
9 |
+
// A pangolin::View is an object oriented encapsulation of a glViewport.
|
10 |
+
// It describes which rectangle of pixels in the window map to the OpenGL
|
11 |
+
// Normalized Device Coordinates (NDC) for rendering.
|
12 |
+
// See https://www.songho.ca/opengl/gl_transform.html for technical description
|
13 |
+
// of OpenGL coordinate systems and transforms.
|
14 |
+
|
15 |
+
// The View::SetBounds() method accepts the bottom, top, left, right arguments
|
16 |
+
// which correspond to the vertical min/max and horizontal min/max locations.
|
17 |
+
// Units can be fractional [0,1] representing bottom/left of window to top/right.
|
18 |
+
// You can use pangolin::Attach::Pix() to specify an absolute pixel coordinate
|
19 |
+
// relative to the bottom or left edge instead of a fractional one.
|
20 |
+
// pangolin::Attach::ReversePix() is instead relative to the top or right edge.
|
21 |
+
//
|
22 |
+
// A View is parented to the main window (pangolin::DisplayBase()) by default,
|
23 |
+
// but that can be changed with the View::AddDisplay() method to re-child to any
|
24 |
+
// other View. The specified bounds are relative to the parent and will
|
25 |
+
// be recalculated automatically during resize events.
|
26 |
+
|
27 |
+
// Create two 'views' representing the left and right half of the window.
|
28 |
+
auto& view1 = pangolin::CreateDisplay().SetBounds(0.0, 1.0, 0.0, 0.5);
|
29 |
+
auto& view2 = pangolin::CreateDisplay().SetBounds(0.0, 1.0, 0.5, 1.0);
|
30 |
+
|
31 |
+
// Create an OpenGL Buffer containing the vertices of a triangle
|
32 |
+
pangolin::GlBuffer vertices(pangolin::GlArrayBuffer,
|
33 |
+
std::vector<Eigen::Vector3f>{
|
34 |
+
{-0.5f, -0.5f, 0.0f},
|
35 |
+
{ 0.5f, -0.5f, 0.0f },
|
36 |
+
{ 0.0f, 0.5f, 0.0f }
|
37 |
+
}
|
38 |
+
);
|
39 |
+
|
40 |
+
// Set the background color
|
41 |
+
glClearColor(0.64f, 0.5f, 0.81f, 0.0f);
|
42 |
+
|
43 |
+
while( !pangolin::ShouldQuit() )
|
44 |
+
{
|
45 |
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
46 |
+
|
47 |
+
// Views also maintain their expected visibility, but since we're doing our own
|
48 |
+
// rendering inside the view we need to check this manually before drawing.
|
49 |
+
if(view1.IsShown()) {
|
50 |
+
// 'Activating()' a view will use the calculated absolute pixel
|
51 |
+
// positions and pass them on to glViewport to describe where
|
52 |
+
// to render on the screen.
|
53 |
+
view1.Activate();
|
54 |
+
|
55 |
+
// Render a blue triangle
|
56 |
+
glColor3f(0.29f, 0.71f, 1.0f);
|
57 |
+
pangolin::RenderVbo(vertices, GL_TRIANGLES);
|
58 |
+
}
|
59 |
+
|
60 |
+
if(view2.IsShown()) {
|
61 |
+
view2.Activate();
|
62 |
+
|
63 |
+
// Render a green triangle
|
64 |
+
glColor3f(0.71f, 1.0f, 0.29f);
|
65 |
+
pangolin::RenderVbo(vertices, GL_TRIANGLES);
|
66 |
+
}
|
67 |
+
|
68 |
+
// FinishFrame processes any pending windowing events such as
|
69 |
+
// mouse clicks and resizes. Resizes will trigger View absolute
|
70 |
+
// bounds recalculations.
|
71 |
+
pangolin::FinishFrame();
|
72 |
+
}
|
73 |
+
}
|
74 |
+
|
75 |
+
int main( int /*argc*/, char** /*argv*/ )
|
76 |
+
{
|
77 |
+
sample();
|
78 |
+
return 0;
|
79 |
+
}
|
third-party/DPVO/Pangolin/examples/BasicOpenGL/5_gl_intro_view_transforms.cpp
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#include <pangolin/display/display.h>
|
2 |
+
#include <pangolin/display/view.h>
|
3 |
+
#include <pangolin/gl/opengl_render_state.h>
|
4 |
+
#include <pangolin/gl/gldraw.h>
|
5 |
+
|
6 |
+
// Please read the following fantastic resource on the
|
7 |
+
// OpenGL Transformation pipeline:
|
8 |
+
// http://www.songho.ca/opengl/gl_transform.html
|
9 |
+
//
|
10 |
+
// In order to manipulate the position of objects within a scene and the
|
11 |
+
// position and projection action of a virtual OpenGL camera, we must
|
12 |
+
// understand the distinct set of coordinate systems that OpenGL uses.
|
13 |
+
// The Terminology for OpenGL, DirectX, Computer Vision and Robotics Literature
|
14 |
+
// are all a bit different but the concepts remain the same.
|
15 |
+
|
16 |
+
void sample()
|
17 |
+
{
|
18 |
+
// Define the core parameters of a virtual OpenGL Camera which we will use
|
19 |
+
// to show a simple scene. These quantities are best thought of as analogous
|
20 |
+
// to the backplane dimensions and focal length (all in the same unit, say mm)
|
21 |
+
// of a virtual pinhole camera (i.e. a camera with no lens and just a pinhole
|
22 |
+
// apperture).
|
23 |
+
const int gl_camera_width = 100;
|
24 |
+
const int gl_camera_height = 100;
|
25 |
+
const double gl_camera_focal_length = 100.0;
|
26 |
+
|
27 |
+
// Create an application window of arbitrary dimensions
|
28 |
+
pangolin::CreateWindowAndBind("Pango GL Triangle with VBO", 1024, 768);
|
29 |
+
|
30 |
+
// Create a child view within the parent window which will stretch and grow
|
31 |
+
// with the application window but maintain a fixed aspect of width to height
|
32 |
+
// that will match the virtual camera we wish to render with respect to.
|
33 |
+
auto& view = pangolin::CreateDisplay()
|
34 |
+
.SetAspect( (float)gl_camera_width / (float)gl_camera_height);
|
35 |
+
|
36 |
+
pangolin::OpenGlRenderState render_transforms(
|
37 |
+
// The OpenGL Projection Matrix handles the transform from
|
38 |
+
// Camera coordinates into Normalized Device Coordinates (within the Viewport)
|
39 |
+
// It can be used to specify the action of the virtual OpenGL Camera
|
40 |
+
pangolin::ProjectionMatrix(
|
41 |
+
gl_camera_width, gl_camera_height,
|
42 |
+
gl_camera_focal_length, gl_camera_focal_length,
|
43 |
+
gl_camera_width/2.0, gl_camera_height/2.0,
|
44 |
+
0.01, // 'Near clipping plane' - The closest and furthest thing we can render
|
45 |
+
100.0 // 'Far clipping plane' in scene units. Required when rasterizing
|
46 |
+
// due to finite numerical precision.
|
47 |
+
),
|
48 |
+
// The OpenGL ModelView Matrix handles the transform from
|
49 |
+
// Object coordinates into Camera Coordinates. It is useful when
|
50 |
+
// vertices and renderable objects are specified in a fixed
|
51 |
+
// 'object centric' coordinate system but we want to transform
|
52 |
+
// how the object appears to move relative to the camera.
|
53 |
+
pangolin::ModelViewLookAt(
|
54 |
+
2.0, 2.0, 2.0, // coordinates of center of camera
|
55 |
+
0.0, 0.0, 0.0, // coordinates to point camera towards
|
56 |
+
pangolin::AxisY // The 'up' axis for the camera
|
57 |
+
)
|
58 |
+
);
|
59 |
+
|
60 |
+
// We explicitly tell GL to only 'paint' pixels if they are in-front
|
61 |
+
// of what is already in the framebuffer. Try turning this off and see
|
62 |
+
// what happens!
|
63 |
+
glEnable(GL_DEPTH_TEST);
|
64 |
+
|
65 |
+
// Set the background color set during the glClear operation below.
|
66 |
+
glClearColor(0.64f, 0.5f, 0.81f, 0.0f);
|
67 |
+
|
68 |
+
for( double time=0.0; !pangolin::ShouldQuit(); time += 0.01 )
|
69 |
+
{
|
70 |
+
// Clear both the color buffer but also the depth buffer.
|
71 |
+
// Try just clearing the color buffer and see what happens!
|
72 |
+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
73 |
+
|
74 |
+
// Set the camera position as a function of time.
|
75 |
+
// Here we use a circular orbit in XZ Plane.
|
76 |
+
render_transforms.SetModelViewMatrix(
|
77 |
+
pangolin::ModelViewLookAt(
|
78 |
+
2.0*sin(time), 2.0, 2.0*cos(time),
|
79 |
+
0.0, 0.0, 0.0,
|
80 |
+
pangolin::AxisY
|
81 |
+
)
|
82 |
+
);
|
83 |
+
|
84 |
+
if(view.IsShown()) {
|
85 |
+
view.Activate(render_transforms);
|
86 |
+
render_transforms.Apply();
|
87 |
+
pangolin::glDrawColouredCube();
|
88 |
+
}
|
89 |
+
|
90 |
+
pangolin::FinishFrame();
|
91 |
+
}
|
92 |
+
}
|
93 |
+
|
94 |
+
int main( int /*argc*/, char** /*argv*/ )
|
95 |
+
{
|
96 |
+
sample();
|
97 |
+
return 0;
|
98 |
+
}
|
third-party/DPVO/Pangolin/examples/BasicOpenGL/CMakeLists.txt
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Find Pangolin (https://github.com/stevenlovegrove/Pangolin)
|
2 |
+
find_package(Pangolin 0.8 REQUIRED)
|
3 |
+
include_directories(${Pangolin_INCLUDE_DIRS})
|
4 |
+
|
5 |
+
set(sample_srcs
|
6 |
+
1_gl_intro_classic_triangle.cpp
|
7 |
+
1_gl_intro_pango_triangle.cpp
|
8 |
+
2_gl_intro_classic_triangle_vbo.cpp
|
9 |
+
2_gl_intro_pango_triangle_vbo.cpp
|
10 |
+
3_gl_intro_classic_triangle_vbo_shader.cpp
|
11 |
+
3_gl_intro_pango_triangle_vbo_shader.cpp
|
12 |
+
4_gl_intro_viewport.cpp
|
13 |
+
5_gl_intro_view_transforms.cpp
|
14 |
+
)
|
15 |
+
|
16 |
+
foreach( sample_src ${sample_srcs})
|
17 |
+
get_filename_component(sample_name ${sample_src} NAME_WE)
|
18 |
+
set(target_name "tutorial_${sample_name}")
|
19 |
+
add_executable(${target_name} ${sample_src})
|
20 |
+
target_link_libraries(${target_name} pango_display)
|
21 |
+
endforeach()
|
22 |
+
|
third-party/DPVO/Pangolin/examples/BasicOpenGL/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Basic OpenGL Tutorial with Pangolin
|
2 |
+
|
3 |
+
By popular request, here is a set of basic OpenGL intros for people wanting to get going quickly with Pangolin.
|
4 |
+
|
5 |
+
#### gl_intro_triangle
|
6 |
+
|
7 |
+
Drawing a triangle is the *HelloWorld* of OpenGL and a good place for us to start.
|
8 |
+
|
9 |
+
`1_gl_intro_classic_triangle.cpp`: In the **classic** version, Pangolin's main use is creating a window and OpenGL context that we can use without platform specific code. [GLFW](https://www.glfw.org/) would be a good alternative if this is your only aim. Notice drawing the triangle takes 3 function calls and there is a matched Enable... / Disable... call that we must take care to pair.
|
10 |
+
|
11 |
+
`1_gl_intro_pango_triangle.cpp`: This is the **pango** version where we make use of a simple utilitiy method to accomplish the same as before. Not much to write home about, but you can see the general ethos of Pangolin is to reduce OpenGL boilerplate without getting too fancy.
|
12 |
+
|
13 |
+
|
14 |
+
|
15 |
+
#### gl_intro_triangle_vbo
|
16 |
+
|
17 |
+
`2_gl_intro_classic_triangle_vbo.cpp`: In the previous example, the vertices of the triangle live in your machines main memory. Your poor graphics card is being drip fed the vertices each time the image is redrawn every frame. This VBO (Vertex Buffer Object) example instead copies the vertex data to your GPU's memory once, and then only references it later. Your graphics card then gets to keep it handy (in probably faster memory) without repeated transfers over a shared bus. The **classic** version uses only the regular OpenGL API. You'll notice it is a C API, and we're notably missing C++ idioms like [RAII](https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization) to make sure we don't leak resources such as the VBO buffer.
|
18 |
+
|
19 |
+
`2_gl_intro_pango_triangle_vbo.cpp`: The **pango** version gets a bit more compact where we make use of the `pangolin::GlBuffer` wrapper to manage the GL object lifetime.
|
20 |
+
|
21 |
+
|
22 |
+
|
23 |
+
#### gl_intro_triangle_vbo_shader
|
24 |
+
|
25 |
+
`3_gl_intro_classic_triangle_vbo_shader.cpp`: Okay, so I lied - people don't really recommend using the 'fixed pipeline' for rendering stuff in OpenGL anymore - you're meant to use *GLSL shaders*, code that can run on your graphics hardware to more explicitly outline how you want things to appear. Most of what we had before was fine, but `glColor3f` is normally replaced by shader output, plus a bunch of other stuff that we've not introduced anyway, so don't panic. Personally, the **classic** version gives me shudders - just look at all the boilerplate!
|
26 |
+
|
27 |
+
`3_gl_intro_pango_triangle_vbo_shader.cpp`: The **pango** version shows how to use some of its C++ wrappers that you'll hopefully agree improve readability. Pangolin also has a few utilities to help with writing shaders, most notable here for those familiar with GLSL is Pangolins preprocessor which allows you to `#include`, `#expect` and `#define` things, as well as load multiple shaders from one file with the `@start` annotations.
|
28 |
+
|
29 |
+
|
30 |
+
|
31 |
+
#### gl_intro_viewport
|
32 |
+
|
33 |
+
Okay, I've finished trying to outline how much boilerplate is needed with the regular OpenGL API. Here is a simple intro on how to use Pangolin to organize sub-views of the main window. Pangolin will take care of resizing these views as the window is resized which you would otherwise do manually. Notice we've ditched the shaders again because unless you really care about performance, I wont tell people you're using the deprecated pipeline if you dont. It's really your choice - you can use Pangolin or not either way.
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
#### gl_intro_view_transforms
|
38 |
+
|
39 |
+
In this example we've moved from Flatland into 3D! Doing so requires us to get acquinted with a few important transforms that map from different coordinate systems. In the previous examples, our trusty triangle was getting defined on the XY plane within a box from bottom-left (-1.0,-1.0) to top-right (1.0,1.0) which is then mapped to pixels by the specified viewport. This coordinate system is called *normalized device coordinates*, so named because it is independent of final render resolution but represents the final 2D mapping of render primitives. There are some other important coordinate systems in OpenGL (namely clip coordinates), but what really matters is that whatever coordinate system we care about can somehow get converted into one OpenGL cares about. For this, we talk about coordinate transforms! To make like easy, we try to use linear transforms. Since perspective projection isn't linear, we use homogeneous coordinates to cheat. OpenGL's *fixed pipeline* has several built-in capabilities for working with these 4x4 homogeneous transforms, but the modern OpenGL way is to handle all that stuff ourselves. I'd recommend learning a bit more from the excellent resource [songho](https://www.songho.ca/opengl/gl_transform.html), but you can probably pick up a lot as a practitioner from the code.
|
third-party/DPVO/Pangolin/examples/CMakeLists.txt
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
add_subdirectory(BasicOpenGL)
|
2 |
+
|
3 |
+
add_subdirectory(HelloPangolin)
|
4 |
+
add_subdirectory(HelloPangolinThreads)
|
5 |
+
add_subdirectory(SimpleDisplay)
|
6 |
+
add_subdirectory(SimpleMultiDisplay)
|
7 |
+
add_subdirectory(SimpleDisplayImage)
|
8 |
+
add_subdirectory(SimplePlot)
|
9 |
+
add_subdirectory(SimpleVideo)
|
10 |
+
add_subdirectory(SimpleRecord)
|
11 |
+
|
12 |
+
if(NOT EMSCRIPTEN)
|
13 |
+
add_subdirectory(HelloPangolinOffscreen)
|
14 |
+
add_subdirectory(SimpleScene)
|
15 |
+
endif()
|