133 lines
3.3 KiB
TypeScript
133 lines
3.3 KiB
TypeScript
import $ from 'jquery';
|
|
import {fetchObj, loadObj} from './objutils';
|
|
import {loadTexture} from './texture';
|
|
|
|
/**
|
|
* Initialize the parameters
|
|
* @param {any} context the program context
|
|
*/
|
|
export function initParams(context: any) {
|
|
let instanceNumber = parseFloat(<string>$('#instance').val());
|
|
if (($('input[name=instance]:checked', '#instanceoptions').val() ==
|
|
'one')) {
|
|
instanceNumber = 1;
|
|
} else if (($('input[name=instance]:checked', '#instanceoptions').val() ==
|
|
'zero')) {
|
|
instanceNumber = 0;
|
|
}
|
|
context.params = {
|
|
distance: parseFloat(<string>$('#distance').val()) +
|
|
parseFloat(<string>$('#distancefine').val()),
|
|
circleSize: parseFloat(<string>$('#circlesize').val()),
|
|
fov: parseFloat(<string>$('#fov').val()),
|
|
len: 0,
|
|
range: 0,
|
|
rot: {
|
|
x: parseFloat(<string>$('#rotx').val()),
|
|
y: parseFloat(<string>$('#roty').val()),
|
|
z: parseFloat(<string>$('#rotz').val()),
|
|
},
|
|
pos: {
|
|
x: parseFloat(<string>$('#posx').val()),
|
|
y: parseFloat(<string>$('#posy').val()),
|
|
z: parseFloat(<string>$('#posz').val()),
|
|
},
|
|
scale: {
|
|
x: parseFloat(<string>$('#scalex').val()),
|
|
y: parseFloat(<string>$('#scaley').val()),
|
|
z: parseFloat(<string>$('#scalez').val()),
|
|
},
|
|
rotSpeed: parseFloat(<string>$('#rotspeed').val()),
|
|
instanceNumber: instanceNumber,
|
|
squareRotation: 0,
|
|
camRot: {
|
|
x: 0,
|
|
y: 0,
|
|
},
|
|
camPos: {
|
|
x: 0,
|
|
y: 0,
|
|
z: 0,
|
|
},
|
|
keyboard: {
|
|
w: false,
|
|
a: false,
|
|
s: false,
|
|
d: false,
|
|
space: false,
|
|
shift: false,
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Initialize the cache with an object
|
|
* @param {any} context the program context
|
|
*/
|
|
export async function initCache(context: any) {
|
|
const racerData = await fetchObj('/static/objs/racer.obj');
|
|
const racerObj = loadObj(racerData);
|
|
const racerTexture = loadTexture(context.gl, '/static/textures/racer.png');
|
|
context.cache = {
|
|
objs: {
|
|
fox: null,
|
|
mecha: null,
|
|
racer: racerObj,
|
|
sphere: null,
|
|
teapot: null,
|
|
},
|
|
textures: {
|
|
fox: null,
|
|
ice: null,
|
|
noise: null,
|
|
racerWireframe: null,
|
|
racer: racerTexture,
|
|
wall: null,
|
|
},
|
|
};
|
|
context.obj = {
|
|
positions: racerObj.positions,
|
|
indices: racerObj.indices,
|
|
normals: racerObj.normals,
|
|
uvs: racerObj.uvs,
|
|
};
|
|
context.params.len = racerObj.indices.length;
|
|
context.params.range = racerObj.range;
|
|
context.texture = racerTexture;
|
|
}
|
|
|
|
/**
|
|
* Set the program info
|
|
* @param {any} context the program context
|
|
* @param {any} shaderProgram the shaderProgram needing info to be set
|
|
* @return {any} the set program info
|
|
*/
|
|
export function setProgramInfo(context: any,
|
|
shaderProgram: any) {
|
|
return {
|
|
program: shaderProgram,
|
|
attribLocations: {
|
|
vertexPosition: context.gl.getAttribLocation(shaderProgram,
|
|
'aVertexPosition'),
|
|
vertexNormal: context.gl.getAttribLocation(shaderProgram,
|
|
'aVertexNormal'),
|
|
textureCoord: context.gl.getAttribLocation(shaderProgram,
|
|
'aTextureCoord'),
|
|
},
|
|
uniformLocations: {
|
|
projectionMatrix: context.gl.getUniformLocation(
|
|
shaderProgram, 'uProjectionMatrix'),
|
|
viewMatrix: context.gl.getUniformLocation(
|
|
shaderProgram, 'uviewMatrix'),
|
|
modelMatrix: context.gl.getUniformLocation(
|
|
shaderProgram, 'umodelMatrix'),
|
|
normalMatrix: context.gl.getUniformLocation(
|
|
shaderProgram, 'unormalMatrix'),
|
|
uSampler: context.gl.getUniformLocation(
|
|
shaderProgram, 'uSampler'),
|
|
time: context.gl.getUniformLocation(
|
|
shaderProgram, 'time'),
|
|
},
|
|
};
|
|
}
|