Advertisement
CapsAdmin

Untitled

Aug 30th, 2013
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.68 KB | None | 0 0
  1.  
  2. function utilities.CreateSphere(res)
  3.     local sphereMesh = {}
  4.  
  5.     local n = math.round(res * 2)
  6.     local ndiv2 = n/2
  7.  
  8.     --[[
  9.     Original code by Paul Bourke
  10.     A more efficient contribution by Federico Dosil (below)
  11.     Draw a point for zero radius spheres
  12.     Use CCW facet ordering
  13.     http://paulbourke.net/texture_colour/texturemap/
  14.     ]]
  15.  
  16.     local theta2 = math.pi * 2
  17.     local phi1 = -math.pi / 2
  18.     local phi2 = math.pi / 2
  19.     local r = 1 -- normalize the verts
  20.  
  21.     --sphereMesh.clear()
  22.     --sphereMesh.setMode(OF_PRIMITIVE_TRIANGLE_STRIP)
  23.  
  24.     local i, j
  25.     local theta1 = 0
  26.     local jdivn = 1/n
  27.     local j1divn,idivn,dosdivn,unodivn,t1,t2,t3,cost1,cost2,cte1,cte3 = 0,0,0,0,0,0,0,0,0,0,0
  28.    
  29.     cte3 = (theta2-theta1)/n
  30.     cte1 = (phi2-phi1)/ndiv2
  31.     dosdivn = 2*unodivn
  32.     local e,p,e2,p2 = Vec3(), Vec3(), Vec3(), Vec3()
  33.  
  34.     if n < 0 then
  35.     n = -n
  36.     ndiv2 = -ndiv2
  37.     end
  38.  
  39.     if n < 4 then n = 4 ndiv2=n/2 end
  40.     if r <= 0 then r = 1 end
  41.  
  42.     t2=phi1
  43.     cost2=math.cos(phi1)
  44.     j1divn=0
  45.  
  46.     for j = 1, ndiv2 do
  47.         t1 = t2
  48.         t2 = t2 + cte1
  49.         t3 = theta1 - cte3
  50.         cost1 = cost2
  51.         cost2 = math.cos(t2)
  52.         e.y = math.sin(t1)
  53.         e2.y = math.sin(t2)
  54.         p.y = r * e.y
  55.         p2.y = r * e2.y
  56.  
  57.         idivn=0
  58.         jdivn=j1divn
  59.         j1divn=j1divn + dosdivn
  60.         for i = 1, n do
  61.             t3 = t3 + cte3
  62.             e.x = cost1 * math.cos(t3)
  63.             e.z = cost1 * math.sin(t3)
  64.             p.x = r * e.x
  65.             p.z = r * e.z
  66.  
  67.             table.insert(sphereMesh, {normal = e:Copy(), uv = Vec2(idivn, jdivn), pos = p:Copy()})
  68.  
  69.             e2.x = cost2 * math.cos(t3)
  70.             e2.z = cost2 * math.sin(t3)
  71.             p2.x = r * e2.x
  72.             p2.z = r * e2.z
  73.  
  74.             table.insert(sphereMesh, {normal = e2:Copy(), uv = Vec2(idivn, jdivn), pos = p2:Copy()})
  75.  
  76.             idivn = idivn + unodivn
  77.         end
  78.     end
  79.  
  80.     return sphereMesh
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement