Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Last active December 17, 2015 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dermotbalson/5632911 to your computer and use it in GitHub Desktop.
Save dermotbalson/5632911 to your computer and use it in GitHub Desktop.
ttexturing
function setup()
LoadImages()
end
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,imgBeach,.1)
b1=AddFourWalls(0,0,400,150,60,200,imgWall,.1)
end
function draw()
if imageStatus~="Ready" then return end
background(220)
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)
for i,m in pairs(meshTable) do
m:draw()
end
end
function AddFourWalls(x,y,z,w,h,d,img,s)
local m=mesh()
m.texture=img
local v,t={},{}
local nx,ny=w/img.width/s,h/img.height/s
v,t=AddImage(x,y,-z,w,h,0,nx,ny,v,t) --front
v,t=AddImage(x,y,-z-d,0,h,-d,nx,ny,v,t) --left
v,t=AddImage(x+w,y,-z-d,-w,h,0,nx,ny,v,t) --back
v,t=AddImage(x+w,y,-z,0,h,d,nx,ny,v,t) --right
m.vertices=v
m:setColors(color(255))
m.texCoords=t
m.shader = shader(autoTilerShader.vertexShader, autoTilerShader.fragmentShader)
table.insert(meshTable,m)
B[#B+1]={x=x,y=y,z=z,w=w,d=d}
return #B
end
function AddImage(x,y,z,w,h,d,nx,ny,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(nx,0)
v[#v+1]=vec3(x+w,y+h,z-d) t[#t+1]=vec2(nx,ny)
v[#v+1]=vec3(x+w,y+h,z-d) t[#t+1]=vec2(nx,ny)
v[#v+1]=vec3(x,y+h,z) t[#t+1]=vec2(0,ny)
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,s)
local m=mesh()
m.texture=i
local v,t={},{}
local nx,nz=w/i.width/s,d/i.height/s
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(nx,0)
v[#v+1]=vec3(x+w,y,-z-d) t[#t+1]=vec2(nx,nz)
v[#v+1]=vec3(x+w,y,-z-d) t[#t+1]=vec2(nx,nz)
v[#v+1]=vec3(x,y,-z-d) t[#t+1]=vec2(0,nz)
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)
m.shader = shader(autoTilerShader.vertexShader, autoTilerShader.fragmentShader)
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
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
imgBeach=LoadImage("Dropbox:3D-beach",
"http://i1303.photobucket.com/albums/ag142/ignatz_mouse/map-beach4_zps8fcd982e.jpg")
imgWall=LoadImage("Dropbox:3D-wall",
"http://i1303.photobucket.com/albums/ag142/ignatz_mouse/map-stonewall1_zps875ba0f0.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
autoTilerShader = {
vertexShader = [[
//
// A basic vertex shader
//
//This is the current model * view * projection matrix
// Codea sets it automatically
uniform mat4 modelViewProjection;
//This is the current mesh vertex position, color and tex coord
// Set automatically
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
//This is an output variable that will be passed to the fragment shader
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
void main()
{
//Pass the mesh color to the fragment shader
vColor = color;
vTexCoord = texCoord;
//Multiply the vertex position by our combined transform
gl_Position = modelViewProjection * position;
}
]],
fragmentShader = [[
//
// A basic fragment shader
//
//Default precision qualifier
precision highp float;
//This represents the current texture on the mesh
uniform lowp sampler2D texture;
//The interpolated vertex color for this fragment
varying lowp vec4 vColor;
//The interpolated texture coordinate for this fragment
varying highp vec2 vTexCoord;
void main()
{
//Sample the texture at the interpolated coordinate
lowp vec4 col = texture2D( texture, vec2(mod(vTexCoord.x,1.0), mod(vTexCoord.y,1.0))) * vColor;
//Set the output color to the texture color
gl_FragColor = col;
}
]]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment