Okay, so I am a bit puzzled. I am trying to write a super-basic lighting shader, and everything seems to go great until I go and try to multiply the texture color by the dot product of the light and normal vectors. Then, the cube doesn’t render at all.
Here is my vertex shader:
uniform mat4 modelViewProjection;
attribute vec4 position;
attribute vec4 color;
attribute vec2 texCoord;
attribute vec3 normals;
uniform 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 = normalize(vec4(normals,0.0)*mModel);
gl_Position = modelViewProjection * position;
}
The fragment shader:
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, 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;
gl_FragColor = col;
}
Here is the code for the actual block I try to render:
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 normals = { vec3(-1,-1,1):normalize(), vec3(1,-1,1):normalize(), vec3(1,1,1):normalize(), vec3(-1,1,1):normalize(), vec3(-1,-1,-1):normalize(), vec3(1,-1,-1):normalize(), vec3(1,1,-1):normalize(), vec3(-1,1,-1):normalize() } 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,v in ipairs(triangles) do table.insert(cubeVerts,vertices[v]) end local normalVecs = {} 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 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("Documents:LightingTest") 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:draw() end function Block:touched(touch) -- Codea does not automatically call this method end ``` And finally, my Main:-- 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 ```