Advertisement
Guest User

Untitled

a guest
Oct 9th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. function setup()
  2. --this block assumes we are looking at it top down, ie using OpenGL defaults
  3. blockTop=MakeBlock(20,30,50,nil,readImage("Platformer Art:Block Brick"))
  4. --now make another block that will look exactly the same if viewed from the front
  5. --we need to swap the y and z values, after changing the sign of the z values first
  6. --I'm lazy, so first I'll just make an exact copy of the first block
  7. --this saves me having to copy texture positions etc, all I need to do is modify the vertex positions
  8. blockFront=MakeBlock(20,30,50,color(255,0,0),readImage("Platformer Art:Block Brick"))
  9. blockFront:setColors(color(255,255,0)) --give it a tint so we see when it is used
  10. --this is how you modify vertex positions in a mesh, first get a reference to them
  11. local vec=blockFront:buffer("position")
  12. for i=1,blockFront.size do
  13. local v=vec[i]
  14. vec[i]=vec3(vec[i].x,-vec[i].z,vec[i].y) --swap y,z, change sign of z
  15. end
  16. camDistance=200 --camera settings
  17. camHeight=50
  18. camX=-50
  19. View=1 --we'll swap between the two views every couple of seconds
  20. lastSwapTime=0
  21. end
  22.  
  23. function draw()
  24. background(0)
  25. fill(255) fontSize(18)
  26. if View==1 then text("Top down view",WIDTH/2,600) else text("Front view",WIDTH/2,600) end
  27. text("Adjusted",WIDTH/2,200)
  28. text("Not adjusted",WIDTH/2+200,200)
  29. perspective()
  30. if View==1 then --top down
  31. camera(camX,camHeight,camDistance,0,0,0,0,1,0)
  32. blockTop:draw()
  33. else --front facing
  34. camera(camX,-camDistance,camHeight,0,0,0,0,0,1)
  35. blockFront:draw()
  36. end
  37. --draw blockTop to the side as well
  38. translate(50,0,0)
  39. blockTop:draw()
  40. if ElapsedTime>lastSwapTime+2then
  41. View=3-View
  42. lastSwapTime=ElapsedTime
  43. end
  44. end
  45.  
  46. function MakeBlock(w,h,d,c,tex) --width,height,depth, colour,texture
  47. local m=mesh()
  48. --define the 8 corners of the block ,centred on (0,0,0)
  49. local fbl=vec3(-w/2,-h/2,d/2) --front bottom left
  50. local fbr=vec3(w/2,-h/2,d/2) --front bottom right
  51. local ftr=vec3(w/2,h/2,d/2) --front top right
  52. local ftl=vec3(-w/2,h/2,d/2) --front top left
  53. local bbl=vec3(-w/2,-h/2,-d/2) --back bottom left (as viewed from the front)
  54. local bbr=vec3(w/2,-h/2,-d/2) --back bottom right
  55. local btr=vec3(w/2,h/2,-d/2) --back top right
  56. local btl=vec3(-w/2,h/2,-d/2) --back top left
  57. --now create the 6 faces of the block, each is two triangles with 3 vertices (arranged anticlockwise)
  58. --so that is 36 vertices
  59. --for each face, I'm going to start at bottom left, then bottom right, then top right, and for the second
  60. --triangle, top right, top left, then bottom left
  61. local v={
  62. fbl,fbr,ftr, ftr,ftl,fbl, --front face
  63. bbl,fbl,ftl, ftl,btl,bbl, --left face
  64. fbr,bbr,btr, btr,ftr,fbr, --right face
  65. ftl,ftr,btr, btr,btl,ftl, --top face
  66. bbl,bbr,fbr, fbr,fbl,bbl, --bottom face
  67. bbr,bbl,btl, btl,btr,bbr --back face
  68. }
  69. m.vertices=v
  70. local t={}
  71. if tex then
  72. --add texture positions, we will use the same image for each face so we only need 4 corner positions
  73. local bl,br,tr,tl=vec2(0,0),vec2(1,0),vec2(1,1),vec2(0,1)
  74. for i=1,6 do --use a loop to add texture positions for each face, as they are the same for each face
  75. t[#t+1],t[#t+2],t[#t+3],t[#t+4],t[#t+5],t[#t+6]=bl,br,tr,tr,tl,bl
  76. end
  77. m.texCoords=t
  78. m.texture=tex
  79. m:setColors(color(255))
  80. else m:setColors(c)
  81. end
  82.  
  83. return m,v,t,c
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement