-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.cpp
152 lines (113 loc) · 5.07 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
150
151
152
#include <vsg/all.h>
#ifdef vsgXchange_FOUND
# include <vsgXchange/all.h>
#endif
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMdiArea>
#include <vsgQt/Window.h>
#include <iostream>
vsgQt::Window* createWindow(vsg::ref_ptr<vsgQt::Viewer> viewer, vsg::ref_ptr<vsg::WindowTraits> traits, vsg::ref_ptr<vsg::Node> vsg_scene, QWindow* parent, const QString& title = {})
{
auto window = new vsgQt::Window(viewer, traits, parent);
window->setTitle(title);
window->initializeWindow();
// if this is the first window to be created, use its device for future window creation.
if (!traits->device) traits->device = window->windowAdapter->getOrCreateDevice();
// 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;
uint32_t width = window->traits->width;
uint32_t height = window->traits->height;
vsg::ref_ptr<vsg::EllipsoidModel> ellipsoidModel(vsg_scene->getObject<vsg::EllipsoidModel>("EllipsoidModel"));
vsg::ref_ptr<vsg::Camera> camera;
{
// 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;
if (ellipsoidModel)
{
perspective = vsg::EllipsoidPerspective::create(
lookAt, ellipsoidModel, 30.0,
static_cast<double>(width) /
static_cast<double>(height),
nearFarRatio, false);
}
else
{
perspective = vsg::Perspective::create(
30.0,
static_cast<double>(width) /
static_cast<double>(height),
nearFarRatio * radius, radius * 4.5);
}
camera = vsg::Camera::create(perspective, lookAt, vsg::ViewportState::create(VkExtent2D{width, height}));
}
auto trackball = vsg::Trackball::create(camera, ellipsoidModel);
trackball->addWindow(*window);
viewer->addEventHandler(trackball);
auto commandGraph = vsg::createCommandGraphForView(*window, camera, vsg_scene);
viewer->addRecordAndSubmitTaskAndPresentation({commandGraph});
return window;
}
int main(int argc, char* argv[])
{
QApplication application(argc, argv);
vsg::CommandLine arguments(&argc, argv);
// set up vsg::Options to pass in filepaths, ReaderWriters and other IO
// related 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_FOUND
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"});
arguments.read("--samples", windowTraits->samples);
arguments.read({"--window", "-w"}, windowTraits->width, windowTraits->height);
if (arguments.read({"--fullscreen", "--fs"})) windowTraits->fullscreen = true;
bool continuousUpdate = !arguments.read({"--event-driven", "--ed"});
auto interval = arguments.value<int>(8, "--interval");
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 scene graph. Please specify a valid 3d "
"model or image file on the command line."
<< std::endl;
return 1;
}
// create the viewer that will manage all the rendering of the views
auto viewer = vsgQt::Viewer::create();
// add close handler to respond to the close window button and pressing escape
viewer->addEventHandler(vsg::CloseHandler::create(viewer));
// create the windows
auto firstWindow = createWindow(viewer, windowTraits, vsg_scene, nullptr, "First Window");
auto secondWindow = createWindow(viewer, windowTraits, vsg_scene, nullptr, "Second Window");
auto thirdWindow = createWindow(viewer, windowTraits, vsg_scene, nullptr, "Third Window");
firstWindow->setGeometry(0, 0, 640, 480);
firstWindow->show();
secondWindow->setGeometry(680, 0, 640, 480);
secondWindow->show();
thirdWindow->setGeometry(1360, 0, 640, 480);
thirdWindow->show();
if (interval >= 0) viewer->setInterval(interval);
viewer->continuousUpdate = continuousUpdate;
viewer->compile();
return application.exec();
}