Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function utilities.CreateSphere(res)
- local sphereMesh = {}
- local n = math.round(res * 2)
- local ndiv2 = n/2
- --[[
- Original code by Paul Bourke
- A more efficient contribution by Federico Dosil (below)
- Draw a point for zero radius spheres
- Use CCW facet ordering
- http://paulbourke.net/texture_colour/texturemap/
- ]]
- local theta2 = math.pi * 2
- local phi1 = -math.pi / 2
- local phi2 = math.pi / 2
- local r = 1 -- normalize the verts
- --sphereMesh.clear()
- --sphereMesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP)
- local i, j
- local theta1 = 0
- local jdivn = 1/n
- local j1divn,idivn,dosdivn,unodivn,t1,t2,t3,cost1,cost2,cte1,cte3 = 0,0,0,0,0,0,0,0,0,0,0
- cte3 = (theta2-theta1)/n
- cte1 = (phi2-phi1)/ndiv2
- dosdivn = 2*unodivn
- local e,p,e2,p2 = Vec3(), Vec3(), Vec3(), Vec3()
- if n < 0 then
- n = -n
- ndiv2 = -ndiv2
- end
- if n < 4 then n = 4 ndiv2=n/2 end
- if r <= 0 then r = 1 end
- t2=phi1
- cost2=math.cos(phi1)
- j1divn=0
- for j = 1, ndiv2 do
- t1 = t2
- t2 = t2 + cte1
- t3 = theta1 - cte3
- cost1 = cost2
- cost2 = math.cos(t2)
- e.y = math.sin(t1)
- e2.y = math.sin(t2)
- p.y = r * e.y
- p2.y = r * e2.y
- idivn=0
- jdivn=j1divn
- j1divn=j1divn + dosdivn
- for i = 1, n do
- t3 = t3 + cte3
- e.x = cost1 * math.cos(t3)
- e.z = cost1 * math.sin(t3)
- p.x = r * e.x
- p.z = r * e.z
- table.insert(sphereMesh, {normal = e:Copy(), uv = Vec2(idivn, jdivn), pos = p:Copy()})
- e2.x = cost2 * math.cos(t3)
- e2.z = cost2 * math.sin(t3)
- p2.x = r * e2.x
- p2.z = r * e2.z
- table.insert(sphereMesh, {normal = e2:Copy(), uv = Vec2(idivn, jdivn), pos = p2:Copy()})
- idivn = idivn + unodivn
- end
- end
- return sphereMesh
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement