I have the following code from Fiddler himself to create a sphere. The first two methods will grant you an index array and a vertex array, respectively.

publicstatic Vertex[] CalculateVertices2(float radius, float height, byte segments, byte rings){var data = new Vertex[segments * rings];
int i = 0;
for(double y = 0; y < rings; y++){double phi = (y / (rings - 1)) * Math.PI; //was /2 for(double x = 0; x < segments; x++){double theta = (x / (segments - 1)) * 2 * Math.PI;
Vector3 v = newVector3(){
X = (float)(radius * Math.Sin(phi) * Math.Cos(theta)),
Y = (float)(height * Math.Cos(phi)),
Z = (float)(radius * Math.Sin(phi) * Math.Sin(theta)),
};
Vector3 n = Vector3.Normalize(v);
Vector2 uv = newVector2(){
X = (float)(x / (segments - 1)),
Y = (float)(y / (rings - 1))};
// Using data[i++] causes i to be incremented multiple times in Mono 2.2 (bug #479506).
data[i] = new Vertex(){ Position = v, Normal = n, TexCoord = uv };
i++;
}}return data;
}publicstaticushort[] CalculateElements(float radius, float height, byte segments, byte rings){var num_vertices = segments * rings;
var data = newushort[num_vertices * 6];
ushort i = 0;
for(byte y = 0; y < rings - 1; y++){for(byte x = 0; x < segments - 1; x++){
data[i++] = (ushort)((y + 0) * segments + x);
data[i++] = (ushort)((y + 1) * segments + x);
data[i++] = (ushort)((y + 1) * segments + x + 1);
data[i++] = (ushort)((y + 1) * segments + x + 1);
data[i++] = (ushort)((y + 0) * segments + x + 1);
data[i++] = (ushort)((y + 0) * segments + x);
}}// Verify that we don't access any vertices out of bounds:foreach(int index in data)if(index >= segments * rings)thrownew IndexOutOfRangeException();
return data;
}publicstruct Vertex
{// mimic InterleavedArrayFormat.T2fN3fV3fpublicVector2 TexCoord;
publicVector3 Normal;
publicVector3 Position;
}protectedoverridevoid OnRenderFrame(OpenTK.FrameEventArgs e){base.OnRenderFrame(e);
Vertex[] SphereVertices = Shape.CalculateVertices2(0.5f, 0.5f, 100, 100);
ushort[] SphereElements = Shape.CalculateElements(0.5f, 0.5f, 100, 100);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadMatrix(ref modelview);
GL.Begin(BeginMode.Triangles);
foreach(var element in SphereElements){GL.Color4(1.5f, 0.0f, 1.0f, 0.50f);
var vertex = SphereVertices[element];
GL.TexCoord2(vertex.TexCoord);
GL.Normal3(vertex.Normal);
GL.Vertex3(vertex.Position);
}GL.End();
SwapBuffers();
}

## Comments

## Re: How to draw a sphere?

It is.

Check this and the comments below.

## Re: How to draw a sphere?

I'd like to know also how it can be done? I didn't find any sphere class or function.

## Re: How to draw a sphere?

Oh, there was a link. I'll check that.

## Re: How to draw a sphere?

Hey Ostenlund,

I have the following code from Fiddler himself to create a sphere. The first two methods will grant you an index array and a vertex array, respectively.

Hope this helps!

-TheNerd

## Re: How to draw a sphere and a cylinder?

Thx, it works.

Does anyone know how to draw a cylinder?

Olli

## Re: How to draw a sphere and a cylinder?

I found the answer how to draw a cylinder!

Change your phi to this:

double phi = (y / (rings - 1));

And X, Y and Z to this:

X = (float)(radius * Math.Cos(theta)),

Y = (float)(height * phi)

Z = (float)(radius * Math.Sin(theta)),

Olli

## Re: How to draw a sphere?

Hi, ...

This you'll like ... GLShadingSubdivideSphere

I added the source code. This works on CsGL, just port it to OpenTK.

Regards, ...

## Re: How to draw a sphere?

Guys, I tried above code and get a blank window. What is the line of code to create modelView in OnRenderFrame?

Matrix4 modelview = Matrix4.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);

Does not work for me.

Thanks.

## Re: How to draw a sphere?

When i ran the above code

it gives

system.windows.shapes.shape does not contain a definition for calculate vertices 2

Anyone help me?