# [osg-users] Building an Altimeter

Rusty Shackleford brianboyle.it at gmail.com
Sat Apr 9 13:44:39 PDT 2011

```Hi,

I'm working on a tool that visualizes a spacecraft in orbit around a planetary body. As it is set up now, the position of that spacecraft is given relative to the center of the body as an x,y,z coordinate. What I'm trying to do right now is set up a simple altimeter that looks essentially like a ruler reaching from the planetary surface up to a little past the spacecraft. That is, a straight line with perpendicular lines crossing it at regular intervals.

The black circle is the spacecraft, the black blob at the bottom is the planetary surface, and I'm trying to draw that "ladder" with regular demarkations.

I'm VERY new at OSG (two weeks in) so I could be going about this completely wrong, please let me know if so. What needs to happen is for the altimeter to follow the spacecraft (position given by "end" in the code) such that the central line always intersects it and the origin stays at the center of the planet (given by "start").

I've included the code here for building the altimeter. It outputs a group that is attached to the root node to which the planet is also attached.

Code:

osg::Group* Visualizer::getAltimeter(osg::Vec3 start, osg::Vec3 end, int interval)
{
int distance = sqrt(pow(end-start,2) + pow(end-start,2) + pow(end-start,2));

osg::Group* g = new osg::Group();
osg::Geode* geode = new osg::Geode();

//Line running up the middle
osg::Geometry* line_x_red = new osg::Geometry();
osg::Vec3Array* verticesx = new osg::Vec3Array(2);
(*verticesx)=start;
(*verticesx)=end;
line_x_red->setVertexArray(verticesx);

// set the color
osg::Vec4Array* red = new osg::Vec4Array;
red->push_back(osg::Vec4(1.0f,0.0f,0.0f,1.0f));
line_x_red->setColorArray(red);
line_x_red->setColorBinding(osg::Geometry::BIND_OVERALL);

// set the normal in the same way color.
osg::Vec3Array* normals = new osg::Vec3Array;
normals->push_back(osg::Vec3(0.0f,0.0f,-1.0f));
line_x_red->setNormalArray(normals);
line_x_red->setNormalBinding(osg::Geometry::BIND_OVERALL);

//Surface of planet, no need to mark below this
int surfaceE = 100000;
int numMarkers = (distance-surfaceE)/interval;

osg::Geometry* marker[numMarkers];

//Construct elevation marks

for(int i = 0; i<numMarkers;i++)
{
marker[i] = new osg::Geometry();
osg::Vec3Array* vertices = new osg::Vec3Array(2);
(*vertices)=osg::Vec3(-50,i*interval + surfaceE,0);
(*vertices)=osg::Vec3(50,i*interval + surfaceE,0);
marker[i]->setVertexArray(vertices);
marker[i]->setColorArray(red);
marker[i]->setColorBinding(osg::Geometry::BIND_OVERALL);
marker[i]->setNormalArray(normals);
marker[i]->setNormalBinding(osg::Geometry::BIND_OVERALL);
}

altimeterFrame = new osg::PositionAttitudeTransform();

return g;
}

If this is a good way to go about it, my next question is about normals, which I don't fully understand. I'd like the lines to appear bright red, regardless of which angle they're viewed from. An added bonus would be for the markers to rotate around the central line to always face the camera. I imagine this could be done with Billboards, but some initial experimentation showed I have a lot to learn regarding them.

This is my first post on this board, so excuse me if I've missed a point of etiquette, I'd be glad to elaborate on anything if need be. All advice is greatly appreciated.

Thank you!

Cheers,
Rusty

------------------