Sphere Drawing with SlimDx ( dx11)

Started by
1 comment, last by ShahzadAli 12 years ago
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;
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;
Advertisement
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.”

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

[background=rgb(250, 251, 252)]int six = 0;[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]float slices = (float)tesselation;[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]float stacks = (float)radious;[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]for (ushort slice = 0; slice < slices; ++slice)[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]{[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]for (float stack = 0; stack < stacks; ++stack)[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]{[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]int v = (ushort)(slice * (stacks + 1) + stack);[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]indices[six++] = v;[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]indices[six++] = (int)(v + 1);[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]indices[six++] = (int)(v + (stacks + 1));[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]indices[six++] = (int)(v + (stacks + 1));[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]indices[six++] = (int)(v + 1);[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]indices[six++] = (int)(v + (stacks + 1) + 1);[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]}[/background]

[/font]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]}[/background]

[/font][color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]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 [/background]

[/font]

This topic is closed to new replies.

Advertisement