webgl/src/client/draw.ts

101 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-11-28 17:53:59 +00:00
import {initMatrices, initModelAndNormalMatrices} from './matrix';
2020-11-26 14:15:55 +00:00
import {initVertexAttribs} from './vertexattrib';
/**
* Draws a webgl scene
* @param {any} gl the WebGL context
* @param {any} programInfo WebGL program information
* @param {any} buffers the buffers to draw
* @param {number} params various parameterss
* @param {any} texture the texture to load
2020-11-28 17:53:59 +00:00
* @param {Array<any>} scene the scene array
2020-11-27 17:55:43 +00:00
* @param {number} now the current time
2020-11-26 14:15:55 +00:00
*/
export function drawScene(gl: any,
programInfo: any,
buffers: any,
params: any,
2020-11-27 17:55:43 +00:00
texture: any,
2020-11-28 17:53:59 +00:00
scene: Array<any>,
2020-11-27 17:55:43 +00:00
now: number) {
2020-11-26 14:15:55 +00:00
gl.clearColor(0.0, 0.0, 0.0, 1.0);
gl.clearDepth(1.0);
gl.enable(gl.DEPTH_TEST);
gl.depthFunc(gl.LEQUAL);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
initVertexAttribs(gl, programInfo, buffers);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers.indices);
gl.useProgram(programInfo.program);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.uniform1i(programInfo.uniformLocations.uSampler, 0);
2020-11-27 17:55:43 +00:00
gl.uniform1f(programInfo.uniformLocations.time, now);
2020-11-26 14:15:55 +00:00
const [
projectionMatrix,
viewMatrix,
modelMatrix,
normalMatrix,
2020-11-26 14:15:55 +00:00
] = initMatrices(gl, params);
for (let i = 0; i < params.instanceNumber; i++) {
gl.uniformMatrix4fv(
programInfo.uniformLocations.projectionMatrix,
false,
projectionMatrix);
gl.uniformMatrix4fv(
programInfo.uniformLocations.viewMatrix,
false,
viewMatrix);
gl.uniformMatrix4fv(
programInfo.uniformLocations.modelMatrix,
false,
modelMatrix[i]);
gl.uniformMatrix4fv(
programInfo.uniformLocations.normalMatrix,
2020-11-26 14:15:55 +00:00
false,
normalMatrix[i]);
const vertexCount = params.len;
2020-11-26 14:15:55 +00:00
const type = gl.UNSIGNED_SHORT;
const offset = 0;
gl.drawElements(gl.TRIANGLES, vertexCount, type, offset);
}
2020-11-28 17:53:59 +00:00
for (let i = 0; i < scene.length; i++) {
initVertexAttribs(gl, scene[i].programInfo, scene[i].buffers);
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, scene[i].buffers.indices);
gl.useProgram(scene[i].programInfo.program);
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, scene[i].texture);
gl.uniform1i(scene[i].programInfo.uniformLocations.uSampler, 0);
gl.uniform1f(scene[i].programInfo.uniformLocations.time, now);
const [
modelMatrix,
normalMatrix,
] = initModelAndNormalMatrices(scene[i].params);
for (let j = 0; j < scene[i].params.instanceNumber; j++) {
gl.uniformMatrix4fv(
scene[i].programInfo.uniformLocations.projectionMatrix,
false,
projectionMatrix);
gl.uniformMatrix4fv(
scene[i].programInfo.uniformLocations.viewMatrix,
false,
viewMatrix);
gl.uniformMatrix4fv(
scene[i].programInfo.uniformLocations.modelMatrix,
false,
modelMatrix[j]);
gl.uniformMatrix4fv(
scene[i].programInfo.uniformLocations.normalMatrix,
false,
normalMatrix[j]);
const vertexCount = scene[i].params.len;
const type = gl.UNSIGNED_SHORT;
const offset = 0;
gl.drawElements(gl.TRIANGLES, vertexCount, type, offset);
}
}
2020-11-26 14:15:55 +00:00
}