42_ft_transcendence/frontend/static/js/3D/cube.js
2024-02-22 06:01:56 +01:00

43 lines
824 B
JavaScript

import { shaderInfos } from "../3D/shaders.js"
function renderCube(ctx, x, y, z, angle = 0, sx = 1, sy = 1, sz = 1)
{
const modelMatrix = mat4.create();
mat4.translate(
modelMatrix,
modelMatrix,
[x, y, z]
);
mat4.rotate(
modelMatrix,
modelMatrix,
angle,
[0, 1, 0],
);
mat4.scale(
modelMatrix,
modelMatrix,
[sx, sy, sz]
);
mat4.translate(
modelMatrix,
modelMatrix,
[-1, 0, 0] // wtf, this works ?
);
const normalMatrix = mat4.create();
mat4.invert(normalMatrix, modelMatrix);
mat4.transpose(normalMatrix, normalMatrix);
ctx.uniformMatrix4fv(shaderInfos.uniformLocations.modelMatrix, false, modelMatrix);
ctx.uniformMatrix4fv(shaderInfos.uniformLocations.normalMatrix, false, normalMatrix);
ctx.drawElements(ctx.TRIANGLES, 36, ctx.UNSIGNED_SHORT, 0);
}
export { renderCube };