|
楼主 |
发表于 2014-12-6 23:12:37
|
显示全部楼层
求助各位,还是不知道为什么。画立方体每个面的代码如下:
- osg::ref_ptr<osg::Group> drawFilledBox(string name, string point_name, osg::Vec3 min, osg::Vec3 max, osg::Vec4 color, double alpha, bool setDepth, int setDepthNum)
- {
- float minX, minY, minZ, maxX, maxY, maxZ;
- if(min.x() <= max.x())
- {
- minX = min.x();
- maxX = max.x();
- }
- else
- {
- minX = max.x();
- maxX = min.x();
- }
- if(min.z() <= max.z())
- {
- minZ = min.z();
- maxZ = max.z();
- }
- else
- {
- minZ = max.z();
- maxZ = min.z();
- }
- if(min.y() <= max.y())
- {
- minY = min.y();
- maxY = max.y();
- }
- else
- {
- minY = max.y();
- maxY = min.y();
- }
- minX = min.x();
- maxX = max.x();
- minZ = min.z();
- maxZ = max.z();
- minY = min.y();
- maxY = max.y();
- osg::Vec3 leftTopNear = osg::Vec3(minX, maxY, minZ);
- osg::Vec3 leftTopFar = osg::Vec3(minX, maxY, maxZ);
- osg::Vec3 rightTopNear = osg::Vec3(maxX, maxY, minZ);
- osg::Vec3 rightTopFar = osg::Vec3(maxX, maxY, maxZ);
- osg::Vec3 leftBottomNear = osg::Vec3(minX, minY, minZ);
- osg::Vec3 leftBottomFar = osg::Vec3(minX, minY, maxZ);
- osg::Vec3 rightBottomNear = osg::Vec3(maxX, minY, minZ);
- osg::Vec3 rightBottomFar = osg::Vec3(maxX, minY, maxZ);
- osg::ref_ptr<osg::StateSet> stateset = new osg::StateSet(*createStateSet(), osg::CopyOp::SHALLOW_COPY);
- if(setDepth)
- stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
- else
- stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
- stateset->setMode(GL_DEPTH_TEST, osg::StateAttribute::ON);
- stateset->setMode( GL_LIGHTING, osg::StateAttribute::OFF);
- stateset->setMode( GL_POLYGON_SMOOTH, osg::StateAttribute::ON);
- stateset->setRenderBinDetails( setDepthNum, "TraversalOrderBin",
- osg::StateSet::USE_RENDERBIN_DETAILS );
- stateset->setNestRenderBins(true);
- osg::ref_ptr<osg::CullFace> cull = new osg::CullFace();
- cull->setMode(osg::CullFace::BACK);
- stateset->setAttributeAndModes(cull, osg::StateAttribute::ON);
- osgUtil::SmoothingVisitor sv;
- // geode->accept(sv);
- // geode->setName(name);
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
- osg::ref_ptr<osg::Geode> geode1 = new osg::Geode;
- osg::ref_ptr<osg::Geode> geode2 = new osg::Geode;
- osg::ref_ptr<osg::Geode> geode3 = new osg::Geode;
- osg::ref_ptr<osg::Geode> geode4 = new osg::Geode;
- osg::ref_ptr<osg::Geode> geode5 = new osg::Geode;
- osg::ref_ptr<osg::Geode> geode6 = new osg::Geode;
- osg::ref_ptr<osg::Geometry> gem = new osg::Geometry;
- osg::ref_ptr<osg::Geometry> gem1 = new osg::Geometry;
- osg::ref_ptr<osg::Geometry> gem2 = new osg::Geometry;
- osg::ref_ptr<osg::Geometry> gem3 = new osg::Geometry;
- osg::ref_ptr<osg::Geometry> gem4 = new osg::Geometry;
- osg::ref_ptr<osg::Geometry> gem5 = new osg::Geometry;
- osg::ref_ptr<osg::Geometry> gem6 = new osg::Geometry;
- osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
- osg::ref_ptr<osg::Vec3Array> vertices1 = new osg::Vec3Array;
- osg::ref_ptr<osg::Vec3Array> vertices2 = new osg::Vec3Array;
- osg::ref_ptr<osg::Vec3Array> vertices3 = new osg::Vec3Array;
- osg::ref_ptr<osg::Vec3Array> vertices4 = new osg::Vec3Array;
- osg::ref_ptr<osg::Vec3Array> vertices5 = new osg::Vec3Array;
- osg::ref_ptr<osg::Vec3Array> vertices6 = new osg::Vec3Array;
- //points
- vertices->push_back(leftTopNear);
- vertices->push_back(leftTopFar);
- vertices->push_back(rightTopFar);
- vertices->push_back(rightTopNear);
- vertices->push_back(leftBottomFar);
- vertices->push_back(leftBottomNear);
- vertices->push_back(rightBottomNear);
- vertices->push_back(rightBottomFar);
- osg::ref_ptr<osg::DrawArrays> da = new osg::DrawArrays(osg::PrimitiveSet::POINTS, 0, vertices->size());
- // top
- vertices1->push_back(leftTopNear);
- vertices1->push_back(leftTopFar);
- vertices1->push_back(rightTopFar);
- vertices1->push_back(rightTopNear);
- osg::DrawElementsUInt* topBox = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
- topBox->push_back(0);
- topBox->push_back(1);
- topBox->push_back(2);
- topBox->push_back(3);
- topBox->setName("top");
- //left
- vertices2->push_back(leftBottomFar);
- vertices2->push_back(leftTopFar);
- vertices2->push_back(leftTopNear);
- vertices2->push_back(leftBottomNear);
- osg::DrawElementsUInt* leftBox = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
- leftBox->push_back(0);
- leftBox->push_back(1);
- leftBox->push_back(2);
- leftBox->push_back(3);
- leftBox->setName("left");
- //right
- vertices3->push_back(rightTopFar);
- vertices3->push_back(rightBottomFar);
- vertices3->push_back(rightBottomNear);
- vertices3->push_back(rightTopNear);
- osg::DrawElementsUInt* rightBox = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
- rightBox->push_back(0);
- rightBox->push_back(1);
- rightBox->push_back(2);
- rightBox->push_back(3);
- leftBox->setName("right");
- //Far
- vertices4->push_back(rightBottomFar);
- vertices4->push_back(rightTopFar);
- vertices4->push_back(leftTopFar);
- vertices4->push_back(leftBottomFar);
- osg::DrawElementsUInt* farBox = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
- farBox->push_back(0);
- farBox->push_back(1);
- farBox->push_back(2);
- farBox->push_back(3);
- leftBox->setName("far");
- //Near
- vertices5->push_back(leftTopNear);
- vertices5->push_back(rightTopNear);
- vertices5->push_back(rightBottomNear);
- vertices5->push_back(leftBottomNear);
-
- osg::DrawElementsUInt* nearBox = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
- nearBox->push_back(0);
- nearBox->push_back(1);
- nearBox->push_back(2);
- nearBox->push_back(3);
- nearBox->setName("near");
- //Bottom
- vertices6->push_back(leftBottomFar);
- vertices6->push_back(leftBottomNear);
- vertices6->push_back(rightBottomNear);
- vertices6->push_back(rightBottomFar);
- osg::DrawElementsUInt* bottomBox = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
- bottomBox->push_back(0);
- bottomBox->push_back(1);
- bottomBox->push_back(2);
- bottomBox->push_back(3);
- bottomBox->setName("bottom");
- osg::ref_ptr<osg::Vec4Array> colorArray = new osg::Vec4Array;
- osg::ref_ptr<osg::Vec4Array> colorArray1 = new osg::Vec4Array;
- osg::ref_ptr<osg::Vec4Array> colorArray2 = new osg::Vec4Array;
- osg::ref_ptr<osg::Vec4Array> colorArray3 = new osg::Vec4Array;
- osg::ref_ptr<osg::Vec4Array> colorArray4 = new osg::Vec4Array;
- osg::ref_ptr<osg::Vec4Array> colorArray5 = new osg::Vec4Array;
- osg::ref_ptr<osg::Vec4Array> colorArray6 = new osg::Vec4Array;
- colorArray->push_back(osg::Vec4(1,0,0,0.0));
- colorArray1->push_back(osg::Vec4(1,0,0,alpha));
- colorArray2->push_back(osg::Vec4(0,1,0,alpha));
- colorArray3->push_back(osg::Vec4(0,0,1,alpha));
- colorArray4->push_back(osg::Vec4(1,0,1,alpha));
- colorArray5->push_back(osg::Vec4(1,1,0,alpha));
- colorArray6->push_back(osg::Vec4(0,1,1,alpha));
- gem->addPrimitiveSet(da);
- gem1->addPrimitiveSet(topBox);
- gem2->addPrimitiveSet(leftBox);
- gem3->addPrimitiveSet(rightBox);
- gem4->addPrimitiveSet(farBox);
- gem5->addPrimitiveSet(nearBox);
- gem6->addPrimitiveSet(bottomBox);
- gem->setVertexArray(vertices);
- gem1->setVertexArray(vertices1);
- gem2->setVertexArray(vertices2);
- gem3->setVertexArray(vertices3);
- gem4->setVertexArray(vertices4);
- gem5->setVertexArray(vertices5);
- gem6->setVertexArray(vertices6);
- gem->setColorArray(colorArray);
- gem1->setColorArray(colorArray1);
- gem2->setColorArray(colorArray2);
- gem3->setColorArray(colorArray3);
- gem4->setColorArray(colorArray4);
- gem5->setColorArray(colorArray5);
- gem6->setColorArray(colorArray6);
- gem->setColorBinding(osg::Geometry::BIND_OVERALL);
- gem1->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
- gem2->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
- gem3->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
- gem4->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
- gem5->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
- gem6->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE_SET);
- geode->addDrawable(gem);
- geode1->addDrawable(gem1);
- geode2->addDrawable(gem2);
- geode3->addDrawable(gem3);
- geode4->addDrawable(gem4);
- geode5->addDrawable(gem5);
- geode6->addDrawable(gem6);
- geode->setName(point_name);
- geode1->setName("top");
- geode2->setName("left");
- geode3->setName("right");
- geode4->setName("far");
- geode5->setName("near");
- geode6->setName("bottom");
- osg::ref_ptr<Group> group = new osg::Group;
- group->addChild(geode);
- group->addChild(geode1);
- group->addChild(geode2);
- group->addChild(geode3);
- group->addChild(geode4);
- group->addChild(geode5);
- group->addChild(geode6);
- group->setStateSet(stateset);
- return group;
- }
复制代码 |
|