|
一个正方体,8个顶点,6个面如何较少的内存建立模型。
即只用8个顶点,6个法向来确定,其他使用索引。
下面是一个用24个顶点,24个法向,建立的,但是结果法向好像错了。
坐边的是错误的,右边的是正确的。
下面给出代码
- #include <osgGA/TrackballManipulator>
- #include <osg/PositionAttitudeTransform>
- #include <osgGA/GUIEventHandler>
- #include <osg/Material>
- #include <osg/ShapeDrawable>
- #include <osg/Geometry>
- #include <osg/Geode>
- #include <valarray>
- #include <osg/Light>
- #include <osg/LightSource>
- #include <osgViewer/Viewer>
- #include <osgViewer/ViewerEventHandlers>
- #include <osgGA/StateSetManipulator>
- #pragma comment(lib, "osgd.lib")
- #pragma comment(lib, "osgDBd.lib")
- #pragma comment(lib, "osgGAd.lib")
- #pragma comment(lib, "osgUtild.lib")
- #pragma comment(lib, "osgViewerd.lib")
- #pragma comment(lib, "osgManipulatord.lib")
- osg::Geode *createCube() {
- // vertex array
- osg::Vec3Array *vertexArray = new osg::Vec3Array();
- // bottom front left
- vertexArray->push_back(osg::Vec3(-1, -1, 0));
- vertexArray->push_back(osg::Vec3(-1, -1, 0));
- vertexArray->push_back(osg::Vec3(-1, -1, 0));
- // bottom front right
- vertexArray->push_back(osg::Vec3(+1, -1, 0));
- vertexArray->push_back(osg::Vec3(+1, -1, 0));
- vertexArray->push_back(osg::Vec3(+1, -1, 0));
- // bottom back right
- vertexArray->push_back(osg::Vec3(+1, +1, 0));
- vertexArray->push_back(osg::Vec3(+1, +1, 0));
- vertexArray->push_back(osg::Vec3(+1, +1, 0));
- // bottom back left
- vertexArray->push_back(osg::Vec3(-1, +1, 0));
- vertexArray->push_back(osg::Vec3(-1, +1, 0));
- vertexArray->push_back(osg::Vec3(-1, +1, 0));
- // top front left
- vertexArray->push_back(osg::Vec3(-1, -1, 2));
- vertexArray->push_back(osg::Vec3(-1, -1, 2));
- vertexArray->push_back(osg::Vec3(-1, -1, 2));
- // top front right
- vertexArray->push_back(osg::Vec3(+1, -1, 2));
- vertexArray->push_back(osg::Vec3(+1, -1, 2));
- vertexArray->push_back(osg::Vec3(+1, -1, 2));
- // top back right
- vertexArray->push_back(osg::Vec3(+1, +1, 2));
- vertexArray->push_back(osg::Vec3(+1, +1, 2));
- vertexArray->push_back(osg::Vec3(+1, +1, 2));
- // top back left
- vertexArray->push_back(osg::Vec3(-1, +1, 2));
- vertexArray->push_back(osg::Vec3(-1, +1, 2));
- vertexArray->push_back(osg::Vec3(-1, +1, 2));
- // face array
- osg::DrawElementsUInt *faceArray = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
- // bottom
- faceArray->push_back(0); // face 1
- faceArray->push_back(9);
- faceArray->push_back(3);
- faceArray->push_back(9); // face 2
- faceArray->push_back(6);
- faceArray->push_back(3);
- // top
- faceArray->push_back(21); //face 3
- faceArray->push_back(12);
- faceArray->push_back(18);
- faceArray->push_back(12); //face 4
- faceArray->push_back(15);
- faceArray->push_back(18);
- // left
- faceArray->push_back(22); //face 5
- faceArray->push_back(10);
- faceArray->push_back(13);
- faceArray->push_back(10); //face 6
- faceArray->push_back(1);
- faceArray->push_back(13);
- // right
- faceArray->push_back(16); //face 7
- faceArray->push_back(4);
- faceArray->push_back(19);
- faceArray->push_back(4); //face 8
- faceArray->push_back(7);
- faceArray->push_back(19);
- // front
- faceArray->push_back(14); //face 9
- faceArray->push_back(2);
- faceArray->push_back(17);
- faceArray->push_back(2); //face 10
- faceArray->push_back(5);
- faceArray->push_back(17);
- // back
- faceArray->push_back(20); //face 11
- faceArray->push_back(8);
- faceArray->push_back(23);
- faceArray->push_back(8); //face 12
- faceArray->push_back(11);
- faceArray->push_back(23);
- // normal array
- osg::Vec3Array *normalArray = new osg::Vec3Array();
- normalArray->push_back(osg::Vec3(0, 0, -1));
- normalArray->push_back(osg::Vec3(0, -1, 0));
- normalArray->push_back(osg::Vec3(+1, 0, 0));
- normalArray->push_back(osg::Vec3(0, 0, -1));
- normalArray->push_back(osg::Vec3(0, +1, 0));
- normalArray->push_back(osg::Vec3(+1, 0, 0));
- normalArray->push_back(osg::Vec3(0, 0, -1));
- normalArray->push_back(osg::Vec3(0, +1, 0));
- normalArray->push_back(osg::Vec3(-1, 0, 0));
- normalArray->push_back(osg::Vec3(0, 0, -1));
- normalArray->push_back(osg::Vec3(0, -1, 0));
- normalArray->push_back(osg::Vec3(-1, 0, 0));
- normalArray->push_back(osg::Vec3(0, 0, +1));
- normalArray->push_back(osg::Vec3(0, -1, 0));
- normalArray->push_back(osg::Vec3(+1, 0, 0));
- normalArray->push_back(osg::Vec3(0, 0, +1));
- normalArray->push_back(osg::Vec3(0, +1, 0));
- normalArray->push_back(osg::Vec3(+1, 0, 0));
- normalArray->push_back(osg::Vec3(0, 0, +1));
- normalArray->push_back(osg::Vec3(0, +1, 0));
- normalArray->push_back(osg::Vec3(-1, 0, 0));
- normalArray->push_back(osg::Vec3(0, 0, +1));
- normalArray->push_back(osg::Vec3(0, -1, 0));
- normalArray->push_back(osg::Vec3(-1, 0, 0));
- osg::Geometry *geometry = new osg::Geometry();
- geometry->setVertexArray(vertexArray);
- geometry->setNormalArray(normalArray);
- geometry->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);
- geometry->addPrimitiveSet(faceArray);
- //创建颜色数组
- osg::ref_ptr<osg::Vec4Array> vc = new osg::Vec4Array();
- //添加数据
- vc->push_back(osg::Vec4(0.2, 0.8, 0.0, 0.5));
- //设置颜色数组
- geometry->setColorArray(vc.get());
- //设置颜色的绑定方式为
- geometry->setColorBinding(osg::Geometry::BIND_OVERALL);
- osg::Geode *cube = new osg::Geode();
- cube->addDrawable(geometry);
- return cube;
- }
- osg::Geode *CreateShape()
- {
- osg::ref_ptr<osg::Geode> geode = new osg::Geode;
- osg::ref_ptr<osg::ShapeDrawable> dd = new osg::ShapeDrawable(new osg::Box(osg::Vec3(10, 0,0), 2));
- dd->setColor(osg::Vec4(0.2, 0.8, 0.0, 0.5));
- geode->addDrawable(dd);
- return geode.release();
- }
- int main(int argc, const char* argv[]) {
-
- osgViewer::Viewer viewer;
- osg::ref_ptr<osg::Group> group = new osg::Group;
- group->addChild(createCube());
- group->addChild(CreateShape());
- viewer.setSceneData(group);
- viewer.addEventHandler(new osgViewer::StatsHandler());
- viewer.addEventHandler(new osgViewer::WindowSizeHandler());
- viewer.addEventHandler(new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()));
- return viewer.run();
- }
复制代码
|
|