Last active
December 17, 2015 13:39
-
-
Save dermotbalson/5618321 to your computer and use it in GitHub Desktop.
rotate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setup() | |
--load images first | |
--they may need to come from the internet, so don't do anything else until they're done | |
LoadImages() | |
end | |
--we'll run the rest of the setup when the images are ready | |
function setup2() | |
parameter.integer("Viewpoint",-2000,2000,20) | |
speed,angle=0,0 | |
ds,da=0,0 | |
posX,posY,posZ=-20,0,10 --starting position | |
rad=math.pi/180 | |
meshTable={} | |
B={} | |
AddLevel(-100,0,0,400,1000,imgLevel) | |
b1=AddFourWalls(0,0,400,150,40,200,imgWall) | |
AddItem(30,0,200,20,imgDog) --rotating dog | |
AddItem(-50,0,200,40,imgTree) --rotating tree | |
AddItem(-20,0,200,60,imgTree,45) --fixed position at 45 degrees | |
FPS=60 | |
end | |
function draw() | |
background(220) | |
if imageStatus~="Ready" then return end | |
perspective(45,WIDTH/HEIGHT) | |
if Viewpoint<10 then Viewpoint=10 end | |
angle=angle+da*DeltaTime | |
posX,posZ=posX+ds*DeltaTime*math.sin(angle*rad),posZ+ds*DeltaTime*math.cos(angle*rad) | |
--look in the same direction as we are facing | |
lookX,lookY,lookZ=posX+1000*math.sin(angle*rad),20,posZ+1000*math.cos(angle*rad) | |
camera(posX,Viewpoint,-posZ,lookX,lookY,-lookZ, 0,1,0) | |
fill(255) | |
for i,m in pairs(meshTable) do | |
--rotate free standing objects if required, so they face us | |
if m.rotate==true then | |
pushMatrix() | |
translate(m.pos.x,m.pos.y,-m.pos.z) | |
local dx,dz=m.pos.x-posX,-m.pos.z+posZ | |
local ang=math.atan(dx/-dz)/rad | |
rotate(-ang,0,1,0) | |
m:draw() | |
popMatrix() | |
elseif m.ang==nil then m:draw() | |
else | |
pushMatrix() | |
translate(m.pos.x,m.pos.y,-m.pos.z) | |
rotate(-m.ang,0,1,0) | |
m:draw() | |
popMatrix() | |
end | |
end | |
ortho() --this is needed to get text written on the screen | |
viewMatrix(matrix()) --and this | |
FPS=FPS*.9+.1/DeltaTime | |
pushStyle() | |
fill(0) | |
fontSize(18) | |
strokeWidth(3) | |
textMode(CORNER) | |
text("FPS: "..string.format("%d",FPS),50,HEIGHT-50) | |
text("x="..string.format("% d",posX),50,HEIGHT-70) | |
text("y="..string.format("% d",Viewpoint),50,HEIGHT-90) | |
text("z="..string.format("% d",posZ),50,HEIGHT-110) | |
popStyle() | |
end | |
function CreateWallImage() | |
local i=image(200,110) | |
pushStyle() | |
setContext(i) | |
fill(255,255,0,220) | |
rectMode(CORNER) | |
rect(0,0,i.width,i.height) | |
fill(255,0,100,255) | |
ellipse(55,i.height/2,100) | |
setContext() | |
popStyle() | |
return i | |
end | |
function CreateWindowImage() | |
local i=image(300,200) | |
pushStyle() | |
setContext(i) | |
stroke(120, 103, 96, 255) | |
strokeWidth(20) | |
fill(255) | |
rectMode(CORNER) | |
rect(1,1,i.width,i.height) | |
fill(255,0,0) | |
strokeWidth(0) | |
ellipse(50,50,50) | |
fill(255,0,100,255) | |
setContext() | |
popStyle() | |
return i | |
end | |
function CreateTreeImage() | |
local i=image(300,200) | |
pushStyle() | |
setContext(i) | |
fill(0,0,0,0) | |
rect(1,1,i,width,i.height) | |
stroke(75, 62, 57, 255) | |
strokeWidth(2) | |
fill(160, 123, 87, 255) | |
rect(140,0,20,240) | |
fill(116, 255, 0, 255) | |
strokeWidth(0) | |
ellipse(150,125,150) | |
fill(255,0,100,255) | |
stroke(160, 123, 87, 255) | |
strokeWidth(5) | |
line(148,50,150,200) | |
line(148,50,120,150) | |
line(148,50,180,150) | |
setContext() | |
popStyle() | |
return i | |
end | |
function AddFourWalls(x,y,z,w,h,d,img) | |
local m=mesh() | |
m.texture=img | |
local v,t={},{} | |
v,t=AddImage(x,y,-z,w,h,0,v,t) --front | |
v,t=AddImage(x,y,-z-d,0,h,-d,v,t) --left | |
v,t=AddImage(x+w,y,-z-d,-w,h,0,v,t) --back | |
v,t=AddImage(x+w,y,-z,0,h,d,v,t) --right | |
m.vertices=v | |
m:setColors(color(255)) | |
m.texCoords=t | |
table.insert(meshTable,m) | |
B[#B+1]={x=x,y=y,z=z,w=w,d=d} | |
return #B | |
end | |
function AddFeature(b,n,x,y,w,i) | |
local m =0.3 | |
local v,t={},{} | |
local h=i.height*w/i.width --calculate height, based on width | |
if n==1 then --front | |
v,t=AddImage(B[b].x+x,y,-B[b].z+m,w,h,0,v,t) | |
elseif n==2 then --left | |
local zz=-B[b].z-B[b].d+x | |
v,t=AddImage(B[b].x-m,y,zz,0,h,-w,v,t) | |
elseif n==3 then --right | |
local xx=B[b].x+B[b].w+m | |
v,t=AddImage(xx,y,-B[b].z-x,0,h,w,v,t) | |
else --back | |
local xx=B[b].x+B[b].w-x --starting x and z | |
local zz=-B[b].z-B[b].d-m | |
v,t=AddImage(xx,y,zz,-w,h,0,v,t) | |
end | |
local m=mesh() | |
m.texture=i | |
m.vertices=v | |
m:setColors(color(255)) | |
m.texCoords=t | |
table.insert(meshTable,m) | |
end | |
function AddItem(x,y,z,w,i,r) --centred on x | |
local h=i.height*w/i.width | |
local m=mesh() | |
m.texture=i | |
local v,t={},{} | |
v,t=AddImage(-w/2,0,0,w,h,0,v,t) | |
m.pos=vec3(x,y,z) | |
if r~=nil then m.ang=r else m.rotate=true end | |
m.vertices=v | |
m:setColors(color(255)) | |
m.texCoords=t | |
table.insert(meshTable,m) | |
end | |
function AddImage(x,y,z,w,h,d,v,t) | |
v[#v+1]=vec3(x,y,z) t[#t+1]=vec2(0,0) | |
v[#v+1]=vec3(x+w,y,z-d) t[#t+1]=vec2(1,0) | |
v[#v+1]=vec3(x+w,y+h,z-d) t[#t+1]=vec2(1,1) | |
v[#v+1]=vec3(x+w,y+h,z-d) t[#t+1]=vec2(1,1) | |
v[#v+1]=vec3(x,y+h,z) t[#t+1]=vec2(0,1) | |
v[#v+1]=vec3(x,y,z) t[#t+1]=vec2(0,0) | |
return v,t | |
end | |
function AddLevel(x,y,z,w,d,i) | |
local m=mesh() | |
m.texture=i | |
local v,t={},{} | |
v[#v+1]=vec3(x,y,-z) t[#t+1]=vec2(0,0) | |
v[#v+1]=vec3(x+w,y,-z) t[#t+1]=vec2(1,0) | |
v[#v+1]=vec3(x+w,y,-z-d) t[#t+1]=vec2(1,1) | |
v[#v+1]=vec3(x+w,y,-z-d) t[#t+1]=vec2(1,1) | |
v[#v+1]=vec3(x,y,-z-d) t[#t+1]=vec2(0,1) | |
v[#v+1]=vec3(x,y,-z) t[#t+1]=vec2(0,0) | |
m.vertices=v | |
m:setColors(color(255)) | |
m.texCoords=t | |
m.pos=vec3(x,y,z) | |
table.insert(meshTable,m) | |
end | |
function touched(touch) | |
local center=true | |
if touch.x<WIDTH/4 then | |
da=da-2 | |
center=false | |
elseif touch.x>WIDTH*3/4 then | |
da=da+2 | |
center=false | |
end | |
if touch.y<HEIGHT/4 then | |
ds=ds-3 | |
center=false | |
elseif touch.y>HEIGHT*3/4 then | |
ds=ds+3 | |
center=false | |
end | |
if center then ds=0 da=0 end | |
end | |
--this loads the images | |
function LoadImages() | |
imageStatus="Ready" --tells draw it's ok to draw the scene (will be turned off if we have to download images) | |
output.clear() | |
--pass through Codea name of image and internet url | |
--not in Codea, will be downloaded and saved | |
imgTree=LoadImage("Dropbox:3D-tree", | |
"http://i1303.photobucket.com/albums/ag142/ignatz_mouse/map-tree27c_zpsd56f4f5f.png") | |
imgLevel=LoadImage("SpaceCute:Background") | |
imgWall=LoadImage("Dropbox:3D-wall", | |
"http://i1303.photobucket.com/albums/ag142/ignatz_mouse/map-stonewall1_zps875ba0f0.png") | |
imgDog=LoadImage("Dropbox:3D-dog", | |
"http://i1303.photobucket.com/albums/ag142/ignatz_mouse/map-dog4_zpsd9dc17b6.png") | |
if imageStatus=="Ready" then setup2() end | |
end | |
--downloads images one by one | |
function LoadImage(fileName,url) | |
local i=readImage(fileName) | |
if i~=nil then return i end | |
--not found, we need to download, add to queue (ie table) | |
if imageTable==nil then imageTable={} end | |
imageTable[#imageTable+1]={name=fileName,url=url} | |
print('Queueing',fileName) | |
imageStatus='Loading' | |
--if the first one, go ahead and download | |
if #imageTable==1 then | |
http.request(imageTable[1].url,ImageDownloaded) | |
print('loading',imageTable[1].name) | |
end | |
end | |
--saves downloaded images | |
function ImageDownloaded(img) | |
print(imageTable[1].name,'loaded') | |
saveImage(imageTable[1].name,img) --save | |
table.remove(imageTable,1) | |
--load next one if we have any more to do | |
if #imageTable>0 then | |
http.request(imageTable[1].url,ImageDownloaded) | |
print('loading',imageTable[1].name) | |
else | |
LoadImages() | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment