示例#1
0
bool Primitive_readLocalData(Input& fr,osg::Geometry& geom)
{
    bool iteratorAdvanced = false;
    bool firstMatched = false;
    if ((firstMatched = fr.matchSequence("DrawArrays %w %i %i %i")) ||
         fr.matchSequence("DrawArrays %w %i %i") )
    {

        GLenum mode;
        Geometry_matchPrimitiveModeStr(fr[1].getStr(),mode);

        int first;
        fr[2].getInt(first);

        int count;
        fr[3].getInt(count);

        int numInstances = 0;
        if (firstMatched)
        {
            fr[4].getInt(numInstances);
            fr += 5;
        }
        else
        {
            fr += 4;
        }

        geom.addPrimitiveSet(new DrawArrays(mode, first, count, numInstances));


        iteratorAdvanced = true;

    }
    else if ((firstMatched = fr.matchSequence("DrawArrayLengths %w %i %i %i {")) ||
         fr.matchSequence("DrawArrayLengths %w %i %i {") )
    {
        int entry = fr[1].getNoNestedBrackets();

        GLenum mode;
        Geometry_matchPrimitiveModeStr(fr[1].getStr(),mode);

        int first;
        fr[2].getInt(first);

        int capacity;
        fr[3].getInt(capacity);

        int numInstances = 0;
        if (firstMatched)
        {
            fr[4].getInt(numInstances);
            fr += 6;
        }
        else
        {
            fr += 5;
        }

        DrawArrayLengths* prim = new DrawArrayLengths;
        prim->setMode(mode);
        prim->setNumInstances(numInstances);
        prim->setFirst(first);
        prim->reserve(capacity);

        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int i;
            if (fr[0].getUInt(i))
            {
                prim->push_back(i);
                ++fr;
            }
        }
         ++fr;

         geom.addPrimitiveSet(prim);

        iteratorAdvanced = true;
    }
    else if ((firstMatched = fr.matchSequence("DrawElementsUByte %w %i %i {")) ||
         fr.matchSequence("DrawElementsUByte %w %i {"))
    {
        int entry = fr[1].getNoNestedBrackets();

        GLenum mode;
        Geometry_matchPrimitiveModeStr(fr[1].getStr(),mode);

        int capacity;
        fr[2].getInt(capacity);

        int numInstances = 0;
        if (firstMatched)
        {
            fr[3].getInt(numInstances);
            fr += 5;
        }
        else
        {
            fr += 4;
        }

        DrawElementsUByte* prim = new DrawElementsUByte;
        prim->setMode(mode);
        prim->setNumInstances(numInstances);
        prim->reserve(capacity);

        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int i;
            if (fr[0].getUInt(i))
            {
                prim->push_back(i);
                ++fr;
            }
        }
         ++fr;

         geom.addPrimitiveSet(prim);

        iteratorAdvanced = true;
    }
    else if ((firstMatched = fr.matchSequence("DrawElementsUShort %w %i %i {")) ||
         fr.matchSequence("DrawElementsUShort %w %i {"))
    {
        int entry = fr[1].getNoNestedBrackets();

        GLenum mode;
        Geometry_matchPrimitiveModeStr(fr[1].getStr(),mode);

        int capacity;
        fr[2].getInt(capacity);

        int numInstances = 0;
        if (firstMatched)
        {
            fr[3].getInt(numInstances);
            fr += 5;
        }
        else
        {
            fr += 4;
        }

        DrawElementsUShort* prim = new DrawElementsUShort;
        prim->setMode(mode);
        prim->setNumInstances(numInstances);
        prim->reserve(capacity);

        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int i;
            if (fr[0].getUInt(i))
            {
                prim->push_back(i);
                ++fr;
            }
        }
         ++fr;

         geom.addPrimitiveSet(prim);

        iteratorAdvanced = true;
    }
    else if ((firstMatched = fr.matchSequence("DrawElementsUInt %w %i %i {")) ||
              fr.matchSequence("DrawElementsUInt %w %i {"))
    {
        int entry = fr[1].getNoNestedBrackets();

        GLenum mode;
        Geometry_matchPrimitiveModeStr(fr[1].getStr(),mode);

        int capacity;
        fr[2].getInt(capacity);

        int numInstances = 0;
        if (firstMatched)
        {
            fr[3].getInt(numInstances);
            fr += 5;
        }
        else
        {
            fr += 4;
        }

        DrawElementsUInt* prim = new DrawElementsUInt;
        prim->setMode(mode);
        prim->setNumInstances(numInstances);
        prim->reserve(capacity);

        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int i;
            if (fr[0].getUInt(i))
            {
                prim->push_back(i);
                ++fr;
            }
        }
         ++fr;

         geom.addPrimitiveSet(prim);

        iteratorAdvanced = true;
    }

    return iteratorAdvanced;
}
示例#2
0
osg::Node *JTOpenPlugin::createShape(JtkShape *partShape, const char *objName)
{
    //cout << "JtkSHAPE\n";

    Geode *geode = new Geode();
    ref_ptr<Geometry> geom = new Geometry();
    StateSet *geoState = geode->getOrCreateStateSet();
    Vec3Array *vert = new Vec3Array;
    Vec3Array *normalArray = new Vec3Array();
    Vec3Array *colorArray = new Vec3Array();
    Vec2Array *tcArray = new Vec2Array();

    DrawArrayLengths *primitives = NULL;
    if (partShape->typeID() == JtkEntity::JtkPOLYGONSET)
    {
        primitives = new DrawArrayLengths(PrimitiveSet::POLYGON);
    }
    else if (partShape->typeID() == JtkEntity::JtkLINESTRIPSET)
    {
        primitives = new DrawArrayLengths(PrimitiveSet::LINE_STRIP);
    }
    else if (partShape->typeID() == JtkEntity::JtkTRISTRIPSET)
    {
        primitives = new DrawArrayLengths(PrimitiveSet::TRIANGLE_STRIP);
    }
    else
    {
        cerr << "unknown partShape->typeID " << partShape->typeID() << endl;
    }
    geode->setName(objName);
    if (primitives)
    {
        for (int set = 0; set < partShape->numOfSets(); set++)
        {
            float *vertex = NULL,
                  *normal = NULL,
                  *color = NULL,
                  *texture = NULL;
            int vertexCount = -1,
                normCount = -1,
                colorCount = -1,
                textCount = -1;

            partShape->getInternal(vertex, vertexCount, normal, normCount,
                                   color, colorCount, texture, textCount, set);

            primitives->push_back(vertexCount);

            // backFaceCulling nur dann, wenn es im CoviseConfig enabled ist
            /*if(backFaceCulling && (mask & Viewer::MASK_SOLID))
         {
         CullFace *cullFace = new CullFace();        // da viele Modelle backface Culling nicht vertragen (nicht richtig modelliert sind)
         cullFace->setMode(CullFace::BACK);
         geoState->setAttributeAndModes(cullFace, StateAttribute::ON);
         }

         // already done in updateMaterial()
         #if 0
         if(Blended)
         {
         BlendFunc *blendFunc = new BlendFunc();
         blendFunc->setFunction(BlendFunc::SRC_ALPHA, BlendFunc::ONE_MINUS_SRC_ALPHA);
         geoState->setAttributeAndModes(blendFunc, StateAttribute::ON);
         #if 1
         AlphaFunc *alphaFunc = new AlphaFunc();
         alphaFunc->setFunction(AlphaFunc::ALWAYS,1.0);
         geoState->setAttributeAndModes(alphaFunc, StateAttribute::OFF);
         #endif
         }
         #endif
         #ifdef HAVE_OSGNV
         if((strncmp(d_currentObject->node->name(),"combineTextures",15)==0)||(strncmp(objName,"combineTextures",15)==0))
         {
         geoState->setAttributeAndModes(combineTextures.get(), StateAttribute::ON);
         }
         if((strncmp(d_currentObject->node->name(),"combineEnvTextures",15)==0)||(strncmp(objName,"combineEnvTextures",15)==0))
         {
         geoState->setAttributeAndModes(combineEnvTextures.get(), StateAttribute::ON);
         }
         #endif*/

            if (vertex && (vertexCount > 0))
            {
                for (int elems = 0; elems < vertexCount; elems++)
                {
                    vert->push_back(Vec3(vertex[elems * 3 + 0], vertex[elems * 3 + 1], vertex[elems * 3 + 2]));
                }
                JtkEntityFactory::deleteMemory(vertex);
            }

            if (normal && (normCount > 0))
            {
                for (int elems = 0; elems < normCount; elems++)
                {
                    normalArray->push_back(Vec3(normal[elems * 3 + 0], normal[elems * 3 + 1], normal[elems * 3 + 2]));
                }
                if (normCount == vertexCount)
                {
                }
                else
                {
                    //geom->setNormalBinding(Geometry::BIND_PER_PRIMITIVE);
                    std::cerr << "JTOpen: normals per primitive not supported" << std::endl;
                }
                JtkEntityFactory::deleteMemory(normal);
            }
            else // generate normals
            {
            }

            if (color && (colorCount > 0))
            {

                for (int elems = 0; elems < colorCount; elems++)
                {
                    colorArray->push_back(Vec3(color[elems * 3 + 0], color[elems * 3 + 1], color[elems * 3 + 2]));
                }

                if (colorCount == vertexCount)
                {
                }
                else
                {
                    //geom->setColorBinding(Geometry::BIND_PER_PRIMITIVE);
                    std::cerr << "JTOpen: colors per primitive not supported" << std::endl;
                }
                JtkEntityFactory::deleteMemory(color);
            }

            if (texture && (textCount > 0))
            {

                for (int elems = 0; elems < textCount; elems++)
                {
                    tcArray->push_back(Vec2(texture[elems * 2 + 0], texture[elems * 2 + 1]));
                }
                JtkEntityFactory::deleteMemory(texture);
            }

            /*   if(!(mask & Viewer::MASK_CONVEX))
         {
         osgUtil::Tesselator *tess = new osgUtil::Tesselator;
         tess->retesselatePolygons(*geom);
         //delete[] tess;
         }*/

            // if enabled, generate tri strips, but not for animated objects
            // if(genStrips && strncmp(objName, "Animated", 8))
            {
                //      d_stripper->stripify(*geom);
            }
        }
        geom->setVertexArray(vert);
        geom->addPrimitiveSet(primitives);
        if (normalArray->size() > 0)
        {
            geom->setNormalArray(normalArray);
            geom->setNormalBinding(Geometry::BIND_PER_VERTEX);
        }
        if (colorArray->size() > 0)
        {
            geom->setColorArray(colorArray);
            geom->setColorBinding(Geometry::BIND_PER_VERTEX);
        }
        if (tcArray->size() > 0)
            geom->setTexCoordArray(0, tcArray);
        if (normalArray->size() == 0)
        {
            osgUtil::SmoothingVisitor::smooth(*(geom.get()), 40.0 / 180.0 * M_PI);
        }
        geode->addDrawable(geom.get());
        geode->setStateSet(geoState);
        return geode;
    }
    return NULL;
}
示例#3
0
Geode *ClipPlanePlugin::loadPlane()
{

    // *5---*6---*7
    // |    |    |
    // *3--------*4
    // |    |    |
    // *0---*1---*2

    float w = cover->getSceneSize() * 0.1; // width of plane

    Vec3Array *lineCoords = new Vec3Array(12);
    (*lineCoords)[0].set(-w, -0.01, -w);
    (*lineCoords)[1].set(w, -0.01, -w);
    (*lineCoords)[2].set(-w, -0.01, 0.0f);
    (*lineCoords)[3].set(w, -0.01, 0.0f);
    (*lineCoords)[4].set(-w, -0.01, w);
    (*lineCoords)[5].set(w, -0.01, w);
    (*lineCoords)[6].set(-w, -0.01, -w);
    (*lineCoords)[7].set(-w, -0.01, w);
    (*lineCoords)[8].set(0.0f, -0.01, -w);
    (*lineCoords)[9].set(0.0f, -0.01, w);
    (*lineCoords)[10].set(w, -0.01, -w);
    (*lineCoords)[11].set(w, -0.01, w);

    DrawArrayLengths *primitives = new DrawArrayLengths(PrimitiveSet::LINE_STRIP);
    for (int i = 0; i < 6; i++)
    {
        primitives->push_back(2);
    }

    Vec3Array *lineColors = new Vec3Array(12);
    for (int i = 0; i < 12; i++)
    {
        (*lineColors)[i].set(Vec3(1.0f, 1.0f, 1.0f));
    }

    Geometry *geoset = new Geometry();
    geoset->setVertexArray(lineCoords);
    geoset->addPrimitiveSet(primitives);
    geoset->setColorArray(lineColors);

    Material *mtl = new Material;
    mtl->setColorMode(Material::AMBIENT_AND_DIFFUSE);
    mtl->setAmbient(Material::FRONT_AND_BACK, Vec4(0.2f, 0.2f, 0.2f, 1.0f));
    mtl->setDiffuse(Material::FRONT_AND_BACK, Vec4(0.9f, 0.9f, 0.9f, 1.0f));
    mtl->setSpecular(Material::FRONT_AND_BACK, Vec4(0.9f, 0.9f, 0.9f, 1.0f));
    mtl->setEmission(Material::FRONT_AND_BACK, Vec4(0.0f, 0.0f, 0.0f, 1.0f));
    mtl->setShininess(Material::FRONT_AND_BACK, 16.0f);

    Geode *geode = new Geode;
    geode->setName("ClipPlane");
    geode->addDrawable(geoset);

    StateSet *geostate = geode->getOrCreateStateSet();
    geostate->setAttributeAndModes(mtl, StateAttribute::ON);
    geostate->setMode(GL_LIGHTING, StateAttribute::OFF);
    LineWidth *lineWidth = new LineWidth(3.0);
    geostate->setAttributeAndModes(lineWidth, StateAttribute::ON);
    geode->setStateSet(geostate);

    return geode;
}
示例#4
0
文件: Normals.cpp 项目: amhagan/osg
void Normals::MakeNormalsVisitor::apply( Geode &geode )
{
    for( unsigned int i = 0; i < geode.getNumDrawables(); i++ )
    {
        Geometry *geom = dynamic_cast<Geometry *>(geode.getDrawable(i));
        if( geom )
        {
            if (geom->containsDeprecatedData()) geom->fixDeprecatedData();
            
            Vec3Array *coords   = dynamic_cast<Vec3Array*>(geom->getVertexArray());
            if( coords == 0L )
                continue;

            Vec3Array *normals  = dynamic_cast<Vec3Array*>(geom->getNormalArray());
            if( normals == 0L )
                continue;

            Geometry::AttributeBinding binding = geom->getNormalBinding();
            if( binding == Geometry::BIND_OFF )
                continue;

            if( binding == Geometry::BIND_OVERALL )
            {
                Vec3 v(0,0,0);
                Vec3 n = normals->front();

                Vec3Array::iterator coord_index = coords->begin();
                while( coord_index != coords->end() )
                  v += *(coord_index++) * _mat;
                v /= (float)(coords->size());

                n *= _normal_scale;
                _local_coords->push_back( v );
                _local_coords->push_back( (v + n));
            }
            else // BIND_PER_PRIMITIVE_SET, BIND_PER_VERTEX
            {
                Geometry::PrimitiveSetList& primitiveSets = geom->getPrimitiveSetList();
                Geometry::PrimitiveSetList::iterator itr;

                Vec3Array::iterator coord_index   = coords->begin();
                Vec3Array::iterator normals_index = normals->begin();

                for(itr=primitiveSets.begin(); itr!=primitiveSets.end(); ++itr)
                {
#ifdef DEBUG
                    _printPrimitiveType( (*itr).get() );
#endif
                    if( binding == Geometry::BIND_PER_PRIMITIVE_SET )
                    {
                        Vec3 v(0,0,0);
                        Vec3 n = *(normals_index++);
                        int ni = (*itr)->getNumIndices();
                        for( int i = 0; i < ni; i++ )
                            v += *(coord_index++) * _mat;
                        v /= (float)(ni);

                        n *= _normal_scale;
                        _local_coords->push_back( v );
                        _local_coords->push_back( (v + n));
                    }
                    else
                    {
                        switch((*itr)->getMode())
                        {
                            case(PrimitiveSet::TRIANGLES):
                            {
                                for( unsigned int j = 0; j < (*itr)->getNumPrimitives(); j++ )
                                {
                                    _processPrimitive( 3, coord_index, normals_index, binding );
                                    coord_index += 3;
                                    normals_index+=3;
                                }
                                break;
                            }
                            case(PrimitiveSet::TRIANGLE_STRIP):
                            {
                                for( unsigned int j = 0; j < (*itr)->getNumIndices()-2; j++ )
                                {
                                    _processPrimitive( 3, coord_index, normals_index, binding );
                                    coord_index++;
                                    normals_index++;
                                }
                                coord_index += 2;
                                if( binding == Geometry::BIND_PER_VERTEX )
                                    normals_index += 2;
                                break;
                            }
                            case(PrimitiveSet::TRIANGLE_FAN):
                                break;

                            case(PrimitiveSet::QUADS):
                            {
                                for( unsigned int j = 0; j < (*itr)->getNumPrimitives(); j++ )
                                {
                                    _processPrimitive( 4, coord_index, normals_index, binding );
                                    coord_index += 4;
                                    normals_index +=4;
                                }
                                break;
                            }
                            case(PrimitiveSet::QUAD_STRIP):
                                break;

                            case(PrimitiveSet::POLYGON):
                            {
                                DrawArrayLengths* dal = dynamic_cast<DrawArrayLengths*>((*itr).get());
                                if (dal) {
                                    for (unsigned int j = 0; j < dal->size(); ++j) {
                                        unsigned int num_prim = (*dal)[j];
                                        //OSG_WARN << "j=" << j << " num_prim=" << num_prim << std::endl;
                                        _processPrimitive(num_prim, coord_index, normals_index, binding);
                                        coord_index += num_prim;
                                        normals_index += num_prim;
                                    }
                                }
                                break;
                            }

                            default:
                                break;
                        }
                    }
                }
            }
        }
    }
    traverse( geode );
}
示例#5
0
文件: Wire.cpp 项目: nixz/covise
void Wire::cutWire(osg::Vec3 normal, float dist)
{

    normal.normalize();

    DrawArrayLengths *newPrimitives = new DrawArrayLengths(PrimitiveSet::POLYGON);
    UShortArray *newIndices = new UShortArray();

    int startIndex = 0;

    int index = vert->getNumElements();
    for (unsigned int i = 0; i < primitives->getNumPrimitives(); i++)
    {
        float oldd = -1;
        int numInd = 0;
        for (int v = 0; v < (*primitives)[i]; v++)
        {
            osg::Vec3 p = (*vert)[indices->index(startIndex + v)];
            float d = (p - normal * dist) * normal;
            /* Grundidee:
			* wir laufen allen Punkte von jedem Polygon einmal
			* durch; liegt der Punkte im "Verbleib" Bereich, wird
			* wird er einfach kopiert, liegt er ausserhalb, dann
			* wird er weggelassen
			* !! Immer wenn ein Wechsel zwischen "verbleibt" und\
			* und "weglassen" erfolgt, dann muss mindestens ein
			* zusaetzlicher Punkt eingefuegt werden.
			*/
            if (v == 0 && d >= 0)
            {
                newIndices->push_back((*indices)[startIndex + v]);
                numInd++;
            }
            else
            {
                if (d < 0)
                {
                    if (oldd > 0)
                    {
                        osg::Vec3 b = (*vert)[(*indices)[startIndex + v - 1]];
                        osg::Vec3 r = p - b;
                        float t = (dist - normal * p) / (normal * r);
                        osg::Vec3 newP = p + r * t;
                        osg::Vec3 n1 = (*normals)[(*indices)[startIndex + v]];
                        osg::Vec3 n2 = (*normals)[(*indices)[startIndex + v - 1]];
                        osg::Vec3 newN = n1 + (n1 - n2) * t;
                        vert->push_back(newP);
                        normals->push_back(newN);
                        newIndices->push_back(index);
                        index++;
                        numInd++;
                    }
                    else
                    {
                    }
                }
                else
                {
                    if (oldd < 0)
                    {
                        osg::Vec3 b = (*vert)[(*indices)[startIndex + v - 1]];
                        osg::Vec3 r = p - b;
                        float t = (dist - normal * p) / (normal * r);
                        osg::Vec3 newP = p + r * t;
                        osg::Vec3 n1 = (*normals)[(*indices)[startIndex + v]];
                        osg::Vec3 n2 = (*normals)[(*indices)[startIndex + v - 1]];
                        osg::Vec3 newN = n1 + (n1 - n2) * t;
                        vert->push_back(newP);
                        normals->push_back(newN);
                        newIndices->push_back(index);
                        index++;
                        numInd++;
                    }

                    newIndices->push_back((*indices)[startIndex + v]);
                    numInd++;
                }
            }
            oldd = d;
        }

        osg::Vec3 p = (*vert)[indices->index(startIndex)];
        float d = (p - normal * dist) * normal;

        if (d < 0)
        {
            if (oldd > 0)
            {
                osg::Vec3 b = (*vert)[(*indices)[startIndex + (*primitives)[i] - 1]];
                osg::Vec3 r = p - b;
                float t = (dist - normal * p) / (normal * r);
                osg::Vec3 newP = p + r * t;
                osg::Vec3 n1 = (*normals)[(*indices)[startIndex]];
                osg::Vec3 n2 = (*normals)[(*indices)[startIndex + (*primitives)[i] - 1]];
                osg::Vec3 newN = n1 + (n1 - n2) * t;
                vert->push_back(newP);
                normals->push_back(newN);
                newIndices->push_back(index);
                index++;
                numInd++;
            }
        }
        else
        {
            if (oldd < 0)
            {
                osg::Vec3 b = (*vert)[(*indices)[startIndex + (*primitives)[i] - 1]];
                osg::Vec3 r = p - b;
                float t = (dist - normal * p) / (normal * r);
                osg::Vec3 newP = p + r * t;
                osg::Vec3 n1 = (*normals)[(*indices)[startIndex]];
                osg::Vec3 n2 = (*normals)[(*indices)[startIndex + (*primitives)[i] - 1]];
                osg::Vec3 newN = n1 + (n1 - n2) * t;
                vert->push_back(newP);
                normals->push_back(newN);
                newIndices->push_back(index);
                index++;
                numInd++;
            }
        }
        newPrimitives->push_back(numInd);
        startIndex += (*primitives)[i];
    }

    geom->setVertexIndices(newIndices);
    geom->setNormalIndices(newIndices);

    geom->removePrimitiveSet(0, geom->getNumPrimitiveSets());
    geom->addPrimitiveSet(newPrimitives);
    primitives = newPrimitives;
    indices = newIndices;

    // calcColors();
    geom->dirtyBound();
    geom->dirtyDisplayList();
}