forked from vsg-dev/vsgQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
149 lines (110 loc) · 4.99 KB
/
main.cpp
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
#include <vsg/all.h>
#ifdef vsgXchange_FOUND
# include <vsgXchange/all.h>
#endif
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <vsgQt/ViewerWindow.h>
int main(int argc, char* argv[])
{
vsg::CommandLine arguments(&argc, argv);
// set up vsg::Options to pass in filepaths and ReaderWriter's and other IO
// realted options to use when reading and writing files.
auto options = vsg::Options::create();
options->fileCache = vsg::getEnv("VSG_FILE_CACHE");
options->paths = vsg::getEnvPaths("VSG_FILE_PATH");
#ifdef vsgXchange_all
// add vsgXchange's support for reading and writing 3rd party file formats
options->add(vsgXchange::all::create());
#endif
arguments.read(options);
auto windowTraits = vsg::WindowTraits::create();
windowTraits->windowTitle = "vsgQt viewer";
windowTraits->debugLayer = arguments.read({"--debug", "-d"});
windowTraits->apiDumpLayer = arguments.read({"--api", "-a"});
if (arguments.read({"--fullscreen", "--fs"})) windowTraits->fullscreen = true;
if (arguments.read({"--window", "-w"}, windowTraits->width,windowTraits->height))
{
windowTraits->fullscreen = false;
}
auto horizonMountainHeight = arguments.value(0.0, "--hmh");
if (arguments.errors())
return arguments.writeErrorMessages(std::cerr);
if (argc <= 1)
{
std::cout << "Please specify a 3d model or image file on the command line."
<< std::endl;
return 1;
}
vsg::Path filename = arguments[1];
auto vsg_scene = vsg::read_cast<vsg::Node>(filename, options);
if (!vsg_scene)
{
std::cout << "Failed to load a valid scenene graph, Please specify a 3d "
"model or image file on the command line."
<< std::endl;
return 1;
}
QApplication application(argc, argv);
QMainWindow* mainWindow = new QMainWindow();
auto* viewerWindow = new vsgQt::ViewerWindow();
viewerWindow->traits = windowTraits;
// provide the calls to set up the vsg::Viewer that will be used to render to the QWindow subclass vsgQt::ViewerWindow
viewerWindow->initializeCallback = [&](vsgQt::ViewerWindow& vw) {
auto& window = vw.proxyWindow;
if (!window) return false;
auto& viewer = vw.viewer;
if (!viewer) viewer = vsg::Viewer::create();
viewer->addWindow(window);
// compute the bounds of the scene graph to help position camera
vsg::ComputeBounds computeBounds;
vsg_scene->accept(computeBounds);
vsg::dvec3 centre = (computeBounds.bounds.min + computeBounds.bounds.max) * 0.5;
double radius = vsg::length(computeBounds.bounds.max - computeBounds.bounds.min) * 0.6;
double nearFarRatio = 0.001;
// set up the camera
auto lookAt = vsg::LookAt::create(centre + vsg::dvec3(0.0, -radius * 3.5, 0.0), centre, vsg::dvec3(0.0, 0.0, 1.0));
vsg::ref_ptr<vsg::ProjectionMatrix> perspective;
vsg::ref_ptr<vsg::EllipsoidModel> ellipsoidModel(vsg_scene->getObject<vsg::EllipsoidModel>("EllipsoidModel"));
if (ellipsoidModel)
{
perspective = vsg::EllipsoidPerspective::create(
lookAt, ellipsoidModel, 30.0,
static_cast<double>(window->extent2D().width) /
static_cast<double>(window->extent2D().height),
nearFarRatio, horizonMountainHeight);
}
else
{
perspective = vsg::Perspective::create(
30.0,
static_cast<double>(window->extent2D().width) /
static_cast<double>(window->extent2D().height),
nearFarRatio * radius, radius * 4.5);
}
auto camera = vsg::Camera::create(perspective, lookAt, vsg::ViewportState::create(window->extent2D()));
// add close handler to respond the close window button and pressing escape
viewer->addEventHandler(vsg::CloseHandler::create(viewer));
// add trackball to enable mouse driven camera view control.
viewer->addEventHandler(vsg::Trackball::create(camera, ellipsoidModel));
auto commandGraph = vsg::createCommandGraphForView(window, camera, vsg_scene);
viewer->assignRecordAndSubmitTaskAndPresentation({commandGraph});
viewer->compile();
return true;
};
// provide the calls to invokve the vsg::Viewer to render a frame.
viewerWindow->frameCallback = [](vsgQt::ViewerWindow& vw) {
if (!vw.viewer || !vw.viewer->advanceToNextFrame()) return false;
// pass any events into EventHandlers assigned to the Viewer
vw.viewer->handleEvents();
vw.viewer->update();
vw.viewer->recordAndSubmit();
vw.viewer->present();
return true;
};
auto widget = QWidget::createWindowContainer(viewerWindow, mainWindow);
mainWindow->setCentralWidget(widget);
mainWindow->resize(windowTraits->width, windowTraits->height);
mainWindow->show();
return application.exec();
}