Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function setup()
- --this block assumes we are looking at it top down, ie using OpenGL defaults
- blockTop=MakeBlock(20,30,50,nil,readImage("Platformer Art:Block Brick"))
- --now make another block that will look exactly the same if viewed from the front
- --we need to swap the y and z values, after changing the sign of the z values first
- --I'm lazy, so first I'll just make an exact copy of the first block
- --this saves me having to copy texture positions etc, all I need to do is modify the vertex positions
- blockFront=MakeBlock(20,30,50,color(255,0,0),readImage("Platformer Art:Block Brick"))
- blockFront:setColors(color(255,255,0)) --give it a tint so we see when it is used
- --this is how you modify vertex positions in a mesh, first get a reference to them
- local vec=blockFront:buffer("position")
- for i=1,blockFront.size do
- local v=vec[i]
- vec[i]=vec3(vec[i].x,-vec[i].z,vec[i].y) --swap y,z, change sign of z
- end
- camDistance=200 --camera settings
- camHeight=50
- camX=-50
- View=1 --we'll swap between the two views every couple of seconds
- lastSwapTime=0
- end
- function draw()
- background(0)
- fill(255) fontSize(18)
- if View==1 then text("Top down view",WIDTH/2,600) else text("Front view",WIDTH/2,600) end
- text("Adjusted",WIDTH/2,200)
- text("Not adjusted",WIDTH/2+200,200)
- perspective()
- if View==1 then --top down
- camera(camX,camHeight,camDistance,0,0,0,0,1,0)
- blockTop:draw()
- else --front facing
- camera(camX,-camDistance,camHeight,0,0,0,0,0,1)
- blockFront:draw()
- end
- --draw blockTop to the side as well
- translate(50,0,0)
- blockTop:draw()
- if ElapsedTime>lastSwapTime+2then
- View=3-View
- lastSwapTime=ElapsedTime
- end
- end
- function MakeBlock(w,h,d,c,tex) --width,height,depth, colour,texture
- local m=mesh()
- --define the 8 corners of the block ,centred on (0,0,0)
- local fbl=vec3(-w/2,-h/2,d/2) --front bottom left
- local fbr=vec3(w/2,-h/2,d/2) --front bottom right
- local ftr=vec3(w/2,h/2,d/2) --front top right
- local ftl=vec3(-w/2,h/2,d/2) --front top left
- local bbl=vec3(-w/2,-h/2,-d/2) --back bottom left (as viewed from the front)
- local bbr=vec3(w/2,-h/2,-d/2) --back bottom right
- local btr=vec3(w/2,h/2,-d/2) --back top right
- local btl=vec3(-w/2,h/2,-d/2) --back top left
- --now create the 6 faces of the block, each is two triangles with 3 vertices (arranged anticlockwise)
- --so that is 36 vertices
- --for each face, I'm going to start at bottom left, then bottom right, then top right, and for the second
- --triangle, top right, top left, then bottom left
- local v={
- fbl,fbr,ftr, ftr,ftl,fbl, --front face
- bbl,fbl,ftl, ftl,btl,bbl, --left face
- fbr,bbr,btr, btr,ftr,fbr, --right face
- ftl,ftr,btr, btr,btl,ftl, --top face
- bbl,bbr,fbr, fbr,fbl,bbl, --bottom face
- bbr,bbl,btl, btl,btr,bbr --back face
- }
- m.vertices=v
- local t={}
- if tex then
- --add texture positions, we will use the same image for each face so we only need 4 corner positions
- local bl,br,tr,tl=vec2(0,0),vec2(1,0),vec2(1,1),vec2(0,1)
- for i=1,6 do --use a loop to add texture positions for each face, as they are the same for each face
- t[#t+1],t[#t+2],t[#t+3],t[#t+4],t[#t+5],t[#t+6]=bl,br,tr,tr,tl,bl
- end
- m.texCoords=t
- m.texture=tex
- m:setColors(color(255))
- else m:setColors(c)
- end
- return m,v,t,c
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement