首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OpenScenegraph示例代码问题

OpenScenegraph示例代码问题
EN

Stack Overflow用户
提问于 2012-06-20 22:33:24
回答 3查看 7.3K关注 0票数 2

下面的代码来自一本书。当我尝试运行它时,它会失败。

osg::ref_ptr geom =新osg::几何();

而且,除了告诉我它崩溃的原因之外,输出窗口似乎没有包含太多关于它崩溃的信息。知道我在下面的代码中做错了什么吗?提前谢谢。

下面是在Visual 2010(windows 7 64)中运行此操作时窗口出现的错误

窗口在OSGPracticeLab.exe中触发了一个断点。这可能是由于堆损坏所致,它表示OSGPracticeLab.exe或其加载的任何DLL中存在错误。这也可能是由于用户按下F12,而OSGPracticeLab.exe有焦点。输出窗口可能有更多的诊断信息。

在尝试调试代码时,我能够将问题跟踪到新函数调用。在下面的代码中,应该跳过while循环,并返回p的空值(没有分配内存,因此下面代码中的几何对象没有实例化)。

代码语言:javascript
复制
void *__CRTDECL operator new(size_t size) _THROW1(_STD bad_alloc)
        {       // try to allocate size bytes
        void *p;
        while ((p = malloc(size)) == 0)
                if (_callnewh(size) == 0)
                {       // report no memory
                static const std::bad_alloc nomem;
                _RAISE(nomem);
                }

        return (p);
        }

下面是我绘制一些形状和显示的程序。

代码语言:javascript
复制
#include <osg/ShapeDrawable>
#include <osg/Geode>
#include <osgViewer/Viewer> 


int main()
{
    //An octahedron is a polyhedron having eight triangle faces. 
    //It is really a nice example to show why primitive indexing is important
    // we will sketch the octahedron structure now 

    osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(6);
    //octahedron has six vertices, each shaed by four triangles.  
    //withe the help of an index array and the osg::DrawElementsUInt class, we can allocate
    //a vertex array with only six elements
    (*vertices)[0].set( 0.0f, 0.0f, 1.0f);
    (*vertices)[1].set(-0.5f,-0.5f, 0.0f);
    (*vertices)[2].set( 0.5f,-0.5f, 0.0f);
    (*vertices)[3].set( 0.5f, 0.5f, 0.0f);
    (*vertices)[4].set(-0.5f, 0.5f, 0.0f);
    (*vertices)[5].set( 0.0f, 0.0f,-1.0f);
    //The osg::DrawElementsUInt accepts a size parameter besides the drawing mode parameter, too. 
    //After that, we will specify the indices of vertices to describe all eight triangle faces.
    osg::ref_ptr<osg::DrawElementsUInt> indices = new osg::DrawElementsUInt(GL_TRIANGLES, 24);
    (*indices)[0] = 0; (*indices)[1] = 1; (*indices)[2] = 2;
    (*indices)[3] = 0; (*indices)[4] = 2; (*indices)[5] = 3;
    (*indices)[6] = 0; (*indices)[7] = 3; (*indices)[8] = 4;
    (*indices)[9] = 0; (*indices)[10]= 4; (*indices)[11]= 1;
    (*indices)[12]= 5; (*indices)[13]= 2; (*indices)[14]= 1;
    (*indices)[15]= 5; (*indices)[16]= 3; (*indices)[17]= 2;
    (*indices)[18]= 5; (*indices)[19]= 4; (*indices)[20]= 3;
    (*indices)[21]= 5; (*indices)[22]= 1; (*indices)[23]= 4;

    //To create a geometry with a default white color, we only set the vertex array 
    //and the osg::DrawElementsUInt primitive set. The normal array is also required but is not easy
    //to compute manually. We will use a smoothed normal calculator to automatically obtain it. This calculator
    //will be described in the next section, Using polygonal techniques.
    osg::ref_ptr<osg::Geometry> geom = new osg::Geometry();
    geom->setVertexArray( vertices.get() );
    geom->addPrimitiveSet( indices.get() );
    //osgUtil::SmoothingVisitor::smooth( *geom );
    //Add the geometry to an osg::Geode object and make it the scene root
    osg::ref_ptr<osg::Geode> root = new osg::Geode;
    root->addDrawable( geom.get() );
    osgViewer::Viewer viewer;
    viewer.setSceneData( root.get() );
    return viewer.run();
}

int drawShapeUsingVertices()
{
    //Create the vertex array and push the four corner points to the back of the array by using vector like operations:
    osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
    vertices->push_back( osg::Vec3(0.0f, 0.0f, 0.0f) );
    vertices->push_back( osg::Vec3(1.0f, 0.0f, 0.0f) );
    vertices->push_back( osg::Vec3(1.0f, 0.0f, 1.0f) );
    vertices->push_back( osg::Vec3(0.0f, 0.0f, 1.0f) );
    //We have to indicate the normal of each vertex; otherwise OpenGL will use a default (0, 0, 1) normal vector
    //and the lighting equation calculation may be incorrect. The four vertices actually face the same direction,
    //so a single normal vector is enough. We will also set the setNormalBinding() method to BIND_OVERALL later.
    osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array; 
    normals->push_back( osg::Vec3(0.0f,-1.0f, 0.0f) );
    osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
    //here We will indicate a unique color value to each vertex and make them colored. By default, 
    //OpenGL will use smooth coloring and blend colors at each vertex together:
    colors->push_back( osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) );
    colors->push_back( osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) );
    colors->push_back( osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) );
    colors->push_back( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );
    //Next, we create the osg::Geometry object and set the prepared vertex, normal, and color arrays to it. 
    //We also indicate that the single normal should be bound to the entire geometry and that the colors
    //should be bound per vertex:
    osg::ref_ptr<osg::Geometry> quad = new osg::Geometry;
    quad->setVertexArray( vertices.get() );
    quad->setNormalArray( normals.get() );
    quad->setNormalBinding( osg::Geometry::BIND_OVERALL );
    quad->setColorArray( colors.get() );
    quad->setColorBinding( osg::Geometry::BIND_PER_VERTEX );

    //The last step required to finish a geometry and add it to the scene graph is to specify the primitive set.
    //A newly allocated osg::DrawArrays instance with the drawing mode set to GL_QUADS is used here, in order to
    //render the four vertices as quad corners in a counter-clockwise order:
    quad->addPrimitiveSet( new osg::DrawArrays(GL_QUADS, 0, 4) );
    //Add the geometry to an osg::Geode object and render it in the scene viewer:
    osg::ref_ptr<osg::Geode> root = new osg::Geode;
    root->addDrawable( quad.get() );
    osgViewer::Viewer viewer;
    viewer.setSceneData( root.get() );
    return viewer.run();
}
EN

回答 3

Stack Overflow用户

发布于 2012-07-04 05:47:36

我对密码没有任何问题。它是从初学者指南上学来的,效果很好:

代码语言:javascript
复制
#include <osg/Geometry>
#include <osg/Geode>
#include <osgViewer/Viewer>

int main()
{
    osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
    vertices->push_back( osg::Vec3(0.0f, 0.0f, 0.0f) );
    vertices->push_back( osg::Vec3(1.0f, 0.0f, 0.0f) );
    vertices->push_back( osg::Vec3(1.0f, 0.0f, 1.0f) );
    vertices->push_back( osg::Vec3(0.0f, 0.0f, 1.0f) );

    osg::ref_ptr<osg::Vec3Array> normals = new osg::Vec3Array;
    normals->push_back( osg::Vec3(0.0f,-1.0f, 0.0f) );

    osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
    colors->push_back( osg::Vec4(1.0f, 0.0f, 0.0f, 1.0f) );
    colors->push_back( osg::Vec4(0.0f, 1.0f, 0.0f, 1.0f) );
    colors->push_back( osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f) );
    colors->push_back( osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f) );

    osg::ref_ptr<osg::Geometry> quad = new osg::Geometry;
    quad->setVertexArray( vertices.get() );
    quad->setNormalArray( normals.get() );
    quad->setNormalBinding( osg::Geometry::BIND_OVERALL );
    quad->setColorArray( colors.get() );
    quad->setColorBinding( osg::Geometry::BIND_PER_VERTEX );

    quad->addPrimitiveSet( new osg::DrawArrays(GL_QUADS, 0, 4) );

    osg::ref_ptr<osg::Geode> root = new osg::Geode;
    root->addDrawable( quad.get() );
    osgViewer::Viewer viewer;
    viewer.setSceneData( root.get() );
    return viewer.run();
}  

我建议您检查项目属性。

您是否包括了其他包含目录:$(OSG_ROOT)\include;$(OSG_SOURCE)\include;$(OSG_ROOT)\include\osg;

如果您正处于调试模式,那么在您的预处理器定义中是否有此特性?_DEBUG;WIN32;

您是否指定了链接器附加目录:$(OSG_ROOT)\lib

您是否指定了链接器附加依赖项?:osgWidgetd.lib;osgVolumed.lib;osgViewerd.lib;osgUtild.lib;osgTextd.lib;osgTerraind.lib;osgSimd.lib;osgShadowd.lib;osgPresentationd.lib;osgParticled.lib;osgManipulatord.lib;osgGAd.lib;osgFXd.lib;osgDBd.lib;osgd.lib;osgAnimationd.lib;OpenThreadsd.lib;;;;;;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)

是否将配置属性>调试>工作目录指定为:$(OSG_ROOT)\bin

如果是极端情况,可能是因为Visual安装已损坏。尝试重新安装Visual,如果OSG安装已损坏,则重新安装OSG (从源代码构建)。提到这一点是因为我的一个朋友在运行OSG时遇到了问题,因为他的Visual已经损坏了。重新安装它。

票数 4
EN

Stack Overflow用户

发布于 2012-06-21 17:16:38

osg会建吗?你在OSG内部运行了“安装”项目吗?即使您这样做了,权限也可以在Win7中保存--您可能必须手动安装到程序文件中。

上面发布的示例在Win7 / VS 2008 /Win7 32发行版构建配置上为我完美地编译,构建于OSG的3.1.0版本。我刚刚用上面粘贴的代码替换了OSG解决方案中的一个示例项目中的main,它构建并运行时没有您列出的错误。

我正在使用OSG从主干-可能至少有一个小版本之前的任何预构建,但它应该工作从预构建,如果你有你的路径,等等,设置正确。当然,您也可以尝试从作者下载的示例开始:http://www.skew-matrix.com/OSGQSG/ --他们已经设置了正确的项目文件等等。

票数 2
EN

Stack Overflow用户

发布于 2012-06-20 22:37:05

您没有在代码中定义osg::Geometry类,因此最可能的问题是没有正确链接到定义对象或库的对象或库。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11129148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档