webgl/src/client/main.ts

123 lines
3.2 KiB
TypeScript
Raw Normal View History

import $ from 'jquery';
2020-11-25 20:07:21 +00:00
import vsSource from './shaders/shader.vert';
import fsSource4 from './shaders/texture.frag';
import {fetchObj, updateObj} from './objutils';
import {initShaderProgram} from './shaders';
2020-11-24 18:18:45 +00:00
import {loadTexture} from './texture';
2020-11-26 14:15:55 +00:00
import {drawScene} from './draw';
2020-11-22 14:16:08 +00:00
import {uiUpdateParams,
uiUpdateTexture,
uiUpdateObject,
uiUpdateShader} from './uijquery';
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,
texture: null,
params: null,
buffers: null,
programInfo: null,
shaderProgram: null,
fragmentShader: null,
vertexShader: null,
};
2020-11-22 14:09:28 +00:00
const canvas: any = document.querySelector('#glCanvas')!;
context.gl = canvas.getContext('webgl');
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>`);
}
const data = await fetchObj('/static/objs/racer.obj');
2020-11-25 12:51:39 +00:00
const distance: any = $('#distance').val();
const distanceFine: any = $('#distancefine').val();
const instanceNumber: any = $('#instance').val();
context.params = {
2020-11-25 12:51:39 +00:00
distance: parseFloat(distance) + parseFloat(distanceFine),
circleSize: $('#circlesize').val(),
fov: $('#fov').val(),
length: 0,
avg: {
x: 0,
y: 0,
z: 0,
},
range: 0,
rot: {
x: $('#rotx').val(),
y: $('#roty').val(),
z: $('#rotz').val(),
},
2020-11-25 12:45:25 +00:00
rotSpeed: $('#rotspeed').val(),
instanceNumber: parseInt(instanceNumber),
squareRotation: 0,
};
[context.buffers, context.params] = updateObj(
context.gl, data, context.buffers, context.params, true);
initShaderProgram(context, vsSource, fsSource4);
context.programInfo = {
program: context.shaderProgram,
2020-11-24 12:53:30 +00:00
attribLocations: {
vertexPosition: context.gl.getAttribLocation(context.shaderProgram,
2020-11-24 12:53:30 +00:00
'aVertexPosition'),
vertexNormal: context.gl.getAttribLocation(context.shaderProgram,
2020-11-24 12:53:30 +00:00
'aVertexNormal'),
textureCoord: context.gl.getAttribLocation(context.shaderProgram,
2020-11-24 18:18:45 +00:00
'aTextureCoord'),
2020-11-24 12:53:30 +00:00
},
uniformLocations: {
projectionMatrix: context.gl.getUniformLocation(
context.shaderProgram, 'uProjectionMatrix'),
viewMatrix: context.gl.getUniformLocation(
context.shaderProgram, 'uviewMatrix'),
modelMatrix: context.gl.getUniformLocation(
context.shaderProgram, 'umodelMatrix'),
normalModelMatrix: context.gl.getUniformLocation(
context.shaderProgram, 'unormalModelMatrix'),
uSampler: context.gl.getUniformLocation(
context.shaderProgram, 'uSampler'),
2020-11-24 12:53:30 +00:00
},
};
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;
drawScene(context.gl,
context.programInfo,
context.buffers,
context.params,
context.texture);
2020-11-24 12:53:30 +00:00
requestAnimationFrame(render);
}
uiUpdateParams(context.params);
uiUpdateTexture(context);
uiUpdateObject(context);
uiUpdateShader(context);
2020-11-24 12:53:30 +00:00
requestAnimationFrame(render);
2020-11-22 14:09:28 +00:00
}