| 1 | // Copyright (C) 2017 James Turner |
| 2 | // |
| 3 | // This program is free software; you can redistribute it and/or |
| 4 | // modify it under the terms of the GNU General Public License as |
| 5 | // published by the Free Software Foundation; either version 2 of the |
| 6 | // License, or (at your option) any later version. |
| 7 | // |
| 8 | // This program is distributed in the hope that it will be useful, but |
| 9 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | // General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with this program; if not, write to the Free Software |
| 15 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | |
| 17 | |
| 18 | #include "config.h" |
| 19 | |
| 20 | #include "PUICamera.hxx" |
| 21 | |
| 22 | #include <osg/StateSet> |
| 23 | #include <osg/State> |
| 24 | #include <osg/Texture2D> |
| 25 | #include <osg/Version> |
| 26 | #include <osg/RenderInfo> |
| 27 | #include <osg/Geometry> |
| 28 | #include <osg/Geode> |
| 29 | #include <osg/BlendFunc> |
| 30 | |
| 31 | #include <osg/NodeVisitor> |
| 32 | #include <osgGA/GUIEventAdapter> |
| 33 | #include <osgGA/GUIEventHandler> |
| 34 | #include <osgUtil/CullVisitor> |
| 35 | #include <osgViewer/Viewer> |
| 36 | |
| 37 | #include <simgear/scene/util/SGNodeMasks.hxx> |
| 38 | |
| 39 | #include <GUI/FlightGear_pu.h> |
| 40 | #include <Main/fg_props.hxx> |
| 41 | #include <Main/globals.hxx> |
| 42 | #include <Main/locale.hxx> |
| 43 | #include <Viewer/CameraGroup.hxx> |
| 44 | #include <Viewer/FGEventHandler.hxx> |
| 45 | #include <Viewer/renderer.hxx> |
| 46 | |
| 47 | #include <Input/input.hxx> |
| 48 | #include <Input/FGMouseInput.hxx> |
| 49 | |
| 50 | // Old versions of PUI are missing these defines |
| 51 | #ifndef PU_SCROLL_UP_BUTTON |
| 52 | #define PU_SCROLL_UP_BUTTON 3 |
| 53 | #endif |
| 54 | #ifndef PU_SCROLL_DOWN_BUTTON |
| 55 | #define PU_SCROLL_DOWN_BUTTON 4 |
| 56 | #endif |
| 57 | |
| 58 | using namespace flightgear; |
| 59 | |
| 60 | double static_pixelRatio = 1.0; |
| 61 | |
| 62 | class PUIDrawable : public osg::Drawable |
| 63 | { |
| 64 | public: |
| 65 | PUIDrawable() |
| 66 | { |
| 67 | setUseDisplayList(false); |
| 68 | setDataVariance(Object::DYNAMIC); |
| 69 | } |
| 70 | |
| 71 | void drawImplementation(osg::RenderInfo& renderInfo) const override |
| 72 | { |
| 73 | osg::State* state = renderInfo.getState(); |
| 74 | state->setActiveTextureUnit(0); |
| 75 | state->setClientActiveTextureUnit(0); |
| 76 | state->disableAllVertexArrays(); |
| 77 | |
| 78 | state->applyMode(GL_FOG, false); |
| 79 | state->applyMode(GL_DEPTH_TEST, false); |
| 80 | state->applyMode(GL_LIGHTING, false); |
| 81 | |
| 82 | glPushAttrib(GL_ALL_ATTRIB_BITS); |
| 83 | glPushClientAttrib(~0u); |
| 84 | |
| 85 | glEnable ( GL_BLEND ) ; |
| 86 | glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ) ; |
| 87 | |
| 88 | // reset pixel storage stuff for PLIB FNT drawing via glBitmap |
| 89 | glPixelStorei(GL_PACK_ALIGNMENT, 4); |
| 90 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); |
| 91 | glPixelStorei(GL_PACK_ROW_LENGTH, 0); |
| 92 | glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); |
| 93 | |
| 94 | puDisplay(); |
| 95 | |
| 96 | glPopClientAttrib(); |
| 97 | glPopAttrib(); |
| 98 | } |
| 99 | |
| 100 | osg::Object* cloneType() const override { return new PUIDrawable; } |
| 101 | osg::Object* clone(const osg::CopyOp&) const override { return new PUIDrawable; } |
| 102 | |
| 103 | private: |
| 104 | }; |
| 105 | |
| 106 | class PUIEventHandler : public osgGA::GUIEventHandler |
| 107 | { |
| 108 | public: |
| 109 | PUIEventHandler(PUICamera* cam) : |
| 110 | _puiCamera(cam) |
| 111 | { |
| 112 | _mouse0RightButtonNode = fgGetNode("/devices/status/mice/mouse[0]/button[2]", true); |
| 113 | } |
| 114 | |
| 115 | bool handle(const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa, osg::Object *, osg::NodeVisitor *nv) override |
| 116 | { |
| 117 | if (ea.getHandled()) return false; |
| 118 | |
| 119 | // PUI expects increasing downward mouse coords |
| 120 | const int fixedY = (ea.getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS) ? |
| 121 | ea.getWindowHeight() - ea.getY() : ea.getY(); |
| 122 | const int scaledX = static_cast<int>(ea.getX() / static_pixelRatio); |
| 123 | const int scaledY = static_cast<int>(fixedY / static_pixelRatio); |
| 124 | |
| 125 | switch(ea.getEventType()) |
| 126 | { |
| 127 | case(osgGA::GUIEventAdapter::DRAG): |
| 128 | if (!_is_dragging) |
| 129 | return false; |
| 130 | // No break |
| 131 | case(osgGA::GUIEventAdapter::MOVE): |
| 132 | return puMouse(scaledX, scaledY); |
| 133 | |
| 134 | case(osgGA::GUIEventAdapter::PUSH): |
| 135 | case(osgGA::GUIEventAdapter::RELEASE): |
| 136 | { |
| 137 | // during splash/reset, either of these can return nullptr |
| 138 | const auto input = globals->get_subsystem<FGInput>(); |
| 139 | const auto mouseSubsystem = input ? input->get_subsystem<FGMouseInput>() : nullptr; |
| 140 | if (mouseSubsystem && !mouseSubsystem->isActiveModePassThrough()) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | bool mouse_up = (ea.getEventType() == osgGA::GUIEventAdapter::RELEASE); |
| 145 | bool handled = puMouse(osgButtonToPUI(ea), mouse_up, scaledX, scaledY); |
| 146 | if (!mouse_up && handled) |
| 147 | { |
| 148 | _is_dragging = true; |
| 149 | } |
| 150 | // Release drag if no more buttons are pressed |
| 151 | else if (mouse_up && !ea.getButtonMask()) |
| 152 | { |
| 153 | _is_dragging = false; |
| 154 | } |
| 155 | return handled; |
| 156 | } |
| 157 | |
| 158 | case(osgGA::GUIEventAdapter::KEYDOWN): |
| 159 | case(osgGA::GUIEventAdapter::KEYUP): |
| 160 | { |
| 161 | const bool isKeyRelease = (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP); |
| 162 | const int key = flightgear::FGEventHandler::translateKey(ea); |
| 163 | bool handled = puKeyboard(key, isKeyRelease); |
| 164 | return handled; |
| 165 | } |
| 166 | |
| 167 | case osgGA::GUIEventAdapter::SCROLL: |
| 168 | { |
| 169 | const int button = buttonForScrollEvent(ea); |
| 170 | if (button != PU_NOBUTTON) { |
| 171 | // sent both down and up events for a single scroll, for |
| 172 | // compatability |
| 173 | bool handled = puMouse(button, PU_DOWN, scaledX, scaledY); |
| 174 | handled |= puMouse(button, PU_UP, scaledX, scaledY); |
| 175 | return handled; |
| 176 | } |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | case (osgGA::GUIEventAdapter::RESIZE): |
| 181 | _puiCamera->resizeUi(ea.getWindowWidth(), ea.getWindowHeight()); |
| 182 | break; |
| 183 | |
| 184 | default: |
| 185 | return false; |
| 186 | } |
| 187 | return false; |
| 188 | } |
| 189 | private: |
| 190 | int osgButtonToPUI(const osgGA::GUIEventAdapter &ea) const |
| 191 | { |
| 192 | switch (ea.getButton()) { |
| 193 | case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON: |
| 194 | return 0; |
| 195 | case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON: |
| 196 | return 1; |
| 197 | case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON: |
| 198 | return 2; |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | int buttonForScrollEvent(const osgGA::GUIEventAdapter &ea) const |
| 205 | { |
| 206 | if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_2D) { |
| 207 | int button = PU_NOBUTTON; |
| 208 | if (ea.getScrollingDeltaY() > 0) |
| 209 | button = PU_SCROLL_UP_BUTTON; |
| 210 | else if (ea.getScrollingDeltaY() < 0) |
| 211 | button = PU_SCROLL_DOWN_BUTTON; |
| 212 | |
| 213 | #if defined(SG_MAC) |
| 214 | // bug https://code.google.com/p/flightgear-bugs/issues/detail?id=1286 |
| 215 | // Mac (Cocoa) interprets shift+wheel as horizontal scroll |
| 216 | if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_SHIFT) { |
| 217 | if (ea.getScrollingDeltaX() > 0) { |
| 218 | button = PU_SCROLL_UP_BUTTON; |
| 219 | } else if (ea.getScrollingDeltaX() < 0) { |
| 220 | button = PU_SCROLL_DOWN_BUTTON; |
| 221 | } |
| 222 | } |
| 223 | #endif |
| 224 | return button; |
| 225 | } else if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP) { |
| 226 | return PU_SCROLL_UP_BUTTON; |
| 227 | } |
| 228 | |
| 229 | return PU_SCROLL_DOWN_BUTTON; |
| 230 | } |
| 231 | |
| 232 | PUICamera* _puiCamera = nullptr; |
| 233 | bool _is_dragging = false; |
| 234 | |
| 235 | SGPropertyNode_ptr _mouse0RightButtonNode; |
| 236 | }; |
| 237 | |
| 238 | // The pu getWindow callback is supposed to return a window ID that |
| 239 | // would allow drawing a GUI on different windows. All that stuff is |
| 240 | // broken in multi-threaded OSG, and we only have one GUI "window" |
| 241 | // anyway, so just return a constant. |
| 242 | int PUICamera::puGetWindow() |
| 243 | { |
| 244 | return 1; |
| 245 | } |
| 246 | |
| 247 | void PUICamera::puGetWindowSize(int* width, int* height) |
| 248 | { |
| 249 | *width = 0; |
| 250 | *height = 0; |
| 251 | osg::Camera* camera = getGUICamera(CameraGroup::getDefault()); |
| 252 | if (!camera) |
| 253 | return; |
| 254 | |
| 255 | osg::Viewport* vport = camera->getViewport(); |
| 256 | *width = static_cast<int>(vport->width() / static_pixelRatio); |
| 257 | *height = static_cast<int>(vport->height() / static_pixelRatio); |
| 258 | } |
| 259 | |
| 260 | void PUICamera::initPUI() |
| 261 | { |
| 262 | puSetWindowFuncs(PUICamera::puGetWindow, nullptr, |
| 263 | PUICamera::puGetWindowSize, nullptr); |
| 264 | puRealInit(); |
| 265 | } |
| 266 | PUICamera::PUICamera() : |
| 267 | osg::Camera() |
| 268 | { |
| 269 | } |
| 270 | |
| 271 | PUICamera::~PUICamera() |
| 272 | { |
| 273 | SG_LOG(SG_GL, SG_INFO, "Deleting PUI camera"); |
| 274 | |
| 275 | // depending on if we're doing shutdown or reset, various things can be |
| 276 | // null here. |
| 277 | auto renderer = globals->get_renderer(); |
| 278 | auto view = renderer ? renderer->getView() : nullptr; |
| 279 | if (view) { |
| 280 | view->removeEventHandler(_eventHandler); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | void PUICamera::init(osg::Group* parent, osgViewer::View* view) |
| 285 | { |
| 286 | setName("PUI FBO camera"); |
| 287 | |
| 288 | _fboTexture = new osg::Texture2D; |
| 289 | _fboTexture->setInternalFormat(GL_RGBA); |
| 290 | _fboTexture->setResizeNonPowerOfTwoHint(false); |
| 291 | _fboTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); |
| 292 | _fboTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); |
| 293 | |
| 294 | // setup the camera as render to texture |
| 295 | setReferenceFrame(osg::Transform::ABSOLUTE_RF); |
| 296 | setViewMatrix(osg::Matrix::identity()); |
| 297 | setClearMask( GL_COLOR_BUFFER_BIT ); |
| 298 | setClearColor( osg::Vec4( 0.0, 0.0, 0.0, 0.0 ) ); |
| 299 | setAllowEventFocus(false); |
| 300 | setCullingActive(false); |
| 301 | setRenderTargetImplementation( osg::Camera::FRAME_BUFFER_OBJECT ); |
| 302 | setRenderOrder(osg::Camera::PRE_RENDER); |
| 303 | attach(osg::Camera::COLOR_BUFFER, _fboTexture); |
| 304 | |
| 305 | // set the camera's node mask, ensure the pick bit is clear |
| 306 | setNodeMask(SG_NODEMASK_GUI_BIT); |
| 307 | |
| 308 | // geode+drawable to call puDisplay, as a child of this FBO-camera |
| 309 | osg::Geode* geode = new osg::Geode; |
| 310 | geode->setName("PUIDrawableGeode"); |
| 311 | geode->addDrawable(new PUIDrawable); |
| 312 | addChild(geode); |
| 313 | |
| 314 | // geometry (full-screen quad) to draw the output |
| 315 | _fullScreenQuad = new osg::Geometry; |
| 316 | _fullScreenQuad = osg::createTexturedQuadGeometry(osg::Vec3(0.0, 0.0, 0.0), |
| 317 | osg::Vec3(200.0, 0.0, 0.0), |
| 318 | osg::Vec3(0.0, 200.0, 0.0)); |
| 319 | _fullScreenQuad->setSupportsDisplayList(false); |
| 320 | _fullScreenQuad->setName("PUI fullscreen quad"); |
| 321 | |
| 322 | // state used for drawing the quad (not for rendering PUI, that's done in the |
| 323 | // drawbale above) |
| 324 | osg::StateSet* stateSet = _fullScreenQuad->getOrCreateStateSet(); |
| 325 | stateSet->setRenderBinDetails(1001, "RenderBin"); |
| 326 | stateSet->setTextureMode(0, GL_TEXTURE_2D, osg::StateAttribute::ON); |
| 327 | stateSet->setTextureAttribute(0, _fboTexture); |
| 328 | |
| 329 | // use GL_ONE becuase we pre-multiplied by alpha when building the FBO texture |
| 330 | stateSet->setAttribute(new osg::BlendFunc(osg::BlendFunc::ONE, osg::BlendFunc::ONE_MINUS_SRC_ALPHA)); |
| 331 | stateSet->setMode(GL_BLEND, osg::StateAttribute::ON); |
| 332 | stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF); |
| 333 | stateSet->setMode(GL_FOG, osg::StateAttribute::OFF); |
| 334 | stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); |
| 335 | |
| 336 | bool use_vertex_attribute_aliasing = false; |
| 337 | auto guiCamera = getGUICamera(CameraGroup::getDefault()); |
| 338 | if (guiCamera) { |
| 339 | auto gc = guiCamera->getGraphicsContext(); |
| 340 | use_vertex_attribute_aliasing = gc->getState()->getUseVertexAttributeAliasing(); |
| 341 | } |
| 342 | |
| 343 | if (use_vertex_attribute_aliasing) { |
| 344 | const std::string vertex_source = |
| 345 | "#version 330 core\n" |
| 346 | "\n" |
| 347 | "layout(location = 0) in vec4 pos;\n" |
| 348 | "layout(location = 3) in vec4 multitexcoord0;\n" |
| 349 | "\n" |
| 350 | "out vec2 texcoord;\n" |
| 351 | "\n" |
| 352 | "uniform mat4 osg_ModelViewProjectionMatrix;\n" |
| 353 | "\n" |
| 354 | "void main()\n" |
| 355 | "{\n" |
| 356 | " gl_Position = osg_ModelViewProjectionMatrix * pos;\n" |
| 357 | " texcoord = multitexcoord0.st;\n" |
| 358 | "}\n"; |
| 359 | osg::Shader *vertex_shader = new osg::Shader(osg::Shader::VERTEX, vertex_source); |
| 360 | |
| 361 | const std::string fragment_source = |
| 362 | "#version 330 core\n" |
| 363 | "\n" |
| 364 | "layout(location = 0) out vec4 fragColor;\n" |
| 365 | "\n" |
| 366 | "in vec2 texcoord;\n" |
| 367 | "\n" |
| 368 | "uniform sampler2D tex;\n" |
| 369 | "\n" |
| 370 | "void main()\n" |
| 371 | "{\n" |
| 372 | " fragColor = texture(tex, texcoord);\n" |
| 373 | "}\n"; |
| 374 | osg::Shader *fragment_shader = new osg::Shader(osg::Shader::FRAGMENT, fragment_source); |
| 375 | |
| 376 | osg::Program *program = new osg::Program; |
| 377 | program->addShader(vertex_shader); |
| 378 | program->addShader(fragment_shader); |
| 379 | stateSet->setAttribute(program); |
| 380 | stateSet->addUniform(new osg::Uniform("tex", 0)); |
| 381 | } |
| 382 | |
| 383 | // geode to display the FSquad in the parent scene (which is GUI camera) |
| 384 | osg::Geode* fsQuadGeode = new osg::Geode; |
| 385 | fsQuadGeode->addDrawable(_fullScreenQuad); |
| 386 | fsQuadGeode->setName("PUI fullscreen Geode"); |
| 387 | fsQuadGeode->setNodeMask(SG_NODEMASK_GUI_BIT); |
| 388 | parent->addChild(this); |
| 389 | parent->addChild(fsQuadGeode); |
| 390 | |
| 391 | osg::Camera* camera = getGUICamera(CameraGroup::getDefault()); |
| 392 | if (camera) { |
| 393 | osg::Viewport* vport = camera->getViewport(); |
| 394 | resizeUi(vport->width(), vport->height()); |
| 395 | } |
| 396 | |
| 397 | // push_front so we keep the order of event handlers the opposite of |
| 398 | // the rendering order (i.e top-most UI layer has the front-most event |
| 399 | // handler) |
| 400 | _eventHandler = new PUIEventHandler(this); |
| 401 | view->getEventHandlers().push_front(_eventHandler); |
| 402 | } |
| 403 | |
| 404 | // remove once we require OSG 3.4 |
| 405 | void PUICamera::manuallyResizeFBO(int width, int height) |
| 406 | { |
| 407 | _fboTexture->setTextureSize(width, height); |
| 408 | _fboTexture->dirtyTextureObject(); |
| 409 | } |
| 410 | |
| 411 | void PUICamera::resizeUi(int width, int height) |
| 412 | { |
| 413 | static_pixelRatio = fgGetDouble("/sim/rendering/gui-pixel-ratio", 1.0); |
| 414 | const int scaledWidth = static_cast<int>(width / static_pixelRatio); |
| 415 | const int scaledHeight = static_cast<int>(height / static_pixelRatio); |
| 416 | |
| 417 | setViewport(0, 0, scaledWidth, scaledHeight); |
| 418 | |
| 419 | osg::Camera::resize(scaledWidth, scaledHeight); |
| 420 | resizeAttachments(scaledWidth, scaledHeight); |
| 421 | |
| 422 | const float puiZ = 1.0; |
| 423 | // resize the full-screen quad |
| 424 | osg::Vec3Array* fsQuadVertices = static_cast<osg::Vec3Array*>(_fullScreenQuad->getVertexArray()); |
| 425 | (*fsQuadVertices)[0] = osg::Vec3(0.0, height, puiZ); |
| 426 | (*fsQuadVertices)[1] = osg::Vec3(0.0, 0.0, puiZ); |
| 427 | (*fsQuadVertices)[2] = osg::Vec3(width, 0.0, puiZ); |
| 428 | (*fsQuadVertices)[3] = osg::Vec3(width, height, puiZ); |
| 429 | |
| 430 | fsQuadVertices->dirty(); |
| 431 | } |
| 432 |