| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/View> |
|---|
| 14 | #include <osg/Notify> |
|---|
| 15 | #include <osg/TexEnv> |
|---|
| 16 | #include <osg/DeleteHandler> |
|---|
| 17 | |
|---|
| 18 | using namespace osg; |
|---|
| 19 | |
|---|
| 20 | View::View(): |
|---|
| 21 | Object(true) |
|---|
| 22 | { |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | setLightingMode(HEADLIGHT); |
|---|
| 26 | |
|---|
| 27 | _camera = new osg::Camera; |
|---|
| 28 | _camera->setView(this); |
|---|
| 29 | |
|---|
| 30 | double height = osg::DisplaySettings::instance()->getScreenHeight(); |
|---|
| 31 | double width = osg::DisplaySettings::instance()->getScreenWidth(); |
|---|
| 32 | double distance = osg::DisplaySettings::instance()->getScreenDistance(); |
|---|
| 33 | double vfov = osg::RadiansToDegrees(atan2(height/2.0f,distance)*2.0); |
|---|
| 34 | |
|---|
| 35 | _camera->setProjectionMatrixAsPerspective( vfov, width/height, 1.0f,10000.0f); |
|---|
| 36 | |
|---|
| 37 | _camera->setClearColor(osg::Vec4f(0.2f, 0.2f, 0.4f, 1.0f)); |
|---|
| 38 | |
|---|
| 39 | osg::StateSet* stateset = _camera->getOrCreateStateSet(); |
|---|
| 40 | stateset->setGlobalDefaults(); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | View::View(const osg::View& view, const osg::CopyOp& copyop): |
|---|
| 44 | Object(view,copyop), |
|---|
| 45 | _lightingMode(view._lightingMode), |
|---|
| 46 | _light(view._light), |
|---|
| 47 | _camera(view._camera), |
|---|
| 48 | _slaves(view._slaves) |
|---|
| 49 | { |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | View::~View() |
|---|
| 54 | { |
|---|
| 55 | OSG_INFO<<"Destructing osg::View"<<std::endl; |
|---|
| 56 | |
|---|
| 57 | if (_camera.valid()) |
|---|
| 58 | { |
|---|
| 59 | _camera->setView(0); |
|---|
| 60 | _camera->setCullCallback(0); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | for(Slaves::iterator itr = _slaves.begin(); |
|---|
| 65 | itr != _slaves.end(); |
|---|
| 66 | ++itr) |
|---|
| 67 | { |
|---|
| 68 | Slave& cd = *itr; |
|---|
| 69 | cd._camera->setView(0); |
|---|
| 70 | cd._camera->setCullCallback(0); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | _camera = 0; |
|---|
| 74 | _slaves.clear(); |
|---|
| 75 | _light = 0; |
|---|
| 76 | |
|---|
| 77 | #if 0 |
|---|
| 78 | if (osg::Referenced::getDeleteHandler()) |
|---|
| 79 | { |
|---|
| 80 | |
|---|
| 81 | osg::Referenced::getDeleteHandler()->flushAll(); |
|---|
| 82 | } |
|---|
| 83 | #endif |
|---|
| 84 | |
|---|
| 85 | OSG_INFO<<"Done destructing osg::View"<<std::endl; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | void View::take(osg::View& rhs) |
|---|
| 89 | { |
|---|
| 90 | |
|---|
| 91 | _lightingMode = rhs._lightingMode; |
|---|
| 92 | _light = rhs._light; |
|---|
| 93 | _camera = rhs._camera; |
|---|
| 94 | _slaves = rhs._slaves; |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | if (_camera.valid()) _camera->setView(this); |
|---|
| 98 | |
|---|
| 99 | for(unsigned int i=0; i<_slaves.size(); ++i) |
|---|
| 100 | { |
|---|
| 101 | if (_slaves[i]._camera.valid()) _slaves[i]._camera->setView(this); |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | rhs._light = 0; |
|---|
| 106 | rhs._camera = 0; |
|---|
| 107 | rhs._slaves.clear(); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | |
|---|
| 111 | void View::setLightingMode(LightingMode lightingMode) |
|---|
| 112 | { |
|---|
| 113 | _lightingMode = lightingMode; |
|---|
| 114 | if (_lightingMode != NO_LIGHT && !_light) |
|---|
| 115 | { |
|---|
| 116 | _light = new osg::Light; |
|---|
| 117 | _light->setThreadSafeRefUnref(true); |
|---|
| 118 | _light->setLightNum(0); |
|---|
| 119 | _light->setAmbient(Vec4(0.00f,0.0f,0.00f,1.0f)); |
|---|
| 120 | _light->setDiffuse(Vec4(0.8f,0.8f,0.8f,1.0f)); |
|---|
| 121 | _light->setSpecular(Vec4(1.0f,1.0f,1.0f,1.0f)); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | void View::setCamera(osg::Camera* camera) |
|---|
| 127 | { |
|---|
| 128 | if (_camera.valid()) _camera->setView(0); |
|---|
| 129 | |
|---|
| 130 | _camera = camera; |
|---|
| 131 | |
|---|
| 132 | if (_camera.valid()) |
|---|
| 133 | { |
|---|
| 134 | _camera->setView(this); |
|---|
| 135 | _camera->setRenderer(createRenderer(camera)); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | void View::updateSlaves() |
|---|
| 140 | { |
|---|
| 141 | for(unsigned int i=0; i<_slaves.size(); ++i) |
|---|
| 142 | { |
|---|
| 143 | Slave& slave = _slaves[i]; |
|---|
| 144 | slave.updateSlave(*this); |
|---|
| 145 | } |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | void View::Slave::updateSlaveImplementation(View& view) |
|---|
| 149 | { |
|---|
| 150 | if (!view.getCamera()) return; |
|---|
| 151 | |
|---|
| 152 | if (_camera->getReferenceFrame()==osg::Transform::RELATIVE_RF) |
|---|
| 153 | { |
|---|
| 154 | _camera->setProjectionMatrix(view.getCamera()->getProjectionMatrix() * _projectionOffset); |
|---|
| 155 | _camera->setViewMatrix(view.getCamera()->getViewMatrix() * _viewOffset); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | _camera->inheritCullSettings(*(view.getCamera()), _camera->getInheritanceMask()); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | bool View::addSlave(osg::Camera* camera, const osg::Matrix& projectionOffset, const osg::Matrix& viewOffset, bool useMastersSceneData) |
|---|
| 162 | { |
|---|
| 163 | if (!camera) return false; |
|---|
| 164 | |
|---|
| 165 | camera->setView(this); |
|---|
| 166 | |
|---|
| 167 | unsigned int i = _slaves.size(); |
|---|
| 168 | |
|---|
| 169 | if (useMastersSceneData) |
|---|
| 170 | { |
|---|
| 171 | camera->removeChildren(0,camera->getNumChildren()); |
|---|
| 172 | |
|---|
| 173 | if (_camera.valid()) |
|---|
| 174 | { |
|---|
| 175 | for(unsigned int i=0; i<_camera->getNumChildren(); ++i) |
|---|
| 176 | { |
|---|
| 177 | camera->addChild(_camera->getChild(i)); |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | _slaves.push_back(Slave(camera, projectionOffset, viewOffset, useMastersSceneData)); |
|---|
| 183 | |
|---|
| 184 | _slaves[i].updateSlave(*this); |
|---|
| 185 | |
|---|
| 186 | camera->setRenderer(createRenderer(camera)); |
|---|
| 187 | |
|---|
| 188 | return true; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | bool View::removeSlave(unsigned int pos) |
|---|
| 192 | { |
|---|
| 193 | if (pos >= _slaves.size()) return false; |
|---|
| 194 | |
|---|
| 195 | _slaves[pos]._camera->setView(0); |
|---|
| 196 | _slaves[pos]._camera->setCullCallback(0); |
|---|
| 197 | |
|---|
| 198 | _slaves.erase(_slaves.begin()+pos); |
|---|
| 199 | |
|---|
| 200 | return true; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| 204 | View::Slave * View::findSlaveForCamera(osg::Camera* camera) |
|---|
| 205 | { |
|---|
| 206 | unsigned int i = findSlaveIndexForCamera(camera); |
|---|
| 207 | |
|---|
| 208 | if (i >= getNumSlaves()) return (NULL); |
|---|
| 209 | |
|---|
| 210 | return &(_slaves[i]); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | unsigned int View::findSlaveIndexForCamera(osg::Camera* camera) const |
|---|
| 214 | { |
|---|
| 215 | if (_camera == camera) return _slaves.size(); |
|---|
| 216 | |
|---|
| 217 | for(unsigned int i=0; i<_slaves.size(); ++i) |
|---|
| 218 | { |
|---|
| 219 | if (_slaves[i]._camera == camera) return (i); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | return _slaves.size(); |
|---|
| 223 | } |
|---|