This commit is contained in:
Kbz-8
2024-02-12 18:34:01 +01:00
parent 54c4ae77f2
commit 7f26b839c3
3 changed files with 28 additions and 55 deletions

View File

@ -8,33 +8,10 @@ const vertex_shader_source = `
uniform mat4 uNormalMat;
varying highp vec3 vLighting;
/*
vec3 lightColor = vec3(1.0, 0.8, 0.8);
vec3 lightDir = normalize(vec3(-0.2, -1.0, -0.3));
vec3 viewPos = vec3(0.0, 0.0, 0.0);
vec3 calculateLighting()
{
vec3 norm = normalize(aNormal);
// ambient
vec3 ambient = 0.3 * lightColor;
// diffuse
float diff = max(dot(lightDir, norm), 0.0);
vec3 diffuse = diff * lightColor;
// specular
vec3 viewDir = normalize(viewPos - vec3(aPos.xyz));
vec3 reflectDir = reflect(-lightDir, norm);
vec3 halfwayDir = normalize(lightDir + viewDir);
float spec = pow(max(dot(norm, halfwayDir), 0.0), 32.0);
vec3 specular = vec3(0.3) * spec; // assuming bright white light color
return (ambient + diffuse + specular);
}
*/
void main()
{
gl_Position = uView * uProj * uMod * aPos;
gl_Position = uProj * uView * uMod * aPos;
highp vec3 ambientLight = vec3(0.3, 0.3, 0.3);
highp vec3 directionalLightColor = vec3(1, 1, 1);
@ -44,8 +21,6 @@ const vertex_shader_source = `
highp float directional = max(dot(transformedNormal.xyz, directionalVector), 0.0);
vLighting = ambientLight + (directionalLightColor * directional);
// vLighting = calculateLighting();
}
`;
@ -59,37 +34,37 @@ const fragment_shader_source = `
}
`;
function initShaderProgram(gl)
export function initShaderProgram(gl)
{
const vertexShader = loadShader(gl, gl.VERTEX_SHADER, vertex_shader_source);
const fragmentShader = loadShader(gl, gl.FRAGMENT_SHADER, fragment_shader_source);
const shader_prog = gl.createProgram();
gl.attachShader(shader_prog, vertexShader);
gl.attachShader(shader_prog, fragmentShader);
gl.linkProgram(shader_prog);
if(!gl.getProgramParameter(shader_prog, gl.LINK_STATUS))
{
alert(`Unable to initialize the shader program: ${gl.getProgramInfoLog(shaderProgram)}`);
return null;
}
const prog = gl.createProgram();
gl.attachShader(prog, vertexShader);
gl.attachShader(prog, fragmentShader);
gl.linkProgram(prog);
shaderInfos = {
program: shader_prog,
program: prog,
attribLocations: {
vertexPosition: gl.getAttribLocation(shader_prog, "aPos"),
vertexNormal: gl.getAttribLocation(shader_prog, "aNormal"),
vertexPosition: gl.getAttribLocation(prog, "aPos"),
vertexNormal: gl.getAttribLocation(prog, "aNormal"),
},
uniformLocations: {
projectionMatrix: gl.getUniformLocation(shader_prog, "uProj"),
modelMatrix: gl.getUniformLocation(shader_prog, "uMod"),
viewMatrix: gl.getUniformLocation(shader_prog, "uView"),
normalMatrix: gl.getUniformLocation(shader_prog, "uNormalMat"),
projectionMatrix: gl.getUniformLocation(prog, "uProj"),
modelMatrix: gl.getUniformLocation(prog, "uMod"),
viewMatrix: gl.getUniformLocation(prog, "uView"),
normalMatrix: gl.getUniformLocation(prog, "uNormalMat"),
},
};
return shader_prog;
if(!gl.getProgramParameter(prog, gl.LINK_STATUS))
{
alert(`Unable to initialize the shader program: ${gl.getProgramInfoLog(prog)}`);
return null;
}
return prog;
}
function loadShader(gl, type, source)
@ -110,4 +85,3 @@ function loadShader(gl, type, source)
}
export let shaderInfos;
export { initShaderProgram };