I red @Ignatz 3D ebook and took the cube example to make a color cube to visualize rgb color space.
It rotates when you drag over the screen.
--# Main
-- Color Cube
function setup()
--set camera position
--This code is unchanged from our earlier example...
camX,camY,camZ=0,0,100
lookX,lookY,lookZ=0,0,-1000
--set where the camera is looking, straight ahead
X,Y,Z=0,0,-1000
--set object position, slightly to right of centre
--move object toward us
moveZ=0
--movement per draw
--rotate object --this is new code.....
--initial rotation
--we'll rotate the cube so we can see it properly
RX,RY,RZ=0,0,0
--change (in pixels) in rotation per draw
--we'll rotate in all three axes
rX,rY,rZ=1,0.5,0.5
SetupCube(400)
--set up the cube (NEW)
end
function SetupCube(s)
-- s = length of each side
--define all the cube corners
--we want to centre the cube on 0,0,0
--each corner will be exactly 0.5 x s away from the centre local
d=0.5*s
--now define the positions of the 8 corners
local c = {
vec3(-d, -d, d), -- Left bottom front
vec3( d, -d, d), -- Right bottom front
vec3( d, d, d), -- Right top front
vec3(-d, d, d), -- Left top front
vec3(-d, -d, -d), -- Left bottom back
vec3( d, -d, -d), -- Right bottom back
vec3( d, d, -d), -- Right top back
vec3(-d, d, -d), -- Left top back
}
-- now construct all the triangles, 6 sides x 2 triangles each x 3 vertices = 36
local vert = {
c[1], c[2], c[3], c[1], c[3], c[4], --Front
c[2], c[6], c[7], c[2], c[7], c[3], --Right
c[6], c[5], c[8], c[6], c[8], c[7], --Back
c[5], c[1], c[4], c[5], c[4], c[8], --Left
c[4], c[3], c[7], c[4], c[7], c[8], --Top
c[5], c[6], c[2], c[5], c[2], c[1], --Bottom
}
-- all the unique texture positions needed
--bottom left, bottom right, top left, top right
local t = { vec2(0,0),vec2(1,0),vec2(0,1),vec2(1,1) }
-- apply the texture coordinates to each vertex
local tex = {
t[1], t[2], t[4], t[1], t[4], t[3], --Front
t[1], t[2], t[4], t[1], t[4], t[3], --Right
t[1], t[2], t[4], t[1], t[4], t[3], --Back
t[1], t[2], t[4], t[1], t[4], t[3], --Left
t[1], t[2], t[4], t[1], t[4], t[3], --Top
t[1], t[2], t[4], t[1], t[4], t[3], --Bottom
}
local cr = {color(0, 0, 0), color(255,0,0), color(0,255,0), color(0,0,255),
color(255,255,0), color(255,0,255), color(0,255,255), color(255,255,255)}
cubecolors = {
cr[1], cr[2], cr[3], cr[1], cr[4], cr[3], --Front
cr[1], cr[2], cr[4], cr[1], cr[4], cr[3], --Right
cr[1], cr[2], cr[4], cr[1], cr[4], cr[3], --Back
cr[1], cr[2], cr[4], cr[1], cr[4], cr[3], --Left
cr[1], cr[2], cr[4], cr[1], cr[4], cr[3], --Top
cr[1], cr[2], cr[4], cr[1], cr[4], cr[3], --Bottom
}
m = mesh()
m.vertices = vert
m:setColors(255,255,255,255)
m:color(1,0,0,0)
m:color(2,255,0,0)
m:color(3,255,255,0)
m:color(4,0,0,0)
m:color(5,255,255,0)
m:color(6,0,255,0)
m:color(7,255,0,0)
m:color(8,255,0,255)
m:color(9,255,255,255)
m:color(10,255,0,0)
m:color(11,255,255,255)
m:color(12,255,255,0)
m:color(13,255,0,255)
m:color(14,0,0,255)
m:color(15,0,255,255)
m:color(16,255,0,255)
m:color(16,255,0,255)
m:color(17,0,255,255)
m:color(18,255,255,255)
m:color(19,0,0,255)
m:color(20,0,0,0)
m:color(21,0,255,0)
m:color(22,0,0,255)
m:color(23,0,255,0)
m:color(24,0,255,255)
m:color(25,0,255,0)
m:color(26,255,255,0)
m:color(27,255,255,255)
m:color(28,0,255,0)
m:color(29,255,255,255)
m:color(30,0,255,255)
m:color(31,0,0,255)
m:color(32,255,0,255)
m:color(33,255,0,0)
m:color(34,0,0,255)
m:color(35,255,0,0)
m:color(36,0,0,0)
end
function touched(t)
local dx
local dy
if t.state== MOVING
then
RX=RX- 0.5*t.deltaY
RY=RY+ 0.5*t.deltaX
end
end
function draw()
background(127, 127, 127, 255) --pale background
perspective() --tells Codea we are drawing 3D
camera(camX,camY,camZ,lookX,lookY,lookZ)
--now let's draw our cube
pushMatrix()
translate(X,Y,Z)
rotate(RX,1,0,0)
rotate(RY,0,1,0)
rotate(RZ,0,0,1)
m:draw()
popMatrix()
--move cube toward or away from us
Z=Z+moveZ
if Z>0 or Z<-2000 then moveZ=-moveZ end
-- RX,RY,RZ=RX+rX,RY+rY,RZ+rZ
--adjust the rotation
end