# OpenGL Drawing a sphere using SlimDx

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

## Recommended Posts

Hi All, I am new to graphics and slimdx in general. I want to draw a sphere of a given radius. void DrawSphere(float radius, Device device) { ... } I was using the opengl examples(below) but am not sure how to convert it to SlimDX. Thanks void drawSphere(double r, int lats, int longs) { 7 int i, j; 8 for(i = 0; i <= lats; i++) { 9 double lat0 = M_PI * (-0.5 + (double) (i - 1) / lats); 10 double z0 = sin(lat0); 11 double zr0 = cos(lat0); 12 double lat1 = M_PI * (-0.5 + (double) i / lats); 13 double z1 = sin(lat1); 14 double zr1 = cos(lat1); 15 glBegin(GL_QUAD_STRIP); 16 for(j = 0; j <= longs; j++) { 17 double lng = 2 * M_PI * (double) (j - 1) / longs; 18 double x = cos(lng); 19 double y = sin(lng); 20 glNormal3f(x * zr0, y * zr0, z0); 21 glVertex3f(x * zr0, y * zr0, z0); 22 glNormal3f(x * zr1, y * zr1, z1); 23 glVertex3f(x * zr1, y * zr1, z1); 24 } 25 glEnd(); 26 } 27 }

##### Share on other sites
If you're using Direct3D 9, the easiest way is probably just to use Mesh.CreateSphere.

##### Share on other sites

so I should do something like this

void Render()
{
...
}

void RenderSphere(Device device, int r)
{
Mesh.CreateSphere(device, r, 100,100);
}

Is that all. Coz I am not seeing the sphere....

##### Share on other sites
Hi
An update

I am doing this

private void RenderLightSphere(Device device)
{
Mesh msh = Mesh.CreateSphere(device, 1, 5, 5);
device.SetStreamSource(0, msh.VertexBuffer, 0, 16);
device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, msh.VertexCount);
}

I can see a bunch of triangles on the screen but no sphere. Obviously I am not using the correct primitive type.

Thanks

##### Share on other sites
Firstly, don't create the Mesh each frame, that is going to have MAJOR performance implications.

Secondly, you're not disposing of your Mesh object at all, are you using the debug runtimes? They'll warn you of this.

Finally, Mesh objects use PrimitiveType.TriangleList and you can draw them by calling DrawSubset() instead of binding the vertex/index buffers to the device manually.

##### Share on other sites
Thanks for that. Yes I realized that as soon as I posted (the mesh create inside the render function)

##### Share on other sites
What about Direct3D11 ? Is there any better way in that ?

If you're using Direct3D 9, the easiest way is probably just to use Mesh.CreateSphere.

1. 1
2. 2
3. 3
Rutin
14
4. 4
5. 5

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633672
• Total Posts
3013273
×