Drawing a sphere using SlimDx
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 }
Thanks for the quick reply...
so I should do something like this
void Render()
{
...
RenderSphere(device, radius);
}
void RenderSphere(Device device, int r)
{
Mesh.CreateSphere(device, r, 100,100);
}
Is that all. Coz I am not seeing the sphere....
so I should do something like this
void Render()
{
...
RenderSphere(device, radius);
}
void RenderSphere(Device device, int r)
{
Mesh.CreateSphere(device, r, 100,100);
}
Is that all. Coz I am not seeing the sphere....
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.
Please advise.
Thanks
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.
Please advise.
Thanks
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.
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.
Thanks for that. Yes I realized that as soon as I posted (the mesh create inside the render function)
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement