2012-06-20 3 views
2

아래 코드는 책에서 가져온 것입니다. 실행하려고하면 라인에서 실패합니다.OpenScenegraph 샘플 코드 문제

osg :: ref_ptr geom = new osg :: Geometry();

및 출력 창에 충돌 이유에 대한 많은 정보가 포함되어있는 것 같지 않습니다. 아래 코드에서 내가 뭘 잘못하고 있는지 알 수 있습니까? 미리 감사드립니다. 나는 윈도우 OSGPracticeLab.exe에 중단 점을 트리거하고있다

비주얼 스튜디오 2010 (윈도우 7 64)에서이를 실행하려고하면 다음

창문 오류 팝업입니다. OSGPracticeLab.exe 또는로드 한 DLL의 버그를 나타내는 힙 손상으로 인한 것일 수 있습니다. OSGPracticeLab.exe에 포커스가있을 때 사용자가 F12 키를 누를 때도 발생할 수 있습니다. 출력 창에 진단 정보가 더 많을 수 있습니다.

코드를 디버깅 할 때 새로운 함수 호출로 문제를 추적 할 수있었습니다. 아래의 코드에서는 while 루프는 스킵되는 것, 그리고 널 값이 더 메모리가 할당되지 (P에 대한 반환 등이 아래의 코드 내 기하 객체가 인스턴스화되지 않습니다. 아래

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

모든 중간 파일을 삭제하고 처음부터 재구성 해 보았습니까? 저기 어리 석음 보이는 여기에 아무것도, 그리고 나는이 예제는 일반적으로 잘 작동 알아. 과거에 이렇게 난처한 것들을 보았을 때, 깨끗한 재건 작업이 진행되었습니다 (아마 길을 따라 무언가가 손상되었을 것입니다). – tmpearce

+0

모든 빌드를 수행하기 전에 모든 중간 파일을 제거하기위한 '솔루션 정리'를 수행하십시오. 나는 수동으로 파일을 직접 제거하여 어딘가에 보관하지 않도록했다.하지만 그건 효과가 없다. – Kobojunkie

답변

0

당신은 당신의 코드에서 osg::Geometry 클래스를 정의하지 않습니다. 어떤 모양과 표시를 그려 내 프로그램입니다, 그래서 당신은 제대로 개체 또는 라이브러리에 연결되지 않습니다 가능성이 가장 높은 문제는 어디 이 정의 됨.

+0

죄송합니다, osg :: Geometry는 OpenSceneGraph 라이브러리의 일부입니다. – Kobojunkie

+0

라인, osg :: ref_ptr geom = 새로운 osg :: Geometry; , OpensceneGraph 퀵 스타트 가이드에서 직접 복사됩니다. – Kobojunkie

+0

@Kobojunkie'#include '가 도움이되지 않았습니까? 그게 문제라면 성공적으로 링크 할 수 있다고 믿을 수는 없지만 확인을 해치지는 않습니다. – tmpearce

2

osg가 빌드됩니까? OSG 내에서 "설치"프로젝트를 실행 했습니까? 비록 당신이했다면, 권한은 Win7에서 borked 수 있습니다 - 수동으로 프로그램 파일에 설치해야 할 수도 있습니다.

위에 게시 된 샘플은 OSG 버전 3.1.0에 대해 작성된 Win7/VS 2008/Win32-Release 빌드 구성에서 완벽하게 컴파일되었습니다. OSG 솔루션의 예제 프로젝트 중 하나에서 메인을 위의 붙여 넣은 코드로 바꿨습니다. 나열된 오류없이 빌드되고 실행됩니다.

트렁크에서 OSG를 사용하고 있습니다. 아마도 적어도 사전 빌드보다 앞서 부 버전을 사용하고있을 것입니다. 그러나 경로 등이 올바로 설정되어 있다면 사전 빌드에서 작동해야합니다. 물론 저자의 예제 다운로드 (http://www.skew-matrix.com/OSGQSG/)부터 시도해 볼 수 있습니다. 프로젝트 파일 등이 이미 올바르게 설정되어 있습니다.

4

코드에 아무런 문제가 없었습니다. 초보자 가이드에서 가져와 잘 작동합니다.

#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
링커 추가 종속성을 지정 했습니까?: 당신의 비주얼 스튜디오 설치가 손상 되었기 때문에 극단적 인 경우, 그것은있을 수 있습니다 경우 $(OSG_ROOT)\bin

:
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)는 등의 구성 속성> 디버깅> 작업 디렉토리를 지정했습니다. Visual Studio를 다시 설치하고 OSG 설치가 손상된 경우 OSG (소스에서 빌드)를 다시 설치하십시오. Visual Studio가 손상 되었기 때문에 내 친구가 OSG를 실행하는 데 문제가 있었기 때문에이를 언급했습니다. 그것을 다시 설치하십시오.