Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2014
474
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.94 KB | None | 0 0
  1. function setup()
  2.  
  3. --[[ Sphere generator v1
  4.  
  5. This is an app made in Codea written by Muffincoder on Codeas' forum
  6. You may use this code for whatever you like, I would be glad to see
  7. whatever you come up with using this or use this for.
  8.  
  9. Also note that the algorithm I came up with is not optimal, it works, that's it.
  10. If you improve the algorithm then please tell me what and how you did it.
  11.  
  12. This was purely a fun project that I created on my pastime. Enjoy!
  13. ]]
  14.  
  15. -- These define the amount of fragments vertically and horizontally
  16. segmentsH = 100
  17. segmentsV = 30
  18.  
  19. --The radius of the sphere
  20. size = 100
  21.  
  22. --This is the texture for the sphere, use a rectangular texture for example an image of earth.
  23. texture = "Documents:earth"
  24.  
  25. --Also note that the sphere can be either colored with random colors or textured
  26. --This is made possible with the use of 2 shaders, one for pure color and one basic texture shader
  27. colorOnlyShader = shader([[
  28. uniform mat4 modelViewProjection;
  29.  
  30. attribute vec4 position;
  31. attribute vec4 color;
  32.  
  33. varying lowp vec4 vColor;
  34.  
  35. void main()
  36. {
  37. vColor = color;
  38. gl_Position = modelViewProjection * position;
  39. }
  40.  
  41. ]],[[
  42. precision highp float;
  43.  
  44. varying lowp vec4 vColor;
  45.  
  46. void main()
  47. {
  48. gl_FragColor = vColor;
  49. }
  50. ]])
  51. textureShader = shader([[
  52. uniform mat4 modelViewProjection;
  53.  
  54. attribute vec4 position;
  55. attribute vec4 color;
  56. attribute vec2 texCoord;
  57.  
  58. varying lowp vec4 vColor;
  59. varying highp vec2 vTexCoord;
  60.  
  61. void main()
  62. {
  63. vColor = color;
  64. vTexCoord = texCoord;
  65. gl_Position = modelViewProjection * position;
  66. }
  67. ]],[[
  68. precision highp float;
  69.  
  70. uniform lowp sampler2D texture;
  71.  
  72. varying lowp vec4 vColor;
  73. varying lowp vec2 vTexCoord;
  74.  
  75. void main()
  76. {
  77. lowp vec4 col = texture2D(texture, vTexCoord) * vColor;
  78. gl_FragColor = col;
  79. }
  80. ]])
  81.  
  82. --Variable init
  83. meshS = mesh()
  84. verts = {}
  85. vertices = {}
  86. colors = {}
  87. texels = {}
  88. texCoords = {}
  89.  
  90. parameter.integer("segmentsH",5,150,segmentsH)
  91. parameter.integer("segmentsV",3,150,segmentsV)
  92.  
  93. parameter.action("generateSphere",generateSphere)
  94. parameter.boolean("randomColors",true)
  95. parameter.integer("dist",-1000,0,-350)
  96.  
  97. parameter.integer("rotX",0,360,0)
  98. parameter.integer("rotY",0,360,0)
  99. parameter.integer("rotZ",0,360,0)
  100.  
  101. generateSphere()
  102.  
  103. end
  104.  
  105. function generateSphere()
  106.  
  107. --Calculate how big of an angle each fragment of the sphere has
  108. local segmentHangle = (2*math.pi)/segmentsH
  109. local segmentVangle = (math.pi/(segmentsV+1))
  110.  
  111. --Reset arrays if set already, or initialize them if this is the first run
  112. meshS = mesh()
  113. verts = {}
  114. vertices = {}
  115. colors = {}
  116. texels = {}
  117. texCoords = {}
  118.  
  119. --First part is to generate each unique vertex for the sphere
  120. --Starting with the topmost vertex
  121. table.insert(verts,vec3(0,size,0))
  122. table.insert(texels,vec2(0.5,1.0))
  123.  
  124. for i=1,segmentsV do
  125. local texelY = 1-(i / segmentsV)*((segmentsV-1)/segmentsV)
  126. for j=1,segmentsH do
  127. --Radius parameter exposed for easier manipulation of the surface
  128. -- you can for example add some noise here to make the surface 'grainy'
  129. local length = size
  130.  
  131. local posX = math.cos(j*segmentHangle)*length*math.cos(math.pi/2 + (i*segmentVangle))
  132. local posZ = math.sin(j*segmentHangle)*length*math.cos(math.pi/2 + (i*segmentVangle))
  133. local posY = math.sin(math.pi/2 + (i*segmentVangle))*length
  134.  
  135. local texelX = 1-(j / segmentsH)
  136.  
  137. table.insert(verts,vec3(posX,posY,posZ))
  138. table.insert(texels,vec2(texelX,texelY))
  139.  
  140. end
  141.  
  142. end
  143.  
  144. --And ending with the bottom vertex
  145. table.insert(verts,vec3(0,-size,0))
  146. table.insert(texels,vec2(0.5,0.0))
  147. print("Nr of unique vertices: "..#verts)
  148. print("Nr of unique texels: "..#texels)
  149.  
  150. --Then generate the mesh out of the unique vertices
  151. for i=1,#verts do
  152.  
  153. --[[I split the sphere into 3 parts;
  154. A Top part, a middle part and a bottom part.
  155.  
  156. -The top and bottom ones are pretty much the same but the latter turned upside down.
  157. -The top vertex and the first ring of vertices assemble a circle using
  158. triangles that all share the top vertex. The same for the bottom part.
  159. -The middle part is just a mesh of the remaining vertices
  160. ]]
  161.  
  162. --Top part
  163. if i == 1 then
  164. for j=1,segmentsH-1 do
  165. local curIndex = i + j
  166. table.insert(vertices,verts[i])
  167. table.insert(vertices,verts[curIndex+1])
  168. table.insert(vertices,verts[curIndex])
  169.  
  170. table.insert(texCoords,texels[i])
  171. table.insert(texCoords,texels[curIndex+1])
  172. table.insert(texCoords,texels[curIndex])
  173. end
  174.  
  175. table.insert(vertices,verts[i])
  176. table.insert(vertices,verts[i+1])
  177. table.insert(vertices,verts[i+segmentsH])
  178.  
  179. table.insert(texCoords,texels[i])
  180. table.insert(texCoords,texels[i+1])
  181. table.insert(texCoords,vec2(1.0,texels[i+segmentsH].y))
  182. i = i + segmentsH
  183.  
  184. --Middle part
  185. elseif i > segmentsH and i < #verts then
  186. local lastRow = i - segmentsH
  187. local thisRow = ((i-1) % segmentsH)+1
  188.  
  189. if lastRow > 1 then
  190. table.insert(vertices,verts[i])
  191. table.insert(vertices,verts[lastRow+1])
  192. table.insert(vertices,verts[lastRow])
  193.  
  194.  
  195. if thisRow == 1 then
  196. table.insert(texCoords,vec2(1.0,texels[i].y))
  197. table.insert(texCoords,texels[lastRow+1])
  198. table.insert(texCoords,vec2(1.0,texels[lastRow].y))
  199.  
  200. else
  201. table.insert(texCoords,texels[i])
  202. table.insert(texCoords,texels[lastRow+1])
  203. table.insert(texCoords,texels[lastRow])
  204. end
  205.  
  206. end
  207.  
  208. if i+1 < #verts then
  209. table.insert(vertices,verts[i])
  210. table.insert(vertices,verts[i+1])
  211. table.insert(vertices,verts[lastRow+1])
  212.  
  213. if thisRow == 1 then
  214. --table.insert(texCoords,vec2(1.0,texels[lastRow].y))
  215. table.insert(texCoords,vec2(1.0,texels[i].y))
  216. table.insert(texCoords,texels[i+1])
  217. table.insert(texCoords,texels[lastRow+1])
  218.  
  219. else
  220. table.insert(texCoords,texels[i])
  221. table.insert(texCoords,texels[i+1])
  222. table.insert(texCoords,texels[lastRow+1])
  223. end
  224.  
  225. end
  226.  
  227. --Bottom part
  228. if i == (#verts-segmentsH) then
  229. for j=1,segmentsH-1 do
  230. local curIndex2 = i + j
  231. table.insert(vertices,verts[#verts])
  232. table.insert(vertices,verts[curIndex2])
  233. table.insert(vertices,verts[curIndex2-1])
  234.  
  235. table.insert(texCoords,texels[#verts])
  236. table.insert(texCoords,texels[curIndex2])
  237. table.insert(texCoords,texels[curIndex2-1])
  238. end
  239. table.insert(vertices,verts[#verts])
  240. table.insert(vertices,verts[#verts-1])
  241. table.insert(vertices,verts[#verts-segmentsH])
  242.  
  243. table.insert(texCoords,texels[#verts])
  244. table.insert(texCoords,vec2(1.0,texels[#verts-1].y))
  245. table.insert(texCoords,texels[#verts-segmentsH])
  246. i = i + segmentsH
  247. end
  248. end
  249. end
  250.  
  251. meshS.vertices = vertices
  252. meshS.texCoords = texCoords
  253.  
  254. if randomColors then
  255. for i=1,#vertices,3 do-- Color each triangle with a different color
  256. randomCol(3)
  257. end
  258. meshS.colors = colors
  259. meshS.shader = colorOnlyShader
  260. meshS.texture = ""
  261. else
  262. meshS:setColors(255,255,255,255)
  263. meshS.texture = texture
  264. meshS.shader = textureShader
  265. end
  266.  
  267. print("Final mesh vertex count: "..#vertices)
  268. print("Color array length: "..#colors)
  269. print("Tex-coord array length: "..#texCoords)
  270. print("---------")
  271. end
  272.  
  273. function draw()
  274. background(0, 0, 0, 255)
  275. fill(255, 255, 255, 255)
  276.  
  277. --Camera setup
  278. camera(0,0,5, 0,0,0, 0,1,0)
  279. perspective(45,WIDTH/HEIGHT,0.1,10000)
  280.  
  281. --Translate the sphere a bit into the screen
  282. translate(0,0,dist)
  283. --And rotate the object
  284. rotate(rotX,1,0,0)
  285. rotate(rotY+ElapsedTime*10,0,1,0)
  286. rotate(rotZ,0,0,1)
  287. --Draw sphere
  288. meshS:draw()
  289. end
  290.  
  291. function randomCol(n)
  292. local col = vec4(math.random(),math.random(),math.random(),1.0)
  293. for q=1,n do
  294. table.insert(colors,col)
  295. end
  296. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement