Best way to draw cylinder

Started by
7 comments, last by Jason Z 11 years, 1 month ago

Hi

I need some effective way to draw cylinder, many cilinders. Basicly, I'm rendering primitives on my scene, but making cylinder from primitives is very unprofitably (especially circles) - much additional points needed. So, I'm searching alternative way to draw awesome fast-drawing cylinders.

I thought about textures, but I don't know how to make textured cylinder, which will stay cylinder when I'll rotate camera.

Advertisement

In D3d9, you can try using D3DXCreateCylinder. There are tons of working examples you can find on the web to use this method.

You will be able to draw a hell of a lot of cylinders before you reach the point where it's too many polygons for any vaguely modern hardware. You could generate multiple LODs if necessary so cylinders far away from your camera use fewer triangles.

Otherwise, I imagine you could render quads and do some clever maths in a pixel shader to draw a cylinder, but that's much harder and probably more expensive than doing it with triangles.

You could also look at imposters, 2D sprites which you can update as your camera position changes, but only when it changes beyond a certain threshold. Again, lots of work, probably little gain, just render the triangles.

I would make a single unit-cylinder (radius = 1.0, height range from -0.5 to +0.5), and pass in the desired radius and height. Then multiply the vertex in the unit cylinder by float3( desiredRadius, desiredHeight, desiredRadius ) before the transform. Allowing you to just have one geometry block shared across all cylinders for a small memory footprint.

As someone else said, you should be able to draw an awful lot of cylinders before it becomes a problem.

n!

I see two different possibilities:

1. Define a cylinder as a your base mesh, using radius = 1.0 and height = 1.0. Then you could use this as the instance basis, and then just pass the per-instance data that is needed to scale the cylinder and place it where you want it in the proper orientation (i.e. a transformation matrix only, since you can do a scale in Y for scaling the height, scale in X and Z for radius, then the standard rotation and translation can all be concatenated into one matrix). This should be very efficient, with the vertex shader simply being a single transformation, and it should be faster than one draw call for each cylinder (plus a significant reduction in vertex buffer size needed).

2. You can try to use imposters if the precision isn't too strictly needed. This is essentially rendering a few cylinders into textures, and then you use billboards to put them into the correct location within your scene. This would be hard to get working in an acceptable quality, but could be very fast if it meets your requirements...

I would prefer #1, but your mileage may vary!

In D3d9, you can try using D3DXCreateCylinder. There are tons of working examples you can find on the web to use this method.

Can't find this method in SharpDX.. Seems it's not exist in this library

I'm displaying quite big 3D surface on my scene, it has enough primitives even without those cylinders (near 500 000, can be even more), so I'm quite limited in resources, even with modern hardware smile.png .

Jason Zink

If I understood you right, you suggest (#1) not to store needed vertices, but to recalculate those cylinders with new parameters on each draw call to reduce buffer size?

I would make a single unit-cylinder (radius = 1.0, height range from -0.5 to +0.5), and pass in the desired radius and height. Then multiply the vertex in the unit cylinder by float3( desiredRadius, desiredHeight, desiredRadius ) before the transform. Allowing you to just have one geometry block shared across all cylinders for a small memory footprint.

As someone else said, you should be able to draw an awful lot of cylinders before it becomes a problem.

n!

One geometry block shared across all cylinders - sounds nice. You wrote about initialization above, give me more information about where I need to store those points? Maybe, some code samples?

Jason Zink

If I understood you right, you suggest (#1) not to store needed vertices, but to recalculate those cylinders with new parameters on each draw call to reduce buffer size?

Basically just to use instanced rendering. You will have a base mesh to represent your 'unit' cylinder, and then you load another vertex buffer with per-instance data (the transformation I mentioned above). That second vertex buffer will allow you to draw all of your cylinders in one draw call, and your total memory consumption is significantly reduced.

This topic is closed to new replies.

Advertisement