Created
September 27, 2014 16:22
-
-
Save malexw/f14f2de3fad0dc2e4277 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def make_cylinder(): | |
FACETS = 12 | |
DFACETS = FACETS * 2 | |
RADIUS = 0.5 | |
HEIGHT = 1 | |
HALF_STEP = math.pi / FACETS | |
TRIG_STEP = HALF_STEP * 2 | |
geom = Geometry() | |
geom.add_normal(Vector3(0, -1, 0)) | |
geom.add_normal(Vector3(0, 1, 0)) | |
geom.add_vertex(Vector3(0, 0, 0)) | |
geom.add_vertex(Vector3(0, HEIGHT, 0)) | |
for i in xrange(FACETS): | |
norm_x = math.cos(TRIG_STEP*i + HALF_STEP) * RADIUS | |
norm_z = math.sin(TRIG_STEP*i + HALF_STEP) * RADIUS | |
geom.add_normal(Vector3(norm_x, 0, norm_z)) | |
norm_i = i + 2 | |
x = math.cos(TRIG_STEP*i) * RADIUS | |
z = math.sin(TRIG_STEP*i) * RADIUS | |
geom.add_vertex(Vector3(x, 0, z)) | |
geom.add_vertex(Vector3(x, HEIGHT, z)) | |
vert_gen = lambda n: ((i * 2 + n) % DFACETS) + 2 | |
# Counter clockwise winding | |
geom.add_face(Face(vert_gen(0), vert_gen(2), vert_gen(1), norm_i, norm_i, norm_i)) | |
geom.add_face(Face(vert_gen(1), vert_gen(2), vert_gen(3), norm_i, norm_i, norm_i)) | |
# Top and bottom lid | |
geom.add_face(Face(0, vert_gen(2), vert_gen(0), 0, 0, 0)) | |
geom.add_face(Face(1, vert_gen(1), vert_gen(3), 1, 1, 1)) | |
return geom |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment