2007-12-10 23:15:56 +08:00
|
|
|
/* OpenSceneGraph example, osgtext.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2010-07-15 02:50:41 +08:00
|
|
|
#include <osg/ArgumentParser>
|
2011-03-07 20:35:30 +08:00
|
|
|
#include <osg/Material>
|
2010-07-15 02:50:41 +08:00
|
|
|
#include <osg/PositionAttitudeTransform>
|
2011-03-07 20:35:30 +08:00
|
|
|
#include <osg/io_utils>
|
|
|
|
|
|
|
|
#include <osgDB/ReadFile>
|
2010-07-15 02:50:41 +08:00
|
|
|
#include <osgDB/WriteFile>
|
|
|
|
#include <osgGA/StateSetManipulator>
|
2007-12-10 23:15:56 +08:00
|
|
|
#include <osgViewer/Viewer>
|
|
|
|
#include <osgViewer/ViewerEventHandlers>
|
|
|
|
|
2010-11-17 18:16:45 +08:00
|
|
|
#include <osgText/Text3D>
|
2010-08-24 22:22:58 +08:00
|
|
|
|
2010-11-24 01:33:43 +08:00
|
|
|
#include "TextNode.h"
|
|
|
|
|
2017-04-06 17:51:47 +08:00
|
|
|
class Text3DAttributeHandler : public osgGA::GUIEventHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Text3DAttributeHandler(osgText::Text3D* aText3D)
|
|
|
|
: m_Text3D(aText3D)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~Text3DAttributeHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-08-15 23:35:23 +08:00
|
|
|
virtual bool handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter&)
|
2017-04-06 17:51:47 +08:00
|
|
|
{
|
|
|
|
if (ea.getEventType() == osgGA::GUIEventAdapter::KEYUP)
|
|
|
|
{
|
|
|
|
if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Up)
|
|
|
|
{
|
2017-09-04 16:44:30 +08:00
|
|
|
m_Text3D->setCharacterSize(m_Text3D->getCharacterHeight() + 0.1);
|
|
|
|
OSG_NOTICE<<"m_Text3D->getCharacterHeight() = " << m_Text3D->getCharacterHeight() << std::endl;
|
2017-04-06 17:51:47 +08:00
|
|
|
}
|
|
|
|
else if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Down)
|
|
|
|
{
|
2017-09-04 16:44:30 +08:00
|
|
|
m_Text3D->setCharacterDepth(m_Text3D->getCharacterDepth() + 0.1);
|
|
|
|
OSG_NOTICE<<"m_Text3D->getCharacterDepth() = " << m_Text3D->getCharacterDepth() << std::endl;
|
2017-04-06 17:51:47 +08:00
|
|
|
}
|
|
|
|
else if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Left)
|
|
|
|
{
|
2017-09-04 16:44:30 +08:00
|
|
|
static int counter = 1;
|
|
|
|
if (counter%3 == 0)
|
|
|
|
m_Text3D->setText("Press arrow keys.", osgText::String::ENCODING_UTF8);
|
|
|
|
else if (counter%3 == 1)
|
|
|
|
m_Text3D->setText("setText\nworks!", osgText::String::ENCODING_UTF8);
|
|
|
|
else if (counter%3 == 2)
|
|
|
|
m_Text3D->setText("setText really works?", osgText::String::ENCODING_UTF8);
|
|
|
|
else if (counter%3 == 3)
|
|
|
|
m_Text3D->setText("setText works, really!", osgText::String::ENCODING_UTF8);
|
|
|
|
|
|
|
|
++counter;
|
|
|
|
|
|
|
|
OSG_NOTICE<<"m_Text3D->getText().size() = " << m_Text3D->getText().size() << std::endl;
|
2017-04-06 17:51:47 +08:00
|
|
|
}
|
|
|
|
else if (ea.getKey() == osgGA::GUIEventAdapter::KEY_Right)
|
|
|
|
{
|
|
|
|
m_Text3D->setLineSpacing(m_Text3D->getLineSpacing() + 0.1);
|
2017-09-04 16:44:30 +08:00
|
|
|
OSG_NOTICE<<"m_Text3D->getLineSpacing() = " << m_Text3D->getLineSpacing() << std::endl;
|
2017-04-06 17:51:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
osgText::Text3D* m_Text3D;
|
|
|
|
};
|
|
|
|
|
2010-11-17 18:16:45 +08:00
|
|
|
|
2010-09-03 16:26:46 +08:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
osg::ArgumentParser arguments(&argc, argv);
|
|
|
|
|
|
|
|
osgViewer::Viewer viewer(arguments);
|
|
|
|
|
|
|
|
std::string fontFile("arial.ttf");
|
|
|
|
while(arguments.read("-f",fontFile)) {}
|
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osgText::Font> font = osgText::readRefFontFile(fontFile);
|
2010-09-03 16:26:46 +08:00
|
|
|
if (!font) return 1;
|
|
|
|
OSG_NOTICE<<"Read font "<<fontFile<<" font="<<font.get()<<std::endl;
|
|
|
|
|
2017-09-04 16:44:30 +08:00
|
|
|
std::string word("Press arrow keys.");
|
2010-09-03 16:26:46 +08:00
|
|
|
while (arguments.read("-w",word)) {}
|
|
|
|
|
2010-09-03 23:03:42 +08:00
|
|
|
osg::ref_ptr<osgText::Style> style = new osgText::Style;
|
|
|
|
|
2010-09-06 23:43:59 +08:00
|
|
|
float thickness = 0.1f;
|
2010-09-03 23:03:42 +08:00
|
|
|
while(arguments.read("--thickness",thickness)) {}
|
|
|
|
style->setThicknessRatio(thickness);
|
|
|
|
|
2010-09-06 23:43:59 +08:00
|
|
|
// set up any bevel if required
|
|
|
|
float r;
|
|
|
|
osg::ref_ptr<osgText::Bevel> bevel;
|
|
|
|
while(arguments.read("--rounded",r)) { bevel = new osgText::Bevel; bevel->roundedBevel2(r); }
|
|
|
|
while(arguments.read("--rounded")) { bevel = new osgText::Bevel; bevel->roundedBevel2(0.25); }
|
|
|
|
while(arguments.read("--flat",r)) { bevel = new osgText::Bevel; bevel->flatBevel(r); }
|
|
|
|
while(arguments.read("--flat")) { bevel = new osgText::Bevel; bevel->flatBevel(0.25); }
|
|
|
|
while(arguments.read("--bevel-thickness",r)) { if (bevel.valid()) bevel->setBevelThickness(r); }
|
2015-10-22 21:42:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (bevel.valid())
|
2015-05-26 18:05:47 +08:00
|
|
|
{
|
|
|
|
while(arguments.read("--smooth-concave-Junctions") || arguments.read("--scj"))
|
|
|
|
{
|
|
|
|
bevel->setSmoothConcaveJunctions(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
|
2010-09-17 21:33:09 +08:00
|
|
|
style->setBevel(bevel.get());
|
2010-09-06 23:43:59 +08:00
|
|
|
|
2010-09-08 02:18:35 +08:00
|
|
|
// set up outline.
|
|
|
|
while(arguments.read("--outline",r)) { style->setOutlineRatio(r); }
|
|
|
|
|
2011-03-07 20:35:30 +08:00
|
|
|
|
2011-01-11 19:39:50 +08:00
|
|
|
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
|
|
|
|
viewer.addEventHandler(new osgViewer::StatsHandler);
|
|
|
|
|
|
|
|
#if 1
|
2011-03-07 20:35:30 +08:00
|
|
|
osg::ref_ptr<osg::Group> group = new osg::Group;
|
2011-01-11 19:39:50 +08:00
|
|
|
|
|
|
|
float characterSize = 1.0f;
|
|
|
|
while(arguments.read("--size",characterSize)) {}
|
|
|
|
|
|
|
|
if (arguments.read("--2d"))
|
|
|
|
{
|
|
|
|
osgText::Text* text2D = new osgText::Text;
|
|
|
|
text2D->setFont(font.get());
|
|
|
|
text2D->setCharacterSize(characterSize);
|
|
|
|
text2D->setFontResolution(256,256);
|
|
|
|
text2D->setDrawMode(osgText::Text::TEXT | osgText::Text::BOUNDINGBOX);
|
|
|
|
text2D->setAxisAlignment(osgText::Text::XZ_PLANE);
|
|
|
|
text2D->setText(word);
|
2011-03-07 20:35:30 +08:00
|
|
|
osg::Geode* geode = new osg::Geode;
|
2011-01-11 19:39:50 +08:00
|
|
|
geode->addDrawable(text2D);
|
2011-03-07 20:35:30 +08:00
|
|
|
group->addChild(geode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arguments.read("--TextNode"))
|
|
|
|
{
|
|
|
|
// experimental text node
|
|
|
|
osgText::TextNode* text = new osgText::TextNode;
|
|
|
|
text->setFont(font.get());
|
|
|
|
text->setStyle(style.get());
|
|
|
|
text->setTextTechnique(new osgText::TextTechnique);
|
|
|
|
text->setText(word);
|
|
|
|
text->update();
|
|
|
|
|
|
|
|
group->addChild(text);
|
2011-01-11 19:39:50 +08:00
|
|
|
}
|
2011-03-07 20:35:30 +08:00
|
|
|
else if (!arguments.read("--no-3d"))
|
2011-01-11 19:39:50 +08:00
|
|
|
{
|
|
|
|
osgText::Text3D* text3D = new osgText::Text3D;
|
2017-04-06 17:51:47 +08:00
|
|
|
|
|
|
|
// Does not help
|
|
|
|
text3D->setDataVariance(osg::Object::DYNAMIC);
|
|
|
|
|
2011-01-11 19:39:50 +08:00
|
|
|
text3D->setFont(font.get());
|
|
|
|
text3D->setStyle(style.get());
|
|
|
|
text3D->setCharacterSize(characterSize);
|
|
|
|
text3D->setDrawMode(osgText::Text3D::TEXT | osgText::Text3D::BOUNDINGBOX);
|
|
|
|
text3D->setAxisAlignment(osgText::Text3D::XZ_PLANE);
|
|
|
|
text3D->setText(word);
|
2011-03-07 20:35:30 +08:00
|
|
|
|
|
|
|
osg::Geode* geode = new osg::Geode;
|
2011-01-11 19:39:50 +08:00
|
|
|
geode->addDrawable(text3D);
|
2011-03-07 20:35:30 +08:00
|
|
|
group->addChild(geode);
|
|
|
|
|
|
|
|
osg::Vec4 color(1.0f, 1.0f, 1.0f, 1.0f);
|
|
|
|
while(arguments.read("--color",color.r(),color.g(),color.b(),color.a()))
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<"--color "<<color<<std::endl;
|
|
|
|
text3D->setColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string imageFilename;
|
|
|
|
while(arguments.read("--image",imageFilename))
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<"--image "<<imageFilename<<std::endl;
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(imageFilename);
|
2011-03-07 20:35:30 +08:00
|
|
|
if (image.valid())
|
|
|
|
{
|
|
|
|
OSG_NOTICE<<" loaded image "<<imageFilename<<std::endl;
|
|
|
|
osg::StateSet* stateset = text3D->getOrCreateStateSet();
|
2015-10-22 21:42:19 +08:00
|
|
|
stateset->setTextureAttributeAndModes(0, new osg::Texture2D(image), osg::StateAttribute::ON);
|
2011-03-07 20:35:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(arguments.read("--wall-color",color.r(),color.g(),color.b(),color.a()))
|
|
|
|
{
|
|
|
|
osg::StateSet* stateset = text3D->getOrCreateWallStateSet();
|
|
|
|
osg::Material* material = new osg::Material;
|
|
|
|
material->setDiffuse(osg::Material::FRONT_AND_BACK, color);
|
|
|
|
stateset->setAttribute(material);
|
|
|
|
}
|
|
|
|
|
|
|
|
while(arguments.read("--wall-image",imageFilename))
|
|
|
|
{
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(imageFilename);
|
2011-03-07 20:35:30 +08:00
|
|
|
if (image.valid())
|
|
|
|
{
|
|
|
|
osg::StateSet* stateset = text3D->getOrCreateWallStateSet();
|
2015-10-22 21:42:19 +08:00
|
|
|
stateset->setTextureAttributeAndModes(0, new osg::Texture2D(image), osg::StateAttribute::ON);
|
2011-03-07 20:35:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while(arguments.read("--back-color",color.r(),color.g(),color.b(),color.a()))
|
|
|
|
{
|
|
|
|
osg::StateSet* stateset = text3D->getOrCreateBackStateSet();
|
|
|
|
osg::Material* material = new osg::Material;
|
|
|
|
material->setDiffuse(osg::Material::FRONT_AND_BACK, color);
|
|
|
|
stateset->setAttribute(material);
|
|
|
|
}
|
|
|
|
|
|
|
|
while(arguments.read("--back-image",imageFilename))
|
|
|
|
{
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(imageFilename);
|
|
|
|
if (image)
|
2011-03-07 20:35:30 +08:00
|
|
|
{
|
|
|
|
osg::StateSet* stateset = text3D->getOrCreateBackStateSet();
|
2015-10-22 21:42:19 +08:00
|
|
|
stateset->setTextureAttributeAndModes(0, new osg::Texture2D(image), osg::StateAttribute::ON);
|
2011-03-07 20:35:30 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (arguments.read("--size-quad"))
|
|
|
|
{
|
|
|
|
geode->addDrawable( osg::createTexturedQuadGeometry(osg::Vec3(0.0f,characterSize*thickness,0.0f),osg::Vec3(characterSize,0.0,0.0),osg::Vec3(0.0f,0.0,characterSize), 0.0, 0.0, 1.0, 1.0) );
|
|
|
|
}
|
2017-04-06 17:51:47 +08:00
|
|
|
|
2017-09-04 16:44:30 +08:00
|
|
|
if (arguments.read("--add-axes"))
|
2017-11-27 19:05:31 +08:00
|
|
|
{
|
|
|
|
group->addChild(osgDB::readRefNodeFile("axes.osgt"));
|
|
|
|
}
|
2017-09-04 16:44:30 +08:00
|
|
|
|
|
|
|
std::string mode;
|
|
|
|
if (arguments.read("--character-size-mode", mode))
|
|
|
|
{
|
|
|
|
if (mode == "screen_coords")
|
|
|
|
{
|
|
|
|
text3D->setCharacterSizeMode(osgText::TextBase::SCREEN_COORDS);
|
|
|
|
text3D->setCharacterSize(1080/4);
|
|
|
|
}
|
|
|
|
}
|
2017-08-16 03:11:15 +08:00
|
|
|
|
2017-04-06 17:51:47 +08:00
|
|
|
viewer.addEventHandler(new Text3DAttributeHandler(text3D));
|
2011-01-11 19:39:50 +08:00
|
|
|
}
|
2010-09-08 02:18:35 +08:00
|
|
|
|
2015-10-22 21:42:19 +08:00
|
|
|
viewer.setSceneData(group);
|
2011-03-07 20:35:30 +08:00
|
|
|
|
2011-01-11 19:39:50 +08:00
|
|
|
#endif
|
2010-09-03 16:26:46 +08:00
|
|
|
|
2011-01-11 19:39:50 +08:00
|
|
|
return viewer.run();
|
2010-09-03 16:26:46 +08:00
|
|
|
}
|