public static void generateCylinder ( TFloatList positions , TFloatList normals , TIntList indices , float radius , float height ) { // 0,0,0 will be halfway up the cylinder in the middle final float halfHeight = height / 2 ; // The positions at the rims of the cylinders final List < Vector3f > rims = new ArrayList <> ( ) ; for ( int angle = 0 ; angle < 360 ; angle += 15 ) { final double angleRads = Math . toRadians ( angle ) ; rims . add ( new Vector3f ( radius * TrigMath . cos ( angleRads ) , halfHeight , radius * - TrigMath . sin ( angleRads ) ) ) ; } // The normals for the triangles of the top and bottom faces final Vector3f topNormal = new Vector3f ( 0 , 1 , 0 ) ; final Vector3f bottomNormal = new Vector3f ( 0 , - 1 , 0 ) ; // Add the top and bottom face center vertices addVector ( positions , new Vector3f ( 0 , halfHeight , 0 ) ) ; // 0 addVector ( normals , topNormal ) ; addVector ( positions , new Vector3f ( 0 , - halfHeight , 0 ) ) ; // 1 addVector ( normals , bottomNormal ) ; // Add all the faces section by section, turning around the y axis final int rimsSize = rims . size ( ) ; for ( int i = 0 ; i < rimsSize ; i ++ ) { // Get the top and bottom vertex positions and the side normal final Vector3f t = rims . get ( i ) ; final Vector3f b = new Vector3f ( t . getX ( ) , - t . getY ( ) , t . getZ ( ) ) ; final Vector3f n = new Vector3f ( t . getX ( ) , 0 , t . getZ ( ) ) . normalize ( ) ; // Top face vertex addVector ( positions , t ) ; // index addVector ( normals , topNormal ) ; // Bottom face vertex addVector ( positions , b ) ; // index + 1 addVector ( normals , bottomNormal ) ; // Side top vertex addVector ( positions , t ) ; // index + 2 addVector ( normals , n ) ; // Side bottom vertex addVector ( positions , b ) ; // index + 3 addVector ( normals , n ) ; // Get the current index for our vertices final int currentIndex = i * 4 + 2 ; // Get the index for the next iteration, wrapping around at the end final int nextIndex = ( i == rimsSize - 1 ? 0 : i + 1 ) * 4 + 2 ; // Add the 4 triangles (1 top, 1 bottom, 2 for the side) addAll ( indices , 0 , currentIndex , nextIndex ) ; addAll ( indices , 1 , nextIndex + 1 , currentIndex + 1 ) ; addAll ( indices , currentIndex + 2 , currentIndex + 3 , nextIndex + 2 ) ; addAll ( indices , currentIndex + 3 , nextIndex + 3 , nextIndex + 2 ) ; } }
Generates a cylindrical solid mesh . The center is at middle of the of the cylinder .