# Cube2Sphere

## Recommended Posts

montify    697
Hello everyone.

Im working on a Sphere out of 6 Planes...

But i have no Idea how to Implemet it.

Here's little Math: [url="http://mathproofs.blogspot.co.at/2005/07/mapping-cube-to-sphere.html"]http://mathproofs.bl...-to-sphere.html[/url]

And here is my Code for the Vertex Plane (at the moment only one Plane)

[source lang="csharp"] vertices = new VertexPositionColor[256 * 256];
int index = 0;

for (int z = 0; z < 256; z++)
{
for (int x = 0; x < 256; x++)
{
vertices[z * 256 + x].Position.X = x * 90.0f;
vertices[z * 256 + x].Position.Y = 3;
vertices[z * 256 + x].Position.Z = z * 90.0f;
vertices[z * 256 + x].Color = Color.White;

}
vertexBuffer = new VertexBuffer(gDevice, typeof(VertexPositionColor), 256 * 256, BufferUsage.WriteOnly);
this.vertexBuffer.SetData<VertexPositionColor>(vertices);
}[/source]

next, the IndexBuffer

[source lang="csharp"] int[] indices = new int[255 * 255 * 6];
index = 0;

for (int z = 0; z < 255; z++)
{
for (int x = 0; x < 255; x++)
{
indices[index + 0] = z * 256 + x;
indices[index + 1] = indices[index + 0] + 256 + 1;
indices[index + 2] = indices[index + 0] + 256;
indices[index + 3] = indices[index + 0];
indices[index + 4] = indices[index + 0] + 1;
indices[index + 5] = indices[index + 0] + 256 + 1;

index += 6;
}
}

this.indexBuffer = new IndexBuffer(gDevice , IndexElementSize.ThirtyTwoBits, 255 * 255 * 6, BufferUsage.WriteOnly);
this.indexBuffer.SetData<int>(indices);[/source]

so now the Math says for the X Axis:

[source lang="csharp"](Math.Sqrt(1 - (Math.Pow(vertices[i].Position.Y, 2) / 2)- (Math.Pow(vertices[i].Position.Z, 2) / 2)+ ((Math.Pow(vertices[i].Position.Y, 2) * Math.Pow(vertices[i].Position.Z, 2)) / 3)))[/source]

But how i can Implement this in my Code?

##### Share on other sites
montify    697
Hello

I started a new experiment

[img]https://dl.dropbox.com/u/19373509/c2s/e.png[/img]

What im doing wrong?

VertexBuffer:
[source lang="csharp"]int index = 0;

vertices = new VertexPositionColor[256* 256];
int xA = 0;
int yA = 0;
int scale = 100;
double z = 1.0f;

for (double x = -1.0f; x <= 1.0f; x = x + 0.25f)
{
for (double y = -1.0f; y <= 1.0f; y = y + 0.25f)
{
double xsphere = scale * x * Math.Sqrt(1 - (Math.Pow(y, 2) / 2) - (Math.Pow(z, 2) / 2) + ((Math.Pow(y, 2) * Math.Pow(z, 2)) / 3));
double ysphere = scale * y * Math.Sqrt(1 - (Math.Pow(z, 2) / 2) - (Math.Pow(x, 2) / 2) + ((Math.Pow(z, 2) * Math.Pow(x, 2)) / 3));
double zsphere = scale * z * Math.Sqrt(1 - (Math.Pow(x, 2) / 2) - (Math.Pow(y, 2) / 2) + ((Math.Pow(x, 2) * Math.Pow(y, 2)) / 3));

vertices[xA + yA * 9].Position = new Vector3((float)xsphere, (float)ysphere, (float)zsphere);
vertices[xA + yA * 9].Color = Color.White;
yA += 1;

}
yA = 0;

xA = xA + 1;
}
vertexBuffer = new VertexBuffer(gDevice, typeof(VertexPositionColor), 256 * 256, BufferUsage.WriteOnly);
this.vertexBuffer.SetData<VertexPositionColor>(vertices); [/source]

IndexBuffer:

[source lang="csharp"]#region IndexBuffer
index = 0;

for (int a = 0; z < 255; z++)
{
for (int x = 0; x < 255; x++)
{
indices[index + 0] = a * 256 + x;
indices[index + 1] = indices[index + 0] + 256 + 1;
indices[index + 2] = indices[index + 0] + 256;
indices[index + 3] = indices[index + 0];
indices[index + 4] = indices[index + 0] + 1;
indices[index + 5] = indices[index + 0] + 256 + 1;

index += 6;
}
}

this.indexBuffer = new IndexBuffer(gDevice, IndexElementSize.ThirtyTwoBits, 255 * 255 * 6, BufferUsage.WriteOnly);
this.indexBuffer.SetData<int>(indices);
#endregion [/source]

##### Share on other sites
montify    697
Solved!
[img]https://dl.dropbox.com/u/19373509/jaa.png[/img]

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account