|
本帖最后由 zhaohsww 于 2014-3-31 16:39 编辑
计算几何体的法向量:osgUtil::SmoothingVisitor::smooth(*(geom.get()));
没成功,不知哪里出错了?
代码中倒数4-6行为调试时array的num错误。
- osg::ref_ptr<osg::Node> creatPoints(string fPath)
- {
- //首先定义点
- osg::ref_ptr<osg::Vec3Array> v = new osg::Vec3Array;
- //定义法线数组
- //osg::ref_ptr<osg::Vec3Array> n = new osg::Vec3Array;
- //定义颜色数组
- //osg::ref_ptr<osg::Vec4Array> c = new osg::Vec4Array;
- int count = 0;
- FILE* pfData = fopen(fPath.c_str(), "r");
- if (pfData == NULL)
- {
- cout<<"DataFile does not exist!!!"<<endl;
- return NULL;
- }
- else
- {
- while (!feof(pfData))
- {
- float fx, fy, fz;
- char temp;
- fscanf(pfData, "%f", &fx);
- fscanf(pfData, "%c", &temp);
- fscanf(pfData, "%f", &fy);
- fscanf(pfData, "%c", &temp);
- fscanf(pfData, "%f", &fz);
- v->push_back(osg::Vec3(fx, fy, fz));
- //n->push_back(osg::Vec3(0, 0, 1));
- //c->push_back(osg::Vec4(0.804,0.498,0.196,0.5));//金色
- //c->push_back(osg::Vec4(0.902,0.910,0.980,0.5));//银色
- //c->push_back(osg::Vec4(0.6,0.196,0.804,0.5));//紫色
- //c->push_back(osg::Vec4(0.851,0.851,0.098,0.5));//金黄色
- c->push_back(osg::Vec4(1.0,1.0,1.0,1.0));
- count++;
- }
- fclose(pfData);
- }
- osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
- geom->setDataVariance( osg::Object::DYNAMIC );
- geom->setUseDisplayList( false );
- geom->setUseVertexBufferObjects( true );
- geom->setVertexArray(v.get());
- /*geom->setColorArray(c.get());
- geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX);*/
- //geom->setNormalArray(n.get());
- //geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX
- //设置关联方式
- geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, count));
- osgUtil::SmoothingVisitor::smooth(*(geom.get()));
- //geom->normal
- osg::Array *array = geom->getNormalArray;
- unsigned AAA = array->getNumElements();
- //几何组结点
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
- geode->addDrawable(geom.get());
- return geode.get();
- }
复制代码 |
|