Advertisement
Guest User

Post16

a guest
Mar 27th, 2013
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 KB | None | 0 0
  1.  
  2. --# Main
  3.  
  4. --[[
  5. In this demo, I've added a texture from the internet to make the dice more interesting
  6. I've also added a fake highlight to the dice spots to give them some depth
  7.  
  8. --]]
  9.  
  10.  
  11.  
  12. function setup()
  13. FieldOfView=45
  14. CamHeight=300
  15. Angle=0
  16. local s=75
  17. --create the covering for the block, get a picture from the internet
  18. --only create the dice once we have the image
  19. http.request("http://fc03.deviantart.net/fs47/i/2009/204/4/b/Blue_Venetian_Glass_Texture_by_powerpuffjazz.jpg",
  20. function(img,a,b)
  21. glass=img
  22. local img2,imgData=createDice(s)
  23. blk=Block3(s,s,s,img2,imgData)
  24. end
  25. )
  26. end
  27.  
  28. function draw()
  29. background(152, 182, 196, 255)
  30. while glass==nil do --wait for internet image before drawing
  31. output:clear()
  32. print("Please wait while I load the texture from the internet..")
  33. return
  34. end
  35. -- First arg is FOV, second is aspect
  36. perspective(FieldOfView, WIDTH/HEIGHT)
  37. -- Position the camera up and back, look at origin
  38. camera(0,CamHeight,-300, 0,0,0, 0,1,0)
  39. pushMatrix()
  40. rotate(Angle,0,1,0)
  41. blk:draw()
  42. -- Restore orthographic projection
  43. ortho()
  44. -- Restore the view matrix to the identity
  45. viewMatrix(matrix())
  46. output.clear()
  47. print("We create our own dice")
  48. popMatrix()
  49. end
  50.  
  51. function touched(touch)
  52. local xx,yy=math.floor(touch.x*3/WIDTH),math.floor(touch.y*3/HEIGHT)
  53. if xx==0 then Angle=Angle-15 elseif xx==2 then Angle=Angle+15 end
  54. if yy==0 then CamHeight=CamHeight-50 elseif yy==2 then CamHeight=CamHeight+50 end
  55. end
  56.  
  57. function createDice(s)
  58. local d=s/5
  59. local img=glass:copy(1,1,s,s*6) --create a new image with a slice of the texture, the size we need
  60. --local img=image(s,s*6) --so we don't need this line any more
  61.  
  62. --I'm going to create a dot with a whte spot, to give it depth
  63. --I'll do this with another little image, which I'll create now
  64. local dot=image(d,d) -- CHANGED next few lines are new
  65. pushStyle()
  66. setContext(dot) --draw on my dot
  67. fill(0, 0, 0, 255)
  68. ellipse(d/2,d/2,d)
  69. fill(232, 231, 231, 150) -- add whiteish dot off centre
  70. ellipse(d/2-d/4,d/2,d/4,d/3)
  71.  
  72. setContext(img)
  73. --add a light blue filter over the texture to make it lighter in color
  74. fill(200, 208, 211, 85)
  75. rect(0,0,img.width,img.height)
  76. fill(192, 184, 156, 255)
  77. --rect(0,0,s,s*6) --no need to add a background now
  78. rectMode(CENTER)
  79. fill(0, 0, 0, 255)
  80. -- Insert numbers in this sequence - Front, Right, Back, Left, Top, Bottom to tie in with Block class
  81. --one
  82. sprite(dot,s/2,s/2) --CHANGED secdot image instead of drawing ellipses
  83. --two
  84. local h=s
  85. sprite(dot,s/3,h+s/2)
  86. sprite(dot,s*2/3,h+s/2)
  87. --six
  88. h=h+s
  89. sprite(dot,s/4,h+s/3)
  90. sprite(dot,s/4,h+s*2/3)
  91. sprite(dot,s/2,h+s/3)
  92. sprite(dot,s/2,h+s*2/3)
  93. sprite(dot,s*3/4,h+s/3)
  94. sprite(dot,s*3/4,h+s*2/3)
  95. --five
  96. h = h + s
  97. sprite(dot,s/4,h+s/4)
  98. sprite(dot,s*3/4,h+s/4)
  99. sprite(dot,s/4,h+s*3/4)
  100. sprite(dot,s*3/4,h+s*3/4)
  101. sprite(dot,s/2,h+s/2)
  102. --three
  103. h = h + s
  104. sprite(dot,s/4,h+s/2)
  105. sprite(dot,s/2,h+s/2)
  106. sprite(dot,s*3/4,h+s/2)
  107. --four
  108. h = h + s
  109. sprite(dot,s/3,h+s/3,d)
  110. sprite(dot,s*2/3,h+s/3,d)
  111. sprite(dot,s/3,h+s*2/3,d)
  112. sprite(dot,s*2/3,h+s*2/3,d)
  113.  
  114. --add a border for effect
  115. local clr=color(188, 206, 215, 255)
  116. fill(clr)
  117. stroke(clr)
  118. strokeWidth(1)
  119. for i=1,6 do
  120. local y=(i-1)*s
  121. line(1,y+1,s,y+1)
  122. line(1,y+1,1,y+s)
  123. line(1,y+s,s,y+s)
  124. line(s,y+1,s,y+s)
  125. end
  126. popStyle()
  127. setContext()
  128. saveImage("Documents:test1",img)
  129. local imgData={0,0,1,1/6, 0,1/6,1,2/6, 0,2/6,1,3/6, 0,3/6,1,4/6, 0,4/6,1,5/6, 0,5/6,1,1}
  130. return img,imgData
  131. end
  132.  
  133.  
  134.  
  135. --# Block3
  136. Block3 = class() --taken from 3D lab project
  137.  
  138. --t=either string name of image, or an array of 6 colors, one per block face
  139. function Block3:init(w,h,d,t,r) --width,height,depth,texture/colors, (optional) texture range (see above)
  140. self.width=w
  141. self.height=h
  142. self.depth=d
  143. if type(t)=="string" or type(t)=="userdata" then --CHANGED to trap cases where we provide an actual image
  144. self.type="Image" --CHANGED
  145. self.tex=t --string name of an image in the library, or the image itself
  146. --if no limits specified on which part of image to draw, set to 0,1
  147. if r~=nil then self.texR=r else self.texR={0,0,1,1} end
  148. else --we are given a table of colors, one per face
  149. self.type="Colors" --CHANGED
  150. self.colrs=t
  151. end
  152. self.blk=self:createBlock()
  153. end
  154.  
  155. function Block3:createBlock()
  156. -- all the unique vertices that make up a block
  157. --There are only 8 corners in a cube - we define them as vertices
  158. --all measurements are taken from the centre of the block
  159. --so bottom left front has x of -1/2 width, y of -1/2 height, and z of 1/2 depth
  160. local w,h,d=self.width,self.height,self.depth
  161. local v = {
  162. vec3(-0.5*w, -0.5*h, 0.5*d), -- Left bottom front
  163. vec3( 0.5*w, -0.5*h, 0.5*d), -- Right bottom front
  164. vec3( 0.5*w, 0.5*h, 0.5*d), -- Right top front
  165. vec3(-0.5*w, 0.5*h, 0.5*d), -- Left top front
  166. vec3(-0.5*w, -0.5*h, -0.5*d), -- Left bottom back
  167. vec3( 0.5*w, -0.5*h, -0.5*d), -- Right bottom back
  168. vec3( 0.5*w, 0.5*h, -0.5*d), -- Right top back
  169. vec3(-0.5*w, 0.5*h, -0.5*d), -- Left top back
  170. }
  171.  
  172. -- now construct a block out of the vertices above
  173. --there are 6 sides, each made up of 2 triangles, each with 3 vertices
  174. --so we need to assign 36 vertices in total
  175. --the 8 vectors are assigned to the 8 corners as follows
  176. --1,2,3,4 anticlockwise round front starting bottom left
  177. --5,6,7,8 clockwise round back starting bottom right (assuming you are looking at the back)
  178. --the first three vectors below use vectors 1,2 and 3. If you look above, you'll
  179. --see those are for left and right of bottom front, plus right top of front
  180. --so this is the right hand triangle for the front side
  181. local cubeverts = {
  182. -- Front, Right, Back, Left, Top, Bottom
  183. v[1], v[2], v[3], v[1], v[3], v[4],
  184. v[2], v[6], v[7], v[2], v[7], v[3],
  185. v[6], v[5], v[8], v[6], v[8], v[7],
  186. v[5], v[1], v[4], v[5], v[4], v[8],
  187. v[4], v[3], v[7], v[4], v[7], v[8],
  188. v[5], v[6], v[2], v[5], v[2], v[1],
  189. }
  190.  
  191. -- add texture or colors
  192. if self.type=="Image" then --texture --CHANGED
  193. if #self.texR==4 then
  194. --create vectors to be assigned to four corners of each face
  195. local BL=vec2(self.texR[1],self.texR[2]) --bottom left
  196. local BR=vec2(self.texR[3],self.texR[2]) --bottom right
  197. local TR=vec2(self.texR[3],self.texR[4]) --top right
  198. local TL=vec2(self.texR[1],self.texR[4]) --top left
  199. -- apply the texture coordinates to each triangle
  200. --we need to add them to each of the vertexes in the same order as above
  201. --that means on each face we need to add them in the order BL, BR, TR, BL, TR, TL
  202. self.texCoords = {}
  203. for i=1,6 do
  204. table.insert(self.texCoords,BL)
  205. table.insert(self.texCoords,BR)
  206. table.insert(self.texCoords,TR)
  207. table.insert(self.texCoords,BL)
  208. table.insert(self.texCoords,TR)
  209. table.insert(self.texCoords,TL)
  210. end
  211. else --user has specified image fractions for each face
  212. self.texCoords = {}
  213. local u=0
  214. for i=1,6 do
  215. local BL=vec2(self.texR[u+1],self.texR[u+2]) --bottom left
  216. local BR=vec2(self.texR[u+3],self.texR[u+2]) --bottom right
  217. local TR=vec2(self.texR[u+3],self.texR[u+4]) --top right
  218. local TL=vec2(self.texR[u+1],self.texR[u+4]) --top left
  219. u = u + 4
  220. table.insert(self.texCoords,BL)
  221. table.insert(self.texCoords,BR)
  222. table.insert(self.texCoords,TR)
  223. table.insert(self.texCoords,BL)
  224. table.insert(self.texCoords,TR)
  225. table.insert(self.texCoords,TL)
  226. end
  227. end
  228. else --colors, one per face
  229. self.texColrs={}
  230. for i=1,6 do
  231. for j=1,6 do
  232. table.insert(self.texColrs,self.colrs[i])
  233. end
  234. end
  235. end
  236.  
  237. --put it all together
  238. local ms = mesh()
  239. ms.vertices = cubeverts
  240. ms.texture = self.tex
  241. if self.texCoords~=nil then ms.texCoords = self.texCoords end
  242. ms:setColors(255,255,255,255)
  243. if self.texColrs~=nil then ms.colors=self.texColrs end
  244. return ms
  245. end
  246.  
  247. function Block3:draw()
  248. self.blk:draw()
  249. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement