我有一些对象想要合并到场景图中。
Street
继承自Geode
,并具有由GL_LINE_STRIP组成的Geometry
子可绘制对象。 Pointer
继承自PositionAttitudeTransform
,并包含一个Geode
,该Geometry
包含两个Group
多边形。 当我将一堆街道添加到
Group
时,它看起来还不错。当我仅将指针添加到ojit_code时,它也看起来不错。但是,如果我以某种方式将它们都存在,那么第二个就搞砸了。参见两张图片。在上图中,街道网络是所需的,在下图中,指针是所需的。
我将不胜感激!如果您需要查看代码,请告诉我,我将更新问题。
更新1:到目前为止,由于什么都没有发生,因此这里是产生这种现象所需的最少代码量。我毫无疑问地将两个指针放在一起,所以我开始怀疑我把街道弄错了……下一次更新将是一些街道生成代码。
更新2:该代码现在包含街道绘图代码。
更新3:该代码现在还包含指针绘图代码和街道绘图
代码已简化。
// My libraries:
#include <asl/util/color.h>
using namespace asl;
#include <straph/point.h>
#include <straph/straph.h>
using namespace straph;
// Standard and OSG libraries:
#include <utility>
#include <boost/tuple/tuple.hpp> // tie
using namespace std;
#include <osg/ref_ptr>
#include <osg/Array>
#include <osg/Geometry>
#include <osg/Geode>
#include <osg/Group>
#include <osg/LineWidth>
using namespace osg;
#include <osgUtil/Tessellator>
#include <osgViewer/Viewer>
using namespace osgViewer;
/*
* Just FYI: A Polyline looks like this:
*
* typedef std::vector<Point> Polyline;
*
* And a Point basically is a simple struct:
*
* struct Point {
* double x;
* double y;
* };
*/
inline osg::Vec3d toVec3d(const straph::Point& p, double elevation=0.0)
{
return osg::Vec3d(p.x, p.y, elevation);
}
Geometry* createStreet(const straph::Polyline& path)
{
ref_ptr<Vec3dArray> array (new Vec3dArray(path.size()));
for (unsigned i = 0; i < path.size(); ++i) {
(*array)[i] = toVec3d(path[i]);
}
Geometry* geom = new Geometry;
geom->setVertexArray(array.get());
geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP, 0, array->size()));
return geom;
}
Geode* load_streets()
{
unique_ptr<Straph> graph = read_shapefile("mexico/roads", 6);
Geode* root = new Geode();
boost::graph_traits<straph::Straph>::edge_iterator ei, ee;
for (boost::tie(ei, ee) = edges(*graph); ei != ee; ++ei) {
const straph::Segment& s = (*graph)[*ei];
root->addDrawable(createStreet(s.polyline));
}
return root;
}
Geode* createPointer(double width, const Color& body_color, const Color& border_color)
{
float f0 = 0.0f;
float f3 = 3.0f;
float f1 = 1.0f * width;
float f2 = 2.0f * width;
// Create vertex array
ref_ptr<Vec3Array> vertices (new Vec3Array(4));
(*vertices)[0].set( f0 , f0 , f0 );
(*vertices)[1].set( -f1/f3, -f1/f3 , f0 );
(*vertices)[2].set( f0 , f2/f3 , f0 );
(*vertices)[3].set( f1/f3, -f1/f3 , f0 );
// Build the geometry object
ref_ptr<Geometry> polygon (new Geometry);
polygon->setVertexArray( vertices.get() );
polygon->addPrimitiveSet( new DrawArrays(GL_POLYGON, 0, 4) );
// Set the colors
ref_ptr<Vec4Array> body_colors (new Vec4Array(1));
(*body_colors)[0] = body_color.get();
polygon->setColorArray( body_colors.get() );
polygon->setColorBinding( Geometry::BIND_OVERALL );
// Start the tesselation work
osgUtil::Tessellator tess;
tess.setTessellationType( osgUtil::Tessellator::TESS_TYPE_GEOMETRY );
tess.setWindingType( osgUtil::Tessellator::TESS_WINDING_ODD );
tess.retessellatePolygons( *polygon );
// Create the border-lines
ref_ptr<Geometry> border (new Geometry);
border->setVertexArray( vertices.get() );
border->addPrimitiveSet(new DrawArrays(GL_LINE_LOOP, 0, 4));
border->getOrCreateStateSet()->setAttribute(new LineWidth(2.0f));
ref_ptr<Vec4Array> border_colors (new Vec4Array(1));
(*border_colors)[0] = border_color.get();
border->setColorArray( border_colors.get() );
border->setColorBinding( Geometry::BIND_OVERALL );
// Create Geode object
ref_ptr<Geode> geode (new Geode);
geode->addDrawable( polygon.get() );
geode->addDrawable( border.get() );
return geode.release();
}
int main(int, char**)
{
Group* root = new Group();
Geode* str = load_streets();
root->addChild(str);
Geode* p = createPointer(6.0, TangoColor::Scarlet3, TangoColor::Black);
root->addChild(p);
Viewer viewer;
viewer.setSceneData(root);
viewer.getCamera()->setClearColor(Color(TangoColor::White).get());
viewer.run();
}
最佳答案
在函数createStreet
中,我为顶点数组使用了Vec3dArray
,而在createPointer
函数中,我使用了Vec3Array
。在库中,我猜它期望所有节点
由浮点数或双打组成,但不能同时由两者组成。更改这两个功能可以解决此问题:
inline osg::Vec3 toVec3(const straph::Point& p, float elevation=0.0)
{
return osg::Vec3(float(p.x), float(p.y), elevation);
}
Geometry* createStreet(const straph::Polyline& path)
{
ref_ptr<Vec3Array> array (new Vec3Array(path.size()));
for (unsigned i = 0; i < path.size(); ++i) {
(*array)[i] = toVec3(path[i]);
}
Geometry* geom = new Geometry;
geom->setVertexArray(array.get());
geom->addPrimitiveSet(new osg::DrawArrays(GL_LINE_STRIP, 0, array->size()));
return geom;
}
这里是罗伯特·奥斯菲尔德(Robert Osfield)的评论: