簡體   English   中英

OpenScenegraph示例代碼問題

[英]OpenScenegraph sample code issue

下面的代碼來自一本書。 當我嘗試運行它時,它失敗了

osg :: ref_ptr geom =新的osg :: Geometry();

並且,除了告訴我確實如此,輸出窗口似乎並未包含有關其崩潰原因的大量信息。 知道下面的代碼在做什么嗎? 提前致謝。

這是當我嘗試在Visual Studio 2010中運行Windows時彈出的Windows錯誤(Windows 7 64)

Windows在OSGPracticeLab.exe中觸發了一個斷點。 這可能是由於堆損壞所致,這表明OSGPracticeLab.exe或其已加載的任何DLL中存在錯誤。 這也可能是由於OSGPracticeLab.exe具有焦點時用戶按下F12所致。 輸出窗口可能包含更多診斷信息。

在嘗試調試代碼時,我能夠將問題追溯到新的函數調用。 在下面的代碼中,似乎跳過了while循環,並且為p返回了空值(未分配內存,因此,下面的代碼中的我的Geometry對象未實例化。

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);
        }

下面是我的程序來繪制一些形狀並顯示。

#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();
}

我的代碼沒有任何問題。 從初學者指南中獲取了它,並且效果很好:

#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配置屬性>調試>工作目錄指定為: $(OSG_ROOT)\\bin

如果是極端情況,則可能是因為您的Visual Studio安裝已損壞。 嘗試重新安裝Visual Studio,如果OSG安裝已損壞,則重新安裝OSG(從源生成)。 提及這一點是因為我的一個朋友因為他的Visual Studio損壞而無法運行OSG。 重新安裝修復它。

osg建立嗎? 您是否從OSG內運行“安裝”項目? 即使您這樣做,也可以在Win7中取消權限-您可能必須手動安裝到Program Files。

上面發布的示例對我來說是完全針對OS7 3.1.0版構建的Win7 / VS 2008 / Win32-Release構建配置。 我只是用您上面粘貼的代碼替換了OSG解決方案中示例項目之一的主體,它可以構建並運行,而不會出現您列出的錯誤。

我從主干使用OSG-可能至少在任何預構建版本之前都是次要版本,但是如果您設置正確的路徑等,它應該可以在預構建版本中使用。 當然,您也可以嘗試從示例的作者下載開始: http : //www.skew-matrix.com/OSGQSG/-他們已經具有正確設置的項目文件等。

你不定義osg::Geometry代碼中的類,因此最有可能的問題是你沒有正確連接到其定義的對象或庫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM