X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FMain%2FCameraGroup.cxx;h=9ba93143df6374b22c8743940fd123e4441fa75a;hb=11d15b451347674fba77648700d23c5aaec3c6c2;hp=e41dc05fbeb4eaffdbe86abbbbc825000d52b49c;hpb=52c46e65a1a10ab011fb6144811ab66ccf72a08d;p=flightgear.git diff --git a/src/Main/CameraGroup.cxx b/src/Main/CameraGroup.cxx index e41dc05fb..9ba93143d 100644 --- a/src/Main/CameraGroup.cxx +++ b/src/Main/CameraGroup.cxx @@ -23,6 +23,7 @@ #include "WindowSystemAdapter.hxx" #include #include +#include #include #include @@ -40,6 +41,7 @@ #include #include +#include namespace flightgear { @@ -100,14 +102,12 @@ void makeNewProjMat(Matrixd& oldProj, double znear, void installCullVisitor(Camera* camera) { -#if 0 // Not yet osgViewer::Renderer* renderer = static_cast(camera->getRenderer()); for (int i = 0; i < 2; ++i) { osgUtil::SceneView* sceneView = renderer->getSceneView(i); sceneView->setCullVisitor(new simgear::EffectCullVisitor); } -#endif } } @@ -134,8 +134,12 @@ CameraInfo* CameraGroup::addCamera(unsigned flags, Camera* camera, Camera* farCamera = 0; if ((flags & (GUI | ORTHO)) == 0) { - farCamera = simgear::clone(camera); + farCamera = new Camera; + farCamera->setAllowEventFocus(camera->getAllowEventFocus()); farCamera->setGraphicsContext(camera->getGraphicsContext()); + farCamera->setCullingMode(camera->getCullingMode()); + farCamera->setInheritanceMask(camera->getInheritanceMask()); + farCamera->setReferenceFrame(Transform::ABSOLUTE_RF); // Each camera's viewport is written when the window is // resized; if the the viewport isn't copied here, it gets updated // twice and ends up with the wrong value. @@ -144,15 +148,15 @@ CameraInfo* CameraGroup::addCamera(unsigned flags, Camera* camera, installCullVisitor(farCamera); info->farCamera = farCamera; info->farSlaveIndex = _viewer->getNumSlaves() - 1; - farCamera->setRenderOrder(Camera::NESTED_RENDER, info->farSlaveIndex); + farCamera->setRenderOrder(Camera::POST_RENDER, info->farSlaveIndex); camera->setCullMask(camera->getCullMask() & ~simgear::BACKGROUND_BIT); + camera->setClearMask(GL_DEPTH_BUFFER_BIT); } - camera->setClearMask(GL_DEPTH_BUFFER_BIT); _viewer->addSlave(camera, view, projection, useMasterSceneData); installCullVisitor(camera); info->camera = camera; info->slaveIndex = _viewer->getNumSlaves() - 1; - camera->setRenderOrder(Camera::NESTED_RENDER, info->slaveIndex); + camera->setRenderOrder(Camera::POST_RENDER, info->slaveIndex); _cameras.push_back(info); return info; } @@ -190,18 +194,22 @@ void CameraGroup::update(const osg::Vec3d& position, double left, right, bottom, top, parentNear, parentFar; projectionMatrix.getFrustum(left, right, bottom, top, parentNear, parentFar); - if (parentFar < 100.0) { + if (parentFar < _nearField || _nearField == 0.0f) { camera->setProjectionMatrix(projectionMatrix); camera->setCullMask(camera->getCullMask() | simgear::BACKGROUND_BIT); + camera->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); farCamera->setNodeMask(0); } else { Matrix nearProj, farProj; - makeNewProjMat(projectionMatrix, parentNear, 100.0, nearProj); - makeNewProjMat(projectionMatrix, 100.0, parentFar, farProj); + makeNewProjMat(projectionMatrix, parentNear, _nearField, + nearProj); + makeNewProjMat(projectionMatrix, _nearField, parentFar, + farProj); camera->setProjectionMatrix(nearProj); camera->setCullMask(camera->getCullMask() & ~simgear::BACKGROUND_BIT); + camera->setClearMask(GL_DEPTH_BUFFER_BIT); farCamera->setProjectionMatrix(farProj); farCamera->setNodeMask(camera->getNodeMask()); } @@ -211,11 +219,11 @@ void CameraGroup::update(const osg::Vec3d& position, void CameraGroup::setCameraParameters(float vfov, float aspectRatio) { - const float zNear = .1f; - const float zFar = 120000.0f; - _viewer->getCamera()->setProjectionMatrixAsPerspective(vfov, - 1.0f / aspectRatio, - zNear, zFar); + if (vfov != 0.0f && aspectRatio != 0.0f) + _viewer->getCamera() + ->setProjectionMatrixAsPerspective(vfov, + 1.0f / aspectRatio, + _zNear, _zFar); } } @@ -290,7 +298,6 @@ CameraInfo* CameraGroup::buildCamera(SGPropertyNode* cameraNode) WindowBuilder *wBuild = WindowBuilder::getWindowBuilder(); const SGPropertyNode* windowNode = cameraNode->getNode("window"); GraphicsWindow* window = 0; - static int cameraNum = 0; int cameraFlags = DO_INTERSECTION_TEST; if (windowNode) { // New style window declaration / definition @@ -381,6 +388,7 @@ CameraInfo* CameraGroup::buildCamera(SGPropertyNode* cameraNode) // of mouse events are somewhat bizzare. SGPropertyNode* viewportNode = cameraNode->getNode("viewport", true); buildViewport(info, viewportNode, window->gc->getTraits()); + updateCameras(info); return info; } @@ -391,7 +399,6 @@ CameraInfo* CameraGroup::buildGUICamera(SGPropertyNode* cameraNode, const SGPropertyNode* windowNode = (cameraNode ? cameraNode->getNode("window") : 0); - static int cameraNum = 0; if (!window) { if (windowNode) { // New style window declaration / definition @@ -443,16 +450,25 @@ CameraGroup* CameraGroup::buildCameraGroup(osgViewer::Viewer* viewer, cgroup->buildGUICamera(pNode); } } + bindMemberToNode(gnode, "znear", cgroup, &CameraGroup::_zNear, .1f); + bindMemberToNode(gnode, "zfar", cgroup, &CameraGroup::_zFar, 120000.0f); + bindMemberToNode(gnode, "near-field", cgroup, &CameraGroup::_nearField, + 100.0f); return cgroup; } void CameraGroup::setCameraCullMasks(Node::NodeMask nm) { for (CameraIterator i = camerasBegin(), e = camerasEnd(); i != e; ++i) { - if ((*i)->flags & GUI) + CameraInfo* info = i->get(); + if (info->flags & GUI) continue; - (*i)->camera->setCullMask(nm & ~simgear::BACKGROUND_BIT); - (*i)->farCamera->setCullMask(nm); + if (info->farCamera.valid() && info->farCamera->getNodeMask() != 0) { + info->camera->setCullMask(nm & ~simgear::BACKGROUND_BIT); + info->farCamera->setCullMask(nm); + } else { + info->camera->setCullMask(nm); + } } } @@ -506,9 +522,25 @@ bool computeIntersections(const CameraGroup* cgroup, && y >= viewport->y() - epsilon && y < viewport->y() + viewport->height() -1.0 + epsilon)) continue; - LineSegmentIntersector::CoordinateFrame cf = Intersector::WINDOW; + Vec4d start(x, y, 0.0, 1.0); + Vec4d end(x, y, 1.0, 1.0); + Matrix windowMat = viewport->computeWindowMatrix(); + Matrix startPtMat = Matrix::inverse(camera->getProjectionMatrix() + * windowMat); + Matrix endPtMat; + if (!cinfo->farCamera.valid() || cinfo->farCamera->getNodeMask() == 0) + endPtMat = startPtMat; + else + endPtMat = Matrix::inverse(cinfo->farCamera->getProjectionMatrix() + * windowMat); + start = start * startPtMat; + start /= start.w(); + end = end * endPtMat; + end /= end.w(); ref_ptr picker - = new LineSegmentIntersector(cf, x, y); + = new LineSegmentIntersector(Intersector::VIEW, + Vec3d(start.x(), start.y(), start.z()), + Vec3d(end.x(), end.y(), end.z())); osgUtil::IntersectionVisitor iv(picker.get()); const_cast(camera)->accept(iv); if (picker->containsIntersections()) {