I've been trying to correctly generate 3D figures like planes, spheres and cubes. But I'm trying to apply illumination to them so I also generate normals.
Here's my output from a simple plane, I'm only using triangles and I'm using a positional light:
Vertice 0: 3.000000 3.000000 0.000000
Vertice 1: 3.000000 -3.000000 0.000000
Vertice 2: -3.000000 -3.000000 0.000000
Vertice 3: -3.000000 3.000000 0.000000
Normal: 0.000000 0.000000 1.000000
Normal: -0.000000 0.000000 1.000000
And here's the result:
Sometimes it lights it right and sometimes it shows in that way, with the 2 OBVIOUS triangles.
What do you think might be the reason for this?
Here's the vertex shader:
attribute highp vec4 vertices;
attribute highp vec3 normals;
uniform mediump mat4 projectMatrix;
uniform mediump mat4 viewMatrix;
uniform mediump mat4 modelMatrix;
out highp vec3 normal;
out highp vec4 pos_ws;
out lowp vec4 lightp_cs;
void main (void)
{
vec4 PPos= vec4(3.0, 3.0, 3.0, 1.0);
gl_Position= projectMatrix*viewMatrix*modelMatrix*vertices;
normal= normals;
pos_ws= viewMatrix*modelMatrix * vec4(vertices);
lightp_cs= viewMatrix* PPos;
}
And this is the fragment shader:
in highp vec3 normal;
in highp vec4 pos_ws;
in lowp vec4 lightp_cs;
void main(void)
{
vec4 ambient= vec4 (0.276, 0.342, 0.417, 1.0);
vec4 DiffuseColor= vec4 (0.40234375, 0.40234375, 0.40234375, 1.0);
vec4 PColor= vec4(0.7, 0.7, 0.7, 1.0);
//vec4 PPos= vec4(3.0, 3.0, 3.0, 1.0);
vec3 PAtt= vec3(0.6, 0.050, 0.005);
// Para la luz puntual
//vec4 vToLight= pos_ws - PPos;
vec4 vToLight= pos_ws - lightp_cs;
vToLight= normalize(vToLight);
float distToLight= length(vToLight);
vec4 Vnormal= vec4(normal, 1.0);
float cosNPL= clamp(dot(Vnormal, -vToLight), 0, 1);
float PLAtt= (PAtt.x + PAtt.y * distToLight + PAtt.z * distToLight * distToLight);
gl_FragColor= ambient + (DiffuseColor * (cosNPL * PColor)/PLAtt); // Luz Puntual
}