webgl/src/client/main.ts

86 lines
2.1 KiB
TypeScript
Raw Normal View History

2020-11-25 20:07:21 +00:00
import vsSource from './shaders/shader.vert';
2020-11-27 16:11:28 +00:00
import texture from './shaders/texture.frag';
import {loadObjBuffers} from './objutils';
import {initShaderProgram} from './shaders';
// import {loadTexture} from './texture';
2020-11-26 14:15:55 +00:00
import {drawScene} from './draw';
import {initCache, initParams, setProgramInfo} from './init';
2020-11-22 14:16:08 +00:00
import {uiUpdateParams,
uiUpdateTexture,
uiUpdateObject,
2020-11-28 17:53:59 +00:00
uiUpdateShader,
uiUpdateScene} from './uijquery';
import {initUX} from './ux';
2020-11-27 12:12:24 +00:00
import {updateCamera} from './camera';
2020-11-22 14:09:28 +00:00
main();
/**
* The program purpose is encapsulated in a main function
*/
2020-11-24 12:53:30 +00:00
async function main() {
const context: any = {
gl: null,
2020-11-28 17:53:59 +00:00
obj: null,
texture: null,
params: null,
buffers: null,
programInfo: null,
shaderProgram: null,
fragmentShader: null,
vertexShader: null,
cache: null,
2020-11-28 17:53:59 +00:00
scene: [],
};
2020-11-22 14:09:28 +00:00
const canvas: any = document.querySelector('#glCanvas')!;
2020-11-27 16:11:28 +00:00
context.gl = canvas.getContext('webgl2');
2020-11-22 14:09:28 +00:00
if (context.gl == null) {
2020-11-22 14:09:28 +00:00
canvas.parentNode.removeChild(canvas);
document.getElementById('root')!.insertAdjacentHTML('beforeend',
`<p>Unable to initialize WebGL. Your browser or machine may not
support it.</p>`);
}
initParams(context);
await initCache(context);
loadObjBuffers(context, context.cache.objs.racer);
2020-11-27 16:11:28 +00:00
initShaderProgram(context, vsSource, texture);
setProgramInfo(context);
// context.texture = loadTexture(context.gl, '/static/textures/racer.png');
2020-11-24 12:53:30 +00:00
let then = 0;
let changed = false;
/**
* Draws the scene repeatedly
* @param {number} now the current time
*/
function render(now: any) {
now *= 0.001;
const deltaTime = now - then;
context.params.squareRotation += deltaTime * context.params.rotSpeed;
2020-11-24 12:53:30 +00:00
if (now >= 1 && changed == false) {
changed = true;
2020-11-22 14:09:28 +00:00
}
2020-11-24 12:53:30 +00:00
then = now;
2020-11-27 12:12:24 +00:00
updateCamera(context);
drawScene(context.gl,
context.programInfo,
context.buffers,
context.params,
2020-11-27 17:55:43 +00:00
context.texture,
2020-11-28 17:53:59 +00:00
context.scene,
2020-11-27 17:55:43 +00:00
now);
2020-11-24 12:53:30 +00:00
requestAnimationFrame(render);
}
uiUpdateParams(context.params);
uiUpdateTexture(context);
uiUpdateObject(context);
uiUpdateShader(context);
2020-11-28 17:53:59 +00:00
uiUpdateScene(context);
2020-11-27 11:13:53 +00:00
initUX(context, canvas);
2020-11-24 12:53:30 +00:00
requestAnimationFrame(render);
2020-11-22 14:09:28 +00:00
}