Created
October 2, 2013 13:16
-
-
Save s-shin/6793570 to your computer and use it in GitHub Desktop.
Create a sphere geometry dynamically like glutSolidSphere().
(Ref: http://wgld.org/d/webgl/w025.html, http://marina.sys.wakayama-u.ac.jp/~tokoi/?date=20090912, http://stackoverflow.com/q/7687148)
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
import numpy as np | |
def create_sphere(radius, slices, stacks): | |
"""Create a sphere geometry dynamically like glutSolidSphere(). | |
:param radius: Radius of sphere. | |
:param slices: A number of slices of longitude. | |
:param stacks: A number of stacks of latitude. | |
""" | |
radius = float(radius) | |
if type(slices) is not int or type(stacks) is not int: | |
raise TypeError("The type of `slices` or `stacks` isn't int.") | |
pos, nor, tex = ([], [], []) | |
for i in range(stacks+1): | |
tex_y = float(i) / stacks | |
r = math.pi * tex_y | |
ry = math.cos(r) | |
rr = math.sin(r) | |
for j in range(slices+1): | |
tex_x = float(j) / slices | |
tr = math.pi * 2 * tex_x | |
tx = rr * radius * math.cos(tr) | |
ty = ry * radius | |
tz = rr * radius * math.sin(tr) | |
rx = rr * math.cos(tr) | |
rz = rr * math.sin(tr) | |
pos.append([tx, ty, tz]) | |
nor.append([rx, ry, rz]) | |
tex.append([1-tex_x, tex_y]) | |
idx = [] | |
for i in range(stacks): | |
for j in range(slices): | |
r = (slices + 1) * i + j | |
idx.extend([r, r+1, r+slices+2]) | |
idx.extend([r, r+slices+2, r+slices+1]) | |
return np.float32(pos), np.float32(nor), np.float32(tex), np.int16(idx) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment