Esempio n. 1
0
	void _init() {
		DrawElementsUShort* fan = new DrawElementsUShort(PrimitiveSet::TRIANGLE_FAN);
		fan->reserve(m_Segments+2);
		fan->push_back(m_Segments);
		for (int i=0; i < m_Segments; i++) {
			fan->push_back(i);
		}
		fan->push_back(0);
		(*m_Coords)[m_Segments].set(0.0, 0.0, -1000.0);
		addPrimitiveSet(fan);
		setVertexArray(m_Coords);
		setColorArray(m_Colors);
		UShortArray *ci = new UShortArray(m_Segments+1);
		for (unsigned short i=0; i < m_Segments+1; i++) {
			(*ci)[i] = i;
		}
		setColorIndices(ci);
		setColorBinding(Geometry::BIND_PER_VERTEX);
		updateHorizon(1000.0, 120000.0);
		updateFogColor(Vec4(1.0, 1.0, 1.0, 1.0));
	}
Esempio n. 2
0
ref_ptr<PrimitiveSet> VTXReader::processStrip(unsigned short * indexArray,
        std::istream * str,
        int offset)
{
    VTXStrip                strip;
    DrawElementsUShort *    drawElements;
    ref_ptr<PrimitiveSet>   primSet;
    unsigned short *        start;
    unsigned short *        end;

    // Seek to the strip
    str->seekg(offset);

    // Read it.  We have to do this in a kind of screwy way because of the
    // weird byte packing.  Valve uses pragma pack, but we can't do that
    // because it's non-portable.  Of course, I'm assuming a 4-byte alignment
    // here, which might also be non-portable...
    str->read((char *) &strip, VTX_STRIP_SIZE - 8);
    str->read((char *) &strip.num_bone_state_changes, 8);

    // Get the range of indices in question from the strip
    start = &indexArray[strip.index_offset];
    end = &indexArray[strip.index_offset + strip.num_indices];

    // Create the primitive set (based on the flag)
    if (strip.strip_flags & STRIP_IS_TRI_LIST)
        drawElements =
            new DrawElementsUShort(PrimitiveSet::TRIANGLES, start, end);
    else
        drawElements =
            new DrawElementsUShort(PrimitiveSet::TRIANGLE_STRIP, start, end);

    // Flip the indices to get the front faces correct
    std::reverse(drawElements->begin(), drawElements->end());

    // Return the primitive set
    primSet = drawElements;
    return primSet;
}
Esempio n. 3
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;
}
Esempio n. 4
0
File: sky.cpp Progetto: BodyViz/osg
Node *makeSky( void )
{
    int i, j;
    float lev[] = { -5, -1.0, 1.0, 15.0, 30.0, 60.0, 90.0  };
    float cc[][4] =
    {
        { 0.0, 0.0, 0.15 },
        { 0.0, 0.0, 0.15 },
        { 0.4, 0.4, 0.7 },
        { 0.2, 0.2, 0.6 },
        { 0.1, 0.1, 0.6 },
        { 0.1, 0.1, 0.6 },
        { 0.1, 0.1, 0.6 },
    };
    float x, y, z;
    float alpha, theta;
    float radius = 20.0f;
    int nlev = sizeof( lev )/sizeof(float);

    Geometry *geom = new Geometry;

    Vec3Array& coords = *(new Vec3Array(19*nlev));
    Vec4Array& colors = *(new Vec4Array(19*nlev));
    Vec2Array& tcoords = *(new Vec2Array(19*nlev));
    
    
    int ci = 0;

    for( i = 0; i < nlev; i++ )
    {
        for( j = 0; j <= 18; j++ )
        {
            alpha = osg::DegreesToRadians(lev[i]);
            theta = osg::DegreesToRadians((float)(j*20));

            x = radius * cosf( alpha ) * cosf( theta );
            y = radius * cosf( alpha ) * -sinf( theta );
            z = radius * sinf( alpha );

            coords[ci][0] = x;
            coords[ci][1] = y;
            coords[ci][2] = z;

            colors[ci][0] = cc[i][0];
            colors[ci][1] = cc[i][1];
            colors[ci][2] = cc[i][2];
            colors[ci][3] = 1.0;

            tcoords[ci][0] = (float)j/18.0;
            tcoords[ci][1] = (float)i/(float)(nlev-1);

            ci++;
        }


    }

    for( i = 0; i < nlev-1; i++ )
    {
        DrawElementsUShort* drawElements = new DrawElementsUShort(PrimitiveSet::TRIANGLE_STRIP);
        drawElements->reserve(38);

        for( j = 0; j <= 18; j++ )
        {
            drawElements->push_back((i+1)*19+j);
            drawElements->push_back((i+0)*19+j);
        }

        geom->addPrimitiveSet(drawElements);
    }
    
    geom->setVertexArray( &coords );
    geom->setTexCoordArray( 0, &tcoords );

    geom->setColorArray( &colors );
    geom->setColorBinding( Geometry::BIND_PER_VERTEX );


    Texture2D *tex = new Texture2D;
    tex->setImage(osgDB::readImageFile("Images/white.rgb"));

    StateSet *dstate = new StateSet;

    dstate->setTextureAttributeAndModes(0, tex, StateAttribute::OFF );
    dstate->setTextureAttribute(0, new TexEnv );
    dstate->setMode( GL_LIGHTING, StateAttribute::OFF );
    dstate->setMode( GL_CULL_FACE, StateAttribute::ON );
    

    // clear the depth to the far plane.
    osg::Depth* depth = new osg::Depth;
    depth->setFunction(osg::Depth::ALWAYS);
    depth->setRange(1.0,1.0);   
    dstate->setAttributeAndModes(depth,StateAttribute::ON );

    dstate->setRenderBinDetails(-2,"RenderBin");

    geom->setStateSet( dstate );

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

    geode->setName( "Sky" );

    return geode;
}
Esempio n. 5
0
void SkyDomeNode::init( int numWedges, int numSlices, float *sliceAngles )
{
	m_nlev = numSlices;
	m_nseg = numWedges;
	
	int i, j;
	float x, y, z;
	float alpha, theta;

	float radius = SKY_DOME_RADIUS;

	m_Horizon = new FalseHorizon;
#ifdef TEXDOME
	m_Horizon->init(192);
#else
	m_Horizon->init(m_nseg);
#endif
	m_SkyDome = new Geometry;
	m_Colors = new Vec4Array(m_nseg*m_nlev);
	m_TexCoords = new Vec2Array(m_nseg*m_nlev);
	Vec3Array& coords = *(new Vec3Array(m_nseg*m_nlev));
	Vec4Array& colors = *m_Colors;
	Vec2Array& tcoords = *m_TexCoords;
	assert(m_nseg * m_nlev < 65536);
	UShortArray *cindex = new UShortArray(m_nseg*m_nlev);
//FIXME - leak - m_HorizonColors is created, but not attached to the scene
#ifdef TEXDOME
	m_HorizonColors = new Vec4Array(192);
#else
	m_HorizonColors = new Vec4Array(m_nseg);
#endif

	int ci = 0;

	for (i = 0; i < m_nlev; ++i) {
		for(j = 0; j < m_nseg; ++j) {
			alpha = osg::DegreesToRadians(sliceAngles[i]);
			theta = osg::DegreesToRadians((float)(j*360.0/m_nseg));

			float cos_alpha = cosf(alpha);
			float cos_theta = cosf(theta);
			float sin_theta = sinf(theta);
			x = radius * cos_alpha *  cos_theta;
			y = radius * cos_alpha * -sin_theta;
			z = radius * sinf(alpha);

			coords[ci][0] = x;
			coords[ci][1] = y;
			coords[ci][2] = z;

			colors[ci][0] = 1.0;
			colors[ci][1] = 1.0;
			colors[ci][2] = 1.0;
			colors[ci][3] = 1.0;

			tcoords[ci][0] = 0.5f + std::min(0.5f, (90.0f - sliceAngles[i]) / 180.0f) * cos_theta;
			tcoords[ci][1] = 0.5f + std::min(0.5f, (90.0f - sliceAngles[i]) / 180.0f) * sin_theta;

			(*cindex)[ci] = static_cast<unsigned short>(ci);

			++ci;
		}
	}

	DrawElementsUShort* drawElements = new DrawElementsUShort(PrimitiveSet::TRIANGLE_STRIP);
	drawElements->reserve(m_nlev*(m_nseg+1)*2);
	for (i = 0; i < m_nlev-1; ++i) {
		for (j = 0; j <= m_nseg; ++j) {
			drawElements->push_back((i+1)*m_nseg+(j%m_nseg));
			drawElements->push_back((i+0)*m_nseg+(j%m_nseg));
		}
	}

	// XXX enabling/disabling dl/vbo may impact performance
	m_SkyDome->setSupportsDisplayList(false);
	m_SkyDome->setUseDisplayList(false);
	//m_SkyDome->setUseVertexBufferObjects(false);

	m_SkyDome->addPrimitiveSet(drawElements);
	m_SkyDome->setVertexArray(&coords);
	m_SkyDome->setTexCoordArray( 0, m_TexCoords );
	m_SkyDome->setColorArray( m_Colors );
	m_SkyDome->setColorIndices(cindex);
	m_SkyDome->setColorBinding(Geometry::BIND_PER_VERTEX);
	
	if (1) {
		StateSet *dome_state = m_SkyDome->getOrCreateStateSet();

#ifdef TEXDOME
		Image *image = new Image();
		image->allocateImage(64, 64, 1, GL_RGB, GL_UNSIGNED_BYTE);
		assert(image->data() != NULL);
		memset(image->data(), 255, 64*64*3);
		// setting the internal texture format is required, but
		// its not clear how to determine the correct value.  the
		// following is just what the png loader does, setting
		// the format to the number of color bytes.
		image->setInternalTextureFormat(GL_RGB);
		Texture2D *tex = new Texture2D;
		tex->setDataVariance(osg::Object::DYNAMIC);
		tex->setImage(image);
		tex->setWrap(Texture::WRAP_S, Texture::CLAMP);
		tex->setWrap(Texture::WRAP_T, Texture::CLAMP);
		tex->setFilter(Texture::MIN_FILTER, Texture::LINEAR);
		tex->setFilter(Texture::MAG_FILTER, Texture::LINEAR);
		//tex->setFilter(Texture::MIN_FILTER, Texture::NEAREST);
		//tex->setFilter(Texture::MAG_FILTER, Texture::NEAREST);
		tex->setUseHardwareMipMapGeneration(false);
		tex->setUnRefImageDataAfterApply(false);
		tex->setInternalFormatMode(osg::Texture::USE_IMAGE_DATA_FORMAT);
		m_SkyDomeTextureImage = image;
		m_SkyDomeTexture = tex;

		dome_state->setTextureAttributeAndModes(0, m_SkyDomeTexture.get(), StateAttribute::ON);
		dome_state->setTextureAttributeAndModes(0, new TexEnv);
#endif

	}

	osg::Geode *skyDomeGeode = new osg::Geode();
	skyDomeGeode->addDrawable(m_SkyDome.get());
	addChild(skyDomeGeode);
	
	osg::Geode *horizon = new osg::Geode();
//	horizon->addDrawable(m_Horizon.get());
 	// draw horizon after sky, stars, and moon
	addChild(horizon);

	setName("Sky");
}
Esempio n. 6
0
Node *makeTerrain( void )
{
    int m, n;
    int  i, j;
    float dbcenter[3];
    float dbradius;

    getDatabaseCenterRadius( dbcenter, &dbradius );

    m = (sizeof( vertex ) /(sizeof( float[3])))/39;
    n = 39;

    Vec3Array& v    = *(new Vec3Array(m*n));
    Vec2Array& t    = *(new Vec2Array(m*n));
    Vec4Array& col  = *(new Vec4Array(1));

    col[0][0] = col[0][1] = col[0][2] = col[0][3] = 1.0f;

    for( i = 0; i < m * n; i++ )
    {
        float* vc = vertex[i];
        v[i][0] = vc[0] - dbcenter[0];
        v[i][1] = vc[1] - dbcenter[1];
        v[i][2] = vc[2];

        float* tc = texcoord[i];
        t[i][0] = tc[0];
        t[i][1] = tc[1];
    }

    Geometry *geom = new Geometry;

    geom->setVertexArray( &v );
    geom->setTexCoordArray( 0, &t );

    geom->setColorArray( &col, Array::BIND_OVERALL );

    for( i = 0; i < m-2; i++ )
    {
        DrawElementsUShort* elements = new DrawElementsUShort(PrimitiveSet::TRIANGLE_STRIP);
        elements->reserve(39*2);
        for( j = 0; j < n; j++ )
        {
            elements->push_back((i+0)*n+j);
            elements->push_back((i+1)*n+j);
        }
        geom->addPrimitiveSet(elements);
    }


    Texture2D *tex = new Texture2D;

    tex->setImage(osgDB::readRefImageFile("Images/lz.rgb"));

    StateSet *dstate = new StateSet;
    dstate->setMode( GL_LIGHTING, StateAttribute::OFF );
    dstate->setTextureAttributeAndModes(0, tex, StateAttribute::ON );
    dstate->setTextureAttribute(0, new TexEnv );

    geom->setStateSet( dstate );

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

    return geode;
}