-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
182 lines (135 loc) · 5.75 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
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
#include <vsg/all.h>
#ifdef vsgXchange_FOUND
# include <vsgXchange/all.h>
#endif
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMdiArea>
#include <vsgQt6/Window.h>
#include <iostream>
class MultiViewArea : public QMdiArea
{
public:
vsg::ref_ptr<vsg::WindowTraits> traits;
vsg::ref_ptr<vsgQt6::Viewer> viewer;
MultiViewArea(QWidget *parent = nullptr) :
QMdiArea(parent),
viewer(vsgQt6::Viewer::create())
{
viewer->addEventHandler(vsg::CloseHandler::create(viewer));
}
struct ViewWindow
{
vsgQt6::Window* window = nullptr;
};
using Views = std::vector<ViewWindow>;
Views views;
size_t addView(vsg::ref_ptr<vsg::Node> vsg_scene, const QString& title = {})
{
auto window = new vsgQt6::Window(viewer, traits);
auto widget = QWidget::createWindowContainer(window, this);
widget->setWindowTitle(title);
addSubWindow(widget);
if (views.empty()) widget->showMaximized();
else tileSubWindows();
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});
views.push_back(ViewWindow{window});
return views.size()-1;
}
};
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 = "vsgQt6 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;
}
QMainWindow* mainWindow = new QMainWindow();
auto mdiArea = new MultiViewArea(mainWindow);
mainWindow->setCentralWidget(mdiArea);
mdiArea->traits = windowTraits;
mdiArea->setContextMenuPolicy(Qt::PreventContextMenu);
mdiArea->setViewMode(QMdiArea::ViewMode::SubWindowView);
mdiArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
mdiArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
mdiArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mdiArea->addView(vsg_scene, "First Window");
mdiArea->addView(vsg_scene, "Second Window");
mdiArea->addView(vsg_scene, "Third Window");
mdiArea->viewer->compile();
if (interval >= 0) mdiArea->viewer->setInterval(interval);
mdiArea->viewer->continuousUpdate = continuousUpdate;
mainWindow->show();
return application.exec();
}