| 1 | /* |
| 2 | * SPDX-FileName: CameraGroup.cxx |
| 3 | * SPDX-FileCopyrightText: Copyright (C) 2008 Tim Moore |
| 4 | * SPDX-FileContributor: Copyright (C) 2011 Mathias Froehlich |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "CameraGroup.hxx" |
| 9 | |
| 10 | #include <Main/fg_props.hxx> |
| 11 | #include <Main/globals.hxx> |
| 12 | #include "renderer.hxx" |
| 13 | #include "FGEventHandler.hxx" |
| 14 | #include "WindowBuilder.hxx" |
| 15 | #include "WindowSystemAdapter.hxx" |
| 16 | #include "splash.hxx" |
| 17 | #include "sview.hxx" |
| 18 | #include "VRManager.hxx" |
| 19 | |
| 20 | #include <simgear/math/SGRect.hxx> |
| 21 | #include <simgear/props/props.hxx> |
| 22 | #include <simgear/props/props_io.hxx> // for copyProperties |
| 23 | #include <simgear/structure/exception.hxx> |
| 24 | #include <simgear/scene/material/EffectCullVisitor.hxx> |
| 25 | #include <simgear/scene/util/ProjectionMatrix.hxx> |
| 26 | #include <simgear/scene/util/RenderConstants.hxx> |
| 27 | #include <simgear/scene/util/SGReaderWriterOptions.hxx> |
| 28 | #include <simgear/scene/util/OsgUtils.hxx> |
| 29 | #include <simgear/scene/viewer/Compositor.hxx> |
| 30 | #include <simgear/scene/viewer/CompositorUtil.hxx> |
| 31 | |
| 32 | #include <algorithm> |
| 33 | #include <cstring> |
| 34 | #include <string> |
| 35 | |
| 36 | #include <osg/Camera> |
| 37 | #include <osg/Geometry> |
| 38 | #include <osg/GraphicsContext> |
| 39 | #include <osg/io_utils> |
| 40 | #include <osg/Math> |
| 41 | #include <osg/Matrix> |
| 42 | #include <osg/Notify> |
| 43 | #include <osg/Program> |
| 44 | #include <osg/Quat> |
| 45 | #include <osg/TexMat> |
| 46 | #include <osg/Vec3d> |
| 47 | #include <osg/Viewport> |
| 48 | |
| 49 | #include <osgUtil/IntersectionVisitor> |
| 50 | |
| 51 | #include <osgViewer/Viewer> |
| 52 | #include <osgViewer/GraphicsWindow> |
| 53 | #include <osgViewer/Renderer> |
| 54 | |
| 55 | using namespace osg; |
| 56 | |
| 57 | namespace { |
| 58 | |
| 59 | osg::Matrix |
| 60 | invert(const osg::Matrix& matrix) |
| 61 | { |
| 62 | return osg::Matrix::inverse(matrix); |
| 63 | } |
| 64 | |
| 65 | /// Returns the zoom factor of the master camera. |
| 66 | /// The reference fov is the historic 55 deg |
| 67 | double |
| 68 | zoomFactor() |
| 69 | { |
| 70 | double fov = fgGetDouble("/sim/current-view/field-of-view", 55); |
| 71 | if (fov < 1) |
| 72 | fov = 1; |
| 73 | return tan(55*0.5*SG_DEGREES_TO_RADIANS)/tan(fov*0.5*SG_DEGREES_TO_RADIANS); |
| 74 | } |
| 75 | |
| 76 | osg::Vec2d |
| 77 | preMult(const osg::Vec2d& v, const osg::Matrix& m) |
| 78 | { |
| 79 | osg::Vec3d tmp = m.preMult(osg::Vec3(v, 0)); |
| 80 | return osg::Vec2d(tmp[0], tmp[1]); |
| 81 | } |
| 82 | |
| 83 | osg::Matrix |
| 84 | relativeProjection(const osg::Matrix& P0, const osg::Matrix& R, const osg::Vec2d ref[2], |
| 85 | const osg::Matrix& pP, const osg::Matrix& pR, const osg::Vec2d pRef[2]) |
| 86 | { |
| 87 | // Track the way from one projection space to the other: |
| 88 | // We want |
| 89 | // P = T*S*P0 |
| 90 | // where P0 is the projection template sensible for the given window size, |
| 91 | // T is a translation matrix and S a scale matrix. |
| 92 | // We need to determine T and S so that the reference points in the parents |
| 93 | // projection space match the two reference points in this cameras projection space. |
| 94 | |
| 95 | // Starting from the parents camera projection space, we get into this cameras |
| 96 | // projection space by the transform matrix: |
| 97 | // P*R*inv(pP*pR) = T*S*P0*R*inv(pP*pR) |
| 98 | // So, at first compute that matrix without T*S and determine S and T from that |
| 99 | |
| 100 | // Ok, now osg uses the inverse matrix multiplication order, thus: |
| 101 | osg::Matrix PtoPwithoutTS = invert(pR*pP)*R*P0; |
| 102 | // Compute the parents reference points in the current projection space |
| 103 | // without the yet unknown T and S |
| 104 | osg::Vec2d pRefInThis[2] = { |
| 105 | preMult(pRef[0], PtoPwithoutTS), |
| 106 | preMult(pRef[1], PtoPwithoutTS) |
| 107 | }; |
| 108 | |
| 109 | // To get the same zoom, rescale to match the parents size |
| 110 | double s = (ref[0] - ref[1]).length()/(pRefInThis[0] - pRefInThis[1]).length(); |
| 111 | osg::Matrix S = osg::Matrix::scale(s, s, 1); |
| 112 | |
| 113 | // For the translation offset, incorporate the now known scale |
| 114 | // and recompute the position ot the first reference point in the |
| 115 | // currents projection space without the yet unknown T. |
| 116 | pRefInThis[0] = preMult(pRef[0], PtoPwithoutTS*S); |
| 117 | // The translation is then the difference of the reference points |
| 118 | osg::Matrix T = osg::Matrix::translate(osg::Vec3d(ref[0] - pRefInThis[0], 0)); |
| 119 | |
| 120 | // Compose and return the desired final projection matrix |
| 121 | return P0*S*T; |
| 122 | } |
| 123 | |
| 124 | } // anonymous namespace |
| 125 | |
| 126 | namespace flightgear |
| 127 | { |
| 128 | using namespace simgear; |
| 129 | using namespace compositor; |
| 130 | |
| 131 | class CameraGroupListener : public SGPropertyChangeListener { |
| 132 | public: |
| 133 | CameraGroupListener(CameraGroup* cg, SGPropertyNode* gnode) : |
| 134 | _groupNode(gnode), |
| 135 | _cameraGroup(cg) { |
| 136 | listenToNode("znear", 0.1f); |
| 137 | listenToNode("zfar", 120000.0f); |
| 138 | } |
| 139 | |
| 140 | virtual ~CameraGroupListener() { |
| 141 | unlisten("znear"); |
| 142 | unlisten("zfar"); |
| 143 | } |
| 144 | |
| 145 | virtual void valueChanged(SGPropertyNode* prop) { |
| 146 | if (prop->getNameString() == "znear") { |
| 147 | _cameraGroup->_zNear = prop->getFloatValue(); |
| 148 | } else if (prop->getNameString() == "zfar") { |
| 149 | _cameraGroup->_zFar = prop->getFloatValue(); |
| 150 | } |
| 151 | } |
| 152 | private: |
| 153 | void listenToNode(const std::string& name, double val) { |
| 154 | SGPropertyNode* n = _groupNode->getChild(name); |
| 155 | if (!n) { |
| 156 | n = _groupNode->getChild(name, 0 /* index */, true); |
| 157 | n->setDoubleValue(val); |
| 158 | } |
| 159 | n->addChangeListener(this); |
| 160 | valueChanged(n); // propogate initial state through |
| 161 | } |
| 162 | |
| 163 | void unlisten(const std::string& name) { |
| 164 | _groupNode->getChild(name)->removeChangeListener(this); |
| 165 | } |
| 166 | |
| 167 | SGPropertyNode_ptr _groupNode; |
| 168 | CameraGroup* _cameraGroup; // non-owning reference |
| 169 | }; |
| 170 | |
| 171 | struct GUIUpdateCallback : public Pass::PassUpdateCallback { |
| 172 | virtual void updatePass(Pass &pass, |
| 173 | const osg::Matrix &view_matrix, |
| 174 | const osg::Matrix &proj_matrix) { |
| 175 | // Just set both the view matrix and the projection matrix |
| 176 | pass.camera->setViewMatrix(view_matrix); |
| 177 | pass.camera->setProjectionMatrix(proj_matrix); |
| 178 | } |
| 179 | }; |
| 180 | |
| 181 | typedef std::vector<SGPropertyNode_ptr> SGPropertyNodeVec; |
| 182 | |
| 183 | osg::ref_ptr<CameraGroup> CameraGroup::_defaultGroup; |
| 184 | |
| 185 | CameraGroup::CameraGroup(osgViewer::View* view) : |
| 186 | _viewer(view) |
| 187 | { |
| 188 | } |
| 189 | |
| 190 | CameraGroup::~CameraGroup() |
| 191 | { |
| 192 | |
| 193 | } |
| 194 | |
| 195 | void CameraGroup::update(const osg::Vec3d& position, |
| 196 | const osg::Quat& orientation) |
| 197 | { |
| 198 | const osg::Matrix masterView(osg::Matrix::translate(-position) |
| 199 | * osg::Matrix::rotate(orientation.inverse())); |
| 200 | _viewer->getCamera()->setViewMatrix(masterView); |
| 201 | const osg::Matrix& masterProj = _viewer->getCamera()->getProjectionMatrix(); |
| 202 | double masterZoomFactor = zoomFactor(); |
| 203 | |
| 204 | for (const auto &info : _cameras) { |
| 205 | osg::Matrix view_matrix; |
| 206 | if (info->flags & (CameraInfo::SPLASH | CameraInfo::GUI)) |
| 207 | view_matrix = osg::Matrix::identity(); |
| 208 | else if ((info->flags & CameraInfo::VIEW_ABSOLUTE) != 0) |
| 209 | view_matrix = info->viewOffset; |
| 210 | else |
| 211 | view_matrix = masterView * info->viewOffset; |
| 212 | |
| 213 | osg::Matrix proj_matrix; |
| 214 | if (info->flags & (CameraInfo::SPLASH | CameraInfo::GUI)) { |
| 215 | const osg::GraphicsContext::Traits *traits = |
| 216 | info->compositor->getGraphicsContext()->getTraits(); |
| 217 | proj_matrix = osg::Matrix::ortho2D(0, traits->width, 0, traits->height); |
| 218 | } else if ((info->flags & CameraInfo::PROJECTION_ABSOLUTE) != 0) { |
| 219 | if (info->flags & CameraInfo::ENABLE_MASTER_ZOOM) { |
| 220 | if (info->relativeCameraParent) { |
| 221 | // template projection and view matrices of the current camera |
| 222 | osg::Matrix P0 = info->projOffset; |
| 223 | osg::Matrix R = view_matrix; |
| 224 | |
| 225 | // The already known projection and view matrix of the parent camera |
| 226 | osg::Matrix pP = info->relativeCameraParent->projMatrix; |
| 227 | osg::Matrix pR = info->relativeCameraParent->viewMatrix; |
| 228 | |
| 229 | // And the projection matrix derived from P0 so that the |
| 230 | // reference points match |
| 231 | proj_matrix = relativeProjection(P0, R, info->thisReference, |
| 232 | pP, pR, info->parentReference); |
| 233 | } else { |
| 234 | // We want to zoom, so take the original matrix and apply the |
| 235 | // zoom to it |
| 236 | proj_matrix = info->projOffset; |
| 237 | proj_matrix.postMultScale(osg::Vec3d(masterZoomFactor, |
| 238 | masterZoomFactor, |
| 239 | 1)); |
| 240 | } |
| 241 | } else { |
| 242 | proj_matrix = info->projOffset; |
| 243 | } |
| 244 | } else { |
| 245 | proj_matrix = masterProj * info->projOffset; |
| 246 | } |
| 247 | |
| 248 | osg::Matrix new_proj_matrix = proj_matrix; |
| 249 | if ((info->flags & CameraInfo::SPLASH) == 0 && |
| 250 | (info->flags & CameraInfo::GUI) == 0 && |
| 251 | (info->flags & CameraInfo::FIXED_NEAR_FAR) == 0) { |
| 252 | ProjectionMatrix::clampNearFarPlanes(proj_matrix, _zNear, _zFar, |
| 253 | new_proj_matrix); |
| 254 | } |
| 255 | |
| 256 | info->viewMatrix = view_matrix; |
| 257 | info->projMatrix = new_proj_matrix; |
| 258 | info->compositor->update(view_matrix, new_proj_matrix); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | void CameraGroup::setCameraParameters(float vfov, float aspectRatio) |
| 263 | { |
| 264 | if (vfov != 0.0f && aspectRatio != 0.0f) { |
| 265 | osg::Matrixd m; |
| 266 | ProjectionMatrix::makePerspective(m, vfov, 1.0 / aspectRatio, |
| 267 | _zNear, _zFar, ProjectionMatrix::STANDARD); |
| 268 | _viewer->getCamera()->setProjectionMatrix(m); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | double CameraGroup::getMasterAspectRatio() const |
| 273 | { |
| 274 | if (_cameras.empty()) |
| 275 | return 0.0; |
| 276 | |
| 277 | // The master camera is the first one added |
| 278 | const CameraInfo *info = _cameras.front(); |
| 279 | if (!info) |
| 280 | return 0.0; |
| 281 | const osg::GraphicsContext::Traits *traits = |
| 282 | info->compositor->getGraphicsContext()->getTraits(); |
| 283 | |
| 284 | return static_cast<double>(traits->height) / traits->width; |
| 285 | } |
| 286 | |
| 287 | // FIXME: Port this to the Compositor |
| 288 | #if 0 |
| 289 | // Mostly copied from osg's osgViewer/View.cpp |
| 290 | |
| 291 | static osg::Geometry* createPanoramicSphericalDisplayDistortionMesh( |
| 292 | const Vec3& origin, const Vec3& widthVector, const Vec3& heightVector, |
| 293 | double sphere_radius, double collar_radius, |
| 294 | Image* intensityMap = 0, const Matrix& projectorMatrix = Matrix()) |
| 295 | { |
| 296 | osg::Vec3d center(0.0,0.0,0.0); |
| 297 | osg::Vec3d eye(0.0,0.0,0.0); |
| 298 | |
| 299 | double distance = sqrt(sphere_radius*sphere_radius - collar_radius*collar_radius); |
| 300 | bool flip = false; |
| 301 | bool texcoord_flip = false; |
| 302 | |
| 303 | // create the quad to visualize. |
| 304 | osg::Geometry* geometry = new osg::Geometry(); |
| 305 | |
| 306 | geometry->setSupportsDisplayList(false); |
| 307 | |
| 308 | osg::Vec3 xAxis(widthVector); |
| 309 | float width = widthVector.length(); |
| 310 | xAxis /= width; |
| 311 | |
| 312 | osg::Vec3 yAxis(heightVector); |
| 313 | float height = heightVector.length(); |
| 314 | yAxis /= height; |
| 315 | |
| 316 | int noSteps = 160; |
| 317 | |
| 318 | osg::Vec3Array* vertices = new osg::Vec3Array; |
| 319 | osg::Vec2Array* texcoords0 = new osg::Vec2Array; |
| 320 | osg::Vec2Array* texcoords1 = intensityMap==0 ? new osg::Vec2Array : 0; |
| 321 | osg::Vec4Array* colors = new osg::Vec4Array; |
| 322 | |
| 323 | osg::Vec3 top = origin + yAxis*height; |
| 324 | |
| 325 | osg::Vec3 screenCenter = origin + widthVector*0.5f + heightVector*0.5f; |
| 326 | float screenRadius = heightVector.length() * 0.5f; |
| 327 | |
| 328 | geometry->getOrCreateStateSet()->setMode(GL_CULL_FACE, osg::StateAttribute::OFF | osg::StateAttribute::PROTECTED); |
| 329 | |
| 330 | for(int i=0;i<noSteps;++i) |
| 331 | { |
| 332 | //osg::Vec3 cursor = bottom+dy*(float)i; |
| 333 | for(int j=0;j<noSteps;++j) |
| 334 | { |
| 335 | osg::Vec2 texcoord(double(i)/double(noSteps-1), double(j)/double(noSteps-1)); |
| 336 | double theta = texcoord.x() * 2.0 * osg::PI; |
| 337 | double phi = (1.0-texcoord.y()) * osg::PI; |
| 338 | |
| 339 | if (texcoord_flip) texcoord.y() = 1.0f - texcoord.y(); |
| 340 | |
| 341 | osg::Vec3 pos(sin(phi)*sin(theta), sin(phi)*cos(theta), cos(phi)); |
| 342 | pos = pos*projectorMatrix; |
| 343 | |
| 344 | double alpha = atan2(pos.x(), pos.y()); |
| 345 | if (alpha<0.0) alpha += 2.0*osg::PI; |
| 346 | |
| 347 | double beta = atan2(sqrt(pos.x()*pos.x() + pos.y()*pos.y()), pos.z()); |
| 348 | if (beta<0.0) beta += 2.0*osg::PI; |
| 349 | |
| 350 | double gamma = atan2(sqrt(double(pos.x()*pos.x() + pos.y()*pos.y())), double(pos.z()+distance)); |
| 351 | if (gamma<0.0) gamma += 2.0*osg::PI; |
| 352 | |
| 353 | |
| 354 | osg::Vec3 v = screenCenter + osg::Vec3(sin(alpha)*gamma*2.0/osg::PI, -cos(alpha)*gamma*2.0/osg::PI, 0.0f)*screenRadius; |
| 355 | |
| 356 | if (flip) |
| 357 | vertices->push_back(osg::Vec3(v.x(), top.y()-(v.y()-origin.y()),v.z())); |
| 358 | else |
| 359 | vertices->push_back(v); |
| 360 | |
| 361 | texcoords0->push_back( texcoord ); |
| 362 | |
| 363 | osg::Vec2 texcoord1(alpha/(2.0*osg::PI), 1.0f - beta/osg::PI); |
| 364 | if (intensityMap) |
| 365 | { |
| 366 | colors->push_back(intensityMap->getColor(texcoord1)); |
| 367 | } |
| 368 | else |
| 369 | { |
| 370 | colors->push_back(osg::Vec4(1.0f,1.0f,1.0f,1.0f)); |
| 371 | if (texcoords1) texcoords1->push_back( texcoord1 ); |
| 372 | } |
| 373 | |
| 374 | |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // pass the created vertex array to the points geometry object. |
| 379 | geometry->setVertexArray(vertices); |
| 380 | |
| 381 | geometry->setColorArray(colors); |
| 382 | geometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX); |
| 383 | |
| 384 | geometry->setTexCoordArray(0,texcoords0); |
| 385 | if (texcoords1) geometry->setTexCoordArray(1,texcoords1); |
| 386 | |
| 387 | osg::DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES); |
| 388 | geometry->addPrimitiveSet(elements); |
| 389 | |
| 390 | for(int i=0;i<noSteps-1;++i) |
| 391 | { |
| 392 | for(int j=0;j<noSteps-1;++j) |
| 393 | { |
| 394 | int i1 = j+(i+1)*noSteps; |
| 395 | int i2 = j+(i)*noSteps; |
| 396 | int i3 = j+1+(i)*noSteps; |
| 397 | int i4 = j+1+(i+1)*noSteps; |
| 398 | |
| 399 | osg::Vec3& v1 = (*vertices)[i1]; |
| 400 | osg::Vec3& v2 = (*vertices)[i2]; |
| 401 | osg::Vec3& v3 = (*vertices)[i3]; |
| 402 | osg::Vec3& v4 = (*vertices)[i4]; |
| 403 | |
| 404 | if ((v1-screenCenter).length()>screenRadius) continue; |
| 405 | if ((v2-screenCenter).length()>screenRadius) continue; |
| 406 | if ((v3-screenCenter).length()>screenRadius) continue; |
| 407 | if ((v4-screenCenter).length()>screenRadius) continue; |
| 408 | |
| 409 | elements->push_back(i1); |
| 410 | elements->push_back(i2); |
| 411 | elements->push_back(i3); |
| 412 | |
| 413 | elements->push_back(i1); |
| 414 | elements->push_back(i3); |
| 415 | elements->push_back(i4); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return geometry; |
| 420 | } |
| 421 | |
| 422 | #endif |
| 423 | |
| 424 | void CameraGroup::buildDistortionCamera(const SGPropertyNode* psNode, |
| 425 | Camera* camera) |
| 426 | { |
| 427 | // FIXME: Port this to the Compositor |
| 428 | #if 0 |
| 429 | const SGPropertyNode* texNode = psNode->getNode("texture"); |
| 430 | if (!texNode) { |
| 431 | // error |
| 432 | return; |
| 433 | } |
| 434 | string texName = texNode->getStringValue(); |
| 435 | TextureMap::iterator itr = _textureTargets.find(texName); |
| 436 | if (itr == _textureTargets.end()) { |
| 437 | // error |
| 438 | return; |
| 439 | } |
| 440 | Viewport* viewport = camera->getViewport(); |
| 441 | float width = viewport->width(); |
| 442 | float height = viewport->height(); |
| 443 | TextureRectangle* texRect = itr->second.get(); |
| 444 | double radius = psNode->getDoubleValue("radius", 1.0); |
| 445 | double collar = psNode->getDoubleValue("collar", 0.45); |
| 446 | Geode* geode = new Geode(); |
| 447 | geode->addDrawable(createPanoramicSphericalDisplayDistortionMesh( |
| 448 | Vec3(0.0f,0.0f,0.0f), Vec3(width,0.0f,0.0f), |
| 449 | Vec3(0.0f,height,0.0f), radius, collar)); |
| 450 | |
| 451 | // new we need to add the texture to the mesh, we do so by creating a |
| 452 | // StateSet to contain the Texture StateAttribute. |
| 453 | StateSet* stateset = geode->getOrCreateStateSet(); |
| 454 | stateset->setTextureAttributeAndModes(0, texRect, StateAttribute::ON); |
| 455 | stateset->setMode(GL_LIGHTING, StateAttribute::OFF); |
| 456 | |
| 457 | TexMat* texmat = new TexMat; |
| 458 | texmat->setScaleByTextureRectangleSize(true); |
| 459 | stateset->setTextureAttributeAndModes(0, texmat, osg::StateAttribute::ON); |
| 460 | #if 0 |
| 461 | if (!applyIntensityMapAsColours && intensityMap) |
| 462 | { |
| 463 | stateset->setTextureAttributeAndModes(1, new osg::Texture2D(intensityMap), osg::StateAttribute::ON); |
| 464 | } |
| 465 | #endif |
| 466 | // add subgraph to render |
| 467 | camera->addChild(geode); |
| 468 | camera->setClearMask(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); |
| 469 | camera->setClearColor(osg::Vec4(0.0, 0.0, 0.0, 1.0)); |
| 470 | camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); |
| 471 | camera->setCullingMode(osg::CullSettings::NO_CULLING); |
| 472 | camera->setName("DistortionCorrectionCamera"); |
| 473 | #endif |
| 474 | } |
| 475 | |
| 476 | CameraInfo* CameraGroup::buildCamera(SGPropertyNode* cameraNode) |
| 477 | { |
| 478 | WindowBuilder *wBuild = WindowBuilder::getWindowBuilder(); |
| 479 | const SGPropertyNode* windowNode = cameraNode->getNode("window"); |
| 480 | GraphicsWindow* window = 0; |
| 481 | int cameraFlags = CameraInfo::DO_INTERSECTION_TEST; |
| 482 | if (windowNode) { |
| 483 | // New style window declaration / definition |
| 484 | window = wBuild->buildWindow(windowNode); |
| 485 | } else { |
| 486 | // Old style: suck window params out of camera block |
| 487 | window = wBuild->buildWindow(cameraNode); |
| 488 | } |
| 489 | if (!window) { |
| 490 | return nullptr; |
| 491 | } |
| 492 | |
| 493 | // Set the projection matrix near/far behaviour |
| 494 | ProjectionMatrix::Type proj_type = ProjectionMatrix::STANDARD; |
| 495 | |
| 496 | // Set vr-mirror flag so camera switches to VR mirror when appropriate. |
| 497 | if (cameraNode->getBoolValue("vr-mirror", false)) |
| 498 | cameraFlags |= CameraInfo::VR_MIRROR; |
| 499 | |
| 500 | osg::Matrix vOff; |
| 501 | const SGPropertyNode* viewNode = cameraNode->getNode("view"); |
| 502 | if (viewNode) { |
| 503 | double heading = viewNode->getDoubleValue("heading-deg", 0.0); |
| 504 | double pitch = viewNode->getDoubleValue("pitch-deg", 0.0); |
| 505 | double roll = viewNode->getDoubleValue("roll-deg", 0.0); |
| 506 | double x = viewNode->getDoubleValue("x", 0.0); |
| 507 | double y = viewNode->getDoubleValue("y", 0.0); |
| 508 | double z = viewNode->getDoubleValue("z", 0.0); |
| 509 | // Build a view matrix, which is the inverse of a model |
| 510 | // orientation matrix. |
| 511 | vOff = (Matrix::translate(-x, -y, -z) |
| 512 | * Matrix::rotate(-DegreesToRadians(heading), |
| 513 | Vec3d(0.0, 1.0, 0.0), |
| 514 | -DegreesToRadians(pitch), |
| 515 | Vec3d(1.0, 0.0, 0.0), |
| 516 | -DegreesToRadians(roll), |
| 517 | Vec3d(0.0, 0.0, 1.0))); |
| 518 | if (viewNode->getBoolValue("absolute", false)) |
| 519 | cameraFlags |= CameraInfo::VIEW_ABSOLUTE; |
| 520 | } else { |
| 521 | // Old heading parameter, works in the opposite direction |
| 522 | double heading = cameraNode->getDoubleValue("heading-deg", 0.0); |
| 523 | vOff.makeRotate(DegreesToRadians(heading), osg::Vec3(0, 1, 0)); |
| 524 | } |
| 525 | // Configuring the physical dimensions of a monitor |
| 526 | SGPropertyNode* viewportNode = cameraNode->getNode("viewport", true); |
| 527 | double physicalWidth = viewportNode->getDoubleValue("width", 1024); |
| 528 | double physicalHeight = viewportNode->getDoubleValue("height", 768); |
| 529 | double bezelHeightTop = 0; |
| 530 | double bezelHeightBottom = 0; |
| 531 | double bezelWidthLeft = 0; |
| 532 | double bezelWidthRight = 0; |
| 533 | const SGPropertyNode* physicalDimensionsNode = 0; |
| 534 | if ((physicalDimensionsNode = cameraNode->getNode("physical-dimensions")) != 0) { |
| 535 | physicalWidth = physicalDimensionsNode->getDoubleValue("width", physicalWidth); |
| 536 | physicalHeight = physicalDimensionsNode->getDoubleValue("height", physicalHeight); |
| 537 | const SGPropertyNode* bezelNode = 0; |
| 538 | if ((bezelNode = physicalDimensionsNode->getNode("bezel")) != 0) { |
| 539 | bezelHeightTop = bezelNode->getDoubleValue("top", bezelHeightTop); |
| 540 | bezelHeightBottom = bezelNode->getDoubleValue("bottom", bezelHeightBottom); |
| 541 | bezelWidthLeft = bezelNode->getDoubleValue("left", bezelWidthLeft); |
| 542 | bezelWidthRight = bezelNode->getDoubleValue("right", bezelWidthRight); |
| 543 | } |
| 544 | } |
| 545 | osg::Matrix pOff; |
| 546 | CameraInfo *parentInfo = nullptr; |
| 547 | osg::Vec2d parentReference[2]; |
| 548 | osg::Vec2d thisReference[2]; |
| 549 | SGPropertyNode* projectionNode = 0; |
| 550 | if ((projectionNode = cameraNode->getNode("perspective")) != 0) { |
| 551 | double fovy = projectionNode->getDoubleValue("fovy-deg", 55.0); |
| 552 | double aspectRatio = projectionNode->getDoubleValue("aspect-ratio", |
| 553 | 1.0); |
| 554 | double zNear = projectionNode->getDoubleValue("near", 0.0); |
| 555 | double zFar = projectionNode->getDoubleValue("far", zNear + 20000); |
| 556 | double offsetX = projectionNode->getDoubleValue("offset-x", 0.0); |
| 557 | double offsetY = projectionNode->getDoubleValue("offset-y", 0.0); |
| 558 | double tan_fovy = tan(DegreesToRadians(fovy*0.5)); |
| 559 | double right = tan_fovy * aspectRatio * zNear + offsetX; |
| 560 | double left = -tan_fovy * aspectRatio * zNear + offsetX; |
| 561 | double top = tan_fovy * zNear + offsetY; |
| 562 | double bottom = -tan_fovy * zNear + offsetY; |
| 563 | ProjectionMatrix::makeFrustum(pOff, |
| 564 | left, right, |
| 565 | bottom, top, |
| 566 | zNear, zFar, |
| 567 | proj_type); |
| 568 | cameraFlags |= CameraInfo::PROJECTION_ABSOLUTE; |
| 569 | if (projectionNode->getBoolValue("fixed-near-far", true)) |
| 570 | cameraFlags |= CameraInfo::FIXED_NEAR_FAR; |
| 571 | } else if ((projectionNode = cameraNode->getNode("frustum")) != 0 |
| 572 | || (projectionNode = cameraNode->getNode("ortho")) != 0) { |
| 573 | double top = projectionNode->getDoubleValue("top", 0.0); |
| 574 | double bottom = projectionNode->getDoubleValue("bottom", 0.0); |
| 575 | double left = projectionNode->getDoubleValue("left", 0.0); |
| 576 | double right = projectionNode->getDoubleValue("right", 0.0); |
| 577 | double zNear = projectionNode->getDoubleValue("near", 0.0); |
| 578 | double zFar = projectionNode->getDoubleValue("far", zNear + 20000); |
| 579 | if (cameraNode->getNode("frustum")) { |
| 580 | ProjectionMatrix::makeFrustum(pOff, |
| 581 | left, right, |
| 582 | bottom, top, |
| 583 | zNear, zFar, |
| 584 | proj_type); |
| 585 | cameraFlags |= CameraInfo::PROJECTION_ABSOLUTE; |
| 586 | } else { |
| 587 | ProjectionMatrix::makeOrtho(pOff, |
| 588 | left, right, |
| 589 | bottom, top, |
| 590 | zNear, zFar, |
| 591 | proj_type); |
| 592 | cameraFlags |= (CameraInfo::PROJECTION_ABSOLUTE | CameraInfo::ORTHO); |
| 593 | } |
| 594 | if (projectionNode->getBoolValue("fixed-near-far", true)) |
| 595 | cameraFlags |= CameraInfo::FIXED_NEAR_FAR; |
| 596 | } else if ((projectionNode = cameraNode->getNode("master-perspective")) != 0) { |
| 597 | double zNear = projectionNode->getDoubleValue("eye-distance", 0.4*physicalWidth); |
| 598 | double xoff = projectionNode->getDoubleValue("x-offset", 0); |
| 599 | double yoff = projectionNode->getDoubleValue("y-offset", 0); |
| 600 | double left = -0.5*physicalWidth - xoff; |
| 601 | double right = 0.5*physicalWidth - xoff; |
| 602 | double bottom = -0.5*physicalHeight - yoff; |
| 603 | double top = 0.5*physicalHeight - yoff; |
| 604 | ProjectionMatrix::makeFrustum(pOff, |
| 605 | left, right, |
| 606 | bottom, top, |
| 607 | zNear, zNear + 20000.0, |
| 608 | proj_type); |
| 609 | cameraFlags |= CameraInfo::PROJECTION_ABSOLUTE | CameraInfo::ENABLE_MASTER_ZOOM; |
| 610 | } else if ((projectionNode = cameraNode->getNode("right-of-perspective")) |
| 611 | || (projectionNode = cameraNode->getNode("left-of-perspective")) |
| 612 | || (projectionNode = cameraNode->getNode("above-perspective")) |
| 613 | || (projectionNode = cameraNode->getNode("below-perspective")) |
| 614 | || (projectionNode = cameraNode->getNode("reference-points-perspective"))) { |
| 615 | std::string name = projectionNode->getStringValue("parent-camera"); |
| 616 | auto it = std::find_if(_cameras.begin(), _cameras.end(), |
| 617 | [&name](const auto &c) { return c->name == name; }); |
| 618 | if (it == _cameras.end()) { |
| 619 | SG_LOG(SG_VIEW, SG_ALERT, "CameraGroup::buildCamera: " |
| 620 | "failed to find parent camera for relative camera!"); |
| 621 | return nullptr; |
| 622 | } |
| 623 | parentInfo = (*it); |
| 624 | if (projectionNode->getNameString() == "right-of-perspective") { |
| 625 | double tmp = (parentInfo->physicalWidth + 2*parentInfo->bezelWidthRight)/parentInfo->physicalWidth; |
| 626 | parentReference[0] = osg::Vec2d(tmp, -1); |
| 627 | parentReference[1] = osg::Vec2d(tmp, 1); |
| 628 | tmp = (physicalWidth + 2*bezelWidthLeft)/physicalWidth; |
| 629 | thisReference[0] = osg::Vec2d(-tmp, -1); |
| 630 | thisReference[1] = osg::Vec2d(-tmp, 1); |
| 631 | } else if (projectionNode->getNameString() == "left-of-perspective") { |
| 632 | double tmp = (parentInfo->physicalWidth + 2*parentInfo->bezelWidthLeft)/parentInfo->physicalWidth; |
| 633 | parentReference[0] = osg::Vec2d(-tmp, -1); |
| 634 | parentReference[1] = osg::Vec2d(-tmp, 1); |
| 635 | tmp = (physicalWidth + 2*bezelWidthRight)/physicalWidth; |
| 636 | thisReference[0] = osg::Vec2d(tmp, -1); |
| 637 | thisReference[1] = osg::Vec2d(tmp, 1); |
| 638 | } else if (projectionNode->getNameString() == "above-perspective") { |
| 639 | double tmp = (parentInfo->physicalHeight + 2*parentInfo->bezelHeightTop)/parentInfo->physicalHeight; |
| 640 | parentReference[0] = osg::Vec2d(-1, tmp); |
| 641 | parentReference[1] = osg::Vec2d(1, tmp); |
| 642 | tmp = (physicalHeight + 2*bezelHeightBottom)/physicalHeight; |
| 643 | thisReference[0] = osg::Vec2d(-1, -tmp); |
| 644 | thisReference[1] = osg::Vec2d(1, -tmp); |
| 645 | } else if (projectionNode->getNameString() == "below-perspective") { |
| 646 | double tmp = (parentInfo->physicalHeight + 2*parentInfo->bezelHeightBottom)/parentInfo->physicalHeight; |
| 647 | parentReference[0] = osg::Vec2d(-1, -tmp); |
| 648 | parentReference[1] = osg::Vec2d(1, -tmp); |
| 649 | tmp = (physicalHeight + 2*bezelHeightTop)/physicalHeight; |
| 650 | thisReference[0] = osg::Vec2d(-1, tmp); |
| 651 | thisReference[1] = osg::Vec2d(1, tmp); |
| 652 | } else if (projectionNode->getNameString() == "reference-points-perspective") { |
| 653 | SGPropertyNode* parentNode = projectionNode->getNode("parent", true); |
| 654 | SGPropertyNode* thisNode = projectionNode->getNode("this", true); |
| 655 | SGPropertyNode* pointNode; |
| 656 | |
| 657 | pointNode = parentNode->getNode("point", 0, true); |
| 658 | parentReference[0][0] = pointNode->getDoubleValue("x", 0)*2/parentInfo->physicalWidth; |
| 659 | parentReference[0][1] = pointNode->getDoubleValue("y", 0)*2/parentInfo->physicalHeight; |
| 660 | pointNode = parentNode->getNode("point", 1, true); |
| 661 | parentReference[1][0] = pointNode->getDoubleValue("x", 0)*2/parentInfo->physicalWidth; |
| 662 | parentReference[1][1] = pointNode->getDoubleValue("y", 0)*2/parentInfo->physicalHeight; |
| 663 | |
| 664 | pointNode = thisNode->getNode("point", 0, true); |
| 665 | thisReference[0][0] = pointNode->getDoubleValue("x", 0)*2/physicalWidth; |
| 666 | thisReference[0][1] = pointNode->getDoubleValue("y", 0)*2/physicalHeight; |
| 667 | pointNode = thisNode->getNode("point", 1, true); |
| 668 | thisReference[1][0] = pointNode->getDoubleValue("x", 0)*2/physicalWidth; |
| 669 | thisReference[1][1] = pointNode->getDoubleValue("y", 0)*2/physicalHeight; |
| 670 | } |
| 671 | |
| 672 | ProjectionMatrix::makePerspective(pOff, 45, physicalWidth/physicalHeight, |
| 673 | 1, 20000, proj_type); |
| 674 | cameraFlags |= CameraInfo::PROJECTION_ABSOLUTE | CameraInfo::ENABLE_MASTER_ZOOM; |
| 675 | } else { |
| 676 | // old style shear parameters |
| 677 | double shearx = cameraNode->getDoubleValue("shear-x", 0); |
| 678 | double sheary = cameraNode->getDoubleValue("shear-y", 0); |
| 679 | pOff.makeTranslate(-shearx, -sheary, 0); |
| 680 | } |
| 681 | const SGPropertyNode* psNode = cameraNode->getNode("panoramic-spherical"); |
| 682 | //bool useMasterSceneGraph = !psNode; |
| 683 | |
| 684 | CameraInfo *info = new CameraInfo(cameraFlags); |
| 685 | _cameras.push_back(info); |
| 686 | info->name = cameraNode->getStringValue("name"); |
| 687 | info->physicalWidth = physicalWidth; |
| 688 | info->physicalHeight = physicalHeight; |
| 689 | info->bezelHeightTop = bezelHeightTop; |
| 690 | info->bezelHeightBottom = bezelHeightBottom; |
| 691 | info->bezelWidthLeft = bezelWidthLeft; |
| 692 | info->bezelWidthRight = bezelWidthRight; |
| 693 | info->relativeCameraParent = parentInfo; |
| 694 | info->parentReference[0] = parentReference[0]; |
| 695 | info->parentReference[1] = parentReference[1]; |
| 696 | info->thisReference[0] = thisReference[0]; |
| 697 | info->thisReference[1] = thisReference[1]; |
| 698 | info->viewOffset = vOff; |
| 699 | info->projOffset = pOff; |
| 700 | |
| 701 | osg::Viewport *viewport = new osg::Viewport( |
| 702 | viewportNode->getDoubleValue("x"), |
| 703 | viewportNode->getDoubleValue("y"), |
| 704 | // If no width or height has been specified, fill the entire window |
| 705 | viewportNode->getDoubleValue("width", window->gc->getTraits()->width), |
| 706 | viewportNode->getDoubleValue("height",window->gc->getTraits()->height)); |
| 707 | |
| 708 | std::string compositor_path = cameraNode->getStringValue("compositor", ""); |
| 709 | if (compositor_path.empty()) { |
| 710 | compositor_path = fgGetString("/sim/rendering/default-compositor", |
| 711 | "Compositor/default"); |
| 712 | } else { |
| 713 | // Store the custom path in case we need to reload later |
| 714 | info->compositor_path = compositor_path; |
| 715 | } |
| 716 | |
| 717 | osg::ref_ptr<SGReaderWriterOptions> options = |
| 718 | SGReaderWriterOptions::fromPath(globals->get_fg_root()); |
| 719 | options->setPropertyNode(globals->get_props()); |
| 720 | |
| 721 | SViewSetCompositorParams(options, compositor_path); |
| 722 | |
| 723 | Compositor *compositor = nullptr; |
| 724 | if (info->flags & CameraInfo::VR_MIRROR) |
| 725 | compositor = buildVRMirrorCompositor(window->gc, viewport); |
| 726 | if (!compositor) |
| 727 | compositor = Compositor::create(_viewer, |
| 728 | window->gc, |
| 729 | viewport, |
| 730 | compositor_path, |
| 731 | options); |
| 732 | |
| 733 | if (compositor) { |
| 734 | info->compositor.reset(compositor); |
| 735 | } else { |
| 736 | throw sg_exception(std::string("Failed to create Compositor in path '") + |
| 737 | compositor_path + "'"); |
| 738 | } |
| 739 | |
| 740 | // Distortion camera needs the viewport which is created by addCamera(). |
| 741 | if (psNode) { |
| 742 | info->flags = info->flags | CameraInfo::VIEW_ABSOLUTE; |
| 743 | //buildDistortionCamera(psNode, camera); |
| 744 | } |
| 745 | |
| 746 | return info; |
| 747 | } |
| 748 | |
| 749 | void CameraGroup::removeCamera(CameraInfo *info) |
| 750 | { |
| 751 | for (auto it = _cameras.begin(); it != _cameras.end(); ++it) { |
| 752 | if (*it == info) { |
| 753 | _cameras.erase(it); |
| 754 | return; |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | void CameraGroup::buildSplashCamera(SGPropertyNode* cameraNode, |
| 760 | GraphicsWindow* window) |
| 761 | { |
| 762 | WindowBuilder* wBuild = WindowBuilder::getWindowBuilder(); |
| 763 | const SGPropertyNode* windowNode = (cameraNode |
| 764 | ? cameraNode->getNode("window") |
| 765 | : 0); |
| 766 | if (!window && windowNode) { |
| 767 | // New style window declaration / definition |
| 768 | window = wBuild->buildWindow(windowNode, true /*isMainWindow*/); |
| 769 | } |
| 770 | |
| 771 | if (!window) { // buildWindow can fail |
| 772 | SG_LOG(SG_VIEW, SG_WARN, "CameraGroup::buildSplashCamera: failed to build a window"); |
| 773 | return; |
| 774 | } |
| 775 | |
| 776 | Camera* camera = new Camera; |
| 777 | camera->setName("SplashCamera"); |
| 778 | camera->setAllowEventFocus(false); |
| 779 | camera->setGraphicsContext(window->gc.get()); |
| 780 | // If a viewport isn't set on the camera, then it's hard to dig it |
| 781 | // out of the SceneView objects in the viewer, and the coordinates |
| 782 | // of mouse events are somewhat bizzare. |
| 783 | osg::Viewport* viewport = new osg::Viewport( |
| 784 | 0, 0, window->gc->getTraits()->width, window->gc->getTraits()->height); |
| 785 | camera->setViewport(viewport); |
| 786 | camera->setClearMask(0); |
| 787 | camera->setInheritanceMask(CullSettings::ALL_VARIABLES |
| 788 | & ~(CullSettings::COMPUTE_NEAR_FAR_MODE |
| 789 | | CullSettings::CULLING_MODE |
| 790 | | CullSettings::CLEAR_MASK |
| 791 | )); |
| 792 | camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); |
| 793 | camera->setCullingMode(osg::CullSettings::NO_CULLING); |
| 794 | camera->setProjectionResizePolicy(osg::Camera::FIXED); |
| 795 | |
| 796 | // The camera group will always update the camera |
| 797 | camera->setReferenceFrame(Transform::ABSOLUTE_RF); |
| 798 | |
| 799 | // XXX Camera needs to be drawn just before GUI; eventually the render order |
| 800 | // should be assigned by a camera manager. |
| 801 | camera->setRenderOrder(osg::Camera::POST_RENDER, 9999); |
| 802 | |
| 803 | // Add splash screen! |
| 804 | camera->addChild(globals->get_renderer()->getSplash()); |
| 805 | |
| 806 | Pass* pass = new Pass; |
| 807 | pass->camera = camera; |
| 808 | pass->useMastersSceneData = false; |
| 809 | |
| 810 | // For now we just build a simple Compositor directly from C++ space that |
| 811 | // encapsulates a single osg::Camera. This could be improved by letting |
| 812 | // users change the Compositor config in XML space, for example to be able |
| 813 | // to add post-processing to a HUD. |
| 814 | // However, since many other parts of FG require direct access to the GUI |
| 815 | // osg::Camera object, this is fine for now. |
| 816 | Compositor* compositor = new Compositor(_viewer, window->gc, viewport); |
| 817 | compositor->addPass(pass); |
| 818 | |
| 819 | const int cameraFlags = CameraInfo::SPLASH; |
| 820 | CameraInfo* info = new CameraInfo(cameraFlags); |
| 821 | info->name = "Splash camera"; |
| 822 | info->viewOffset = osg::Matrix::identity(); |
| 823 | info->projOffset = osg::Matrix::identity(); |
| 824 | info->compositor.reset(compositor); |
| 825 | _cameras.push_back(info); |
| 826 | |
| 827 | // Disable statistics for the splash camera. |
| 828 | camera->setStats(0); |
| 829 | } |
| 830 | |
| 831 | void CameraGroup::buildGUICamera(SGPropertyNode* cameraNode, |
| 832 | GraphicsWindow* window) |
| 833 | { |
| 834 | WindowBuilder *wBuild = WindowBuilder::getWindowBuilder(); |
| 835 | const SGPropertyNode* windowNode = (cameraNode |
| 836 | ? cameraNode->getNode("window") |
| 837 | : 0); |
| 838 | if (!window && windowNode) { |
| 839 | // New style window declaration / definition |
| 840 | window = wBuild->buildWindow(windowNode, true /*isMainWindow*/); |
| 841 | } |
| 842 | |
| 843 | if (!window) { // buildWindow can fail |
| 844 | SG_LOG(SG_VIEW, SG_WARN, "CameraGroup::buildGUICamera: failed to build a window"); |
| 845 | return; |
| 846 | } |
| 847 | |
| 848 | Camera* camera = new Camera; |
| 849 | camera->setName( "GUICamera" ); |
| 850 | camera->setAllowEventFocus(false); |
| 851 | camera->setGraphicsContext(window->gc.get()); |
| 852 | // If a viewport isn't set on the camera, then it's hard to dig it |
| 853 | // out of the SceneView objects in the viewer, and the coordinates |
| 854 | // of mouse events are somewhat bizzare. |
| 855 | osg::Viewport *viewport = new osg::Viewport( |
| 856 | 0, 0, window->gc->getTraits()->width, window->gc->getTraits()->height); |
| 857 | camera->setViewport(viewport); |
| 858 | camera->setClearMask(0); |
| 859 | camera->setInheritanceMask(CullSettings::ALL_VARIABLES |
| 860 | & ~(CullSettings::COMPUTE_NEAR_FAR_MODE |
| 861 | | CullSettings::CULLING_MODE |
| 862 | | CullSettings::CLEAR_MASK |
| 863 | )); |
| 864 | camera->setComputeNearFarMode(osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR); |
| 865 | camera->setCullingMode(osg::CullSettings::NO_CULLING); |
| 866 | camera->setProjectionResizePolicy(osg::Camera::FIXED); |
| 867 | |
| 868 | // The camera group will always update the camera |
| 869 | camera->setReferenceFrame(Transform::ABSOLUTE_RF); |
| 870 | |
| 871 | // Draw all nodes in the order they are added to the GUI camera |
| 872 | camera->getOrCreateStateSet() |
| 873 | ->setRenderBinDetails( 0, |
| 874 | "PreOrderBin", |
| 875 | osg::StateSet::OVERRIDE_RENDERBIN_DETAILS ); |
| 876 | |
| 877 | // XXX Camera needs to be drawn last; eventually the render order |
| 878 | // should be assigned by a camera manager. |
| 879 | camera->setRenderOrder(osg::Camera::POST_RENDER, 10000); |
| 880 | |
| 881 | Pass *pass = new Pass; |
| 882 | pass->camera = camera; |
| 883 | pass->useMastersSceneData = false; |
| 884 | pass->update_callback = new GUIUpdateCallback; |
| 885 | |
| 886 | // For now we just build a simple Compositor directly from C++ space that |
| 887 | // encapsulates a single osg::Camera. This could be improved by letting |
| 888 | // users change the Compositor config in XML space, for example to be able |
| 889 | // to add post-processing to a HUD. |
| 890 | // However, since many other parts of FG require direct access to the GUI |
| 891 | // osg::Camera object, this is fine for now. |
| 892 | Compositor *compositor = new Compositor(_viewer, window->gc, viewport); |
| 893 | compositor->addPass(pass); |
| 894 | |
| 895 | const int cameraFlags = CameraInfo::GUI | CameraInfo::DO_INTERSECTION_TEST; |
| 896 | CameraInfo* info = new CameraInfo(cameraFlags); |
| 897 | info->name = "GUI camera"; |
| 898 | info->viewOffset = osg::Matrix::identity(); |
| 899 | info->projOffset = osg::Matrix::identity(); |
| 900 | info->compositor.reset(compositor); |
| 901 | _cameras.push_back(info); |
| 902 | |
| 903 | // Disable statistics for the GUI camera. |
| 904 | camera->setStats(0); |
| 905 | } |
| 906 | |
| 907 | Compositor *CameraGroup::buildVRMirrorCompositor(osg::GraphicsContext* gc, |
| 908 | osg::Viewport *viewport) |
| 909 | { |
| 910 | #ifdef ENABLE_OSGXR |
| 911 | if (VRManager::instance()->getUseMirror()) { |
| 912 | Camera* camera = new Camera; |
| 913 | camera->setName("VRMirror"); |
| 914 | camera->setAllowEventFocus(false); |
| 915 | camera->setGraphicsContext(gc); |
| 916 | camera->setViewport(viewport); |
| 917 | camera->setClearMask(0); |
| 918 | camera->setInheritanceMask(CullSettings::ALL_VARIABLES |
| 919 | & ~(CullSettings::COMPUTE_NEAR_FAR_MODE |
| 920 | | CullSettings::CULLING_MODE |
| 921 | | CullSettings::CLEAR_MASK |
| 922 | )); |
| 923 | camera->setComputeNearFarMode(CullSettings::DO_NOT_COMPUTE_NEAR_FAR); |
| 924 | camera->setCullingMode(CullSettings::NO_CULLING); |
| 925 | camera->setProjectionResizePolicy(Camera::FIXED); |
| 926 | |
| 927 | // The camera group will always update the camera |
| 928 | camera->setReferenceFrame(Transform::ABSOLUTE_RF); |
| 929 | |
| 930 | // Mirror camera needs to be drawn after VR cameras and before GUI |
| 931 | camera->setRenderOrder(Camera::POST_RENDER, 9000); |
| 932 | |
| 933 | // Let osgXR do the mirror camera setup |
| 934 | VRManager::instance()->setupMirrorCamera(camera); |
| 935 | |
| 936 | Pass *pass = new Pass; |
| 937 | pass->camera = camera; |
| 938 | pass->useMastersSceneData = false; |
| 939 | |
| 940 | // We just build a simple Compositor directly from C++ space that |
| 941 | // encapsulates a single osg::Camera. |
| 942 | Compositor *compositor = new Compositor(_viewer, gc, viewport); |
| 943 | compositor->addPass(pass); |
| 944 | |
| 945 | return compositor; |
| 946 | } |
| 947 | #endif |
| 948 | return nullptr; |
| 949 | } |
| 950 | |
| 951 | CameraGroup* CameraGroup::buildCameraGroup(osgViewer::View* view, |
| 952 | SGPropertyNode* gnode) |
| 953 | { |
| 954 | CameraGroup* cgroup = new CameraGroup(view); |
| 955 | cgroup->_listener.reset(new CameraGroupListener(cgroup, gnode)); |
| 956 | |
| 957 | for (int i = 0; i < gnode->nChildren(); ++i) { |
| 958 | SGPropertyNode* pNode = gnode->getChild(i); |
| 959 | std::string name = pNode->getNameString(); |
| 960 | if (name == "camera") { |
| 961 | cgroup->buildCamera(pNode); |
| 962 | } else if (name == "window") { |
| 963 | WindowBuilder::getWindowBuilder()->buildWindow(pNode); |
| 964 | } else if (name == "splash") { |
| 965 | cgroup->buildSplashCamera(pNode); |
| 966 | } else if (name == "gui") { |
| 967 | cgroup->buildGUICamera(pNode); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | return cgroup; |
| 972 | } |
| 973 | |
| 974 | void CameraGroup::setCameraCullMasks(osg::Node::NodeMask nm) |
| 975 | { |
| 976 | for (auto& info : _cameras) { |
| 977 | if (info->flags & CameraInfo::GUI) |
| 978 | continue; |
| 979 | info->compositor->setCullMask(nm); |
| 980 | } |
| 981 | } |
| 982 | |
| 983 | void CameraGroup::resized() |
| 984 | { |
| 985 | for (const auto &info : _cameras) |
| 986 | info->compositor->resized(); |
| 987 | } |
| 988 | |
| 989 | CameraInfo* CameraGroup::getGUICamera() const |
| 990 | { |
| 991 | auto result = std::find_if(_cameras.begin(), _cameras.end(), |
| 992 | [](const osg::ref_ptr<CameraInfo> &i) { |
| 993 | return (i->flags & CameraInfo::GUI) != 0; |
| 994 | }); |
| 995 | if (result == _cameras.end()) |
| 996 | return 0; |
| 997 | return (*result); |
| 998 | } |
| 999 | |
| 1000 | osg::Camera* getGUICamera(CameraGroup* cgroup) |
| 1001 | { |
| 1002 | return cgroup->getGUICamera()->compositor->getPass(0)->camera; |
| 1003 | } |
| 1004 | |
| 1005 | const CameraGroup::CameraList& CameraGroup::getCameras() |
| 1006 | { |
| 1007 | return _cameras; |
| 1008 | } |
| 1009 | |
| 1010 | static bool |
| 1011 | computeCameraIntersection(const CameraGroup *cgroup, |
| 1012 | const CameraInfo *cinfo, |
| 1013 | const osg::Vec2d &windowPos, |
| 1014 | osgUtil::LineSegmentIntersector::Intersections &intersections) |
| 1015 | { |
| 1016 | if (!(cinfo->flags & CameraInfo::DO_INTERSECTION_TEST)) |
| 1017 | return false; |
| 1018 | |
| 1019 | const osg::Viewport *viewport = cinfo->compositor->getViewport(); |
| 1020 | SGRect<double> viewportRect(viewport->x(), viewport->y(), |
| 1021 | viewport->x() + viewport->width() - 1.0, |
| 1022 | viewport->y() + viewport->height()- 1.0); |
| 1023 | double epsilon = 0.5; |
| 1024 | if (!viewportRect.contains(windowPos.x(), windowPos.y(), epsilon)) |
| 1025 | return false; |
| 1026 | |
| 1027 | osg::Vec4d start(windowPos.x(), windowPos.y(), 0.0, 1.0); |
| 1028 | osg::Vec4d end(windowPos.x(), windowPos.y(), 1.0, 1.0); |
| 1029 | osg::Matrix windowMat = viewport->computeWindowMatrix(); |
| 1030 | osg::Matrix invViewMat = osg::Matrix::inverse(cinfo->viewMatrix); |
| 1031 | osg::Matrix invProjMat = osg::Matrix::inverse(cinfo->projMatrix * windowMat); |
| 1032 | start = start * invProjMat; |
| 1033 | end = end * invProjMat; |
| 1034 | start /= start.w(); |
| 1035 | end /= end.w(); |
| 1036 | start = start * invViewMat; |
| 1037 | end = end * invViewMat; |
| 1038 | |
| 1039 | osg::ref_ptr<osgUtil::LineSegmentIntersector> picker = |
| 1040 | new osgUtil::LineSegmentIntersector(osgUtil::Intersector::MODEL, |
| 1041 | osg::Vec3d(start.x(), start.y(), start.z()), |
| 1042 | osg::Vec3d(end.x(), end.y(), end.z())); |
| 1043 | osgUtil::IntersectionVisitor iv(picker); |
| 1044 | iv.setTraversalMask(simgear::PICK_BIT); |
| 1045 | |
| 1046 | if (cinfo->flags & CameraInfo::GUI) { |
| 1047 | // For the GUI camera (2D panels) use the camera's subgraph |
| 1048 | cinfo->compositor->getPass(0)->camera->accept(iv); |
| 1049 | } else { |
| 1050 | // For any other camera use the viewer scene data |
| 1051 | const_cast<CameraGroup*>(cgroup)->getView()->getSceneData()->accept(iv); |
| 1052 | } |
| 1053 | if (picker->containsIntersections()) { |
| 1054 | intersections = picker->getIntersections(); |
| 1055 | return true; |
| 1056 | } |
| 1057 | |
| 1058 | return false; |
| 1059 | } |
| 1060 | |
| 1061 | bool computeIntersections(const CameraGroup* cgroup, |
| 1062 | const osg::Vec2d& windowPos, |
| 1063 | osgUtil::LineSegmentIntersector::Intersections& intersections) |
| 1064 | { |
| 1065 | // test the GUI first |
| 1066 | // This is only required so 2D panels get tested first |
| 1067 | CameraInfo* guiCamera = cgroup->getGUICamera(); |
| 1068 | if (guiCamera && computeCameraIntersection(cgroup, guiCamera, windowPos, intersections)) |
| 1069 | return true; |
| 1070 | |
| 1071 | // Find camera that contains event |
| 1072 | for (const auto &cinfo : cgroup->_cameras) { |
| 1073 | if (cinfo == guiCamera) |
| 1074 | continue; |
| 1075 | |
| 1076 | if (computeCameraIntersection(cgroup, cinfo, windowPos, intersections)) |
| 1077 | return true; |
| 1078 | } |
| 1079 | |
| 1080 | intersections.clear(); |
| 1081 | return false; |
| 1082 | } |
| 1083 | |
| 1084 | void warpGUIPointer(CameraGroup* cgroup, int x, int y) |
| 1085 | { |
| 1086 | using osgViewer::GraphicsWindow; |
| 1087 | osg::Camera* guiCamera = getGUICamera(cgroup); |
| 1088 | if (!guiCamera) |
| 1089 | return; |
| 1090 | osg::Viewport* vport = guiCamera->getViewport(); |
| 1091 | GraphicsWindow* gw |
| 1092 | = dynamic_cast<GraphicsWindow*>(guiCamera->getGraphicsContext()); |
| 1093 | if (!gw) |
| 1094 | return; |
| 1095 | globals->get_renderer()->getEventHandler()->setMouseWarped(); |
| 1096 | // Translate the warp request into the viewport of the GUI camera, |
| 1097 | // send the request to the window, then transform the coordinates |
| 1098 | // for the Viewer's event queue. |
| 1099 | double wx = x + vport->x(); |
| 1100 | double wyUp = vport->height() + vport->y() - y; |
| 1101 | double wy; |
| 1102 | const osg::GraphicsContext::Traits* traits = gw->getTraits(); |
| 1103 | if (gw->getEventQueue()->getCurrentEventState()->getMouseYOrientation() |
| 1104 | == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS) { |
| 1105 | wy = traits->height - wyUp; |
| 1106 | } else { |
| 1107 | wy = wyUp; |
| 1108 | } |
| 1109 | gw->getEventQueue()->mouseWarped(wx, wy); |
| 1110 | gw->requestWarpPointer(wx, wy); |
| 1111 | osgGA::GUIEventAdapter* eventState |
| 1112 | = cgroup->getView()->getEventQueue()->getCurrentEventState(); |
| 1113 | double viewerX |
| 1114 | = (eventState->getXmin() |
| 1115 | + ((wx / double(traits->width)) |
| 1116 | * (eventState->getXmax() - eventState->getXmin()))); |
| 1117 | double viewerY |
| 1118 | = (eventState->getYmin() |
| 1119 | + ((wyUp / double(traits->height)) |
| 1120 | * (eventState->getYmax() - eventState->getYmin()))); |
| 1121 | cgroup->getView()->getEventQueue()->mouseWarped(viewerX, viewerY); |
| 1122 | } |
| 1123 | |
| 1124 | void reloadCompositors(CameraGroup *cgroup) |
| 1125 | { |
| 1126 | auto viewer_base = globals->get_renderer()->getViewerBase(); |
| 1127 | bool should_restart_threading = viewer_base->areThreadsRunning(); |
| 1128 | if (should_restart_threading) { |
| 1129 | viewer_base->stopThreading(); |
| 1130 | } |
| 1131 | |
| 1132 | // Prevent the camera render orders increasing indefinitely with each reload |
| 1133 | Compositor::resetOrderOffset(); |
| 1134 | |
| 1135 | for (auto &info : cgroup->_cameras) { |
| 1136 | // Ignore the splash & GUI camera |
| 1137 | if (info->flags & (CameraInfo::SPLASH | CameraInfo::GUI)) |
| 1138 | continue; |
| 1139 | // Get the viewport and the graphics context from the old Compositor |
| 1140 | osg::ref_ptr<osg::Viewport> viewport = info->compositor->getViewport(); |
| 1141 | osg::ref_ptr<osg::GraphicsContext> gc = |
| 1142 | info->compositor->getGraphicsContext(); |
| 1143 | osg::ref_ptr<SGReaderWriterOptions> options = |
| 1144 | SGReaderWriterOptions::fromPath(globals->get_fg_root()); |
| 1145 | options->setPropertyNode(globals->get_props()); |
| 1146 | |
| 1147 | if (info->reloadCompositorCallback.valid()) |
| 1148 | info->reloadCompositorCallback->preReloadCompositor(cgroup, info); |
| 1149 | |
| 1150 | // Force deletion |
| 1151 | info->compositor.reset(nullptr); |
| 1152 | // Then replace it with a new instance |
| 1153 | std::string compositor_path = info->compositor_path.empty() ? |
| 1154 | fgGetString("/sim/rendering/default-compositor", "Compositor/default") : |
| 1155 | info->compositor_path; |
| 1156 | Compositor *compositor = nullptr; |
| 1157 | if (info->flags & CameraInfo::VR_MIRROR) |
| 1158 | compositor = cgroup->buildVRMirrorCompositor(gc, viewport); |
| 1159 | if (!compositor) |
| 1160 | compositor = Compositor::create(cgroup->_viewer, |
| 1161 | gc, |
| 1162 | viewport, |
| 1163 | compositor_path, |
| 1164 | options); |
| 1165 | info->compositor.reset(compositor); |
| 1166 | |
| 1167 | if (info->reloadCompositorCallback.valid()) |
| 1168 | info->reloadCompositorCallback->postReloadCompositor(cgroup, info); |
| 1169 | } |
| 1170 | |
| 1171 | if (should_restart_threading) { |
| 1172 | viewer_base->startThreading(); |
| 1173 | } |
| 1174 | fgSetBool("/sim/rendering/compositor-reload-required", false); |
| 1175 | } |
| 1176 | |
| 1177 | void CameraGroup::buildDefaultGroup(osgViewer::View* viewer) |
| 1178 | { |
| 1179 | // Look for windows, camera groups, and the old syntax of |
| 1180 | // top-level cameras |
| 1181 | SGPropertyNode* renderingNode = fgGetNode("/sim/rendering"); |
| 1182 | SGPropertyNode* cgroupNode = renderingNode->getNode("camera-group", true); |
| 1183 | bool oldSyntax = !cgroupNode->hasChild("camera"); |
| 1184 | if (oldSyntax) { |
| 1185 | for (int i = 0; i < renderingNode->nChildren(); ++i) { |
| 1186 | SGPropertyNode* propNode = renderingNode->getChild(i); |
| 1187 | const std::string propName = propNode->getNameString(); |
| 1188 | if (propName == "window" || propName == "camera") { |
| 1189 | SGPropertyNode* copiedNode |
| 1190 | = cgroupNode->getNode(propName, propNode->getIndex(), true); |
| 1191 | copyProperties(propNode, copiedNode); |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | SGPropertyNodeVec cameras(cgroupNode->getChildren("camera")); |
| 1196 | SGPropertyNode* masterCamera = 0; |
| 1197 | SGPropertyNodeVec::const_iterator it; |
| 1198 | for (it = cameras.begin(); it != cameras.end(); ++it) { |
| 1199 | if ((*it)->getDoubleValue("shear-x", 0.0) == 0.0 |
| 1200 | && (*it)->getDoubleValue("shear-y", 0.0) == 0.0) { |
| 1201 | masterCamera = it->ptr(); |
| 1202 | break; |
| 1203 | } |
| 1204 | } |
| 1205 | if (!masterCamera) { |
| 1206 | WindowBuilder *windowBuilder = WindowBuilder::getWindowBuilder(); |
| 1207 | masterCamera = cgroupNode->getChild("camera", cameras.size(), true); |
| 1208 | setValue(masterCamera->getNode("window/name", true), |
| 1209 | windowBuilder->getDefaultWindowName()); |
| 1210 | // Use VR mirror compositor when VR is enabled. |
| 1211 | setValue(masterCamera->getNode("vr-mirror", true), true); |
| 1212 | } |
| 1213 | SGPropertyNode* nameNode = masterCamera->getNode("window/name"); |
| 1214 | if (nameNode) |
| 1215 | setValue(cgroupNode->getNode("gui/window/name", true), |
| 1216 | nameNode->getStringValue()); |
| 1217 | } |
| 1218 | |
| 1219 | SGPropertyNode* splashWindowNameNode = cgroupNode->getNode("splash/window/name"); |
| 1220 | if (!splashWindowNameNode) { |
| 1221 | // Find the first camera with a window name |
| 1222 | SGPropertyNodeVec cameras(cgroupNode->getChildren("camera")); |
| 1223 | for (auto it = cameras.begin(); it != cameras.end(); ++it) { |
| 1224 | SGPropertyNode* nameNode = (*it)->getNode("window/name"); |
| 1225 | if (nameNode) { |
| 1226 | // Use that window name for the splash |
| 1227 | setValue(cgroupNode->getNode("splash/window/name", true), |
| 1228 | nameNode->getStringValue()); |
| 1229 | break; |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | CameraGroup* cgroup = buildCameraGroup(viewer, cgroupNode); |
| 1235 | setDefault(cgroup); |
| 1236 | } |
| 1237 | |
| 1238 | } // of namespace flightgear |