DX11 Sphere Drawing with SlimDx ( dx11)

This topic is 2470 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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;

Share on other sites
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.

Share on other sites
Problem I am facing is here [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 13
• 9
• 9
• 15
• Forum Statistics

• Total Topics
634075
• Total Posts
3015349
×