Generate coordinates for Cylinder
How can I generate coordinates for cylinder, the cylinder composed from mesh of triangle
How can I gener开发者_StackOverflow中文版ate the coordinates of triangles to compose the cylinder.
If you need a mesh that can be used for numerical simulation, that's a moderately hard problem. If you just need a surface mesh for graphics, it's pretty easy. Place one vertex in the center of each circular face. Compute a series of nodes around the edges of the face using a parametric equation for a circle (x = r cos t, y = r sin t). Then you make a bunch of long skinny triangles to cover the ends using that center node and pairs of edge nodes. Finally, Imagine a bunch of long, skinny rectangles wrapped around the sides of the cylinder, defined with the same nodes; divide each one in half diagonally to make a pair of triangles.
If we assume that you really want triangles with good aspect ratios (all sides proportional to each other), the best way to do it is to follow EF Hill's last suggestion:
divide each one in half diagonally to make a pair of triangles.
You'll have several rows of triangles along the length of the cylinder; it won't be just one set of rectangles.
The key is to start by choosing how many you want around around the circumference. Ten around the circumference means each one covers 36 degrees of the circumference; 20 will mean each covers 18 degrees. Once you have that, choose the number along the length so that the spacing equals the length of the chord of the first dimension. Then slice along the diagonal of each one to make triangles.
The top and bottom cylinders are tiled by triangles by drawing lines from the center of the circle to meet the points created by the first and last rows of rectangles around the circumference.
Choose the numbers according to how faithfully you have to reproduce the cylindrical surface.
Start with the lowest-poly cylinder -- imagine a triangle on top and a triangle on bottom each having vertex #1, 2 and 3. Call the top Triangle A and the bottom triangle B. Create the following polygons:
- A1, A2, B1
- B1, B2, A2
- A2, A3, B2
- B2, B3, A2
- A3, A1, B3
- B3, B1, A1
In other words:
for(int i = 0; i < vertices; i++)
{
polyList.add(circleVertices1[i], circleVertices1[(i+1) % vertices], circleVertices2[i]);
polyList.add(circleVertices2[i], circleVertices2[(i+1) % vertices], circleVertices1[(i+1) % vertices]);
}
That's just off the top of my head, but it seems to make sense. That doesn't cover the circles on the ends, but I'll assume for now that you can figure out circles.
You can draw a cylinder procedurally by calculating the geometry. If you want to use it for rendering (most cases) you should make it so that it supports triangle stripping if performance matters, you also calculate the mapping coordinates and possibly the normals too. So it will take a bit of thinking to do from scratch, but it's doable if you understand the math.
I have created a module for Unity3D in C# that does exactly this and allows you to tweak the parameters. Watch the video to see what it's about and download the code from GitHub.
精彩评论