Esempio n. 1
0
/** This creates the wireframe PickBox around the widget.
  Volume vertex names:
  <PRE>
      4____ 7        y
     /___ /|         |
   0|   3| |         |___x
    | 5  | /6       /
    |/___|/        z
    1    2
  </PRE>
*/
Geometry *PickBox::createWireframe(const Vec4 &color)
{
    Geometry *geom = new Geometry();

    updateVertices(geom);

    // Set colors:
    Vec4Array *colors = new Vec4Array();
    colors->push_back(color);
    geom->setColorArray(colors);
    geom->setColorBinding(Geometry::BIND_OVERALL);

    // Set normals:
    Vec3Array *normals = new Vec3Array();
    normals->push_back(Vec3(0.0f, 0.0f, 1.0f));
    geom->setNormalArray(normals);
    geom->setNormalBinding(Geometry::BIND_OVERALL);

    // This time we simply use primitive, and hardwire the number of coords
    // to use since we know up front:
    geom->addPrimitiveSet(new DrawArrays(PrimitiveSet::LINES, 0, 24));

    geom->setUseDisplayList(false); // allow dynamic changes

    return geom;
}
Esempio n. 2
0
Geode* ChessUtils::createRectangleWithTexture(Vec3 centerPosition, Image* image, int width, int height, Vec4 color) {
	int halfWidth = width / 2;
	int halfHeight = height / 2;

	Vec3Array* vertices = new Vec3Array();	
	vertices->push_back(Vec3(centerPosition.x() - halfWidth, centerPosition.y() - halfHeight, centerPosition.z()));
	vertices->push_back(Vec3(centerPosition.x() + halfWidth, centerPosition.y() - halfHeight, centerPosition.z()));
	vertices->push_back(Vec3(centerPosition.x() + halfWidth, centerPosition.y() + halfHeight, centerPosition.z()));
	vertices->push_back(Vec3(centerPosition.x() - halfWidth, centerPosition.y() + halfHeight, centerPosition.z()));

	Vec3Array* normals = new Vec3Array();
	normals->push_back(Vec3(0.0f, 0.0f, 1.0f));

	Vec2Array* texcoords = new Vec2Array();
	texcoords->push_back(Vec2(0.0f, 0.0f));
	texcoords->push_back(Vec2(1.0f, 0.0f));
	texcoords->push_back(Vec2(1.0f, 1.0f));
	texcoords->push_back(Vec2(0.0f, 1.0f));

	Vec4Array* colors = new Vec4Array();
	colors->push_back(color);

	Geometry* quad = new Geometry();
	quad->setVertexArray(vertices);
	quad->setNormalArray(normals);
	quad->setNormalBinding(osg::Geometry::BIND_OVERALL);	
	quad->setColorArray(colors);
	quad->setColorBinding(osg::Geometry::BIND_OVERALL);
	quad->setTexCoordArray(0, texcoords);
	quad->addPrimitiveSet(new osg::DrawArrays(GL_QUADS, 0, 4));

	Texture2D* texture = new Texture2D();
	if (image != NULL) {
		texture->setImage(image);
	}			

	Geode* geode = new Geode();
	geode->addDrawable(quad);

	osg::BlendFunc* blendFunc = new osg::BlendFunc();
	blendFunc->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	osg::TexEnv* blendTexEnv = new osg::TexEnv();
	blendTexEnv->setMode(osg::TexEnv::BLEND);

	osg::StateSet* geodeStateset = geode->getOrCreateStateSet();
	geodeStateset->setAttributeAndModes(blendFunc);
	geodeStateset->setTextureAttribute(0, blendTexEnv);
	geodeStateset->setTextureAttributeAndModes(0, texture);
	geodeStateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
	
	return geode;
}
Esempio n. 3
0
Hyperspace::Hyperspace()
{
	setGlows(false);
	//start by creating a bunch of "stars"
	int stars = 2500;
	osg::Geode* geode = new Geode();
	Geometry* geom = new Geometry;
	geode->addDrawable(geom);
	Vec4Array* colors = new Vec4Array();
	colors->push_back(Vec4(1, 1, 1, 1));
	geom->setColorArray(colors);
	geom->setColorBinding(Geometry::BIND_OVERALL);
	Vec3Array* verts = new Vec3Array();
	geom->setVertexArray(verts);
	geom->addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLES, 0, stars*3));
	geom->getOrCreateStateSet()->setMode(GL_LIGHTING, false);
	for(int i  = 0; i < stars; i++)
	{
		float radius = Util::random(10, 150);
		float theta = 6.28 * Util::loggedRandom("Hyperspace theta") / RAND_MAX;
		float s = cosf(theta);
		float c = sinf(theta);
		Vec3 localUp(-s, c, 0);
		float width = Util::random(0.1, 0.25) * radius / 25;;
		float length = Util::random(10, 30) * radius / 20;
		float z = 0;
		Vec3 basePos(c*radius, s*radius, z+length*0.5);
		Vec3 zDir(0, 0, 1);
		verts->push_back(basePos + zDir * length * 0.5 + localUp * width * 0);
//		verts->push_back(basePos + zDir * length * 0.5 - localUp * width * 0.5);
		verts->push_back(basePos - zDir * length * 0.5 - localUp * width * 0.5);
		verts->push_back(basePos - zDir * length * 0.5 + localUp * width * 0.5);
	}
	mPat->setPosition(Vec3(0, 0, -250));
	mPat->addChild(geode);
	mHSTime = -1;
	update(0);
	
}
Esempio n. 4
0
Array* Array_readLocalData(Input& fr)
{
    if (fr[0].matchWord("Use"))
    {
        if (fr[1].isString())
        {
            Object* obj = fr.getObjectForUniqueID(fr[1].getStr());
            if (obj)
            {
                fr+=2;
                return dynamic_cast<Array*>(obj);
            }
        }

        osg::notify(osg::WARN)<<"Warning: invalid uniqueID found in file."<<std::endl;
        return NULL;
    }

    std::string uniqueID;
    if (fr[0].matchWord("UniqueID") && fr[1].isString())
    {
        uniqueID = fr[1].getStr();
        fr += 2;
    }


    int entry = fr[0].getNoNestedBrackets();

    const char* arrayName = fr[0].getStr();

    unsigned int capacity = 0;
    fr[1].getUInt(capacity);
    ++fr;

    fr += 2;


    Array* return_array = 0;

    if (strcmp(arrayName,"ByteArray")==0)
    {
        ByteArray* array = new ByteArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            int int_value;
            if (fr[0].getInt(int_value))
            {
                ++fr;
                array->push_back(int_value);
            }
            else ++fr;
        }
        ++fr;

        return_array = array;
    }
    else if (strcmp(arrayName,"ShortArray")==0)
    {
        ShortArray* array = new ShortArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            int int_value;
            if (fr[0].getInt(int_value))
            {
                ++fr;
                array->push_back(int_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"IntArray")==0)
    {
        IntArray* array = new IntArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            int int_value;
            if (fr[0].getInt(int_value))
            {
                ++fr;
                array->push_back(int_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"UByteArray")==0)
    {
        UByteArray* array = new UByteArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int uint_value;
            if (fr[0].getUInt(uint_value))
            {
                ++fr;
                array->push_back(uint_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"UShortArray")==0)
    {
        UShortArray* array = new UShortArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int uint_value;
            if (fr[0].getUInt(uint_value))
            {
                ++fr;
                array->push_back(uint_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"UIntArray")==0)
    {
        UIntArray* array = new UIntArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int uint_value;
            if (fr[0].getUInt(uint_value))
            {
                ++fr;
                array->push_back(uint_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"UVec4bArray")==0 || strcmp(arrayName,"Vec4ubArray")==0)
    {
        Vec4ubArray* array = new Vec4ubArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g,b,a;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g) &&
                fr[2].getUInt(b) &&
                fr[3].getUInt(a))
            {
                fr+=4;
                array->push_back(osg::Vec4ub(r,g,b,a));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"FloatArray")==0)
    {
        FloatArray* array = new FloatArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            float float_value;
            if (fr[0].getFloat(float_value))
            {
                ++fr;
                array->push_back(float_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"DoubleArray")==0)
    {
        DoubleArray* array = new DoubleArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            double double_value;
            if (fr[0].getFloat(double_value))
            {
                ++fr;
                array->push_back(double_value);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec2Array")==0)
    {
        Vec2Array* array = new Vec2Array;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            Vec2 v;
            if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()))
            {
                fr += 2;
                array->push_back(v);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec2dArray")==0)
    {
        Vec2dArray* array = new Vec2dArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            Vec2d v;
            if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()))
            {
                fr += 2;
                array->push_back(v);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec3Array")==0)
    {
        Vec3Array* array = new Vec3Array;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            Vec3 v;
            if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z()))
            {
                fr += 3;
                array->push_back(v);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec3dArray")==0)
    {
        Vec3dArray* array = new Vec3dArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            Vec3d v;
            if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z()))
            {
                fr += 3;
                array->push_back(v);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec4Array")==0)
    {
        Vec4Array* array = new Vec4Array;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            Vec4 v;
            if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z()) && fr[3].getFloat(v.w()))
            {
                fr += 4;
                array->push_back(v);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec4dArray")==0)
    {
        Vec4dArray* array = new Vec4dArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            Vec4d v;
            if (fr[0].getFloat(v.x()) && fr[1].getFloat(v.y()) && fr[2].getFloat(v.z()) && fr[3].getFloat(v.w()))
            {
                fr += 4;
                array->push_back(v);
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec2bArray")==0)
    {
        Vec2bArray* array = new Vec2bArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g))
            {
                fr+=2;
                array->push_back(osg::Vec2b(r,g));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec3bArray")==0)
    {
        Vec3bArray* array = new Vec3bArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g,b;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g) &&
                fr[2].getUInt(b))
            {
                fr+=3;
                array->push_back(osg::Vec3b(r,g,b));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec4bArray")==0)
    {
        Vec4bArray* array = new Vec4bArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g,b,a;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g) &&
                fr[2].getUInt(b) &&
                fr[3].getUInt(a))
            {
                fr+=4;
                array->push_back(osg::Vec4b(r,g,b,a));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec2sArray")==0)
    {
        Vec2sArray* array = new Vec2sArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g))
            {
                fr+=2;
                array->push_back(osg::Vec2s(r,g));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec3sArray")==0)
    {
        Vec3sArray* array = new Vec3sArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g,b;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g) &&
                fr[2].getUInt(b))
            {
                fr+=3;
                array->push_back(osg::Vec3s(r,g,b));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }
    else if (strcmp(arrayName,"Vec4sArray")==0)
    {
        Vec4sArray* array = new Vec4sArray;
        array->reserve(capacity);
        while (!fr.eof() && fr[0].getNoNestedBrackets()>entry)
        {
            unsigned int r,g,b,a;
            if (fr[0].getUInt(r) &&
                fr[1].getUInt(g) &&
                fr[2].getUInt(b) &&
                fr[3].getUInt(a))
            {
                fr+=4;
                array->push_back(osg::Vec4s(r,g,b,a));
            }
            else ++fr;
        }
        ++fr;
        return_array = array;
    }

    if (return_array)
    {
        if (!uniqueID.empty()) fr.registerUniqueIDForObject(uniqueID.c_str(),return_array);
    }

    return return_array;
}
   void operator ()( const osg::Vec3& s1, const osg::Vec3& s2,
                           const osg::Vec3& s3, bool treatVertexDataAsTemporary )
   {

      Vec3 v1 = s1;
      Vec3 v2 = s2;
      Vec3 v3 = s3;
      if( _matrix ) {
            v1 = v1 * *_matrix;
            v2 = v2 * *_matrix;
            v3 = v3 * *_matrix;
      }
            
      if(_mode == ShadowVolumeGeometryGenerator::CPU_RAW || _mode == ShadowVolumeGeometryGenerator::CPU_SILHOUETTE){
         Vec3 n;
         if(_fronface == ShadowVolumeGeometryGenerator::CCW)
            n = (v2-v1) ^ (v3-v1);          // normal of the face
         else
            n = (v3-v1) ^ (v2-v1);          // normal of the face
         Vec3 lp3 = toVec3( _lightPos );       // lightpos converted to Vec3

         /** 
          * Is the current triangle facing to the light in given vertex ordering.
          * Dot product of normal with to-light vector. The normal is in given CW or CCW order.
          * The > sign means, that when the face is parallel to light ( the normal is orthogonal) the
          * face is considered back face (otherwise there would be >=). This helps with objects with
          * holes (non-solid) so when we are computing silhouette in z-fail we are not making light
          * caps out of it (causing problems with directional light).
          */
         bool front = ( n * ( lp3-v1 * _lightPos.w() ) ) > 0;

         //if(_castface = ShadowVolumeGeometryGenerator::BACK)
         //   front = !front;
         if(_mode == ShadowVolumeGeometryGenerator::CPU_RAW && !front
            && (_castface == ShadowVolumeGeometryGenerator::FRONT_AND_BACK || _castface == ShadowVolumeGeometryGenerator::BACK)
           )
         {
            Vec3 tmp = v1;
            v1 = v2;
            v2 = tmp;
            //return;
         }
         //in case of not shadow casting face in CPU_RAW
         else if(_mode == ShadowVolumeGeometryGenerator::CPU_RAW && 
                  (
                     (!front && _castface == ShadowVolumeGeometryGenerator::FRONT) ||
                     ( front && _castface == ShadowVolumeGeometryGenerator::BACK)
                  )
                )
         {
            return; //the triangle is not shadow casting face
         }
         //In z-fail silhouette case, we need to generate caps now becuse there is no algorithm for
         //creating it from silhouette, and it is impossible in case of light cap. Light cap need
         //to be the actual geometry.
         else if( _mode == ShadowVolumeGeometryGenerator::CPU_SILHOUETTE
               && _method == ShadowVolumeGeometryGenerator::ZFAIL
               && _caps_vert != NULL
               //now we are sure we need caps, so we need to generate them from shadowcasting faces, right?
               &&(
                     ( front && _castface == ShadowVolumeGeometryGenerator::FRONT)
                  || (!front && _castface == ShadowVolumeGeometryGenerator::BACK)
                 )
               )
         {
            //notify(NOTICE)<<"ADD CAP"<<std::endl;
            Vec4 t1(v1,1.0);
            Vec4 t2(v2,1.0);
            Vec4 t3(v3,1.0);
            _caps_vert->push_back(t1);
            _caps_vert->push_back(t2);
            _caps_vert->push_back(t3);

            _caps_col->push_back(Vec4(1.0,0.0,0.0,1.0));
            _caps_col->push_back(Vec4(1.0,0.0,0.0,1.0));
            _caps_col->push_back(Vec4(1.0,0.0,0.0,1.0));

            /* dark caps (lame) */
            Vec4 t0inf = projectToInf(t1, _lightPos);
            Vec4 t1inf = projectToInf(t2, _lightPos);
            Vec4 t2inf = projectToInf(t3, _lightPos);

            _caps_vert->push_back(t0inf);
            _caps_vert->push_back(t2inf);
            _caps_vert->push_back(t1inf);

            _caps_col->push_back(Vec4(0.0,0.0,1.0,1.0));
            _caps_col->push_back(Vec4(0.0,0.0,1.0,1.0));
            _caps_col->push_back(Vec4(0.0,0.0,1.0,1.0));
         }
      }

      // transform vertices
      Vec4 t1( v1,1.);
      Vec4 t2( v2,1.);
      Vec4 t3( v3,1.);
            
      // skip if not front-facing the light
      //Vec3 n = (t2-t1) ^ (t3-t1);          // normal of the face
      //Vec3 lp3 = toVec3( _lightPos );       // lightpos converted to Vec3
      //bool front = ( n * ( lp3-t1 ) ) >= 0; // dot product
      //if( !front )
      //   return;

      // final vertices
      /*Vec4 f1( t1, 1. );
      Vec4 f2( t2, 1. );
      Vec4 f3( t3, 1. );*/
      // store vertices

      _data->push_back( t1 );
      _data->push_back( t2 );
      _data->push_back( t3 );
   }