Hello,

I am at  the Geometry tutorial now

and there the quad has been designed from scratch to simulate water.

In  that process an empty material is created, but i didnt find anywhere
later how that newly created material is added to  the geometry.

After compiling it is giving me a segmentation fault .



What could be the reason?


The code snippet is as follows:


#include "Basic.h"


//The SimpleSceneManager is a useful class which helps us to
//manage simple configurations. It will be discussed in detail later on
SimpleSceneManager *mgr;

//some global variable to simulate the water

//the resolution of the mesh
const int N = 100;

//the 2D array that will store all the height
//values of the mesh  that we will be simulating
Real32 wMesh[N][N];
 

//the origin of the water mesh
Pnt3f wOrigin = Pnt3f(0,0,0);


//width and length of the mesh
UInt32 width = 100;
UInt32 height = 100;

        
//we have a forward declaration here, just to have a better order
//of codepieces
int setupGLUT( int *argc, char *argv[] );
void moveNode(NodePtr, NodePtr, int);
void motion(int,int);
void mouse(int,int,int,int);
void reshape(int,int);
void display();
void keyboard(unsigned char, int,int);


 void initWaterMesh(void);

void initWaterMesh(void)
{
        for(int i =0;i < N; i++) // corresponds to row
                for(int j=0;j < N;j++)// correspods to column
                        wMesh[i][j] = 0;
}

NodePtr createWater(void);


NodePtr createWater(void)
{

        initWaterMesh();


        //now we start creating the geometry 
        //step by step and the first thing is to define the
        //primitives that we may want to use
        //and Quads is sufficient for us for that case
        //EVEN THOUGH IT IS POSSIBLE TO USE MORE THAN ONE PRIMITIVE
        GeoPTypesPtr type = GeoPTypesUI8::create();
                
        beginEditCP(type,GeoPTypesUI8::GeoPropDataFieldMask);
                //we want to use the quad only
                //but that is not restricted to only one quad
                type->addValue(GL_QUADS);
        endEditCP(type,GeoPTypesUI8::GeoPropDataFieldMask);

        //now we tell  th opensg how many vertices 
        //the primitives is going to have
        //the length of the single quad is reasonably 4
        //and we want to have more quads and therefore we multiply
        
        //GeoPLength will define the number of the the vertices 
        //of the used primitives
        GeoPLengthsPtr length = GeoPLengthsUI32::create();


        beginEditCP(length, GeoPLengthsUI32::GeoPropDataFieldMask);
                //the length of the single
                //quad is four
                length->addValue((N-1)*(N-1)*4);
        endEditCP(length, GeoPLengthsUI32::GeoPropDataFieldMask);


        //now we specify  the positions of the vertices
        //used in the specific geometry core
        GeoPositions3fPtr pos = GeoPositions3f::create();

        beginEditCP(pos,GeoPositions3f::GeoPropDataFieldMask);
                //here we put all  h position
                for(int x = 0;x < N;x++)
                        for(int z = 0;z < N; z++)
                                pos->addValue(Pnt3f(x,wMesh[x][z],z));
        endEditCP(pos, GeoPositions3f::GeoPropDataFieldMask);


        //now we assign the color of the geometry
        GeoColors3fPtr colors = GeoColors3f::create();
        
        beginEditCP(colors,GeoColors3f::GeoPropDataFieldMask);
                for(int x = 0;x < N; x++)
                        for(int z = 0;z < N; z++)
                                colors->addValue(Color3f(0,0,(x+1)/(z+1)));
        endEditCP(colors, GeoColors3f::GeoPropDataFieldMask);



        //Settig the normals as we have done that for colors
        GeoNormals3fPtr norms = GeoNormals3f::create();

        beginEditCP(norms, GeoNormals3f::GeoPropDataFieldMask);
                for(int x = 0;x < N; x++)
                        for(int z = 0; z < N; z++)
                                norms->addValue(Vec3f(0,1,0));
        endEditCP(norms, GeoNormals3f::GeoPropDataFieldMask);   

        
        //even though the material will be playing no role
        //we need that because without that the renderer 
        //stop doing its job of rendering leaving me with a blank screen
        SimpleMaterialPtr mat = SimpleMaterial::create();

        //set  the indices for the quads
        //GeoIndicesUI32 points to 
        //all the relevant data used by 
        //the provided primitives
        GeoIndicesUI32Ptr indices = GeoIndicesUI32::create();

        beginEditCP(indices, GeoIndicesUI32::GeoPropDataFieldMask);
                for(int x = 0;x < N; x++)
                        for(int z = 0; z < N; z++)
                        {
                                //point to four vertices that will
                                //define the single QUAD
                                indices->addValue(z*N + x);
                                indices->addValue((z+1)* N + x);
                                indices->addValue((z+1)*N + x + 1);
                                indices->addValue(z*N + x + 1);
                        }
        endEditCP(indices, GeoIndicesUI32::GeoPropDataFieldMask);



        /*
        As we have created all  the required data
        we can create a the geometry object  that we may need
        */

        GeometryPtr geo = Geometry::create();
        
        /*
        *Even though the fiels masks are not manadatory to specify
        *but it is a good practice to specify  them as they do save some of 
        *your time to load up  the geometry
        */
        beginEditCP(geo, 
                Geometry::TypesFieldMask    |
                Geometry::LengthsFieldMask  |
                Geometry::IndicesFieldMask  |
                Geometry::PositionsFieldMask |
                Geometry::NormalsFieldMask  |
                Geometry::ColorsFieldMask   |
                Geometry::MaterialFieldMask);

                        geo->setTypes(type);
                        geo->setLengths(length);
                        geo->setIndices(indices);
                        geo->setPositions(pos);
                        geo->setNormals(norms);
                        geo->setColors(colors);
                        geo->setMaterial(mat);
        endEditCP(geo, 
                Geometry::TypesFieldMask     |
                Geometry::LengthsFieldMask   |
                Geometry::IndicesFieldMask   |
                Geometry::PositionsFieldMask |
                Geometry::NormalsFieldMask   |
                Geometry::ColorsFieldMask    |
                Geometry::MaterialFieldMask);


        //finally we put the newly created core
        //to the nore and return
        NodePtr root = Node::create();

        beginEditCP(root, Node::CoreFieldMask);
                root->setCore(geo);
        endEditCP(root, Node::CoreFieldMask);   
}



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

        //create the GLUTWindow which is same for most of the application
        int winid = setupGLUT(&argc,argv);      

        GLUTWindowPtr gwin = GLUTWindow::create();

        gwin->setId(winid);
        gwin->init();
        

        //Create and setup the SSM
        mgr = new SimpleSceneManager();
        mgr->setWindow(gwin);
        mgr->setRoot(createWater());
        mgr->showAll();

        //give control to  the glut mainloop
        glutMainLoop();

}



//react to  the keys
void keyboard(unsigned char k,int x,int y)
{
        switch(k)
        {
                case 27:
                {
                        OSG::osgExit();
                        exit(0);
                }
                break;
        }
}


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

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

        glutPostRedisplay();
}



// react to size changes
void reshape(int w, int h)
{
    mgr->resize(w, h);
    glutPostRedisplay();
}
// just redraw our scene if this GLUT callback is invoked
void display(void)
{
        
    mgr->redraw();
}
//The GLUT subsystem is set up here. This is very similar to other GLUT
//applications. If you have worked with GLUT before, you may have the
//feeling of meeting old friends again, if you have not used GLUT before
//that is no problem. GLUT will be introduced briefly on the next section
int setupGLUT(int *argc, char *argv[])
{
    glutInit(argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    int winid = glutCreateWindow("Planetory System");

    // register the GLUT callback functions
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc(reshape);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);
    glutKeyboardFunc(keyboard);
    return winid;

}

N.B - i did some trial and error , stil gettig the segmentation fault.


Regards

Sajjad

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to