• Create Account

## Sphere Drawing with SlimDx ( dx11)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1Wangru  Members

208
Like
0Likes
Like

Posted 13 April 2012 - 10:36 AM

I don't know how to fix this code so that It starts rendering sphere properly... Indices buffer is not using all vertices and not using properly I suppose because half of sphere is getting rendered!

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;
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;

### #2Bacterius  Members

13102
Like
0Likes
Like

Posted 13 April 2012 - 03:01 PM

That's not how you make a sphere. You're just making three discs (circles) with this code (as the screenshot shows).

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

### #3Wangru  Members

208
Like
0Likes
Like

Posted 14 April 2012 - 03:26 AM

Problem I am facing is here

int six = 0;

float slices = (float)tesselation;

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.