示例#1
0
osg::Object *RefosgBillboard::getChildObject(unsigned i) {
	if (i < 1) {
		return _object->getUpdateCallback();
	} else {
		i -= 1;
	}
	if (i < 1) {
		return _object->getStateSet();
	} else {
		i -= 1;
	}
	if (i < 1) {
		return _object->getEventCallback();
	} else {
		i -= 1;
	}
	if (i < _object->getNumDrawables()) {
		return _object->getDrawable(i);
	} else {
		i -= _object->getNumDrawables();
	}
	if (i < 1) {
		return _object->getCullCallback();
	} else {
		i -= 1;
	}
	throw std::out_of_range("child");
}
void osgMain::createPipe(osg::Vec3 & pt1, osg::Vec3 & pt2, osg::Vec3 & pt3, osg::Vec3Array* shape,bool startPt,osg::ref_ptr<osg::Geode>  & currentLine)
{
	__android_log_print(ANDROID_LOG_ERROR,"jni client","create pipe");
    //shape must be at xy plane
    if(shape->size()<3)
    {
    	__android_log_print(ANDROID_LOG_ERROR,"jni server","create pipe NOOOO");
    	return ;
    }

    //currentLine->addDrawable(new osg::ShapeDrawable(new osg::Sphere(pt3,1.0f)));
    //return;
    osg::Vec3 v12=(pt1-pt2);
    v12.normalize();
    osg::Vec3 v32=(pt3-pt2);
    v32.normalize();
    osg::ref_ptr<osg::Vec3Array> startShape;
    osg::Vec3 center(0,0,0);
    for(int i=0;i<shape->size();i++)
        center=center+shape->at(i);
    center=center/(float)shape->size();
    __android_log_print(ANDROID_LOG_ERROR,"jni client","create pipe OK1");
    if(startPt)
    {
    	 __android_log_print(ANDROID_LOG_ERROR,"jni client","create pipe OK2");
        startShape=new osg::Vec3Array;

        osg::Vec3 posZ(0.0,0.0,1.0);
        osg::Vec3 rotAxis=posZ^(-v12);
        if(rotAxis.length2()<0.01f)
            return ;
        float rotAngle=(float)acos((double)(posZ*(-v12)));
        osg::Matrixd mat;
        mat.makeRotate(rotAngle,rotAxis);
        for(int i=0;i<shape->size();i++)
            startShape->push_back(mat.preMult(shape->at(i)-center)+pt1);
        __android_log_print(ANDROID_LOG_ERROR,"jni client","create pipe OK3");
    }
    else
        startShape=shape;

    osg::Vec3 p2Dir;
    osg::Vec3 half=(v12+v32)/2.0f;
    half.normalize();
    osg::Vec3 p2DirOrtho=v12^half;
    p2DirOrtho.normalize();
    p2Dir=p2DirOrtho^half;
    p2Dir.normalize();

    osg::Vec3 shapeDir=(startShape->at(2)-startShape->at(1))^(startShape->at(0)-startShape->at(1));
    shapeDir.normalize();
    if(shapeDir*(-v12)<0.0)
        shapeDir=-shapeDir;
    __android_log_print(ANDROID_LOG_ERROR,"jni client","create pipe OK3");
    osg::ref_ptr<osg::Vec3Array> p2Shape=new osg::Vec3Array;
    osg::Vec3 rotAxis=shapeDir^p2Dir;
    float rotAngle=(float)acos((double)(shapeDir*p2Dir));
    osg::Matrixd mat;
    mat.makeRotate(rotAngle,rotAxis);
    osg::Vec3 trans=pt2-pt1;
    center.set(0,0,0);
    for(int i=0;i<startShape->size();i++)
        center=center+startShape->at(i);
    center=center/(float)startShape->size();
    for(int i=0;i<startShape->size();i++)
        p2Shape->push_back(mat.preMult(startShape->at(i)-center)+center+trans);

    __android_log_print(ANDROID_LOG_ERROR,"jni client","create pipe OK4");
    osg::ref_ptr<osg::Geometry> pipe=new osg::Geometry;

    osg::ref_ptr<osg::Vec3Array> allVerts=new osg::Vec3Array;
    int numVerts=p2Shape->size();
    for(unsigned int i=0;i<numVerts;i++)
    {
        allVerts->push_back(startShape->at(i));
        allVerts->push_back(startShape->at((i+1)%numVerts));
        allVerts->push_back(p2Shape->at(i));
        allVerts->push_back(startShape->at((i+1)%numVerts));
        allVerts->push_back(p2Shape->at((i+1)%numVerts));
        allVerts->push_back(p2Shape->at(i));
    }
    __android_log_print(ANDROID_LOG_ERROR,"jni client","create pipe OK5");
    pipe->setVertexArray(allVerts.get());

    osg::ref_ptr<osg::DrawArrays> array=new osg::DrawArrays(GL_TRIANGLES,0,allVerts->size());
    pipe->addPrimitiveSet(array);
    osgUtil::SmoothingVisitor::smooth(*pipe);
    for(int i=0;i<numVerts;i++)
        shape->at(i)=p2Shape->at(i);
    __android_log_print(ANDROID_LOG_ERROR,"jni client","create pipe OK6");
    if(startPt)
    {
        //currentLine=new osg::Geode();
        currentLine->addDrawable(pipe);
    }
    else
    {
         osgUtil::Optimizer::MergeGeometryVisitor::mergeGeometry(*currentLine->getDrawable(0)->asGeometry(),*pipe);
    }
    //return pipe.release();
    __android_log_print(ANDROID_LOG_ERROR,"jni client","create pipe finish");

}