[osg-users] [3rdparty] Use PNG as texture for terrain in osgEarth

Rodrigo Dias rodrigo1406 at gmail.com
Tue Jan 8 18:29:17 PST 2019


So, I learned how to put HUD text, and I'm trying to understand the movement of the camera sending the camera's coordinates to this text. However, the three coordinates (heading, pitch and roll) are always 0 or pi, which seems more a rounding error than anything else. It seems the world is moving, instead of the camera, which I guess is the way that TrackballManipulator works. I want to move the camera around, using WASD keys and mouse movement, but still couldn't get it from the examples (like FirstPersonManipulator). I couldn't even manage to put the camera in a proper initial position! (Here's what I've tried so far: http://forum.openscenegraph.org/viewtopic.php?t=17623)

Other point is that, as you can see in the image below, the whole flat world is white, and Brazil is where it's supposed to be (southern and western hemispheres). But I'd like to have the world transparent instead of white, so only Brazil (or any selected country) would be visible.

[img]https://imgur.com/UVEGwuj[/img]

Here's my source code:


Code:
#include <iostream> // cout
#include <osg/Camera>
#include <osgDB/ReadFile>
#include <osgText/Font>
#include <osgText/Text>
#include <osgGA/TrackballManipulator>
#include <osgViewer/Viewer>
#include <osgEarth/ImageLayer>
#include <osgEarth/Map>
#include <osgEarth/MapNode>
#include <osgEarthDrivers/gdal/GDALOptions>

using namespace std;
using namespace osg;
using namespace osgEarth;
using namespace osgEarth::Drivers;

osg::ref_ptr<osgText::Font> g_font = osgText::readFontFile("fonts/arial.ttf");

osg::Camera* createHUDCamera( double left, double right, double bottom, double top ) {
	osg::ref_ptr<osg::Camera> camera = new osg::Camera;
	camera->setReferenceFrame( osg::Transform::ABSOLUTE_RF );
	camera->setClearMask( GL_DEPTH_BUFFER_BIT );
	camera->setRenderOrder( osg::Camera::POST_RENDER );
	camera->setAllowEventFocus( false );
	camera->setProjectionMatrix( osg::Matrix::ortho2D(left, right, bottom, top) );
	return camera.release();
}

osgText::Text* createText( const osg::Vec3& pos, const std::string& content, float size ) {
	osg::ref_ptr<osgText::Text> text = new osgText::Text;
	text->setFont( g_font.get() );
	text->setCharacterSize( size );
	text->setAxisAlignment( osgText::TextBase::XY_PLANE );
	text->setPosition( pos );
	text->setText( content, osgText::String::ENCODING_UTF8 );
	return text.release();
}

Vec3d getHPRfromQuat(osg::Quat quat) {
	double qx = quat.x();
	double qy = quat.y();
	double qz = quat.z();
	double qw = quat.w();
	double sqx = qx * qx;
	double sqy = qy * qy;
	double sqz = qz * qz;
	double sqw = qw * qw;
	double term1 = 2*(qx*qy+qw*qz);
	double term2 = sqw+sqx-sqy-sqz;
	double term3 = -2*(qx*qz-qw*qy);
	double term4 = 2*(qw*qx+qy*qz);
	double term5 = sqw - sqx - sqy + sqz;
	double heading = atan2(term1, term2);
	double pitch = atan2(term4, term5);
	double roll = asin(term3);
	return Vec3d( heading, pitch, roll );
}

int main (int argc, char** argv) {
	// Cria o texto do HUD
	osg::ref_ptr<osgText::Text> text1 = createText(osg::Vec3(10, 748, 0),"Heading",10.0f);
	osg::ref_ptr<osgText::Text> text2 = createText(osg::Vec3(10, 728, 0),"Pitch",10.0f);
	osg::ref_ptr<osgText::Text> text3 = createText(osg::Vec3(10, 708, 0),"Roll",10.0f);
	
	text1->setDataVariance(osg::Object::DYNAMIC); // pra poder mudar o texto depois
	text2->setDataVariance(osg::Object::DYNAMIC); // pra poder mudar o texto depois
	text3->setDataVariance(osg::Object::DYNAMIC); // pra poder mudar o texto depois
	
	osg::ref_ptr<osg::Geode> textGeode = new osg::Geode;
	textGeode->addDrawable( text1 );
	textGeode->addDrawable( text2 );
	textGeode->addDrawable( text3 );
	osg::Camera* camera = createHUDCamera(0, 1024, 0, 768);
	camera->addChild( textGeode.get() );
	camera->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF );

	// Cria um Mapa na opção "projetado" para mostrar num plano (ao invés de num globo)
	MapOptions mapOpt;
	mapOpt.coordSysType() = MapOptions::CSTYPE_PROJECTED;
	mapOpt.profile() = ProfileOptions("plate-carre");
	osg::ref_ptr<Map> map = new Map(mapOpt);

	// Adiciona uma camada de imagem/textura (vegetação do Brasil num GeoTiff)
	{
		GDALOptions gdal;
		gdal.url() = "br_modified.tif";
		//XMIN	XMAX	YMIN	YMAX
		//-74	-34.79	-33.84	5.37
		osg::ref_ptr<ImageLayer> layer = new ImageLayer( "BR", gdal );
		//layer->setOpacity(0.5);
		printf("\nOpacity: %.2f\n",layer->getOpacity());
		map->addLayer( layer );
	}

	// Adiciona uma camada de elevação (SRTM de um arquivo GeoTiff)
	{
		GDALOptions gdal;
		gdal.url() = "BRalt.tif";
		osg::ref_ptr<ElevationLayer> layer = new ElevationLayer( "SRTM", gdal );
		map->addLayer( layer );
	}

	// Cria um MapNode para renderizar este mapa
	osg::ref_ptr<MapNode> mapNode = new MapNode( map );

	osg::ref_ptr<osg::Group> root = new osg::Group;
	root->addChild( mapNode.get() );
	root->addChild( camera );

	osgViewer::Viewer viewer;
	viewer.setSceneData( root.get() );
	viewer.setCameraManipulator( new osgGA::TrackballManipulator );

	Vec3d v;
	while ( !viewer.done() ) {
		v = getHPRfromQuat( viewer.getCamera()->getProjectionMatrix().getRotate() );
		text1->setText((L"Heading: " + to_wstring(v.x())).c_str());
		text2->setText((L"Pitch: " +   to_wstring(v.y())).c_str());
		text3->setText((L"Roll: " +    to_wstring(v.z())).c_str());
		viewer.frame();
	}
	
	return 0;
}
// g++ main.cpp -losg -losgDB -losgViewer -losgEarth -losgText -losgGA -o main




Thank you!

Cheers,
Rodrigo

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=75432#75432







More information about the osg-users mailing list