C++ - Creating Geometry

367 views Asked by At

I have been given an assignment at uni to compile the code given to us and identify:

  • Type of OpenGL primitives which have been used.
  • How many primitives have been created.
  • How many vertices have been created.
  • Properties of the mesh.

The code given to us is supposed to create a cube, however it does not. I've been looking through it for hours now and cannot figure out why.

I thought it might have been because "createCube()" was not mentioned in main at all, so i added "scene = createCube();" However at first this gave me an error, createCube() has not been defined. After hours of messing about with it, i no longer get this error although there's still no cube. I just get an empty window.

It wouldn't matter that it doesn't work as I could answer the questions without seeing the cube, however the next part of the assignment is to create a Tetrahedron rather than a cube and for that I need the code to work.

If anybody can see what I'm missing, any help would be greatly appreciated.

// 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;

}
1

There are 1 answers

2
Bruno Ferreira On BEST ANSWER

Your OSG::NodeRefPtr createCube() isn't declared by when you called it in your main. So, just declare it:

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

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

And, in your main:

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

You can delete the following statements:

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