0

I have a problem with creating 3D cylinders (without OpenGL). I understand that a mesh is used to create the cylinder surface and triangle fans are used to create the top and bottom caps. I have already implemented the mesh but not the planar triangle fans, so currently my 3D object looks like a cylinder without the bottom and top cap.

I believe this is what I need to do in order to create the bottom and top caps. First, find the center point of the cylinder mesh. Second, find the vertices of the mesh. Third, using the center point and the 2 vertex points, create the triangle. Fourth, repeat the steps until a planar circle is created.

Are the above steps a sufficient way of creating the caps or is there a better way? And how do I find the vertices of the mesh so I can create the triangle fans?

Marika Lam
  • 25
  • 6
  • possible duplicate of [How do I texture a cylinder in OpenGL created with triangle\_strip?](http://stackoverflow.com/questions/26536570/how-do-i-texture-a-cylinder-in-opengl-created-with-triangle-strip) – Reto Koradi Dec 04 '14 at 07:16
  • it uses OpenGL but the post you linked is really useful. thanks – Marika Lam Dec 04 '14 at 07:21
  • Ah, yes, you did say "without OpenGL". I saw that initially, but then forgot about it once you started talking about triangle fans. Here's another similar one that covers just the circle you would need for the caps: http://stackoverflow.com/q/25279009/3530129. – Reto Koradi Dec 04 '14 at 07:30

1 Answers1

0

First some notes:

  1. you did not specify your platform
    • gfx interface
    • language
  2. not enough info about your cylinder either
    • is it axis aligned?
    • what coordinate system (Cartesian/orthogonal/orthonormal)?
    • need additional dimensions like color or texture coordinates?

So I can provide just generic info then

Axis aligned cylinder

  1. choose the granularity N

    • number of points along your cap's circle
    • usually 20-36 is OK but if you need higher precision then sometimes you need even 1000 points or more
    • all depends on the purpose,zoom, angle and distance of view ...
    • and performance issues
    • for now let N=32
  2. you need BR (boundary representation)

    • you did not specify gfx interface but your text implies BR model (surface polygons)
    • also no pivot point position so I will choose middle point of cylinder to be (0,0,0)
    • z axis will be the height of cylinder
    • and the caps will be coplanar with xy plane
    • so for cylinder is enough set of 2 rings (caps)
    • so the points can be defined in C++ like this:

      const int N=32; // mesh complexity
      double p0[N][3],p1[N][3]; // rings`
      double a,da,c,s,r,h2; // some temp variables
      int i;
      r =50.0; // cylinder radius
      h2=100.0*0.5; // half height of cyliner
      da=M_PI/double(N-1);
      for (a=0.0,i=0;i<N;i++,a+=da)
       {
       c=r*cos(a);
       s=r*sin(a);
       p0[i][0]=c;
       p0[i][1]=s;
       p0[i][2]=+h2;
       p1[i][0]=c;
       p1[i][1]=s;
       p1[i][2]=-h2;
       }
      
    • the ring points are as closed loop (p0[0]==p0[N-1])

    • so you do not need additional lines to handle it...
  3. now how to draw

    • cant write the code for unknown api but
    • 'mesh' is something like QUAD_STRIP I assume
    • so just add points to it in this order:
    • QUAD_STRIP = { p0[0],p1[0],p0[1],p1[1],...p0[N-1],p1[N-1] };
    • if you have inverse normal problem then swap p0/p1
    • now for the fans
    • you do not need the middle point (unless you have interpolation aliasing issues)
    • so similar:
    • TRIANGLE_FAN0 = { p0[0],p0[1],...p0[N-1] };
    • TRIANGLE_FAN1 = { p1[0],p1[1],...p1[N-1] };
    • if you still want the middle point then:
    • TRIANGLE_FAN0 = { (0.0,0.0,+h2),p0[0],p0[1],...p0[N-1] };
    • TRIANGLE_FAN1 = { (0.0,0.0,-h2),p1[0],p1[1],...p1[N-1] };
    • if you have inverse normal problem then reverse the points order (middle point stays where it is)

Not axis aligned cylinder?

  • just use transform matrix on your p0[],p1[] point lists to translate/rotate to desired position
  • the rest stays the same
Spektre
  • 49,595
  • 11
  • 110
  • 380