查看: 782|回复: 2

很简单的一段程序,断言错误

[复制链接]

该用户从未签到

发表于 2014-7-7 21:18:21 | 显示全部楼层 |阅读模式
  1. int main()
  2. {
  3.         osg::Group* root = new osg::Group();
  4.         //osg::Geode* pyramidGeode = new osg::Geode;//createGeode();
  5.         //osg::Geometry* pyramidGeometry = createGeometry();
  6.         //pyramidGeode->addDrawable(pyramidGeometry);
  7.         osg::Geode* pyramidGeode = createGeode();
  8.         root->addChild(pyramidGeode);
  9.        

  10.         VertexVisitor vs;
  11.         //root->accept(vs);

  12.         osgViewer::Viewer view;
  13.         view.setSceneData(root);
  14.         view.setUpViewInWindow(100,100,800,600);
  15.         view.run();
  16.         return 0;

  17. }
复制代码

  1. osg::Geode * createGeode()
  2. {
  3.         osg::ref_ptr<osg::Geometry> geo = new osg::Geometry;
  4.         osg::Vec3Array* vecarray = new osg::Vec3Array;     //顶点坐标数组
  5.         osg::UByteArray* vecindex = new osg::UByteArray;   //顶点索引数组
  6.         vecarray->push_back(osg::Vec3(1.0, 0.0, 1.0));
  7.         vecarray->push_back(osg::Vec3(-1.0, 0.0, 1.0));
  8.         vecarray->push_back(osg::Vec3(-1.0, 0.0, -1.0));
  9.         vecarray->push_back(osg::Vec3(1.0, 0.0, -1.0));

  10.         vecindex->push_back(3);
  11.         vecindex->push_back(2);
  12.         vecindex->push_back(1);
  13.         vecindex->push_back(0);

  14.         geo->setVertexArray(vecarray);
  15.         geo->setVertexIndices(vecindex);

  16.         //osg::DrawElements *ele=new osg::DrawElements();
  17.         geo->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,vecindex->size()));

  18.         osg::Vec4Array* colors = new osg::Vec4Array;
  19.         colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) );
  20.         colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) );
  21.         colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) );
  22.         colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
  23.        
  24.         osg::UIntArray * colorIndexArray = new osg::UIntArray();
  25.         geo->setColorArray(colors);
  26.         geo->setColorIndices(colorIndexArray);
  27.         geo->setColorBinding(osg::Geometry::BIND_PER_VERTEX);

  28.         osg::Geode * geode=new osg::Geode;
  29.         geode->addDrawable(geo);
  30.         return geode;
  31. }
复制代码

Debug Assertion Failed
expression: vector subscript out of range.
百思不得其解,不知道那个地方out of range,求指点一下吧,调试的时候,只有到 view.run()的时候才出断言错误,所以不知道是什么原因。

该用户从未签到

发表于 2014-7-8 11:47:53 | 显示全部楼层
那是因为颜色索引数组你没有定义,就是这个变量colorIndexArray。第28行代码你直接new了后没赋值。像vecindex那样赋值就可以了

该用户从未签到

 楼主| 发表于 2014-7-8 19:58:24 | 显示全部楼层
不好意思,是我粘贴错了函数,是调用下面这个函数的时候出现 断言错误
  1. osg::Geometry * createGeometry()
  2. {
  3.         osg::Geometry * pyramidGeometry = new osg::Geometry;
  4.         osg::Vec3Array* pyramidVertices = new osg::Vec3Array;
  5.         pyramidVertices->push_back( osg::Vec3( 0, 0, 0) ); // 左前
  6.         pyramidVertices->push_back( osg::Vec3(10, 0, 0) ); // 右前
  7.         pyramidVertices->push_back( osg::Vec3(10,10, 0) ); // 右后
  8.         pyramidVertices->push_back( osg::Vec3( 0,10, 0) ); // 左后
  9.         pyramidVertices->push_back( osg::Vec3( 5, 5,10) ); // 塔尖
  10.         pyramidGeometry->setVertexArray( pyramidVertices );

  11.         /*
  12.         osg::DrawElementsUInt* pyramidBase = new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
  13.         pyramidBase->push_back(3);
  14.         pyramidBase->push_back(2);
  15.         pyramidBase->push_back(1);
  16.         pyramidBase->push_back(0);
  17.         pyramidGeometry->addPrimitiveSet(pyramidBase);

  18.         osg::DrawElementsUInt* pyramidFaceOne = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
  19.         pyramidFaceOne->push_back(0);
  20.         pyramidFaceOne->push_back(1);
  21.         pyramidFaceOne->push_back(4);
  22.         pyramidGeometry->addPrimitiveSet(pyramidFaceOne);
  23.         osg::DrawElementsUInt* pyramidFaceTwo = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);

  24.         pyramidFaceTwo->push_back(1);
  25.         pyramidFaceTwo->push_back(2);
  26.         pyramidFaceTwo->push_back(4);
  27.         pyramidGeometry->addPrimitiveSet(pyramidFaceTwo);
  28.         osg::DrawElementsUInt* pyramidFaceThree = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
  29.         pyramidFaceThree->push_back(2);
  30.         pyramidFaceThree->push_back(3);
  31.         pyramidFaceThree->push_back(4);
  32.         pyramidGeometry->addPrimitiveSet(pyramidFaceThree);
  33.         osg::DrawElementsUInt* pyramidFaceFour = new osg::DrawElementsUInt(osg::PrimitiveSet::TRIANGLES, 0);
  34.         pyramidFaceFour->push_back(3);
  35.         pyramidFaceFour->push_back(0);
  36.         pyramidFaceFour->push_back(4);
  37.         pyramidGeometry->addPrimitiveSet(pyramidFaceFour);
  38.         */
  39.         osg::UIntArray* vecindex = new osg::UIntArray;   //顶点索引数组
  40.         vecindex->push_back(0);vecindex->push_back(1);vecindex->push_back(4);
  41.         vecindex->push_back(1);vecindex->push_back(2);vecindex->push_back(4);
  42.         vecindex->push_back(2);vecindex->push_back(3);vecindex->push_back(4);
  43.         vecindex->push_back(3);vecindex->push_back(0);vecindex->push_back(4);

  44.         pyramidGeometry->setVertexIndices(vecindex);
  45.         pyramidGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::TRIANGLES,0,vecindex->size()));

  46.         osg::Vec4Array* colors = new osg::Vec4Array;
  47.         colors->push_back(osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) );
  48.         colors->push_back(osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) );
  49.         colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) );
  50.         colors->push_back(osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
  51.         //osg::TemplateIndexArray <unsigned int, osg::Array::UIntArrayType,4,4> *colorIndexArray;
  52.         //colorIndexArray = new osg::TemplateIndexArray<unsigned int, osg::Array::UIntArrayType,4,4>;
  53.         osg::UIntArray * colorIndexArray = new osg::UIntArray();
  54.         colorIndexArray->push_back(0); // vertex 0 assigned color
  55.         colorIndexArray->push_back(1); // vertex 1 assigned color
  56.         colorIndexArray->push_back(2); // vertex 2 assigned color
  57.         colorIndexArray->push_back(3); // vertex 3 assigned color
  58.         colorIndexArray->push_back(0); // vertex 4 assigned color
  59.         pyramidGeometry->setColorArray(colors);
  60.         pyramidGeometry->setColorIndices(colorIndexArray);
  61.         pyramidGeometry->setColorBinding(osg::Geometry::BIND_PER_VERTEX);

  62.         osg::Vec2Array* texcoords = new osg::Vec2Array();
  63.         texcoords->push_back(Vec2(0.00f,0.0f));
  64.         texcoords->push_back(Vec2(0.25f,0.0f));
  65.         texcoords->push_back(Vec2(0.5f,0.0f));
  66.         texcoords->push_back(Vec2(0.75f,0.0f));
  67.         texcoords->push_back(Vec2(0.5f,1.0f));
  68.         pyramidGeometry->setTexCoordArray(0,texcoords);

  69.         return  pyramidGeometry;
  70. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

OSG中国官方论坛-有您OSG在中国才更好

网站简介:osgChina是国内首个三维相关技术开源社区,旨在为国内更多的技术开发人员提供最前沿的技术资讯,为更多的三维从业者提供一个学习、交流的技术平台。

联系我们

  • 工作时间:09:00--18:00
  • 反馈邮箱:1315785073@qq.com
快速回复 返回顶部 返回列表