Exemple #1
0
void display(void)
{
    Real32 time = glutGet(GLUT_ELAPSED_TIME);
    updateMesh(time);
    
    // we extract the core out of the root node
    // as we now this is a geometry node
    GeometryPtr geo = GeometryPtr::dcast(scene->getChild(0)->getCore());
    
    //now modify it's content
    
    // first we need a pointer to the position data field
    GeoPositions3fPtr pos = GeoPositions3fPtr::dcast(geo->getPositions());
    
    //get the data field the pointer is pointing at
    GeoPositions3f::StoredFieldType *posfield = pos->getFieldPtr();
    //get some iterators
    GeoPositions3f::StoredFieldType::iterator last, it;

    // set the iterator to the first data
    it = posfield->begin();
    
    beginEditCP(pos, GeoPositions3f::GeoPropDataFieldMask);
    //now simply run over all entires in the array
    for (int x = 0; x < N; x++)
        for (int z = 0; z < N; z++){
            (*it) = Pnt3f(x, wMesh[x][z], z);
            it++;
        }
    endEditCP(pos, GeoPositions3f::GeoPropDataFieldMask);
    
    mgr->redraw();
}
static void initWave(void)
{
    _size = (int) sqrt((float) _geo->getPositions()->getSize());

    _surf.resize(_size);
    for(UInt32 i=0;i<_size;++i)
        _surf[i].resize(_size);
    
    _force.resize(_size);
    for(UInt32 i=0;i<_size;++i)
        _force[i].resize(_size);
    
    _veloc.resize(_size);
    for(UInt32 i=0;i<_size;++i)
        _veloc[i].resize(_size);
    
    _surfo.resize(_size);
    for(UInt32 i=0;i<_size;++i)
        _surfo[i].resize(_size);

    GeoPositions3fPtr pos = GeoPositions3fPtr::dcast(_geo->getPositions());
    MFPnt3f *p = pos->getFieldPtr();

    beginEditCP(pos);
    {
        int c = 0;

        for(int i=0;i<_size;i++)
        {
            for(int j=0;j<_size;j++)
                _surfo[i][j] = (*p)[c++][2];
        }
    }
    endEditCP(pos);
}
static void updateGeometry(GeometryPtr geo)
{
    GeoPositions3fPtr pos = GeoPositions3fPtr::dcast(geo->getPositions());
    // p->setValue() is faster than pos->setValue()
    MFPnt3f *p = pos->getFieldPtr();
    beginEditCP(pos);
    int c = 0;
        for(int i=0;i<_size;++i)
        {
            for(int j=0;j<_size;++j)
            {
                Pnt3f &pp = (*p)[c++];
                pp[2] = _surfo[i][j] + _surf[i][j];
            }
        }
    endEditCP(pos);
}
/*
  Aufruf dieser Funktion erfolgt bei Traversierung des Szenengraphen
  mittels OpenSG-Funktion traverse().
  Enthaelt ein Knoten verwertbare Geometrieinformation so tragen wir
  Zeiger auf seine Geometrie (OpenSG-Strukturen) im array gla_meshInfo_
  ein.
  Nebenbei bestimmen wir für die Geometrie auch noch die World-Space-
  Transformation (evtl. existiert eine OpenSG-Funktion um diese
  Information zu erhalten, der Autor hat keine in der OpenSG-API
  entdeckt).
*/
Action::ResultE enter(NodePtr &node)
{
    int             i, j, h;
    Pnt3f           v;
    int             numFaces, numFaceVertices, vId, size;

    MeshInfo        meshInfo;
    TinyMatrix      transf;
    FaceIterator    fit;
    int             numQuads;
    NamePtr         namePtr;
    char            name[255];

    namePtr = NamePtr::dcast(node->findAttachment(Name::getClassType().getGroupId()));
    if(namePtr == osg::NullFC)
        strcpy(name, "");
    else
    {
        strcpy(name, namePtr->getFieldPtr()->getValue().c_str());
    }

    SINFO << "Node name = '" << name << "'" << endl << endLog;

    GeometryPtr geo = GeometryPtr::dcast(node->getCore());

    if(geo != NullFC)
    {
        GeoPLengthsUI32Ptr  pLength = GeoPLengthsUI32Ptr::dcast(geo->getLengths());
        GeoPTypesUI8Ptr     pTypes = GeoPTypesUI8Ptr::dcast(geo->getTypes());

        /* pLength and pTypes should not be NullFC, however VRML Importer/Exporter
		  code is instable by now, so this can happen */
        if((pLength != NullFC) && (pTypes != NullFC))
        {
            GeoPLengthsUI32::StoredFieldType * pLengthField = pLength->getFieldPtr();
            GeoPTypesUI8::StoredFieldType * pTypeField = pTypes->getFieldPtr();

            size = pLengthField->size();

            for(h = 0; h < size; h++)
            {
                if(((*pTypeField)[h] == GL_TRIANGLES) ||
                   ((*pTypeField)[h] == GL_QUADS))
                {
                    /* may quads appear in GL_TRIANGLES ? */
                    /* check if all triangles have three vertices */
                    numQuads = 0;
                    fit = geo->beginFaces();
                    while(fit != geo->endFaces())
                    {
                        numFaceVertices = fit.getLength();
                        if(numFaceVertices == 4)
                            numQuads++;
                        if(numFaceVertices > 4)
                        {
                            SWARNING <<
                                "More than 4 vertices in face!" <<
                                endl <<
                                endLog;
                            return Action::Continue;

                            // exit(1);
                        }

                        ++fit;
                    }

                    if(numQuads > 0)
                    {
                        SWARNING << "Quad encountered" << endl << endLog;
                    }

                    if(gl_sga->nodeDepth_ > 0)
                    {
                        for(i = 0; i < gl_sga->nodeDepth_; i++)
                        {
                            meshInfo.transf = meshInfo.transf * gl_sga->transf_[i];
                        }
                    }
                    else
                        meshInfo.transf.identity();

                    /* access to vertices */
                    GeoPositions3fPtr   pPos = GeoPositions3fPtr::dcast(geo->getPositions());
                    GeoPositions3f::StoredFieldType * pPosField = pPos->getFieldPtr();

                    /* access to faces */
                    numFaces = 0;
                    fit = geo->beginFaces();
                    for(fit = geo->beginFaces(); fit != geo->endFaces(); ++fit)
                    {
                        numFaceVertices = fit.getLength();

                        for(j = 0; j < numFaceVertices; j++)
                        {
                            vId = fit.getPositionIndex(j);
                        }

                        numFaces++;
                    }                       /* for fit */

                    /* set other mesh attributes */
                    meshInfo.numQuads = numQuads;
                    meshInfo.geoPtr = geo;
                    meshInfo.vPtr = pPosField;
                    meshInfo.triangularFaces = (numQuads == 0);
                    meshInfo.numVertices = pPosField->size();
                    meshInfo.numFaces = numFaces;

                    gl_sga->meshInfo_.push_back(meshInfo);
                    gl_sga->numGeometryNodes_++;
                }
                else
                {
                    //			SWARNING << "Neither triangle nor quad. Field type = " <<
                    //				        (*pTypeField)[h] << endl << endLog;
                }
            }                               /* for h */
        }                                   /* if pLength!=NullFC */
    }
    else if(node->getCore()->getType().isDerivedFrom(Transform::getClassType()))
    {
        TransformPtr    t = TransformPtr::dcast(node->getCore());
        Matrix          ma;

        ma = t->getMatrix();

        SINFO <<
            "Node type derived from transform, skipping children" <<
            endl <<
            endLog;

        for(i = 0; i < 4; i++)
        {
            for(j = 0; j < 4; j++)
            {
                transf[i][j] = ma[j][i];    /* the matrix is stored as columns/rows ? */
            }
        }

        if(gl_sga->nodeDepth_ > gl_sga->maxNodeDepth_)
        {
            gl_sga->maxNodeDepth_ = gl_sga->nodeDepth_;
            gl_sga->transf_.push_back(transf);
        }
        else
        {
            gl_sga->transf_[gl_sga->nodeDepth_] = transf;
        }

        gl_sga->nodeDepth_++;
    }

    return Action::Continue;
}
static void calcVertexNormals(GeometryPtr geo)
{
    GeoNormals3fPtr norms = GeoNormals3fPtr::dcast(geo->getNormals());
    GeoPositions3fPtr pos = GeoPositions3fPtr::dcast(geo->getPositions());

    MFPnt3f *p = pos->getFieldPtr();
    MFVec3f *n = norms->getFieldPtr();

    beginEditCP(norms);

    Vec3f a, b, c;
    int l = 0;
        for(int i=0; i<_size; ++i)
        {
            for(int j=0; j<_size; ++j)
            {
                int m = i*_size+j;

                if (i!=_size-1 && j!=_size-1)
                {
                    a = (*p)[l+m+1] - (*p)[l+m];
                    b = (*p)[l+m+_size] - (*p)[l+m];
                }
                else
                {
                    a = (*p)[l+m-1] - (*p)[l+m];
                    
                    int index = l+m-_size;
                    if(index < 0)
                        index += norms->getSize();
                    
                    b = (*p)[index] - (*p)[l+m];
                }

                c = a.cross(b);
                c.normalize();
        
                if (i==0 && j==_size-1)
                {
                    a = (*p)[l+m-1] - (*p)[l+m];
                    b = (*p)[l+m+_size] - (*p)[l+m];
        
                    c = a.cross(b);
                    c.normalize();
                    c.negate();
                }

                if (i==_size-1 && j==0)
                {
                    a = (*p)[l+m-_size] - (*p)[l+m];
                    b = (*p)[l+m+1] - (*p)[l+m];
        
                    c = a.cross(b);
                    c.normalize();
                }
                (*n)[l+m] = c;
            }
        }
        l += _size*_size;
    endEditCP(norms);
}