**0**

# Sphere Drawing with SlimDx ( dx11)

###
#1
Members - Reputation: **208**

Posted 13 April 2012 - 10:36 AM

I will really appreciate if somebody can find problem with this code ...

I am attaching a link for output sphere image ...

// Taking radious & Tesselation veriables as input ,, output image is with r-15 and t-20 ...

BufferDescription vertexBuffDesc, indexBuffDesc;

vertexCount = ((int)tesselation + 1) * 6;

indexCount = vertexCount*4*3;

VertexPositionColor [] vertices = new VertexPositionColor[vertexCount];

int[] indices = new int[indexCount];

// compute our step around each circle

float step = ((float)System.Math.PI*2) / tesselation;

// used to track the index into our vertex array

int index = 0;

//create the loop on the XY plane first

for (float angle = 0f; angle < (float)(System.Math.PI * 2); angle += step)

{

vertices[index++] = new VertexPositionColor(new Vector3((float)System.Math.Cos(angle), (float)System.Math.Sin(angle), 0f), new Vector4(0f, 1f, 0f, 0f));

vertices[index++] = new VertexPositionColor(new Vector3((float)System.Math.Cos(angle + step), (float)System.Math.Sin(angle + step), 0f), new Vector4(0f, 0f, 1f, 0f));

}

//next on the XZ plane

for (float angle = 0f; angle < (float)(System.Math.PI * 2); angle += step)

{

vertices[index++] = new VertexPositionColor(new Vector3((float)System.Math.Cos(angle), 0f, (float)System.Math.Sin(angle)), new Vector4(1f, 0f, 0f, 0f));

vertices[index++] = new VertexPositionColor(new Vector3((float)System.Math.Cos(angle + step), 0f, (float)System.Math.Sin(angle + step)), new Vector4(0f, 0f, 1f, 1f));

}

//finally on the YZ plane

for (float angle = 0f; angle < (float)(System.Math.PI*2); angle += step)

{

vertices[index++] = new VertexPositionColor(new Vector3(0f, (float)System.Math.Cos(angle), (float)System.Math.Sin(angle)), new Vector4(0f, 0f, 0f, 1f));

vertices[index++] = new VertexPositionColor(new Vector3(0f, (float)System.Math.Cos(angle + step), (float)System.Math.Sin(angle + step)), new Vector4(1f, 1f, 0f, 0f));

}

int six = 0;

float slices = (float)tesselation;

float stacks = (float)radious;

for (ushort slice = 0; slice < slices; ++slice)

{

for (float stack = 0; stack < stacks; ++stack)

{

int v = (ushort)(slice * (stacks + 1) + stack);

indices[six++] = v;

indices[six++] = (int)(v + 1);

indices[six++] = (int)(v + (stacks + 1));

indices[six++] = (int)(v + (stacks + 1));

indices[six++] = (int)(v + 1);

indices[six++] = (int)(v + (stacks + 1) + 1);

}

}

DataStream vertexDataStream = new DataStream(vertices, true, true);

vertexDataStream.Position = 0;

DataStream indexDataStream = new DataStream(indices, true, true);

indexDataStream.Position = 0;

// vertex buffer initialization

vertexBuffDesc = new BufferDescription

{

Usage = ResourceUsage.Default,

SizeInBytes = System.Runtime.InteropServices.Marshal.SizeOf(typeof(VertexPositionColor)) * vertexCount,

BindFlags = BindFlags.VertexBuffer,

CpuAccessFlags = CpuAccessFlags.None,

StructureByteStride = 0,

OptionFlags = 0

};

vertexBuffer = new Buffer(device, vertexDataStream, vertexBuffDesc);

// index buffer initialization

indexBuffDesc = new BufferDescription {

Usage = ResourceUsage.Default,

SizeInBytes = sizeof(int) * indexCount,

BindFlags = BindFlags.IndexBuffer,

CpuAccessFlags = CpuAccessFlags.None,

StructureByteStride = 0

};

indexBuffer = new Buffer(device, indexDataStream, indexBuffDesc);

return true;

###
#2
Crossbones+ - Reputation: **8135**

Posted 13 April 2012 - 03:01 PM

To draw a sphere the simplest method is to sweep through two angles Phi and Theta (Phi goes from 0 to 2pi, and Theta goes from 0 to pi), and generate the corresponding vertex using spherical coordinates (look at the conversion formulas to convert from the two angles and a radius to a 3D vertex position).

There exist more complicated methods which involve subdividing an icosahedron recursively to generate the sphere with a given tessellation level.

The slowsort algorithm is a perfect illustration of the multiply and surrender paradigm, which is perhaps the single most important paradigm in the development of reluctant algorithms. The basic multiply and surrender strategy consists in replacing the problem at hand by two or more subproblems, each slightly simpler than the original, and continue multiplying subproblems and subsubproblems recursively in this fashion as long as possible. At some point the subproblems will all become so simple that their solution can no longer be postponed, and we will have to surrender. Experience shows that, in most cases, by the time this point is reached the total work will be substantially higher than what could have been wasted by a more direct approach.

- *Pessimal Algorithms and Simplexity Analysis*

###
#3
Members - Reputation: **208**

Posted 14 April 2012 - 03:26 AM

int six = 0;

float slices = (float)tesselation;

float stacks = (float)radious;

for (ushort slice = 0; slice < slices; ++slice)

{

for (float stack = 0; stack < stacks; ++stack)

{

int v = (ushort)(slice * (stacks + 1) + stack);

indices[six++] = v;

indices[six++] = (int)(v + 1);

indices[six++] = (int)(v + (stacks + 1));

indices[six++] = (int)(v + (stacks + 1));

indices[six++] = (int)(v + 1);

indices[six++] = (int)(v + (stacks + 1) + 1);

}

}

If I can link them vertices properly so that they can draw sphere with it .. but you are right I just got points on circles so that won't be a good approach ... I need to use farmula you provided ,, thanks