| 1 | // marker.cxx - 3D Marker pins in FlightGear |
| 2 | // Copyright (C) 2022 Tobias Dammers |
| 3 | // SPDX-License-Identifier: GPL-2.0-or-later OR MIT |
| 4 | |
| 5 | #ifdef HAVE_CONFIG_H |
| 6 | # include <config.h> |
| 7 | #endif |
| 8 | |
| 9 | #include "Scenery/marker.hxx" |
| 10 | #include <simgear/scene/util/SGNodeMasks.hxx> |
| 11 | #include <simgear/scene/util/SGReaderWriterOptions.hxx> |
| 12 | #include <simgear/scene/material/Effect.hxx> |
| 13 | #include <simgear/scene/material/EffectGeode.hxx> |
| 14 | #include <osg/Geometry> |
| 15 | #include <osg/Material> |
| 16 | #include <osg/Node> |
| 17 | #include <osg/Billboard> |
| 18 | #include <osgText/Text> |
| 19 | #include <osgText/String> |
| 20 | #include <osgDB/ReadFile> |
| 21 | #include <osgDB/WriteFile> |
| 22 | #include <osgDB/Registry> |
| 23 | |
| 24 | osg::ref_ptr<osg::Node> |
| 25 | fgCreateMarkerNode(const osgText::String& label, float font_size, float pin_height, float tip_height, const osg::Vec4f& color) |
| 26 | { |
| 27 | auto mainNode = new osg::Group; |
| 28 | |
| 29 | auto textNode = new osg::Billboard; |
| 30 | mainNode->addChild(textNode); |
| 31 | |
| 32 | textNode->setMode(osg::Billboard::AXIAL_ROT); |
| 33 | |
| 34 | auto text = new osgText::Text; |
| 35 | text->setText(label); |
| 36 | text->setAlignment(osgText::Text::CENTER_BOTTOM); |
| 37 | text->setAxisAlignment(osgText::Text::XZ_PLANE); |
| 38 | text->setFont("Fonts/LiberationFonts/LiberationSans-Regular.ttf"); |
| 39 | // text->setCharacterSizeMode(osgText::Text::OBJECT_COORDS_WITH_MAXIMUM_SCREEN_SIZE_CAPPED_BY_FONT_HEIGHT); |
| 40 | text->setCharacterSize(font_size, 1.0f); |
| 41 | text->setFontResolution(std::max(32.0f, font_size), std::max(32.0f, font_size)); |
| 42 | text->setColor(color); |
| 43 | text->setPosition(osg::Vec3(0, 0, pin_height)); |
| 44 | text->setBackdropType(osgText::Text::OUTLINE); |
| 45 | text->setBackdropColor(osg::Vec4(0, 0, 0, 0.75)); |
| 46 | textNode->addDrawable(text); |
| 47 | |
| 48 | float top_spacing = font_size * 0.25; |
| 49 | |
| 50 | if (pin_height - top_spacing > tip_height) { |
| 51 | osg::Vec4f solid = color; |
| 52 | osg::Vec4f transparent = color; |
| 53 | osg::Vec3f nvec(0, 1, 0); |
| 54 | |
| 55 | solid[3] = 1.0f; |
| 56 | transparent[3] = 0.0f; |
| 57 | |
| 58 | auto geoNode = new simgear::EffectGeode; |
| 59 | mainNode->addChild(geoNode); |
| 60 | auto pinGeo = new osg::Geometry; |
| 61 | osg::Vec3Array* vtx = new osg::Vec3Array; |
| 62 | osg::Vec3Array* nor = new osg::Vec3Array; |
| 63 | osg::Vec4Array* rgb = new osg::Vec4Array; |
| 64 | |
| 65 | nor->push_back(nvec); |
| 66 | |
| 67 | vtx->push_back(osg::Vec3f(0, 0, tip_height)); |
| 68 | rgb->push_back(solid); |
| 69 | |
| 70 | vtx->push_back(osg::Vec3f(-font_size * 0.125, 0, pin_height - top_spacing)); |
| 71 | rgb->push_back(transparent); |
| 72 | |
| 73 | vtx->push_back(osg::Vec3f(0, 0, tip_height)); |
| 74 | rgb->push_back(solid); |
| 75 | |
| 76 | vtx->push_back(osg::Vec3f(0, font_size * 0.125, pin_height - top_spacing)); |
| 77 | rgb->push_back(transparent); |
| 78 | |
| 79 | vtx->push_back(osg::Vec3f(0, 0, tip_height)); |
| 80 | rgb->push_back(solid); |
| 81 | |
| 82 | vtx->push_back(osg::Vec3f(font_size * 0.125, 0, pin_height - top_spacing)); |
| 83 | rgb->push_back(transparent); |
| 84 | |
| 85 | vtx->push_back(osg::Vec3f(0, 0, tip_height)); |
| 86 | rgb->push_back(solid); |
| 87 | |
| 88 | vtx->push_back(osg::Vec3f(0, -font_size * 0.125, pin_height - top_spacing)); |
| 89 | rgb->push_back(transparent); |
| 90 | |
| 91 | vtx->push_back(osg::Vec3f(0, 0, tip_height)); |
| 92 | rgb->push_back(solid); |
| 93 | |
| 94 | vtx->push_back(osg::Vec3f(-font_size * 0.125, 0, pin_height - top_spacing)); |
| 95 | rgb->push_back(transparent); |
| 96 | |
| 97 | pinGeo->setVertexArray(vtx); |
| 98 | pinGeo->setColorArray(rgb, osg::Array::BIND_PER_VERTEX); |
| 99 | pinGeo->setNormalArray(nor, osg::Array::BIND_OVERALL); |
| 100 | pinGeo->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUAD_STRIP, 0, vtx->size())); |
| 101 | geoNode->addDrawable(pinGeo); |
| 102 | |
| 103 | auto stateSet = geoNode->getOrCreateStateSet(); |
| 104 | |
| 105 | stateSet->setMode(GL_FOG, osg::StateAttribute::OFF); |
| 106 | stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
| 107 | stateSet->setMode(GL_BLEND, osg::StateAttribute::OFF); |
| 108 | stateSet->setMode(GL_ALPHA_TEST, osg::StateAttribute::ON); |
| 109 | stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON); |
| 110 | |
| 111 | osg::ref_ptr<simgear::SGReaderWriterOptions> opt; |
| 112 | opt = simgear::SGReaderWriterOptions::copyOrCreate(osgDB::Registry::instance()->getOptions()); |
| 113 | simgear::Effect* effect = simgear::makeEffect("Effects/marker-pin", true, opt); |
| 114 | if (effect) |
| 115 | geoNode->setEffect(effect); |
| 116 | } |
| 117 | |
| 118 | mainNode->setNodeMask(~simgear::CASTSHADOW_BIT); |
| 119 | return mainNode; |
| 120 | } |