Cube to Sphere

August 27, 2011

I know my progress is slow, job, education, family, other hobbies, etc. I did manage to create a 3D cube and convert it into a sphere this week. The tried and true technique is to start with the 6 faces of a cube.

In XNA you create each face with 2 triangles. Converting that to a sphere is somewhat difficult in XNA. My solution was to use a quadtree for each face, then modify the vertices to rough out the sphere shape. The deeper the quadtree the better the sphere approximation. The end result seems to roughly resemble tessellation, but requires a lot of work.

With SlimDX Direct3d11 you create each face with a single quad using the PatchListWith4ControlPoints PrimitiveTopology. I modified the domain shader to reposition the vertex position based on a sphere.

	float2 UV : SV_DomainLocation, 
	const OutputPatch<HS_OUTPUT, 4> quad ) 
	DS_OUTPUT Output; 
	float3 verticalPos1 = lerp(quad[0].vPosition,quad[3].vPosition,UV.y);
	float3 verticalPos2 = lerp(quad[1].vPosition,quad[2].vPosition,UV.y);
	float3 finalPos = lerp(verticalPos1,verticalPos2,UV.x);

	float3 norm = normalize(finalPos);
	float3 spherePos = norm * g_fRadius;

	Output.vPosition = mul (float4(spherePos, 1), mul(g_mWorld, g_mViewProjection));

	float4 verticalColor1 = lerp(quad[0].vColor, quad[1].vColor, UV.y);
	float4 verticalColor2 = lerp(quad[3].vColor, quad[2].vColor, UV.y);
	Output.vColor =lerp(verticalColor1,verticalColor2,UV.x); 
	return Output; 

The tessellation is limited to a factor of 64, so I suspect the quadtree solution will need to be revisited. There is a possibility I will try some voxel terrain, but I’m not quite done with my initial foyer into DirectX 11.

2 Responses to “Cube to Sphere”

  1. Hi,

    I see that you use CSG library for XNA in youtube. This library is open-source or similar? Because I am designing my Final Studies Project, and I need a CSG library for XNA…

    A lot of thanks!

  2. I do not use a CSG library. I create all the CSG routines myself. This particular post about “Cube to Sphere” is an example of DirectX 11 and the Hull Shader. It has nothing to do with CSG.

Leave a Reply

You must be logged in to post a comment.