C++ - 创建几何体

标签 c++ visual-studio geometry cube scenegraph

<分区>

我在大学接到了一项任务,负责编译提供给我们的代码并识别:

  • 已使用的 OpenGL 图元类型。
  • 已经创建了多少原语。
  • 创建了多少个顶点。
  • 网格的属性。

提供给我们的代码应该创建一个立方体,但实际上并没有。我已经仔细查看了几个小时,但无法弄清楚原因。

我认为可能是因为“createCube()”在 main 中根本没有被提及,所以我添加了“scene = createCube();” 然而,起初这给了我一个错误,createCube() 尚未定义。经过几个小时的困惑,尽管仍然没有立方体,但我不再收到此错误。我只是得到一个空窗口。

它不起作用并不重要,因为我可以在没有看到立方体的情况下回答问题,但是作业的下一部分是创建一个四面体而不是立方体,为此我需要代码可以工作.

如果有人能看到我遗漏的内容,我们将不胜感激。

// Geometry
//
// This tutorial supports learning
// about assembling a scene in a scene graph
// using transformation cores

// headers for OpenSG configuration and GLUT
#include <OpenSG/OSGGLUT.h>
#include <OpenSG/OSGConfig.h>
#include <OpenSG/OSGSimpleGeometry.h>
#include <OpenSG/OSGGLUTWindow.h>
#include <OpenSG/OSGSimpleSceneManager.h>
#include <OpenSG/OSGSceneFileHandler.h>
#include <OpenSG/OSGGeoProperties.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>

// Simple Scene manager for accesing cameras and geometry
OSG::SimpleSceneManagerRefPtr mgr;

int setupGLUT(int *argc, char *argv[]);

int main(int argc, char **argv)
{

    // initialise OpenSG
    OSG::osgInit(argc, argv);

    // initialise GLUT
    int winid = setupGLUT(&argc, argv);

    {
        // create a OSGGLUT window
        OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create();
        gwin->setGlutId(winid);
        gwin->init();


        //read the file which will be passed as an argument
        OSG::NodeRefPtr scene = OSG::Node::create();
        OSG::NodeRefPtr createCube = OSG::Node::create();
        scene = createCube;

        //commit all changes to OpenSG 
        OSG::commitChanges();

        // create the SimpleSceneManager helper
        mgr = OSG::SimpleSceneManager::create();

        // tell the manager what to manage
        mgr->setWindow(gwin);
        mgr->setRoot(scene);
        // show the whole scene
        mgr->showAll();
    }
    // GLUT main loop
    glutMainLoop();
    return 0;
}

//
// GLUT callback functions
//

// redraw the window
void display(void)
{
    mgr->redraw();
}

// react to size changes
void reshape(int w, int h)
{
    mgr->resize(w, h);
    glutPostRedisplay();
}

// react to mouse button presses
void mouse(int button, int state, int x, int y)
{
    if (!state)
        mgr->mouseButtonPress(button, x, y);

    glutPostRedisplay();
}

// react to mouse motions with pressed buttons
void motion(int x, int y)
{
    mgr->mouseMove(x, y);
    glutPostRedisplay();
}

// react to keys
void keyboard(unsigned char k, int x, int y)
{
    switch (k)
    {
    case 'e':
    {
        // clean up global variables
        mgr = NULL;

        OSG::osgExit();
        exit(0);
    }
    break;
    case 's':
    {
        mgr->setStatistics(!mgr->getStatistics());
    }
    break;
    }
}

// setup the GLUT library which handles the windows for us
int setupGLUT(int *argc, char *argv[])
{
    glutInit(argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);

    int winid = glutCreateWindow("05 Transformations Tutorial");

    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    return winid;
}
OSG::NodeRefPtr createCube()
{
    //create a node to hold the geometry
    OSG::NodeRefPtr geonode = OSG::Node::create();

    //create a geometry
    OSG::GeometryRefPtr geo = OSG::Geometry::create();

    //The primitive types.
    //OpenGL provides us with several different types of shapes that we can     draw 
    //(e.g. GL_LINES, GL_POLYGON, GL_QUADS, GL_TRIANGLES)
    //we need to specify the type of geometry we want to use
    //lets start by using only triangles (although different types can be     freely mixed)
    OSG::GeoUInt8PropertyRefPtr type = OSG::GeoUInt8Property::create();
    //MODIFY HERE 
    type->addValue(GL_TRIANGLES);

    //The primitive lengths.
        //These define the number of vertices to be passed to OpenGL for     each primitive. 
        //Thus there have to be at least as many entries as in the types     property.
    //in the case of the cube we are using 12 triangles which each have 3     vertices (12 X 3 = 36) 
    OSG::GeoUInt32PropertyRefPtr lens = OSG::GeoUInt32Property::create();
    //MODIFY HERE 
    lens->addValue(36);


    // The vertices.
    OSG::GeoPnt3fPropertyRefPtr  pnts = OSG::GeoPnt3fProperty::create();
    //MODIFY HERE with positions of your geometry
    pnts->addValue(OSG::Pnt3f(-0.5, 0.5, 0.5));
    pnts->addValue(OSG::Pnt3f(0.5, 0.5, 0.5));
    pnts->addValue(OSG::Pnt3f(0.5, -0.5, 0.5));
    pnts->addValue(OSG::Pnt3f(-0.5, -0.5, 0.5));
    pnts->addValue(OSG::Pnt3f(-0.5, 0.5, -0.5));
    pnts->addValue(OSG::Pnt3f(0.5, 0.5, -0.5));
    pnts->addValue(OSG::Pnt3f(0.5, -0.5, -0.5));
    pnts->addValue(OSG::Pnt3f(-0.5, -0.5, -0.5));

    // The normals.
    //These are used for lighting calculations and have to point away from     the
    //surface. Normals are standard vectors. 
    OSG::GeoVec3fPropertyRefPtr  norms = OSG::GeoVec3fProperty::create();
    norms->push_back(OSG::Vec3f(0, 0, 1));
    norms->push_back(OSG::Vec3f(1, 0, 0));
    norms->push_back(OSG::Vec3f(0, 0, -1));
    norms->push_back(OSG::Vec3f(-1, 0, 0));
    norms->push_back(OSG::Vec3f(0, 1, 0));
    norms->push_back(OSG::Vec3f(0, -1, 0));

    // The colours.   
    // GeoColor3fProperty stores all color values that will be used
    OSG::GeoColor3fPropertyRecPtr colors =     OSG::GeoColor3fProperty::create();
    colors->addValue(OSG::Color3f(0, 0, 1));
    colors->addValue(OSG::Color3f(0, 0, 1));
    colors->addValue(OSG::Color3f(0, 0, 1));
    colors->addValue(OSG::Color3f(0, 0, 1));
    colors->addValue(OSG::Color3f(0, 0, 1));
    colors->addValue(OSG::Color3f(0, 0, 1));

    // The indices.
    // in order not to replicate the same positions all the time, 
    // use index number of the position 
    OSG::GeoUInt32PropertyRefPtr indices = OSG::GeoUInt32Property::create();

    //face 1: front 
    //face 1 - triangle 1
    indices->addValue(0);
    indices->addValue(2);
    indices->addValue(1);
    //face 1 - triangle 2
    indices->addValue(0);
    indices->addValue(3);
    indices->addValue(2);


    //face 2: right 
    //face 2 - triangle 1
    indices->addValue(1);
    indices->addValue(2);
    indices->addValue(6);
    //face 3 - triangle 2
    indices->addValue(1);
    indices->addValue(6);
    indices->addValue(5);


    //face 3: back
    //face 3 - triangle 1
    indices->addValue(5);
    indices->addValue(6);
    indices->addValue(7);
    //face 3 - triangle 2
    indices->addValue(5);
    indices->addValue(7);
    indices->addValue(4);


    //face 4: left
    //face 4 - triangle 1
    indices->addValue(4);
    indices->addValue(7);
    indices->addValue(3);
    //face 4 - triangle 2
    indices->addValue(4);
    indices->addValue(3);
    indices->addValue(0);

    //face 5: top
    //face 5 - triangle 1
    indices->addValue(4);
    indices->addValue(1);
    indices->addValue(5);
    //face 5 - triangle 2
    indices->addValue(4);
    indices->addValue(0);
    indices->addValue(1);

    //face 6: bottom
    //face 6 - triangle 1
    indices->addValue(2);
    indices->addValue(3);
    indices->addValue(7);
    //face 6 - triangle 2
    indices->addValue(2);
    indices->addValue(7);
    indices->addValue(6);


    // The indices for colours and normals
    // as normals are different for each side of the cube, we use a special         index for this property
    OSG::GeoUInt32PropertyRefPtr indicesnormpos =     OSG::GeoUInt32Property::create();
    //face 1: front 
    //face 1 - triangle 1
    indicesnormpos->addValue(0);
    indicesnormpos->addValue(0);
    indicesnormpos->addValue(0);
    //face 1 - triangle 2
    indicesnormpos->addValue(0);
    indicesnormpos->addValue(0);
    indicesnormpos->addValue(0);


    //face 2: right 
    //face 2 - triangle 1
    indicesnormpos->addValue(1);
    indicesnormpos->addValue(1);
    indicesnormpos->addValue(1);
    //face 3 - triangle 2
    indicesnormpos->addValue(1);
    indicesnormpos->addValue(1);
    indicesnormpos->addValue(1);


    //face 3: back
    //face 3 - triangle 1
    indicesnormpos->addValue(2);
    indicesnormpos->addValue(2);
    indicesnormpos->addValue(2);
    //face 3 - triangle 2
    indicesnormpos->addValue(2);
    indicesnormpos->addValue(2);
    indicesnormpos->addValue(2);


    //face 4: left
    //face 4 - triangle 1
    indicesnormpos->addValue(3);
    indicesnormpos->addValue(3);
    indicesnormpos->addValue(3);
    //face 4 - triangle 2
    indicesnormpos->addValue(3);
    indicesnormpos->addValue(3);
    indicesnormpos->addValue(3);

    //face 5: top
    //face 5 - triangle 1
    indicesnormpos->addValue(4);
    indicesnormpos->addValue(4);
    indicesnormpos->addValue(4);
    //face 5 - triangle 2
    indicesnormpos->addValue(4);
    indicesnormpos->addValue(4);
    indicesnormpos->addValue(4);

    //face 6: bottom
    //face 6 - triangle 1
    indicesnormpos->addValue(5);
    indicesnormpos->addValue(5);
    indicesnormpos->addValue(5);
    //face 6 - triangle 2
    indicesnormpos->addValue(5);
    indicesnormpos->addValue(5);
    indicesnormpos->addValue(5);

    // Put it all together into a Geometry NodeCore.
    geo->setTypes(type);
    geo->setLengths(lens);

    geo->setProperty(pnts, OSG::Geometry::PositionsIndex);
    geo->setIndex(indices, OSG::Geometry::PositionsIndex);

    geo->setProperty(norms, OSG::Geometry::NormalsIndex);
    geo->setIndex(indicesnormpos, OSG::Geometry::NormalsIndex);

    geo->setProperty(colors, OSG::Geometry::ColorsIndex);
    geo->setIndex(indicesnormpos, OSG::Geometry::ColorsIndex);

    // if you were not using any indexing you will simply use:
    //geo->setTypes    (type);
    //geo->setLengths  (lens);
    //geo->setPositions (pnts);
    //geo->setNormals   (norms);
    //geo->setColors    (colors);        

    geonode->setCore(geo);

    return geonode;

}

最佳答案

您的 OSG::NodeRefPtr createCube() 不是在您在 main 中调用它时声明的。所以,只需声明它:

OSG::NodeRefPtr createCube();
int setupGLUT(int *argc, char *argv[]);

int main(int argc, char **argv) { 
    // Rest of code

并且,在你的主要部分:

    //read the file which will be passed as an argument
    OSG::NodeRefPtr scene = createCube();

您可以删除以下语句:

    OSG::NodeRefPtr createCube = OSG::Node::create();
    scene = createCube;

关于C++ - 创建几何体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41492519/

相关文章:

javascript - SVG 圆圈在 d3 中堆积

java - 如何从Java中的其他给定点找到最远的点

c++ - 在没有源代码的情况下确定 exe 接受的参数?

c++ - 如何使用 MsiGetProductInfo 和 MsiOpenDatabase 获取升级代码?

python - 如何将不同进程的任务栏图标与Windows 7结合起来

c# - 推送开始调试后 VS 2013 停止工作

algorithm - 寻找适合一系列矩形的最大矩形的面积

c++ - OpenCV videoCapture 为 iOS 应用程序文档目录中的文件提供了错误的属性

c# - 工具版本不支持 VsTsc 任务

Python - 将 Visual Studio 项目转换为具有所有依赖项的 Docker 镜像