# Cube 3d help

How can I make a 3d cube just with the line(x1,y1,x2,y2) and vec3(x,y,z)
Aka the simplest way to do it

``````-- Cube
function setup()
displayMode(FULLSCREEN)

cube = mesh()

--vertices for the corners of the cube (stolen from 3d lab)
local vertices = {
vec3(-0.5, -0.5,  0.5), -- Left  bottom front
vec3( 0.5, -0.5,  0.5), -- Right bottom front
vec3( 0.5,  0.5,  0.5), -- Right top    front
vec3(-0.5,  0.5,  0.5), -- Left  top    front
vec3(-0.5, -0.5, -0.5), -- Left  bottom back
vec3( 0.5, -0.5, -0.5), -- Right bottom back
vec3( 0.5,  0.5, -0.5), -- Right top    back
vec3(-0.5,  0.5, -0.5), -- Left  top    back
}

-- now construct a cube out of the vertices above
cube.vertices = {
-- 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],
}

--now texture it
-- all the unique texture positions needed
local texvertices = { vec2(0,0),
vec2(1,0),
vec2(0,1),
vec2(1,1) }

-- apply the texture coordinates to each triangle
cube.texCoords = {
-- 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],
}
cube.texture = "SpaceCute:Background"
currentModelMatrix = modelMatrix()
end

function draw()
background(40, 40, 50)
camera(-1,-1,-5,0,0,0)
perspective()
modelMatrix(currentModelMatrix)
--do rotation for touch
if CurrentTouch.state == MOVING then
rotate(CurrentTouch.deltaX,0,1,0)
rotate(CurrentTouch.deltaY,1,0,0)
currentModelMatrix = modelMatrix()
end
cube:draw()
end
``````

This is the simplist way i know of

Here is another version. Slide the Output pane down to show all the parameters.

``````
supportedOrientations(PORTRAIT)

function setup()
parameter.integer("R",-360,360, -170)
parameter.number("x",0,1,.5)
parameter.number("y",0,1,.5)
parameter.number("z",0,1,.5)
parameter.integer("sizex", 0, 50, 20)
parameter.integer("sizey", 0, 50, 20)
parameter.integer("sizez", 0, 50, 20)
end

function draw()
background(0, 0, 0, 255)
perspective(50,WIDTH/HEIGHT)
camera(-200,0,0,0,0,0,0,1,0)
rotate(R,x,y,z)
cube(0,0,0,sizex,sizey,sizez)
end

function cube(x,y,z,w,h,d)
local cube = mesh()

local v = { vec3(x-w,y-h,z+d),vec3(x-w,y+h,z+d),
vec3(x+w,y-h,z+d),vec3(x+w,y+h,z+d),
vec3(x-w,y-h,z-d),vec3(x-w,y+h,z-d),
vec3(x+w,y-h,z-d),vec3(x+w,y+h,z-d) }

local faces = { v[1],v[2],v[3],v[2],v[3],v[4],
v[2],v[4],v[6],v[4],v[6],v[8],
v[1],v[2],v[5],v[2],v[5],v[6],
v[3],v[4],v[7],v[4],v[7],v[8],
v[1],v[3],v[5],v[3],v[5],v[7],
v[5],v[6],v[7],v[6],v[7],v[8] }

local c = { color(255, 0, 0, 255), color(0, 255, 0, 255),
color(255, 243, 0, 255), color(0, 0, 255, 255),
color(255, 255, 255, 255), color(255, 0, 189, 255)}

local colors = {}
for i = 1, 6 do
for j = 1, 6 do
table.insert(colors, c[i])
end
end
cube.vertices = faces
cube.colors = colors
cube:draw()
end

``````