summaryrefslogtreecommitdiffstatshomepage
path: root/3rdparty/bgfx/tools/geometryv/geometryv.cpp
blob: bde6f8d00144bb2022a20624add903049d2da489 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
/*
 * Copyright 2019-2019 Attila Kocsis. All rights reserved.
 * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
 */

#include "common.h"

#include <bgfx/bgfx.h>

#include <bx/commandline.h>
#include <bx/easing.h>
#include <bx/file.h>
#include <bx/math.h>
#include <bx/os.h>
#include <bx/settings.h>

#include <entry/entry.h>
#include <entry/input.h>
#include <entry/cmd.h>
#include <entry/dialog.h>
#include <imgui/imgui.h>
#include <bgfx_utils.h>

#include <tinystl/allocator.h>
#include <tinystl/vector.h>
namespace stl = tinystl;
#include <string>
#include <algorithm>

#include <bgfx/embedded_shader.h>

#include "vs_mesh.bin.h"
#include "fs_mesh.bin.h"

#define SCENE_VIEW_ID 0

#define BGFX_GEOMETRYV_VERSION_MAJOR 1
#define BGFX_GEOMETRYV_VERSION_MINOR 0

static const bgfx::EmbeddedShader s_embeddedShaders[] =
{
	BGFX_EMBEDDED_SHADER(vs_mesh),
	BGFX_EMBEDDED_SHADER(fs_mesh),

	BGFX_EMBEDDED_SHADER_END()
};

static const char* s_supportedExt[] =
{
	"bin",
};

struct Binding
{
	enum Enum
	{
		App,
		View,
		Help,
		About,

		Count
	};
};

static const InputBinding s_bindingApp[] =
{
	{ entry::Key::KeyQ, entry::Modifier::None,  1, NULL, "exit"                },
	{ entry::Key::KeyF, entry::Modifier::None,  1, NULL, "graphics fullscreen" },

	INPUT_BINDING_END
};

const char* s_resetCmd =
	"view dolly\n"
	"view orbit\n"
	;

static const InputBinding s_bindingView[] =
{
	{ entry::Key::Esc,       entry::Modifier::None,       1, NULL, "exit"                    },

	{ entry::Key::Key1,      entry::Modifier::None,       1, NULL, "view dolly"              },

	{ entry::Key::Key0,      entry::Modifier::None,       1, NULL, s_resetCmd                },
	{ entry::Key::Plus,      entry::Modifier::None,       1, NULL, "view dolly +0.1"          },
	{ entry::Key::Minus,     entry::Modifier::None,       1, NULL, "view dolly -0.1"          },

	{ entry::Key::KeyW,      entry::Modifier::None,       1, NULL, "view orbit y -0.1"         },
	{ entry::Key::KeyS,      entry::Modifier::None,       1, NULL, "view orbit y +0.1"         },
	{ entry::Key::KeyA,      entry::Modifier::None,       1, NULL, "view orbit x +0.1"         },
	{ entry::Key::KeyD,      entry::Modifier::None,       1, NULL, "view orbit x -0.1"         },
	
	{ entry::Key::Up,        entry::Modifier::None,       1, NULL, "view file-up"            },
	{ entry::Key::Down,      entry::Modifier::None,       1, NULL, "view file-down"          },

	{ entry::Key::KeyI,      entry::Modifier::None,       1, NULL, "view info"               },

	{ entry::Key::KeyH,      entry::Modifier::None,       1, NULL, "view help"               },

	{ entry::Key::Return,    entry::Modifier::None,       1, NULL, "view files"              },

	{ entry::Key::Space,     entry::Modifier::None,       1, NULL, "view geo\n"              },

	INPUT_BINDING_END
};

static const InputBinding s_bindingHelp[] =
{
	{ entry::Key::Esc,  entry::Modifier::None,  1, NULL, "view help" },
	{ entry::Key::KeyH, entry::Modifier::None,  1, NULL, "view help" },
	INPUT_BINDING_END
};

static const InputBinding s_bindingAbout[] =
{
	{ entry::Key::Esc,  entry::Modifier::None,  1, NULL, "view about" },
	INPUT_BINDING_END
};

static const char* s_bindingName[] =
{
	"App",
	"View",
	"Help",
	"About",
};
BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_bindingName) );

static const InputBinding* s_binding[] =
{
	s_bindingApp,
	s_bindingView,
	s_bindingHelp,
	s_bindingAbout,
};
BX_STATIC_ASSERT(Binding::Count == BX_COUNTOF(s_binding) );

static const char* s_filter = ""
	"Bgfx geometry (bin) | *.bin\n"
	;

struct Camera
{
	Camera()
	{
		init(bx::Vec3(0.0f,0.0f,0.0f), 2.0f);
	}

	void init(const bx::Vec3& _center, float _distance)
	{
		m_target.curr = _center;
		m_target.dest = _center;

		m_pos.curr = _center;
		m_pos.curr.z -= _distance; 
		m_pos.dest = _center;
		m_pos.dest.z -= _distance; 

		m_orbit[0] = 0.0f;
		m_orbit[1] = 0.0f;
	}

	void mtxLookAt(float* _outViewMtx)
	{
		bx::mtxLookAt(_outViewMtx, m_pos.curr, m_target.curr);
	}

	void orbit(float _dx, float _dy)
	{
		m_orbit[0] += _dx;
		m_orbit[1] += _dy;
	}
	
	void distance(float _z)
	{
		const float cnear = 1.0f;
		const float cfar  = 100.0f;
		
		_z = bx::clamp(_z, cnear, cfar);
		
		bx::Vec3 toTarget     = bx::sub(m_target.dest, m_pos.dest);
		bx::Vec3 toTargetNorm = bx::normalize(toTarget);
		
		m_pos.dest = bx::mad(toTargetNorm, -_z, m_target.dest);
	}
	
	void dolly(float _dz)
	{
		const float cnear = 1.0f;
		const float cfar  = 100.0f;

		const bx::Vec3 toTarget     = bx::sub(m_target.dest, m_pos.dest);
		const float toTargetLen     = bx::length(toTarget);
		const float invToTargetLen  = 1.0f / (toTargetLen + bx::kFloatMin);
		const bx::Vec3 toTargetNorm = bx::mul(toTarget, invToTargetLen);

		float delta  = toTargetLen * _dz;
		float newLen = toTargetLen + delta;
		if ( (cnear  < newLen || _dz < 0.0f)
			&&   (newLen < cfar   || _dz > 0.0f) )
		{
			m_pos.dest = bx::mad(toTargetNorm, delta, m_pos.dest);
		}
	}

	void consumeOrbit(float _amount)
	{
		float consume[2];
		consume[0] = m_orbit[0] * _amount;
		consume[1] = m_orbit[1] * _amount;
		m_orbit[0] -= consume[0];
		m_orbit[1] -= consume[1];

		const bx::Vec3 toPos     = bx::sub(m_pos.curr, m_target.curr);
		const float toPosLen     = bx::length(toPos);
		const float invToPosLen  = 1.0f / (toPosLen + bx::kFloatMin);
		const bx::Vec3 toPosNorm = bx::mul(toPos, invToPosLen);

		float ll[2];
		bx::toLatLong(&ll[0], &ll[1], toPosNorm);
		ll[0] += consume[0];
		ll[1] -= consume[1];
		ll[1]  = bx::clamp(ll[1], 0.02f, 0.98f);

		const bx::Vec3 tmp  = bx::fromLatLong(ll[0], ll[1]);
		const bx::Vec3 diff = bx::mul(bx::sub(tmp, toPosNorm), toPosLen);

		m_pos.curr = bx::add(m_pos.curr, diff);
		m_pos.dest = bx::add(m_pos.dest, diff);
	}

	void update(float _dt)
	{
		const float amount = bx::min(_dt / 0.12f, 1.0f);

		consumeOrbit(amount);

		m_target.curr = bx::lerp(m_target.curr, m_target.dest, amount);
		m_pos.curr    = bx::lerp(m_pos.curr,    m_pos.dest,    amount);
	}

	struct Interp3f
	{
		bx::Vec3 curr;
		bx::Vec3 dest;
	};

	Interp3f m_target;
	Interp3f m_pos;
	float m_orbit[2];
};

struct Mouse
{
	Mouse()
	{
		m_dx = 0.0f;
		m_dy = 0.0f;
		m_prevMx = 0.0f;
		m_prevMx = 0.0f;
		m_scroll = 0;
		m_scrollPrev = 0;
	}

	void update(float _mx, float _my, int32_t _mz, uint32_t _width, uint32_t _height)
	{
		const float widthf  = float(int32_t(_width));
		const float heightf = float(int32_t(_height));

		// Delta movement.
		m_dx = float(_mx - m_prevMx)/widthf;
		m_dy = float(_my - m_prevMy)/heightf;

		m_prevMx = _mx;
		m_prevMy = _my;

		// Scroll.
		m_scroll = _mz - m_scrollPrev;
		m_scrollPrev = _mz;
	}

	float m_dx; // Screen space.
	float m_dy;
	float m_prevMx;
	float m_prevMy;
	int32_t m_scroll;
	int32_t m_scrollPrev;
};

struct View
{
	View()
		: m_fileIndex(0)
		, m_width(1280)
		, m_height(720)
		, m_help(false)
		, m_about(false)
		, m_info(false)
		, m_files(false)
		, m_meshCenter(0.0f,0.0f,0.0f)
		, m_meshRadius(1.0f)
		, m_idleTimer(0.0f)
	{
		load();
	}

	~View()
	{
	}
	int32_t cmd(int32_t _argc, char const* const* _argv)
	{
		if (_argc >= 2)
		{
			if (0 == bx::strCmp(_argv[1], "file-up") )
			{
				m_fileIndex = bx::uint32_satsub(m_fileIndex, 1);
			}
			else if (0 == bx::strCmp(_argv[1], "file-down") )
			{
				uint32_t numFiles = bx::uint32_satsub(uint32_t(m_fileList.size() ), 1);
				++m_fileIndex;
				m_fileIndex = bx::uint32_min(m_fileIndex, numFiles);
			}
			else if (0 == bx::strCmp(_argv[1], "help") )
			{
				m_help ^= true;
			}
			else if (0 == bx::strCmp(_argv[1], "about") )
			{
				m_about ^= true;
			}
			else if (0 == bx::strCmp(_argv[1], "save") )
			{
				save();
			}
			else if (0 == bx::strCmp(_argv[1], "info") )
			{
				m_info ^= true;
			}
			else if (0 == bx::strCmp(_argv[1], "files") )
			{
				m_files ^= true;
			}
			else if (0 == bx::strCmp(_argv[1], "dolly") )
			{
				if (_argc >= 3)
				{
					float dolly;
					bx::fromString(&dolly, _argv[2]);

					if (_argv[2][0] == '+'
						||  _argv[2][0] == '-')
					{
						m_camera.dolly(dolly);
						m_idleTimer = 0.0f;
					}
				}
				else
				{
					m_camera.distance(m_meshRadius * 2.0f);
				}
			}
			else if (0 == bx::strCmp(_argv[1], "orbit") )
			{
				if (_argc >= 4)
				{
					int axis = (_argv[2][0] == 'x' ? 0 : 1);
					float orbit[2] = { 0.0f, 0.0f};
					bx::fromString(&orbit[axis], _argv[3]);
					
					m_camera.orbit(orbit[0], orbit[1]);
					m_idleTimer = 0.0f;
				}
				else
				{
					m_camera.m_target.dest = m_meshCenter;
						
					m_camera.m_pos.dest = m_meshCenter;
					m_camera.m_pos.dest.z -= m_meshRadius * 2.0f;
						
					m_camera.m_orbit[0] = 0.0f;
					m_camera.m_orbit[1] = 0.0f;
				}
			}
		}

		return 0;
	}

	static bool sortNameAscending(const std::string& _lhs, const std::string& _rhs)
	{
		return 0 > bx::strCmpV(_lhs.c_str(), _rhs.c_str() );
	}

	void updateFileList(const bx::FilePath& _filePath)
	{
		bx::DirectoryReader dr;

		if (bx::open(&dr, _filePath) )
		{
			m_path = _filePath;
		}
		else if (bx::open(&dr, _filePath.getPath() ) )
		{
			m_path = _filePath.getPath();
		}
		else
		{
			DBG("File path `%s` not found.", _filePath.getCPtr() );
			return;
		}

		bx::Error err;

		m_fileList.clear();

		while (err.isOk() )
		{
			bx::FileInfo fi;
			bx::read(&dr, fi, &err);

			if (err.isOk()
			&&  bx::FileType::File == fi.type)
			{
				bx::StringView ext = fi.filePath.getExt();

				if (!ext.isEmpty() )
				{
					ext.set(ext.getPtr()+1, ext.getTerm() );

					bool supported = false;
					for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
					{
						const bx::StringView supportedExt(s_supportedExt[ii]);

						if (0 == bx::strCmpI(bx::max(ext.getPtr(), ext.getTerm() - supportedExt.getLength() ), supportedExt) )
						{
							supported = true;
							break;
						}
					}

					if (supported)
					{
						const bx::StringView fileName = fi.filePath.getFileName();
						m_fileList.push_back(std::string(fileName.getPtr(), fileName.getTerm() ) );
					}
				}
			}
		}

		bx::close(&dr);

		std::sort(m_fileList.begin(), m_fileList.end(), sortNameAscending);

		m_fileIndex = 0;
		uint32_t idx = 0;

		const bx::StringView fileName = _filePath.getFileName();

		for (FileList::const_iterator it = m_fileList.begin(); it != m_fileList.end(); ++it, ++idx)
		{
			if (0 == bx::strCmpI(it->c_str(), fileName) )
			{
				// If it is case-insensitive match then might be correct one, but keep
				// searching.
				m_fileIndex = idx;

				if (0 == bx::strCmp(it->c_str(), fileName) )
				{
					// If it is exact match we're done.
					break;
				}
			}
		}
	}

	void load()
	{
		bx::FilePath filePath(bx::Dir::Home);
		filePath.join(".config/bgfx/geometryv.ini");

		bx::Settings settings(entry::getAllocator() );

		bx::FileReader reader;
		if (bx::open(&reader, filePath) )
		{
			bx::read(&reader, settings);
			bx::close(&reader);

			if (!bx::fromString(&m_width, settings.get("view/width") ) )
			{
				m_width = 1280;
			}

			if (!bx::fromString(&m_height, settings.get("view/height") ) )
			{
				m_height = 720;
			}
		}
	}

	void save()
	{
		bx::FilePath filePath(bx::Dir::Home);
		filePath.join(".config/bgfx/geometryv.ini");

		if (bx::makeAll(filePath.getPath() ) )
		{
			bx::Settings settings(entry::getAllocator() );

			char tmp[256];

			bx::toString(tmp, sizeof(tmp), m_width);
			settings.set("view/width", tmp);

			bx::toString(tmp, sizeof(tmp), m_height);
			settings.set("view/height", tmp);

			bx::FileWriter writer;
			if (bx::open(&writer, filePath) )
			{
				bx::write(&writer, settings);
				bx::close(&writer);
			}
		}
	}

	bx::FilePath m_path;

	typedef stl::vector<std::string> FileList;
	FileList m_fileList;

	uint32_t m_fileIndex;
	uint32_t m_width;
	uint32_t m_height;
	bool     m_help;
	bool     m_about;
	bool     m_info;
	bool     m_files;

	Camera	m_camera;
	Mouse   m_mouse;
	bx::Vec3 m_meshCenter;
	float	 m_meshRadius;
	float	 m_idleTimer;

}; 

int cmdView(CmdContext* /*_context*/, void* _userData, int _argc, char const* const* _argv)
{
	View* view = static_cast<View*>(_userData);
	return view->cmd(_argc, _argv);
}

template<bx::LerpFn lerpT, bx::EaseFn easeT>
struct InterpolatorT
{
	float from;
	float to;
	float duration;
	int64_t offset;

	InterpolatorT(float _value)
	{
		reset(_value);
	}

	void reset(float _value)
	{
		from     = _value;
		to       = _value;
		duration = 0.0;
		offset   = bx::getHPCounter();
	}

	void set(float _value, float _duration)
	{
		if (_value != to)
		{
			from     = getValue();
			to       = _value;
			duration = _duration;
			offset   = bx::getHPCounter();
		}
	}

	float getValue()
	{
		if (isActive() )
		{
			const double freq = double(bx::getHPFrequency() );
			int64_t now = bx::getHPCounter();
			float time = (float)(double(now - offset) / freq);
			float lerp = bx::clamp(time, 0.0f, duration) / duration;
			return lerpT(from, to, easeT(lerp) );
		}

		return to;
	}

	bool isActive() const
	{
		const double freq = double(bx::getHPFrequency() );
		int64_t now = bx::getHPCounter();
		float time = (float)(double(now - offset) / freq);
		float lerp = bx::clamp(time, 0.0f, duration) / duration;
		return lerp < 1.0f;
	}
};

typedef InterpolatorT<bx::lerp,      bx::easeInOutQuad>  Interpolator;

void keyBindingHelp(const char* _bindings, const char* _description)
{
	ImGui::TextColored(ImVec4(1.0f, 1.0f, 0.0f, 1.0f), _bindings);
	ImGui::SameLine(140);
	ImGui::Text(_description);
}

void help(const char* _error = NULL)
{
	if (NULL != _error)
	{
		fprintf(stderr, "Error:\n%s\n\n", _error);
	}

	fprintf(stderr
		, "geometryv, bgfx geometry viewer tool, version %d.%d.%d.\n"
		  "Copyright 2019-2019 Attila Kocsis. All rights reserved.\n"
		  "License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n\n"
		, BGFX_GEOMETRYV_VERSION_MAJOR
		, BGFX_GEOMETRYV_VERSION_MINOR
		, BGFX_API_VERSION
		);

	fprintf(stderr
		, "Usage: geometryv <file path>\n"
		  "\n"
		  "Supported input file types:\n"
		  );

	for (uint32_t ii = 0; ii < BX_COUNTOF(s_supportedExt); ++ii)
	{
		fprintf(stderr, "    *.%s\n", s_supportedExt[ii]);
	}

	fprintf(stderr
		, "\n"
		  "Options:\n"
		  "  -h, --help               Help.\n"
		  "  -v, --version            Version information only.\n"
		  "\n"
		  "For additional information, see https://github.com/bkaradzic/bgfx\n"
		);
}

int _main_(int _argc, char** _argv)
{
	bx::CommandLine cmdLine(_argc, _argv);

	if (cmdLine.hasArg('v', "version") )
	{
		fprintf(stderr
			, "geometryv, bgfx geometry viewer tool, version %d.%d.%d.\n"
			, BGFX_GEOMETRYV_VERSION_MAJOR
			, BGFX_GEOMETRYV_VERSION_MINOR
			, BGFX_API_VERSION
			);
		return bx::kExitSuccess;
	}

	if (cmdLine.hasArg('h', "help") )
	{
		help();
		return bx::kExitFailure;
	}

	uint32_t debug = BGFX_DEBUG_TEXT;

	inputAddBindings(s_bindingName[Binding::App],  s_binding[Binding::App]);
	inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);

	View view;
	cmdAdd("view", cmdView, &view);

	entry::setWindowFlags(entry::WindowHandle{0}, ENTRY_WINDOW_FLAG_ASPECT_RATIO, false);
	entry::setWindowSize(entry::WindowHandle{0}, view.m_width, view.m_height);

	bgfx::Init init;
	init.resolution.width = view.m_width;
	init.resolution.width = view.m_height;
	init.resolution.reset = 0
		| BGFX_RESET_VSYNC
		| BGFX_RESET_MSAA_X16
		;

	bgfx::init(init);

	// Set view 0 clear state.
	bgfx::setViewClear(SCENE_VIEW_ID
		, BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH
		, 0x000000ff
		, 1.0f
		, 0
		);

	imguiCreate();

	const bgfx::Caps* caps = bgfx::getCaps();
	bgfx::RendererType::Enum type = caps->rendererType;

	bgfx::ShaderHandle vsMesh      = bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_mesh");
	bgfx::ShaderHandle fsMesh      = bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_mesh");

	bgfx::ProgramHandle meshProgram = bgfx::createProgram(
		  vsMesh
		, fsMesh
		, true
		);

	float speed = 0.37f;
	float time  = 0.0f;

	Interpolator menuFade(5.0f);

	auto anyActive = [&]() -> bool
	{
		return false
			|| ImGui::MouseOverArea()
			|| menuFade.isActive()
			;
	};

	const char* filePath = _argc < 2 ? "" : _argv[1];

	std::string path = filePath;
	{
		bx::FilePath fp(filePath);
		view.updateFileList(fp);
	}

	int exitcode = bx::kExitSuccess;
	Mesh* mesh = NULL;

	{
		uint32_t fileIndex = 0;

		entry::WindowState windowState;
		while (!entry::processWindowEvents(windowState, debug, init.resolution.reset) )
		{
			const entry::MouseState& mouseState = windowState.m_mouse;
			view.m_width  = windowState.m_width;
			view.m_height = windowState.m_height;

			if (!windowState.m_dropFile.isEmpty() )
			{
				view.updateFileList(windowState.m_dropFile);
				windowState.m_dropFile.clear();
			}

			imguiBeginFrame(mouseState.m_mx
				,  mouseState.m_my
				, (mouseState.m_buttons[entry::MouseButton::Left  ] ? IMGUI_MBUT_LEFT   : 0)
				| (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT  : 0)
				| (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
				,  mouseState.m_mz
				,  uint16_t(view.m_width)
				,  uint16_t(view.m_height)
				);

			bool modalWindow = view.m_help || view.m_about;
			bool overArea = false
				|| ImGui::GetMousePos().y <= ImGui::GetTextLineHeightWithSpacing()
				|| ImGui::MouseOverArea()
				;
			overArea &= !modalWindow;

			if (overArea)
			{
				menuFade.set(5.0f, 0.25f);
			}
			else if (modalWindow)
			{
				menuFade.reset(0.0f);
			}
			else
			{
				menuFade.set(0.0f, 2.0f);
			}

			ImGui::PushStyleVar(ImGuiStyleVar_Alpha, bx::clamp(menuFade.getValue(), 0.0f, 1.0f) );
			ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 0.0f);

			if (ImGui::BeginMainMenuBar() )
			{
				if (ImGui::BeginMenu("File"))
				{
					if (ImGui::MenuItem("Open File") )
					{
						bx::FilePath tmp = view.m_path;
						if (openFileSelectionDialog(
							  tmp
							, FileSelectionDialogType::Open
							, "geometryv: Open File"
							, s_filter
							) )
						{
							view.updateFileList(tmp);
						}
					}

					if (ImGui::MenuItem("Show File List", NULL, view.m_files) )
					{
						cmdExec("view files");
					}

					ImGui::Separator();
					if (ImGui::MenuItem("Exit") )
					{
						cmdExec("exit");
					}

					ImGui::EndMenu();
				}

				if (ImGui::BeginMenu("View") )
				{
					if (ImGui::MenuItem("Info", NULL, view.m_info) )
					{
						cmdExec("view info");
					}

					if (ImGui::MenuItem("Reset") )
					{
						cmdExec(s_resetCmd);
					}

					ImGui::Separator();

					if (ImGui::MenuItem("Save Options") )
					{
						cmdExec("view save");
					}

					ImGui::EndMenu();
				}

				if (ImGui::BeginMenu("Help") )
				{
					if (ImGui::MenuItem("View Help") )
					{
						cmdExec("view help");
					}

					ImGui::Separator();
					if (ImGui::MenuItem("About") )
					{
						cmdExec("view about");
					}

					ImGui::EndMenu();
				}

				if (0 != view.m_fileList.size() )
				{
					ImGui::Separator();
					ImGui::TextColored(
						  ImVec4(0.0f, 1.0f, 1.0f, 1.0f)
						, view.m_fileList[view.m_fileIndex].c_str()
						);
				}

				ImGui::EndMainMenuBar();
			}

			ImGui::PopStyleVar(2);

			static bool help = false;
			static bool about = false;

			view.m_mouse.update(float(mouseState.m_mx), float(mouseState.m_my), mouseState.m_mz, view.m_width, view.m_height);
			if (!overArea)
			{
				if (mouseState.m_buttons[entry::MouseButton::Left])
				{
					view.m_idleTimer = 0.0f;
					view.m_camera.orbit(view.m_mouse.m_dx, view.m_mouse.m_dy);
				}
				else if (mouseState.m_buttons[entry::MouseButton::Right])
				{
					view.m_idleTimer = 0.0f;
					view.m_camera.dolly(view.m_mouse.m_dx + view.m_mouse.m_dy);
				}
				else if (0 != view.m_mouse.m_scroll)
				{
					view.m_idleTimer = 0.0f;
					view.m_camera.dolly(float(view.m_mouse.m_scroll)*0.1f);
				}
			}

			if (help != view.m_help)
			{
				if (!help)
				{
					ImGui::OpenPopup("Help");
					inputRemoveBindings(s_bindingName[Binding::View]);
					inputAddBindings(s_bindingName[Binding::Help], s_binding[Binding::Help]);
				}
				else
				{
					inputRemoveBindings(s_bindingName[Binding::Help]);
					inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
				}

				help = view.m_help;
			}

			if (about != view.m_about)
			{
				if (!about)
				{
					ImGui::OpenPopup("About");
					inputRemoveBindings(s_bindingName[Binding::View]);
					inputAddBindings(s_bindingName[Binding::About], s_binding[Binding::About]);
				}
				else
				{
					inputRemoveBindings(s_bindingName[Binding::About]);
					inputAddBindings(s_bindingName[Binding::View], s_binding[Binding::View]);
				}

				about = view.m_about;
			}

			if (view.m_info)
			{
				ImGui::SetNextWindowSize(
					  ImVec2(300.0f, 320.0f)
					, ImGuiCond_FirstUseEver
					);

				if (ImGui::Begin("Info", &view.m_info) )
				{
					if (ImGui::BeginChild("##info", ImVec2(0.0f, 0.0f) ) )
					{
						if (NULL == mesh)
						{
							ImGui::Text("Geometry is not loaded.");
						}
						else
						{
							ImGui::Text("Name: %s", view.m_fileList[view.m_fileIndex].c_str() );

							ImGui::Indent();
							for (GroupArray::const_iterator itGroup = mesh->m_groups.begin(), itGroupEnd = mesh->m_groups.end(); itGroup != itGroupEnd; ++itGroup)
							{
								ImGui::Text("Group v %d i %d", itGroup->m_numVertices, itGroup->m_numIndices);
								ImGui::Indent();
								for (PrimitiveArray::const_iterator itPrim = itGroup->m_prims.begin(), itPrimEnd = itGroup->m_prims.end(); itPrim != itPrimEnd; ++itPrim)
								{
									ImGui::Text("Primitive v %d i %d", itPrim->m_numVertices, itPrim->m_numIndices);
								}
								ImGui::Unindent();
							}
							ImGui::Unindent();

							ImGui::Separator();
						}

						ImGui::EndChild();
					}

				}

				ImGui::End();
			}

			if (view.m_files)
			{
				char temp[bx::kMaxFilePath];
				bx::snprintf(temp, BX_COUNTOF(temp), "%s##File", view.m_path.getCPtr() );

				ImGui::SetNextWindowSize(
					  ImVec2(400.0f, 400.0f)
					, ImGuiCond_FirstUseEver
					);

				if (ImGui::Begin(temp, &view.m_files) )
				{
					if (ImGui::BeginChild("##file_list", ImVec2(0.0f, 0.0f) ) )
					{
						ImGui::PushFont(ImGui::Font::Mono);
						const float itemHeight = ImGui::GetTextLineHeightWithSpacing();
						const float listHeight =
							  bx::max(1.0f, bx::floor(ImGui::GetWindowHeight()/itemHeight) )
							* itemHeight
							;

						ImGui::PushItemWidth(-1);
						if (ImGui::ListBoxHeader("##empty", ImVec2(0.0f, listHeight) ) )
						{
							const int32_t itemCount = int32_t(view.m_fileList.size() );

							int32_t start, end;
							ImGui::CalcListClipping(itemCount, itemHeight, &start, &end);

							const int32_t index = int32_t(view.m_fileIndex);
							if (index <= start)
							{
								ImGui::SetScrollY(ImGui::GetScrollY() - (start-index+1)*itemHeight);
							}
							else if (index >= end)
							{
								ImGui::SetScrollY(ImGui::GetScrollY() + (index-end+1)*itemHeight);
							}

							ImGuiListClipper clipper(itemCount, itemHeight);

							for (int32_t pos = clipper.DisplayStart; pos < clipper.DisplayEnd; ++pos)
							{
								ImGui::PushID(pos);

								bool isSelected = uint32_t(pos) == view.m_fileIndex;
								if (ImGui::Selectable(view.m_fileList[pos].c_str(), &isSelected) )
								{
									view.m_fileIndex = pos;
								}

								ImGui::PopID();
							}

							clipper.End();

							ImGui::ListBoxFooter();
						}

						ImGui::PopFont();
						ImGui::EndChild();
					}
				}

				ImGui::End();
			}

			if (ImGui::BeginPopupModal("About", &view.m_about, ImGuiWindowFlags_AlwaysAutoResize) )
			{
				ImGui::SetWindowFontScale(1.0f);

				ImGui::Text(
					"geometryv, bgfx geometry viewer tool " ICON_KI_WRENCH ", version %d.%d.%d.\n"
					"Copyright 2019-2019 Attila Kocsis. All rights reserved.\n"
					"License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause\n"
					, BGFX_GEOMETRYV_VERSION_MAJOR
					, BGFX_GEOMETRYV_VERSION_MINOR
					, BGFX_API_VERSION
					);

				ImGui::Dummy(ImVec2(0.0f, 0.0f) );
				ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
				if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
				|| !view.m_about)
				{
					view.m_about = false;
					ImGui::CloseCurrentPopup();
				}

				ImGui::EndPopup();
			}

			if (ImGui::BeginPopupModal("Help", &view.m_help, ImGuiWindowFlags_AlwaysAutoResize) )
			{
				ImGui::SetWindowFontScale(1.0f);

				ImGui::Text("Key bindings:\n\n");

				ImGui::PushFont(ImGui::Font::Mono);
				keyBindingHelp("ESC", "Exit.");
				keyBindingHelp("h", "Toggle help screen.");
				keyBindingHelp("i", "Toggle info screen.");
				keyBindingHelp("f", "Toggle full-screen.");
				ImGui::NextLine();

				keyBindingHelp("LMB+drag",		  "Orbit.");
				keyBindingHelp("W/A/S/D",		  "Orbit.");
				keyBindingHelp("RMB+drag or MW",  "Dolly.");
				keyBindingHelp("=/-", "Dolly.");
				keyBindingHelp("0",   "Reset.");
				keyBindingHelp("1",   "Fit to window.");
				ImGui::NextLine();

				keyBindingHelp("up",   "Previous geometry.");
				keyBindingHelp("down", "Next geometry.");
				ImGui::NextLine();

				ImGui::NextLine();

				ImGui::PopFont();

				ImGui::Dummy(ImVec2(0.0f, 0.0f) );
				ImGui::SameLine(ImGui::GetWindowWidth() - 136.0f);
				if (ImGui::Button("Close", ImVec2(128.0f, 0.0f) )
				|| !view.m_help)
				{
					view.m_help = false;
					ImGui::CloseCurrentPopup();
				}

				ImGui::EndPopup();
			}

			imguiEndFrame();

			if ( (NULL == mesh || view.m_fileIndex != fileIndex)
			&&  0 != view.m_fileList.size() )
			{
				if (NULL != mesh )
				{
					meshUnload(mesh);
				}

				fileIndex = view.m_fileIndex;

				bx::FilePath fp = view.m_path;
				fp.join(view.m_fileList[view.m_fileIndex].c_str() );

				mesh = meshLoad(fp.getCPtr());

				std::string title;
				if (NULL != mesh )
				{
					uint32_t numPrimitives = 0;
					uint32_t numVertices = 0;
					uint32_t numIndices = 0;
					Aabb boundingBox = {};

					for (GroupArray::const_iterator it = mesh->m_groups.begin(), itEnd = mesh->m_groups.end(); it != itEnd; ++it)
					{
						if ( it == mesh->m_groups.begin())
						{
							boundingBox = it->m_aabb;
						}
						else
						{
							aabbExpand(boundingBox, it->m_aabb.min);
							aabbExpand(boundingBox, it->m_aabb.max);
						}

						numPrimitives += (uint32_t)it->m_prims.size();
						numVertices += (uint32_t)it->m_numVertices;
						numIndices += (uint32_t)it->m_numIndices;
					}

					bx::stringPrintf(title, "%s (g %d, p %d, v %d, i %d)"
						, fp.getCPtr()
						, mesh->m_groups.size()
						, numPrimitives
						, numVertices
						, numIndices
					);

					view.m_meshCenter = getCenter(boundingBox);
					view.m_meshRadius = bx::length(getExtents(boundingBox));

					view.m_camera.init( view.m_meshCenter, view.m_meshRadius * 2.0f);
				}
				else
				{
					bx::stringPrintf(title, "Failed to load %s!", filePath);
				}

				entry::WindowHandle handle = { 0 };
				entry::setWindowTitle(handle, title.c_str() );
			}

			int64_t now = bx::getHPCounter();
			static int64_t last = now;
			const int64_t frameTime = now - last;
			last = now;
			const double freq = double(bx::getHPFrequency() );
			const float deltaTime = float(frameTime/freq);

			time += (float)(frameTime*speed/freq);

			// Update camera.
			float viewMatrix[16];
			view.m_camera.update(deltaTime);
			view.m_camera.mtxLookAt(viewMatrix);

			float projMatrix[16];
			const float aspect = float(view.m_width)/float(view.m_height);
			bx::mtxProj(projMatrix, 60.0f, aspect, 0.1f, 1000.0f, caps->homogeneousDepth);

			bgfx::setViewTransform(SCENE_VIEW_ID, viewMatrix, projMatrix);
			bgfx::setViewRect(SCENE_VIEW_ID, 0, 0, uint16_t(view.m_width), uint16_t(view.m_height) );

			bgfx::touch(SCENE_VIEW_ID);

			bgfx::dbgTextClear();

			float orientation[16];
			bx::mtxIdentity(orientation);
			bgfx::setTransform(orientation);

			float mtx[16];
			bx::mtxIdentity(mtx);

			if (NULL != mesh)
			{
				meshSubmit(mesh
					, SCENE_VIEW_ID
					, meshProgram
					, mtx);
			}

			bgfx::frame();

			// Slow down when nothing is animating...
			if (view.m_idleTimer > 2.0f
			&&  !anyActive() )
			{
				bx::sleep(100);
			}

			view.m_idleTimer += deltaTime;
		}
	}

	if (NULL != mesh )
	{
		meshUnload(mesh);
	}

	bgfx::destroy(meshProgram);

	imguiDestroy();

	bgfx::shutdown();

	return exitcode;
}