Skip to content

Instantly share code, notes, and snippets.

@dermotbalson
Created February 27, 2014 08:58
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/9246685 to your computer and use it in GitHub Desktop.
Save dermotbalson/9246685 to your computer and use it in GitHub Desktop.
lighting cube
-- 3D basic
-- Use this function to perform your initial setup
function setup()
local dirtBlock = Block()
local grassBlock = Block()
grassBlock.cube.texture = "Planet Cute:Grass Block"
geometry={dirtBlock, grassBlock}
t=0
-- currentScene = CubeDemo()
scene = { { {1} } }
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
perspective()
camera(0,500,300, 0,0,0, 0,0,1)
rotate(40*t)
scale(101)
for iz,zv in ipairs(scene) do
for iy,yv in ipairs(zv) do
for ix, xv in ipairs(yv) do
pushMatrix()
translate(ix-1,iy-1,-iz+1)
if xv > 0 then
geometry[xv]:draw()
end
popMatrix()
end
end
end
t = t + DeltaTime
end
Block = class()
function Block:init()
-- you can accept and set parameters here
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
}
local triangles = {
1,2,3,
1,3,4,
2,6,7,
2,7,3,
6,5,8,
6,8,7,
5,1,4,
5,4,8,
4,3,7,
4,7,8,
5,6,2,
5,2,1
}
local cubeVerts = {}
for i=1,#triangles do
cubeVerts[i]=vertices[triangles[i]]
end
local normalVecs = {}
for i=1,6 do normalVecs[i]=vec3(0,0,1) end
for i=7,12 do normalVecs[i]=vec3(1,0,0) end
for i=13,18 do normalVecs[i]=vec3(0,0,-1) end
for i=19,24 do normalVecs[i]=vec3(-1,0,0) end
for i=25,30 do normalVecs[i]=vec3(0,1,0) end
for i=31,36 do normalVecs[i]=vec3(0,-1,0) end
--for i,v in ipairs(triangles) do
-- table.insert(normalVecs,normals[v])
--end
-- 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 triTexCoords = {1,2,4,
1,4,3,
1,2,4,
1,4,3,
1,2,4,
1,4,3,
1,2,4,
1,4,3,
1,2,4,
1,4,3,
1,2,4,
1,4,3
}
cubetexCoords = {}
--for i,v in ipairs(triTexCoords) do
-- table.insert(cubetexCoords,texvertices[v])
--end
for i=1,#triTexCoords do
cubetexCoords[i]=texvertices[triTexCoords[i]]
end
self.cube = mesh()
self.cube.texture = "Planet Cute:Dirt Block"
self.cube.vertices = cubeVerts
self.cube.texCoords = cubetexCoords
self.cube.normals = normalVecs
self.cube.shader= shader(S.v,S.f)
--self.cube.shader.mMatrix = modelMatrix()
--self.cube.shader.mView = viewMatrix()
--self.cube.shader.mProjection = projectionMatrix()
self.cube:setColors(255,255,255,255)
end
function Block:draw()
-- Codea does not automatically call this method
self.cube.shader.mModel = modelMatrix()
self.cube:draw()
end
function Block:touched(touch)
-- Codea does not automatically call this method
end
S = {
v = [[
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
attribute vec3 normal;
uniform lowp mat4 mModel;
//uniform mat4 mView;
//uniform mat4 mProjection;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
varying highp vec4 vNormal;
void main()
{
vColor = color;
vTexCoord = texCoord;
vNormal = mModel * vec4(normal,0.0);
gl_Position = modelViewProjection * position;
}
]],
f = [[
precision highp float;
uniform lowp sampler2D texture;
varying lowp vec4 vColor;
varying highp vec2 vTexCoord;
varying highp vec4 vNormal;
void main()
{
highp vec4 lightDir = normalize(vec4(1,1,1,0));
highp float lit = dot(lightDir, normalize(vNormal));
//lowp vec4 col = texture2D( texture, vTexCoord ) * vColor;
//Commenting out the above, and uncommenting the line below,
//the shader does not work. There is not even a black cube, just nothing
lowp vec4 col = texture2D( texture, vTexCoord ) * vColor * lit;
col.a=1.0;
gl_FragColor = col;
}
]]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment