I want to draw triangle on the earth. If I draw the triangle by the class osgEarth::Features::Feature there is no problem.
for example:
void DrawGeometryByFeature(ListVec3d& vecList, std::vector<unsigned int>& lstIndices)
{
osgEarth::Symbology::Style shapeStyle;
shapeStyle.getOrCreate<osgEarth::Symbology::PolygonSymbol>()->fill()->color() = osgEarth::Symbology::Color::Green;
_polyFeature = new osgEarth::Features::Feature(new osgEarth::Symbology::MultiGeometry, s_mapNode->getMapSRS(), shapeStyle);
_polyNode = new osgEarth::Annotation::FeatureNode(s_mapNode, _polyFeature);
osgEarth::Symbology::MultiGeometry* pGeometry = (MultiGeometry*)_polyNode->getFeature()->getGeometry();
pGeometry->clear();
_polyNode->setStyle(shapeStyle);
int index = 0;
for (std::vector<unsigned int>::iterator iit = lstIndices.begin();
iit != lstIndices.end(); iit++) {
index++;
if ((index + 1) % 3 == 0) {
osgEarth::Symbology::Geometry* polygen = new osgEarth::Symbology::Geometry();
polygen->push_back(vecList[lstIndices[index - 2]]);
polygen->push_back(vecList[lstIndices[index - 1]]);
polygen->push_back(vecList[lstIndices[index]]);
pGeometry->add(polygen);
}
}
_polyNode->init();
BBoxNodes.push_back(_polyNode);
s_mapNode->addChild(_polyNode);
}
but I want to draw it more efficient, so I try to draw it by the osg API
for example:
void DrawGeometryByOsg(std::vector<osg::Vec3d> vecList, std::vector<unsigned int>& lstIndices, int color, long type)
{
// create Geometry object to store all the vertices and lines primitive.
osg::Geometry* polyGeom = new osg::Geometry();
// note, first coord at top, second at bottom, reverse to that buggy OpenGL image..
const size_t numCoords = lstIndices.size();
osg::Vec3* myCoords = new osg::Vec3[numCoords];
unsigned int index = 0;
osg::Vec3Array* normals = new osg::Vec3Array(/*numCoords/3*/);
for (std::vector<unsigned int>::iterator it = lstIndices.begin(); it != lstIndices.end(); it++){
myCoords[index++] = vecList[*it];
if(index%3 == 2){
//
osg::Vec3d kEdge1 = myCoords[index-1] - myCoords[index-2];
osg::Vec3d kEdge2 = myCoords[index] - myCoords[index - 2];
osg::Vec3d normal = kEdge1^kEdge2;
//normal.normalize();
normals->push_back(normal);
//
}
}
osg::Vec3Array* vertices = new osg::Vec3Array(numCoords, myCoords);
polyGeom->setVertexArray(vertices);
osg::Vec4Array* colors = new osg::Vec4Array;
colors->push_back(osg::Vec4(0.0f, 0.8f, 0.0f, 1.0f));
polyGeom->setColorArray(colors, osg::Array::BIND_OVERALL);
polyGeom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES, 0, numCoords));
osg::Geode* geode = new osg::Geode();
geode->addDrawable(polyGeom);
s_mapNode->addChild(geode);
}
but the gemotry which i draw by Osg API is always shaking....( ̄﹏ ̄;)
could you tell me where is the mistake in my code?
Any time you have "shaking" geometry you are likely running into a floating-point precision problem. OpenGL deals in 32-bit floating point coordinates. So if your geometry uses large coordinate values (as it does in a geocentric map like osgEarth), the values will get cropped when they are sent to the GPU and you get shaking/jittering when the camera moves.
To solve this problem, express your data relative to a local origin. Pick a double-precision point somewhere -- the centroid of the geometry is usually a good place -- and make that your local origin. Then translate all your double-precision coordinates so they are relative to that origin. Finally, parent the geometry with a MatrixTransform that translates the localized data to the actual double-precision location.
Hope this helps!