pouic long boiiiiiiii

This commit is contained in:
Kbz-8
2024-01-24 23:49:04 +01:00
parent c429586dc7
commit d0b0d83209
3 changed files with 154 additions and 52 deletions

View File

@ -17,6 +17,7 @@ export default class extends AbstractView
this.shader_prog = null;
this.buffers = null;
this.rotation = 0.0;
this.programInfo = null;
}
initGL()
@ -32,6 +33,19 @@ export default class extends AbstractView
this.shader_prog = initShaderProgram(this.gl);
this.buffers = initBuffers(this.gl);
this.programInfo = {
program: this.shader_prog,
attribLocations: {
vertexPosition: this.gl.getAttribLocation(this.shader_prog, "aPos"),
vertexNormal: this.gl.getAttribLocation(this.shader_prog, "aNormal"),
},
uniformLocations: {
projectionMatrix: this.gl.getUniformLocation(this.shader_prog, "uProj"),
modelViewMatrix: this.gl.getUniformLocation(this.shader_prog, "uModView"),
normalMatrix: this.gl.getUniformLocation(this.shader_prog, "uNormalMat"),
},
};
}
async join_game()
@ -57,20 +71,6 @@ export default class extends AbstractView
if(canvas === null)
return 1;
const programInfo = {
program: this.shader_prog,
attribLocations: {
vertexPosition: this.gl.getAttribLocation(this.shader_prog, "aPos"),
},
uniformLocations: {
projectionMatrix: this.gl.getUniformLocation(
this.shader_prog,
"uProj"
),
modelViewMatrix: this.gl.getUniformLocation(this.shader_prog, "uModView"),
},
};
this.gl.clearColor(0.0, 0.0, 0.0, 1.0);
this.gl.clearDepth(1.0);
this.gl.enable(this.gl.DEPTH_TEST);
@ -85,63 +85,106 @@ export default class extends AbstractView
mat4.perspective(projectionMatrix, fieldOfView, aspect, zNear, zFar);
const modelViewMatrix = mat4.create();
this.setPositionAttribute();
this.setNormalAttribute();
this.rotation += 0.1;
this.gl.useProgram(this.programInfo.program);
this.gl.uniformMatrix4fv(
this.programInfo.uniformLocations.projectionMatrix,
false,
projectionMatrix
);
this.rotation += 0.025;
this.renderCube(0.0, 0.0, -15.0, this.rotation);
this.renderCube(0.0, 1.0, -15.0, this.rotation + 0.1);
this.renderCube(-2.0, 0.0, -15.0, this.rotation + 4.3);
this.renderCube(2.0, 3.0, -15.0, this.rotation - 12.4, 5);
}
renderCube(x, y, z, angle = 0, sx = 1, sy = 1, sz = 1)
{
const modelViewMatrix = mat4.create();
mat4.translate(
modelViewMatrix,
modelViewMatrix,
[-0.0, 0.0, -6.0]
[x, y, z]
);
mat4.rotate(
modelViewMatrix,
modelViewMatrix,
this.rotation,
[1, 1, 1],
angle,
[0, 1, 1],
);
this.setPositionAttribute(programInfo);
this.gl.useProgram(programInfo.program);
this.gl.uniformMatrix4fv(
programInfo.uniformLocations.projectionMatrix,
false,
projectionMatrix
mat4.scale(
modelViewMatrix,
modelViewMatrix,
[sx, sy, sz]
);
const normalMatrix = mat4.create();
mat4.invert(normalMatrix, modelViewMatrix);
mat4.transpose(normalMatrix, normalMatrix);
this.gl.uniformMatrix4fv(
programInfo.uniformLocations.modelViewMatrix,
this.programInfo.uniformLocations.modelViewMatrix,
false,
modelViewMatrix
);
this.gl.uniformMatrix4fv(
this.programInfo.uniformLocations.normalMatrix,
false,
normalMatrix,
);
{
const offset = 0;
const vertexCount = 4;
this.gl.drawArrays(this.gl.TRIANGLE_STRIP, offset, vertexCount);
}
const vertexCount = 36;
const type = this.gl.UNSIGNED_SHORT;
const offset = 0;
this.gl.drawElements(this.gl.TRIANGLES, vertexCount, type, offset);
}
setNormalAttribute()
{
const numComponents = 3;
const type = this.gl.FLOAT;
const normalize = false;
const stride = 0;
const offset = 0;
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.buffers.normal);
this.gl.vertexAttribPointer(
this.programInfo.attribLocations.vertexNormal,
numComponents,
type,
normalize,
stride,
offset,
);
this.gl.enableVertexAttribArray(this.programInfo.attribLocations.vertexNormal);
}
setPositionAttribute(programInfo)
{
const numComponents = 2;
const numComponents = 3;
const type = this.gl.FLOAT;
const normalize = false;
const stride = 0;
const offset = 0;
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.buffers.position);
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, this.buffers.vertex);
this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER, this.buffers.index);
this.gl.vertexAttribPointer(
programInfo.attribLocations.vertexPosition,
this.programInfo.attribLocations.vertexPosition,
numComponents,
type,
normalize,
stride,
offset
);
this.gl.enableVertexAttribArray(programInfo.attribLocations.vertexPosition);
this.gl.enableVertexAttribArray(this.programInfo.attribLocations.vertexPosition);
}
render_game()