3D version of my zoom library in action:
--# Main
-- 3D example
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
scn=Scene()
scn:add(Cube(30,vec3(0,0,0),"Planet Cute:Wood Block"))
scn:add(Cube(30,vec3(35,0,35),"Planet Cute:Dirt Block",color(255, 255, 255, 120)))
parameter("FieldOfView", 10, 140, 20)
zoom3d=Zoom3D(30,20,vec3(35,0,35))
pt3D=vec3(50,15,50)
lkat=vec3(35,0,35)
end
function touched(touch)
zoom3d:touched(touch)
end
-- This function gets called once every frame
function draw()
zoom3d:draw()
-- This sets a dark background color
background(0, 0, 0, 255)
-- Do your 3D drawing here
scn:draw()
local pt=zoom3d:getWorldPoint(pt3D)
local pto=zoom3d:getWorldPoint(zoom3d.lookat)
-- Restore orthographic projection
ortho()
-- Restore the view matrix to the identity
viewMatrix(matrix())
pushStyle()
ellipseMode=CENTER
ellipse(pt.x,pt.y,10)
-- draw center of rotation in red
fill(255, 0, 0, 255)
ellipse(pto.x, pto.y,10)
popStyle()
-- Draw a label at the top of the screen
fill(255)
font("MyriadPro-Bold")
fontSize(30)
text("3D example", WIDTH/2, HEIGHT - 30)
end
--# Cube
Cube = class()
function Cube:init(size,pos,texture,colour)
-- you can accept and set parameters here
size = size or 1
pos = pos or vec3(0,0,0)
colour = colour or color(255, 255, 255, 255)
-- all the unique vertices that make up a cube
local vertices = {
vec3(-0.5*size, -0.5*size, 0.5*size)+pos, -- Left bottom front
vec3( 0.5*size, -0.5*size, 0.5*size)+pos, -- Right bottom front
vec3( 0.5*size, 0.5*size, 0.5*size)+pos, -- Right top front
vec3(-0.5*size, 0.5*size, 0.5*size)+pos, -- Left top front
vec3(-0.5*size, -0.5*size, -0.5*size)+pos, -- Left bottom back
vec3( 0.5*size, -0.5*size, -0.5*size)+pos, -- Right bottom back
vec3( 0.5*size, 0.5*size, -0.5*size)+pos, -- Right top back
vec3(-0.5*size, 0.5*size, -0.5*size)+pos, -- Left top back
}
-- now construct a cube out of the vertices above
local cubeverts = {
-- Front
vertices[1], vertices[2], vertices[3],
vertices[1], vertices[3], vertices[4],
-- Right
vertices[2], vertices[6], vertices[7],
vertices[2], vertices[7], vertices[3],
-- Back
vertices[6], vertices[5], vertices[8],
vertices[6], vertices[8], vertices[7],
-- Left
vertices[5], vertices[1], vertices[4],
vertices[5], vertices[4], vertices[8],
-- Top
vertices[4], vertices[3], vertices[7],
vertices[4], vertices[7], vertices[8],
-- Bottom
vertices[5], vertices[6], vertices[2],
vertices[5], vertices[2], vertices[1],
}
-- all the unique texture positions needed
local texvertices = { vec2(0.03,0.24),
vec2(0.97,0.24),
vec2(0.03,0.69),
vec2(0.97,0.69) }
-- apply the texture coordinates to each triangle
local cubetexCoords = {
-- Front
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Right
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Back
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Left
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Top
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
-- Bottom
texvertices[1], texvertices[2], texvertices[4],
texvertices[1], texvertices[4], texvertices[3],
}
-- now we make our 3 different block types
self.mesh = mesh()
self.mesh.vertices = cubeverts
--sprite("Planet Cute:Wood Block")
self.mesh.texture = texture
self.mesh.texCoords = cubetexCoords
self.mesh:setColors(colour.r,colour.g,colour.b,colour.a)
end
function Cube:draw()
self.mesh:draw()
end
function Cube:touched(touch)
-- Codea does not automatically call this method
end
Scene = class()
function Scene:init()
self.objs = {}
end
function Scene:add(obj)
self.objs[obj]=1
end
function Scene:remove(obj)
self.objs[obj]=nil
end
function Scene:draw()
for obj,_ in pairs(self.objs) do
obj:draw()
end
end