Exemplo n.º 1
0
Sphere::Sphere( bool fullSphere, byte numSubDiv, float dim )
{
	setPrimitiveType( PrimitiveType::Triangles );
	
	VertexData position;
	buildGeometry( fullSphere, numSubDiv, position, dim );
	
	// Build Texture Coordinates.
	BoundingBox box;
	
	for( size_t i = 0; i < position.size(); i++ )
	{
		const Vector3& v = position[i];
		box.add(v);
	}

	Vector3 center = box.getCenter();

	std::vector<Vector3> texCoords;

	for( size_t i = 0; i < position.size(); i++ )
	{
		const Vector3& vert = position[i];
		Vector3 d = vert-center;
		d.normalize();

		// Conveert to spherical coordinates.
		//float t = d.z / sqrt(d.x*d.x+d.y*d.y+d.z*d.z);
		//float delta = acos(t);
		//float phi = atan2(d.y, d.x);

		//float u = delta / Math::PI;
		//float v = phi / 2*Math::PI;
		float u = std::asin(d.x) / PI + 0.5f;
		float v = std::asin(d.y) / PI + 0.5f;

		texCoords.push_back( Vector2(u, v) );
	}

	gb->set( VertexAttribute::Position, position );
	gb->set( VertexAttribute::TexCoord0, texCoords );
}
Exemplo n.º 2
0
void Sphere::buildGeometry( bool fullSphere, byte numSubDiv, 
							VertexData& pos, float dim)
{
	GeometryBufferPtr gb = AllocateThis(GeometryBuffer);
	setGeometryBuffer(gb);

	// Rotate the vertices, else the sphere is not properly aligned.
	Matrix4x3 rot = Matrix4x3::createRotation( EulerAngles(-60, 0, 0) );

	for( size_t i = 0; i < ARRAY_SIZE(IcoDomeIndices); i++ )
	{
		const byte* p = IcoDomeIndices[i];
	
		Vector3 v1( IcoVertices[p[0]][0], IcoVertices[p[0]][2], IcoVertices[p[0]][1] );
		Vector3 v2( IcoVertices[p[1]][0], IcoVertices[p[1]][2], IcoVertices[p[1]][1] );
		Vector3 v3( IcoVertices[p[2]][0], IcoVertices[p[2]][2], IcoVertices[p[2]][1] );

		subdivide( rot*v1, rot*v2, rot*v3, numSubDiv, pos );
	}

	// If we don't want a full sphere, we return here.
	if( fullSphere )
	{
		// These indices are the bottom of the sphere.
		for( size_t i = 0; i < ARRAY_SIZE(IcoSphereIndices); i++ )
		{
			const byte* p = IcoSphereIndices[i];
			Vector3 v1( IcoVertices[p[0]][0], IcoVertices[p[0]][2], IcoVertices[p[0]][1] );
			Vector3 v2( IcoVertices[p[1]][0], IcoVertices[p[1]][2], IcoVertices[p[1]][1] );
			Vector3 v3( IcoVertices[p[2]][0], IcoVertices[p[2]][2], IcoVertices[p[2]][1] );

			subdivide( rot*v1, rot*v2, rot*v3, numSubDiv, pos );
		}
	}

	// Scale all the vertices.
	for( size_t i = 0; i < pos.size(); i++ )
	{
		Vector3& vec = pos[i];	
		vec *= dim;
	}
}