-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsimple_viewer.cpp
215 lines (173 loc) · 8.4 KB
/
simple_viewer.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
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
// A simple viewer for Collada .DAE files using the OgreColladaImporter in "scene" (hierarchy preservation) mode
// Author: Jeff Trull <[email protected]>
/*
Copyright (c) 2012 Jeffrey E Trull
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <utility>
#include <iostream>
#include <numeric>
// my idea of a really really minimal viewer app
#include <OgreCamera.h>
#include <OgreEntity.h>
#include <OgreLogManager.h>
#include <OgreRoot.h>
#include <OgreViewport.h>
#include <OgreSceneManager.h>
#include <OgreRenderWindow.h>
#include <OgreConfigFile.h>
#include <OgreFrameListener.h>
#include <OgreWindowEventUtilities.h>
#include <OgreSceneNode.h>
#include "OgreColladaSaxLoader.h"
struct SimpleViewer : public Ogre::FrameListener, public Ogre::WindowEventListener {
SimpleViewer() : root_(new Ogre::Root("plugins.cfg")), shutdown_(false)
{
if (root_->showConfigDialog()) {
window_ = root_->initialise(true, "Collada Viewer");
scenemgr_ = root_->createSceneManager(Ogre::ST_GENERIC);
camera_ = scenemgr_->createCamera("ViewerCam");
viewport_ = window_->addViewport(camera_);
viewport_->setBackgroundColour(Ogre::ColourValue(0,0,0));
// Alter the camera aspect ratio to match the viewport
camera_->setAspectRatio(Ogre::Real(viewport_->getActualWidth()) /
Ogre::Real(viewport_->getActualHeight()));
// listen so we can know when the window is destroyed
Ogre::WindowEventUtilities::addWindowEventListener(window_, this);
root_->addFrameListener(this);
}
}
~SimpleViewer() {
root_->removeFrameListener(this);
Ogre::WindowEventUtilities::removeWindowEventListener(window_, this);
windowClosed(window_);
}
void go() {
root_->startRendering();
}
void windowClosed(Ogre::RenderWindow*) {
shutdown_ = true;
}
bool frameRenderingQueued(const Ogre::FrameEvent&) {
return !shutdown_;
}
Ogre::SceneManager* getSceneManager() { return scenemgr_; }
Ogre::Camera* getCamera() { return camera_; }
void setCamera(Ogre::Camera* camera) {
camera_ = camera;
viewport_->setCamera(camera_);
camera_->setAspectRatio(Ogre::Real(viewport_->getActualWidth()) /
Ogre::Real(viewport_->getActualHeight()));
}
private:
std::unique_ptr<Ogre::Root> root_;
// in tutorial app only root gets deleted in destructor, so use raw pointers for these:
Ogre::RenderWindow* window_;
Ogre::SceneManager* scenemgr_;
Ogre::Camera* camera_;
Ogre::Viewport* viewport_;
// application state
bool shutdown_;
};
#define BOOST_FILESYSTEM_VERSION 3
#include <boost/filesystem.hpp>
#include <COLLADAFWRoot.h>
#include "OgreSceneWriter.h"
// utility function for setting up camera in the absence of specific instructions
// recursively calculates the bounding box of a SceneNode including transformations
Ogre::AxisAlignedBox worldExtent(Ogre::SceneNode const* sn) {
// sum the bounding boxes of the attached objects
auto bbox = std::accumulate(sn->getAttachedObjectIterator().begin(),
sn->getAttachedObjectIterator().end(),
Ogre::AxisAlignedBox(),
[](Ogre::AxisAlignedBox box,
Ogre::SceneNode::ObjectIterator::PairType const& obj) {
if (obj.second->getMovableType() != "Camera") {
// cameras are not real for the purpose of computing extent
box.merge(obj.second->getBoundingBox());
}
return box;
});
// sum bounding boxes of downstream objects
bbox = std::accumulate(sn->getChildIterator().begin(),
sn->getChildIterator().end(),
bbox,
[](Ogre::AxisAlignedBox box,
Ogre::SceneNode::ChildNodeIterator::PairType const& obj) {
Ogre::SceneNode const * child_node = dynamic_cast<Ogre::SceneNode*>(obj.second);
Ogre::AxisAlignedBox child_box = worldExtent(child_node);
if (!child_box.isNull()) {
Ogre::Matrix4 child_xform;
child_xform.makeTransform(child_node->getPosition(),
child_node->getScale(),
child_node->getOrientation());
child_box.transform(child_xform);
}
box.merge(child_box);
return box;
});
return bbox;
}
int main(int argc, char **argv) {
// Expects just one argument: path to .dae file
if (argc != 2) {
std::cerr << "usage: cview /path/to/model.dae" << std::endl;
return 1;
}
// make sure it's there
std::string fname(argv[1]);
if (!boost::filesystem::exists(fname) ||
!boost::filesystem::is_regular_file(fname)) {
std::cerr << "cannot access file " << fname << std::endl;
return 1;
}
// determine name of parent directory
std::string dir = boost::filesystem::path(fname).parent_path().string();
// create the viewer application
SimpleViewer viewer;
// configure it via scene manager and camera getters
// set camera position and orientation
// viewer position assumed to be +Z
viewer.getCamera()->setPosition(Ogre::Vector3(0,0,100));
// camera looks back along -Z into screen
viewer.getCamera()->lookAt(Ogre::Vector3(0,0,-100));
viewer.getCamera()->setNearClipDistance(5);
viewer.getCamera()->setFarClipDistance(1000);
// set up some lights to illuminate loaded object
Ogre::ColourValue dim(0.25, 0.25, 0.25);
viewer.getSceneManager()->setAmbientLight(dim);
Ogre::Light* cam_light = viewer.getSceneManager()->createLight("cameraLight");
cam_light->setType(Ogre::Light::LT_DIRECTIONAL);
cam_light->setDiffuseColour(dim);
cam_light->setSpecularColour(dim);
cam_light->setDirection(Ogre::Vector3(0, 0, -1));
Ogre::Light* overhead_light = viewer.getSceneManager()->createLight("overheadLight");
overhead_light->setType(Ogre::Light::LT_DIRECTIONAL);
overhead_light->setDiffuseColour(dim);
overhead_light->setSpecularColour(dim);
overhead_light->setDirection(Ogre::Vector3(0, -1, 0));
OgreCollada::SceneWriter writer(viewer.getSceneManager(),
viewer.getSceneManager()->getRootSceneNode()->createChildSceneNode("Top"),
dir);
OgreCollada::SaxLoader loader;
COLLADAFW::Root root(&loader, &writer);
if (!root.loadDocument(fname)) {
std::cerr << "load document failed\n";
return 1;
}
// if a camera was found during the Collada load, use it instead
Ogre::Camera* colladaCamera = writer.getCamera();
if (colladaCamera) {
viewer.setCamera(colladaCamera);
} else {
// try to look at the center of the loaded objects, wherever they may be
// calculate the bounding box of the scene
auto bbox = worldExtent(viewer.getSceneManager()->getRootSceneNode());
viewer.getCamera()->lookAt(bbox.getCenter());
dynamic_cast<Ogre::SceneNode*>(viewer.getSceneManager()->getRootSceneNode()->getChild("Top"))->showBoundingBox(true);
}
viewer.go();
return 0;
}