OSG新建柱元2.0

//设置构建图元关键点
void adBuildingComponents::Pile::setKeyCenter(osg::Vec3f center)
{
this->clearGeometry();
this->Drawables.clear();
osg::Vec3f tempP = center;
osg::ref_ptr<osg::Vec3Array> column = new osg::Vec3Array();
std::vector < osg::Vec3 >verticeArray;
osg::Vec3 dir1 = osg::Vec3(1, 0, 0);
osg::Vec3 dir2 = osg::Vec3(0, 1, 0);
osg::ref_ptr<osg::Vec3Array> column2 = new osg::Vec3Array();//存十字线
if (_pileType == PileType::Pile_circle) {
float halfOfDiameter = _diameter * 0.5;
//存十字线:
column2->push_back(center - dir1 * halfOfDiameter * 4 / 3);
column2->push_back(center + dir1 * halfOfDiameter * 4 / 3);
column2->push_back(center - dir2 * halfOfDiameter * 4 / 3);
column2->push_back(center + dir2 * halfOfDiameter * 4 / 3);
//L的定义1:
//center.normalize();//归一化:线性缩放到区间[-1,+1](嗨嗨,其实就是单位化啦,模变成1),center值变化,务必在之前存十字线
//osg::Vec3f L = center * halfOfDiameter;

//L的定义2:
osg::Vec3f L = osg::Vec3f(1.0f,0.0f,0.0f) * halfOfDiameter;

osg::Vec3f normal;//法线
for (size_t i = 0; i < 120; i++)
{
if (_normals->size() > 2)
normal = _normals->at(2);
else
normal = _normals->at(0);
osg::Vec3 Point = tempP + L * osg::Matrix::rotate(osg::inDegrees(i * 3.0), normal);
Point.z() = 1;
verticeArray.push_back(Point);

}
for (auto one : verticeArray) {
column->push_back(one);
}
}
else if (_pileType == PileType::Pile_square) {
float halfOfSideLength = _sideLength * 0.5;
verticeArray.push_back(center - dir1* halfOfSideLength + dir2 * halfOfSideLength);
verticeArray.push_back(center + dir1 * halfOfSideLength + dir2 * halfOfSideLength);
verticeArray.push_back(center + dir1 * halfOfSideLength - dir2 * halfOfSideLength);
verticeArray.push_back(center - dir1 * halfOfSideLength - dir2 * halfOfSideLength);

for (auto one : verticeArray) {
column->push_back(one);
}
//存十字线:
column2->push_back(center - dir1 * halfOfSideLength * 4 / 3);
column2->push_back(center + dir1 * halfOfSideLength * 4 / 3);
column2->push_back(center - dir2 * halfOfSideLength * 4 / 3);
column2->push_back(center + dir2 * halfOfSideLength * 4 / 3);
}

this->setGeometryType(osg::PrimitiveSet::LINE_LOOP);

std::vector<osg::Geometry*> tempGeomtrys;

GeometricPrimitives* temp = new GeometricPrimitives;
osg::ref_ptr<osg::Geometry> geom = temp->createGeometry(column, osg::PrimitiveSet::Mode::LINE_LOOP);
//颜色
osg::ref_ptr<osg::Vec4Array> vc = new osg::Vec4Array;
vc->push_back(osg::Vec4(1, 1, 1, 1));
geom->setColorArray(vc, osg::Array::BIND_OVERALL);
//基本图元压入图元
tempGeomtrys.push_back(geom);

GeometricPrimitives* temp2 = new GeometricPrimitives;
osg::ref_ptr<osg::Geometry> geom2 = temp2->createGeometry(column2, osg::PrimitiveSet::Mode::LINES);
geom2->setColorArray(vc, osg::Array::BIND_OVERALL);

//辅助线压入图元
tempGeomtrys.push_back(geom2);

for (auto one : tempGeomtrys) {
this->Geometrys.push_back(one);
}


this->buildGeode();
}

posted @ 2022-08-02 10:30  jessicaland  阅读(54)  评论(0)    收藏  举报