2020-11-22 14:09:28 +00:00
|
|
|
// @ts-ignore
|
|
|
|
import mat4 from 'gl-mat4';
|
2020-11-23 12:47:44 +00:00
|
|
|
// @ts-ignore
|
2020-11-24 18:18:45 +00:00
|
|
|
import {convert} from './objparser';
|
2020-11-24 11:10:57 +00:00
|
|
|
import $ from 'jquery';
|
|
|
|
|
2020-11-24 12:53:30 +00:00
|
|
|
import {initBuffers, deleteBuffers} from './buffers';
|
2020-11-24 11:10:57 +00:00
|
|
|
import {initShaderProgram} from './shaders';
|
|
|
|
import {changeFragmentShader} from './changeshader';
|
2020-11-24 18:18:45 +00:00
|
|
|
import {loadTexture} from './texture';
|
2020-11-22 14:09:28 +00:00
|
|
|
|
2020-11-22 14:16:08 +00:00
|
|
|
let squareRotation = 0.0;
|
|
|
|
|
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() {
|
2020-11-22 14:09:28 +00:00
|
|
|
const canvas: any = document.querySelector('#glCanvas')!;
|
2020-11-24 13:10:16 +00:00
|
|
|
const gl = canvas.getContext('webgl');
|
2020-11-22 14:09:28 +00:00
|
|
|
|
|
|
|
if (gl == null) {
|
|
|
|
canvas.parentNode.removeChild(canvas);
|
|
|
|
document.getElementById('root')!.insertAdjacentHTML('beforeend',
|
|
|
|
`<p>Unable to initialize WebGL. Your browser or machine may not
|
|
|
|
support it.</p>`);
|
|
|
|
}
|
|
|
|
|
2020-11-23 16:07:53 +00:00
|
|
|
/* eslint-disable */
|
|
|
|
|
2020-11-22 14:09:28 +00:00
|
|
|
const vsSource = `
|
|
|
|
attribute vec4 aVertexPosition;
|
2020-11-23 16:07:53 +00:00
|
|
|
attribute vec4 aVertexNormal;
|
2020-11-24 18:18:45 +00:00
|
|
|
attribute vec2 aTextureCoord;
|
2020-11-22 14:09:28 +00:00
|
|
|
|
|
|
|
uniform mat4 uProjectionMatrix;
|
2020-11-23 16:07:53 +00:00
|
|
|
uniform mat4 uviewMatrix;
|
|
|
|
uniform mat4 umodelMatrix;
|
2020-11-25 11:43:13 +00:00
|
|
|
uniform mat4 unormalModelMatrix;
|
2020-11-22 14:09:28 +00:00
|
|
|
|
2020-11-23 19:50:43 +00:00
|
|
|
varying highp vec4 vNormal;
|
|
|
|
varying highp vec3 vPosition;
|
2020-11-24 18:18:45 +00:00
|
|
|
varying highp vec2 vTextureCoord;
|
|
|
|
|
2020-11-23 12:47:44 +00:00
|
|
|
void main()
|
|
|
|
{
|
2020-11-22 14:09:28 +00:00
|
|
|
gl_Position = uProjectionMatrix *
|
2020-11-23 16:07:53 +00:00
|
|
|
uviewMatrix *
|
2020-11-25 11:43:13 +00:00
|
|
|
unormalModelMatrix *
|
2020-11-23 16:07:53 +00:00
|
|
|
umodelMatrix *
|
2020-11-22 14:09:28 +00:00
|
|
|
aVertexPosition;
|
2020-11-23 16:07:53 +00:00
|
|
|
vPosition = vec3(aVertexPosition);
|
2020-11-25 11:43:13 +00:00
|
|
|
vNormal = unormalModelMatrix * aVertexNormal;
|
2020-11-24 18:18:45 +00:00
|
|
|
vTextureCoord = aTextureCoord;
|
2020-11-22 14:09:28 +00:00
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2020-11-23 12:47:44 +00:00
|
|
|
const fsSource = `
|
2020-11-23 19:50:43 +00:00
|
|
|
precision highp float;
|
2020-11-23 16:07:53 +00:00
|
|
|
|
2020-11-25 12:15:26 +00:00
|
|
|
varying highp vec2 vTextureCoord;
|
2020-11-23 19:50:43 +00:00
|
|
|
varying highp vec4 vNormal;
|
|
|
|
varying highp vec3 vPosition;
|
2020-11-23 12:47:44 +00:00
|
|
|
|
2020-11-25 12:15:26 +00:00
|
|
|
uniform sampler2D uSampler;
|
|
|
|
|
2020-11-23 19:40:51 +00:00
|
|
|
vec3 extremize(vec3 v, float n) {
|
|
|
|
if (v.x > n / 2.)
|
|
|
|
v.x = n;
|
|
|
|
else
|
|
|
|
v.x = 0.;
|
|
|
|
if (v.y > n / 2.)
|
|
|
|
v.y = n;
|
|
|
|
else
|
|
|
|
v.y = 0.;
|
|
|
|
if (v.z > n / 2.)
|
|
|
|
v.z = n;
|
|
|
|
else
|
|
|
|
v.z = 0.;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2020-11-23 12:47:44 +00:00
|
|
|
void main() {
|
2020-11-24 15:30:59 +00:00
|
|
|
vec3 n = normalize(vec3(-50000., 100000., 50000.) - vPosition);
|
|
|
|
float diffuse = max(dot(normalize(vNormal.xyz), n), 0.);
|
2020-11-23 19:40:51 +00:00
|
|
|
float specular = pow(
|
|
|
|
max(dot(
|
2020-11-24 15:30:59 +00:00
|
|
|
reflect(n, normalize(vNormal.xyz)),
|
2020-11-23 19:40:51 +00:00
|
|
|
normalize(vec3(0., 0., -50.) - vPosition)),
|
|
|
|
0.), 10.);
|
2020-11-24 12:53:30 +00:00
|
|
|
vec3 tmp = extremize(mod(vPosition.xyz + vec3(1000.), vec3(2.)), 2.);
|
|
|
|
float texture = (tmp.x + tmp.y + tmp.z) / 6.;
|
2020-11-24 15:30:59 +00:00
|
|
|
gl_FragColor = vec4((texture * diffuse * 0.8) + (texture * vec3(0.2)) + (specular * vec3(1.)), 1.0);
|
2020-11-23 12:47:44 +00:00
|
|
|
}`;
|
|
|
|
|
2020-11-24 11:10:57 +00:00
|
|
|
const fsSource2 = `
|
|
|
|
precision highp float;
|
|
|
|
|
2020-11-25 12:15:26 +00:00
|
|
|
varying highp vec2 vTextureCoord;
|
2020-11-24 11:10:57 +00:00
|
|
|
varying highp vec4 vNormal;
|
|
|
|
varying highp vec3 vPosition;
|
|
|
|
|
2020-11-25 12:15:26 +00:00
|
|
|
uniform sampler2D uSampler;
|
|
|
|
|
2020-11-24 11:10:57 +00:00
|
|
|
vec3 extremize(vec3 v, float n) {
|
|
|
|
if (v.x > n / 2.)
|
|
|
|
v.x = n;
|
|
|
|
else
|
|
|
|
v.x = 0.;
|
|
|
|
if (v.y > n / 2.)
|
|
|
|
v.y = n;
|
|
|
|
else
|
|
|
|
v.y = 0.;
|
|
|
|
if (v.z > n / 2.)
|
|
|
|
v.z = n;
|
|
|
|
else
|
|
|
|
v.z = 0.;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
void main() {
|
2020-11-24 15:30:59 +00:00
|
|
|
vec3 n = normalize(vec3(-50000., 100000., 50000.) - vPosition);
|
|
|
|
float diffuse = max(dot(normalize(vNormal.xyz), n), 0.);
|
2020-11-24 11:10:57 +00:00
|
|
|
float specular = pow(
|
|
|
|
max(dot(
|
2020-11-24 15:30:59 +00:00
|
|
|
reflect(n, normalize(vNormal.xyz)),
|
2020-11-24 11:10:57 +00:00
|
|
|
normalize(vec3(0., 0., -50.) - vPosition)),
|
|
|
|
0.), 10.);
|
2020-11-24 12:53:30 +00:00
|
|
|
vec3 texture = extremize(mod(vPosition.xyz + vec3(1000.), vec3(2.)), 2.) / vec3(2);
|
2020-11-24 15:30:59 +00:00
|
|
|
gl_FragColor = vec4((texture * diffuse * 0.8) + (texture * vec3(0.2)) + (specular * vec3(1.)), 1.0);
|
|
|
|
}`;
|
|
|
|
|
|
|
|
const fsSource3 = `
|
|
|
|
precision highp float;
|
|
|
|
|
2020-11-25 12:15:26 +00:00
|
|
|
varying highp vec2 vTextureCoord;
|
2020-11-24 15:30:59 +00:00
|
|
|
varying highp vec4 vNormal;
|
|
|
|
varying highp vec3 vPosition;
|
|
|
|
|
2020-11-25 12:15:26 +00:00
|
|
|
uniform sampler2D uSampler;
|
2020-11-24 15:30:59 +00:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
vec3 n = normalize(vec3(-50000., 100000., 50000.) - vPosition);
|
|
|
|
float diffuse = max(dot(normalize(vNormal.xyz), n), 0.);
|
|
|
|
float specular = pow(
|
|
|
|
max(dot(
|
|
|
|
reflect(n, normalize(vNormal.xyz)),
|
|
|
|
normalize(vec3(0., 0., -50.) - vPosition)),
|
|
|
|
0.), 10.);
|
|
|
|
gl_FragColor = vec4((diffuse * 0.8) + (vec3(0.2)) + (specular * vec3(1.)), 1.0);
|
2020-11-24 11:10:57 +00:00
|
|
|
}`;
|
2020-11-24 18:18:45 +00:00
|
|
|
|
|
|
|
const fsSource4 = `
|
|
|
|
precision highp float;
|
|
|
|
|
|
|
|
varying highp vec2 vTextureCoord;
|
|
|
|
|
|
|
|
varying highp vec4 vNormal;
|
|
|
|
varying highp vec3 vPosition;
|
|
|
|
uniform sampler2D uSampler;
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
highp vec4 texelColor = texture2D(uSampler, vTextureCoord);
|
|
|
|
vec3 n = normalize(vec3(-50000., 100000., 50000.) - vPosition);
|
|
|
|
float diffuse = max(dot(normalize(vNormal.xyz), n), 0.);
|
|
|
|
float specular = pow(
|
|
|
|
max(dot(
|
|
|
|
reflect(n, normalize(vNormal.xyz)),
|
|
|
|
normalize(vec3(0., 0., -50.) - vPosition)),
|
|
|
|
0.), 10.);
|
|
|
|
gl_FragColor = vec4((texelColor.xyz * diffuse * 0.8) + (texelColor.xyz * vec3(0.2)) + (specular * vec3(1.)), 1.0);
|
|
|
|
}`;
|
2020-11-24 11:10:57 +00:00
|
|
|
|
2020-11-25 11:43:13 +00:00
|
|
|
const fsSource5 = `
|
|
|
|
precision highp float;
|
|
|
|
|
|
|
|
varying highp vec2 vTextureCoord;
|
|
|
|
varying highp vec4 vNormal;
|
|
|
|
varying highp vec3 vPosition;
|
|
|
|
|
|
|
|
uniform sampler2D uSampler;
|
|
|
|
|
|
|
|
void make_kernel(inout vec4 n[9], sampler2D tex, vec2 coord)
|
|
|
|
{
|
|
|
|
float w = 1.0 / 640.;
|
|
|
|
float h = 1.0 / 640.;
|
|
|
|
|
|
|
|
n[0] = texture2D(tex, coord + vec2( -w, -h));
|
|
|
|
n[1] = texture2D(tex, coord + vec2(0.0, -h));
|
|
|
|
n[2] = texture2D(tex, coord + vec2( w, -h));
|
|
|
|
n[3] = texture2D(tex, coord + vec2( -w, 0.0));
|
|
|
|
n[4] = texture2D(tex, coord);
|
|
|
|
n[5] = texture2D(tex, coord + vec2( w, 0.0));
|
|
|
|
n[6] = texture2D(tex, coord + vec2( -w, h));
|
|
|
|
n[7] = texture2D(tex, coord + vec2(0.0, h));
|
|
|
|
n[8] = texture2D(tex, coord + vec2( w, h));
|
|
|
|
}
|
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
|
|
|
vec4 n[9];
|
|
|
|
make_kernel( n, uSampler, vTextureCoord);
|
|
|
|
|
|
|
|
vec4 sobel_edge_h = n[2] + (2.0*n[5]) + n[8] - (n[0] + (2.0*n[3]) + n[6]);
|
|
|
|
vec4 sobel_edge_v = n[0] + (2.0*n[1]) + n[2] - (n[6] + (2.0*n[7]) + n[8]);
|
|
|
|
vec4 sobel = sqrt((sobel_edge_h * sobel_edge_h) + (sobel_edge_v * sobel_edge_v));
|
|
|
|
|
|
|
|
if ((1. - sobel.r) + (1. - sobel.g) + (1. - sobel.b) < 2.5)
|
|
|
|
gl_FragColor = vec4(1.0 - sobel.rgb, 1.0 );
|
2020-11-25 12:15:26 +00:00
|
|
|
else {
|
|
|
|
vec3 n = normalize(vec3(-50000., 100000., 50000.) - vPosition);
|
|
|
|
float diffuse = max(dot(normalize(vNormal.xyz), n), 0.);
|
|
|
|
float specular = pow(
|
|
|
|
max(dot(
|
|
|
|
reflect(n, normalize(vNormal.xyz)),
|
|
|
|
normalize(vec3(0., 0., -50.) - vPosition)),
|
|
|
|
0.), 10.);
|
|
|
|
gl_FragColor = vec4((diffuse * 0.8) + (vec3(0.2)) + (specular * vec3(1.)), 1.0);
|
|
|
|
}
|
2020-11-25 11:43:13 +00:00
|
|
|
}`;
|
|
|
|
|
2020-11-23 16:07:53 +00:00
|
|
|
/* eslint-enable */
|
|
|
|
|
2020-11-24 12:53:30 +00:00
|
|
|
/**
|
|
|
|
* Fetch an obj file
|
|
|
|
* @param {string} url the url to fetch the object from
|
|
|
|
* @return {string} the raw data of the obj file
|
|
|
|
*/
|
|
|
|
async function getObj(url: string) {
|
|
|
|
const response = await fetch(url);
|
|
|
|
const data = await response.text();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:43:13 +00:00
|
|
|
const data = await getObj('/static/objs/racer.obj');
|
2020-11-24 21:12:32 +00:00
|
|
|
const params: any = {
|
|
|
|
distance: $('#distance').val(),
|
|
|
|
circleSize: $('#circlesize').val(),
|
|
|
|
fov: $('#fov').val(),
|
|
|
|
avg: {
|
|
|
|
x: 0,
|
|
|
|
y: 0,
|
|
|
|
z: 0,
|
|
|
|
},
|
2020-11-25 11:43:13 +00:00
|
|
|
rot: {
|
|
|
|
x: $('#rotx').val(),
|
|
|
|
y: $('#roty').val(),
|
|
|
|
z: $('#rotz').val(),
|
|
|
|
},
|
2020-11-24 21:12:32 +00:00
|
|
|
};
|
|
|
|
|
2020-11-24 12:53:30 +00:00
|
|
|
const [
|
|
|
|
positions,
|
|
|
|
normals,
|
|
|
|
uvs,
|
|
|
|
indices,
|
|
|
|
] = convert(data);
|
2020-11-24 21:12:32 +00:00
|
|
|
let x = 0;
|
|
|
|
let y = 0;
|
|
|
|
let z = 0;
|
|
|
|
for (let i = 0; i < positions.length; i++) {
|
|
|
|
if (i % 3 == 0) {
|
|
|
|
x += positions[i];
|
|
|
|
} else if (i % 3 == 1) {
|
|
|
|
y += positions[i];
|
|
|
|
} else {
|
|
|
|
z += positions[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
params.avg.x = x / (positions.length / 3);
|
|
|
|
params.avg.y = y / (positions.length / 3);
|
|
|
|
params.avg.z = z / (positions.length / 3);
|
2020-11-24 12:53:30 +00:00
|
|
|
let length = indices.length;
|
|
|
|
let [shaderProgram, fragmentShader]: any = initShaderProgram(gl,
|
|
|
|
vsSource,
|
2020-11-24 18:18:45 +00:00
|
|
|
fsSource4);
|
2020-11-24 12:53:30 +00:00
|
|
|
|
|
|
|
let programInfo: any = {
|
|
|
|
program: shaderProgram,
|
|
|
|
attribLocations: {
|
|
|
|
vertexPosition: gl.getAttribLocation(shaderProgram,
|
|
|
|
'aVertexPosition'),
|
|
|
|
vertexNormal: gl.getAttribLocation(shaderProgram,
|
|
|
|
'aVertexNormal'),
|
2020-11-24 18:18:45 +00:00
|
|
|
textureCoord: gl.getAttribLocation(shaderProgram,
|
|
|
|
'aTextureCoord'),
|
2020-11-24 12:53:30 +00:00
|
|
|
},
|
|
|
|
uniformLocations: {
|
|
|
|
projectionMatrix: gl.getUniformLocation(
|
|
|
|
shaderProgram, 'uProjectionMatrix'),
|
|
|
|
viewMatrix: gl.getUniformLocation(
|
|
|
|
shaderProgram, 'uviewMatrix'),
|
|
|
|
modelMatrix: gl.getUniformLocation(
|
|
|
|
shaderProgram, 'umodelMatrix'),
|
2020-11-25 11:43:13 +00:00
|
|
|
normalModelMatrix: gl.getUniformLocation(
|
|
|
|
shaderProgram, 'unormalModelMatrix'),
|
2020-11-24 18:18:45 +00:00
|
|
|
uSampler: gl.getUniformLocation(
|
|
|
|
shaderProgram, 'uSampler'),
|
2020-11-24 12:53:30 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-11-25 11:43:13 +00:00
|
|
|
let texture = loadTexture(gl, '/static/textures/racer.png');
|
2020-11-24 18:18:45 +00:00
|
|
|
let buffers = initBuffers(gl, positions, indices, normals, uvs);
|
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;
|
|
|
|
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(gl,
|
|
|
|
programInfo,
|
|
|
|
buffers,
|
|
|
|
deltaTime,
|
|
|
|
length,
|
2020-11-24 21:12:32 +00:00
|
|
|
params,
|
2020-11-24 18:18:45 +00:00
|
|
|
texture);
|
2020-11-24 12:53:30 +00:00
|
|
|
requestAnimationFrame(render);
|
|
|
|
}
|
2020-11-24 11:10:57 +00:00
|
|
|
|
2020-11-24 15:30:59 +00:00
|
|
|
/**
|
|
|
|
* Pushes a new obj file to the gl buffer
|
|
|
|
* @param {string} data the obj file to push
|
|
|
|
*/
|
|
|
|
function updateObj(data: string) {
|
|
|
|
const [
|
|
|
|
positions,
|
|
|
|
normals,
|
|
|
|
uvs,
|
|
|
|
indices,
|
|
|
|
] = convert(data);
|
|
|
|
length = indices.length;
|
2020-11-24 21:12:32 +00:00
|
|
|
let x = 0;
|
|
|
|
let y = 0;
|
|
|
|
let z = 0;
|
|
|
|
for (let i = 0; i < positions.length; i++) {
|
|
|
|
if (i % 3 == 0) {
|
|
|
|
x += positions[i];
|
|
|
|
} else if (i % 3 == 1) {
|
|
|
|
y += positions[i];
|
|
|
|
} else {
|
|
|
|
z += positions[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
params.avg.x = x / (positions.length / 3);
|
|
|
|
params.avg.y = y / (positions.length / 3);
|
|
|
|
params.avg.z = z / (positions.length / 3);
|
2020-11-24 15:30:59 +00:00
|
|
|
deleteBuffers(gl, buffers);
|
2020-11-24 18:18:45 +00:00
|
|
|
buffers = initBuffers(gl, positions, indices, normals, uvs);
|
2020-11-24 15:30:59 +00:00
|
|
|
}
|
|
|
|
|
2020-11-24 12:53:30 +00:00
|
|
|
$(function() {
|
2020-11-24 21:12:32 +00:00
|
|
|
$('#distance').on('input', function() {
|
|
|
|
const distance: any = $('#distance').val();
|
|
|
|
params.distance = parseFloat(distance);
|
2020-11-24 12:53:30 +00:00
|
|
|
});
|
2020-11-24 21:12:32 +00:00
|
|
|
$('#circlesize').on('input', function() {
|
|
|
|
const circleSize: any = $('#circlesize').val();
|
|
|
|
params.circleSize = parseFloat(circleSize);
|
|
|
|
});
|
2020-11-25 11:43:13 +00:00
|
|
|
$('#rotx').on('input', function() {
|
|
|
|
const rotx: any = $('#rotx').val();
|
|
|
|
params.rot.x = parseFloat(rotx);
|
|
|
|
});
|
|
|
|
$('#roty').on('input', function() {
|
|
|
|
const roty: any = $('#roty').val();
|
|
|
|
params.rot.y = parseFloat(roty);
|
|
|
|
});
|
|
|
|
$('#rotz').on('input', function() {
|
|
|
|
const rotz: any = $('#rotz').val();
|
|
|
|
params.rot.z = parseFloat(rotz);
|
|
|
|
});
|
2020-11-24 21:12:32 +00:00
|
|
|
$('#fov').on('input', function() {
|
|
|
|
const fov: any = $('#fov').val();
|
|
|
|
params.fov = parseFloat(fov);
|
2020-11-24 15:30:59 +00:00
|
|
|
});
|
2020-11-24 18:18:45 +00:00
|
|
|
$('#s_blackandwhite').on('click', function() {
|
2020-11-24 12:53:30 +00:00
|
|
|
[programInfo, fragmentShader] = changeFragmentShader(gl,
|
|
|
|
shaderProgram, fragmentShader, fsSource, vsSource);
|
|
|
|
});
|
2020-11-24 18:18:45 +00:00
|
|
|
$('#s_color').on('click', function() {
|
2020-11-24 12:53:30 +00:00
|
|
|
[programInfo, fragmentShader] = changeFragmentShader(gl,
|
|
|
|
shaderProgram, fragmentShader, fsSource2, vsSource);
|
|
|
|
});
|
2020-11-25 12:15:26 +00:00
|
|
|
$('#s_grey').on('click', function() {
|
2020-11-24 15:30:59 +00:00
|
|
|
[programInfo, fragmentShader] = changeFragmentShader(gl,
|
|
|
|
shaderProgram, fragmentShader, fsSource3, vsSource);
|
|
|
|
});
|
2020-11-24 18:18:45 +00:00
|
|
|
$('#s_texture').on('click', function() {
|
|
|
|
[programInfo, fragmentShader] = changeFragmentShader(gl,
|
|
|
|
shaderProgram, fragmentShader, fsSource4, vsSource);
|
|
|
|
});
|
2020-11-25 11:43:13 +00:00
|
|
|
$('#s_sobel').on('click', function() {
|
|
|
|
[programInfo, fragmentShader] = changeFragmentShader(gl,
|
|
|
|
shaderProgram, fragmentShader, fsSource5, vsSource);
|
|
|
|
});
|
2020-11-24 16:28:40 +00:00
|
|
|
$('#o_sphere').on('click', async function() {
|
2020-11-24 12:53:30 +00:00
|
|
|
const data = await getObj('/static/objs/sphere.obj');
|
2020-11-24 15:30:59 +00:00
|
|
|
updateObj(data);
|
2020-11-24 12:53:30 +00:00
|
|
|
});
|
2020-11-24 16:28:40 +00:00
|
|
|
$('#o_teapot').on('click', async function() {
|
2020-11-24 12:53:30 +00:00
|
|
|
const data = await getObj('/static/objs/teapot.obj');
|
2020-11-24 15:30:59 +00:00
|
|
|
updateObj(data);
|
|
|
|
});
|
2020-11-24 16:28:40 +00:00
|
|
|
$('#o_fox').on('click', async function() {
|
2020-11-24 15:30:59 +00:00
|
|
|
const data = await getObj('/static/objs/fox.obj');
|
|
|
|
updateObj(data);
|
2020-11-24 11:10:57 +00:00
|
|
|
});
|
2020-11-25 11:43:13 +00:00
|
|
|
$('#o_mecha').on('click', async function() {
|
|
|
|
const data = await getObj('/static/objs/mecha.obj');
|
|
|
|
updateObj(data);
|
|
|
|
});
|
|
|
|
$('#o_racer').on('click', async function() {
|
|
|
|
const data = await getObj('/static/objs/racer.obj');
|
|
|
|
updateObj(data);
|
|
|
|
});
|
2020-11-24 18:18:45 +00:00
|
|
|
$('#t_wall').on('click', async function() {
|
|
|
|
texture = loadTexture(gl, '/static/textures/wall.png');
|
|
|
|
});
|
|
|
|
$('#t_ice').on('click', async function() {
|
|
|
|
texture = loadTexture(gl, '/static/textures/ice.png');
|
|
|
|
});
|
|
|
|
$('#t_noise').on('click', async function() {
|
|
|
|
texture = loadTexture(gl, '/static/textures/noise.png');
|
|
|
|
});
|
|
|
|
$('#t_fox').on('click', async function() {
|
|
|
|
texture = loadTexture(gl, '/static/textures/fox.png');
|
|
|
|
});
|
2020-11-25 11:43:13 +00:00
|
|
|
$('#t_racer').on('click', async function() {
|
|
|
|
texture = loadTexture(gl, '/static/textures/racer.png');
|
|
|
|
});
|
|
|
|
$('#t_racer_wireframe').on('click', async function() {
|
|
|
|
texture = loadTexture(gl, '/static/textures/racer_wireframe.png');
|
|
|
|
});
|
2020-11-23 12:47:44 +00:00
|
|
|
});
|
2020-11-24 12:53:30 +00:00
|
|
|
requestAnimationFrame(render);
|
2020-11-22 14:09:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draw a webgl scene
|
|
|
|
* @param {any} gl the WebGL context
|
|
|
|
* @param {any} programInfo WebGL program information
|
|
|
|
* @param {any} buffers the buffers to draw
|
2020-11-22 14:16:08 +00:00
|
|
|
* @param {number} deltaTime the difference in time since last call
|
2020-11-23 12:47:44 +00:00
|
|
|
* @param {number} length the index buffer length
|
2020-11-24 21:12:32 +00:00
|
|
|
* @param {number} params various parameterss
|
2020-11-24 18:18:45 +00:00
|
|
|
* @param {any} texture the texture to load
|
2020-11-22 14:09:28 +00:00
|
|
|
*/
|
2020-11-23 12:47:44 +00:00
|
|
|
function drawScene(gl: any,
|
|
|
|
programInfo: any,
|
|
|
|
buffers: any,
|
|
|
|
deltaTime: number,
|
2020-11-24 12:10:47 +00:00
|
|
|
length: number,
|
2020-11-24 21:12:32 +00:00
|
|
|
params: any,
|
2020-11-24 18:18:45 +00:00
|
|
|
texture: any) {
|
2020-11-22 14:09:28 +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);
|
|
|
|
// Clear the canvas before we start drawing on it.
|
|
|
|
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
// Create a perspective matrix, a special matrix that is
|
|
|
|
// used to simulate the distortion of perspective in a camera.
|
|
|
|
// Our field of view is 45 degrees, with a width/height
|
|
|
|
// ratio that matches the display size of the canvas
|
|
|
|
// and we only want to see objects between 0.1 units
|
|
|
|
// and 100 units away from the camera.
|
2020-11-24 21:12:32 +00:00
|
|
|
const fieldOfView = params.fov * Math.PI / 180;
|
2020-11-22 14:09:28 +00:00
|
|
|
const aspect = gl.canvas.clientWidth / gl.canvas.clientHeight;
|
|
|
|
const zNear = 0.1;
|
2020-11-24 15:30:59 +00:00
|
|
|
const zFar = 1000.0;
|
2020-11-22 14:09:28 +00:00
|
|
|
const projectionMatrix = mat4.create();
|
|
|
|
|
|
|
|
// note: glmatrix.js always has the first argument
|
|
|
|
// as the destination to receive the result.
|
|
|
|
mat4.perspective(
|
|
|
|
projectionMatrix,
|
|
|
|
fieldOfView,
|
|
|
|
aspect,
|
|
|
|
zNear,
|
|
|
|
zFar);
|
|
|
|
|
|
|
|
|
2020-11-25 11:43:13 +00:00
|
|
|
const normalModelMatrix = mat4.create();
|
|
|
|
|
|
|
|
mat4.rotateX(normalModelMatrix,
|
|
|
|
normalModelMatrix,
|
|
|
|
params.rot.x);
|
|
|
|
mat4.rotateY(normalModelMatrix,
|
|
|
|
normalModelMatrix,
|
|
|
|
params.rot.y);
|
|
|
|
mat4.rotateZ(normalModelMatrix,
|
|
|
|
normalModelMatrix,
|
|
|
|
params.rot.z);
|
2020-11-23 16:07:53 +00:00
|
|
|
|
2020-11-25 11:43:13 +00:00
|
|
|
const modelMatrix = mat4.create();
|
|
|
|
mat4.translate(modelMatrix,
|
2020-11-23 16:07:53 +00:00
|
|
|
modelMatrix,
|
2020-11-23 12:47:44 +00:00
|
|
|
[
|
2020-11-25 11:43:13 +00:00
|
|
|
-params.avg.x,
|
|
|
|
-params.avg.y,
|
|
|
|
-params.avg.z,
|
2020-11-23 12:47:44 +00:00
|
|
|
]);
|
2020-11-22 14:16:08 +00:00
|
|
|
|
2020-11-23 16:07:53 +00:00
|
|
|
// Set the drawing position to the "identity" point, which is
|
|
|
|
// the center of the scene.
|
|
|
|
const viewMatrix = mat4.create();
|
|
|
|
|
2020-11-23 19:40:51 +00:00
|
|
|
mat4.translate(
|
|
|
|
viewMatrix,
|
2020-11-24 15:30:59 +00:00
|
|
|
viewMatrix, [
|
2020-11-24 21:12:32 +00:00
|
|
|
Math.cos(squareRotation) * params.circleSize,
|
|
|
|
Math.sin(squareRotation) * params.circleSize,
|
2020-11-24 15:30:59 +00:00
|
|
|
0,
|
|
|
|
]);
|
2020-11-23 19:40:51 +00:00
|
|
|
|
2020-11-23 16:07:53 +00:00
|
|
|
mat4.translate(
|
|
|
|
viewMatrix,
|
|
|
|
viewMatrix,
|
2020-11-24 21:12:32 +00:00
|
|
|
[0.0, 0.0, -params.distance]);
|
2020-11-23 16:07:53 +00:00
|
|
|
|
2020-11-22 14:09:28 +00:00
|
|
|
// Tell WebGL how to pull out the positions from the position
|
|
|
|
// buffer into the vertexPosition attribute.
|
|
|
|
{
|
2020-11-22 19:56:56 +00:00
|
|
|
const numComponents = 3;
|
2020-11-22 14:09:28 +00:00
|
|
|
const type = gl.FLOAT;
|
|
|
|
const normalize = false;
|
|
|
|
const stride = 0;
|
|
|
|
const offset = 0;
|
2020-11-24 18:18:45 +00:00
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.positions);
|
2020-11-22 14:09:28 +00:00
|
|
|
gl.vertexAttribPointer(
|
|
|
|
programInfo.attribLocations.vertexPosition,
|
|
|
|
numComponents,
|
|
|
|
type,
|
|
|
|
normalize,
|
|
|
|
stride,
|
|
|
|
offset);
|
|
|
|
gl.enableVertexAttribArray(
|
|
|
|
programInfo.attribLocations.vertexPosition);
|
|
|
|
}
|
|
|
|
|
2020-11-24 14:05:35 +00:00
|
|
|
|
2020-11-23 16:07:53 +00:00
|
|
|
{
|
|
|
|
const numComponents = 3;
|
|
|
|
const type = gl.FLOAT;
|
|
|
|
const normalize = false;
|
|
|
|
const stride = 0;
|
|
|
|
const offset = 0;
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.normals);
|
|
|
|
gl.vertexAttribPointer(
|
|
|
|
programInfo.attribLocations.vertexNormal,
|
|
|
|
numComponents,
|
|
|
|
type,
|
|
|
|
normalize,
|
|
|
|
stride,
|
|
|
|
offset);
|
|
|
|
gl.enableVertexAttribArray(
|
|
|
|
programInfo.attribLocations.vertexNormal);
|
|
|
|
}
|
|
|
|
|
2020-11-24 18:18:45 +00:00
|
|
|
// tell webgl how to pull out the texture coordinates from buffer
|
|
|
|
{
|
|
|
|
const num = 2; // every coordinate composed of 2 values
|
|
|
|
const type = gl.FLOAT; // the data in the buffer is 32 bit float
|
|
|
|
const normalize = false; // don't normalize
|
|
|
|
const stride = 0; // how many bytes to get from one set to the next
|
|
|
|
const offset = 0; // how many bytes inside the buffer to start from
|
|
|
|
gl.bindBuffer(gl.ARRAY_BUFFER, buffers.uvs);
|
|
|
|
gl.vertexAttribPointer(programInfo.attribLocations.textureCoord,
|
|
|
|
num,
|
|
|
|
type,
|
|
|
|
normalize,
|
|
|
|
stride,
|
|
|
|
offset);
|
|
|
|
gl.enableVertexAttribArray(programInfo.attribLocations.textureCoord);
|
|
|
|
}
|
|
|
|
|
2020-11-22 19:56:56 +00:00
|
|
|
// Tell WebGL which indices to use to index the vertices
|
|
|
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, buffers.indices);
|
|
|
|
|
2020-11-22 14:09:28 +00:00
|
|
|
// Tell WebGL to use our program when drawing
|
|
|
|
gl.useProgram(programInfo.program);
|
|
|
|
// Set the shader uniforms
|
|
|
|
gl.uniformMatrix4fv(
|
|
|
|
programInfo.uniformLocations.projectionMatrix,
|
|
|
|
false,
|
|
|
|
projectionMatrix);
|
|
|
|
gl.uniformMatrix4fv(
|
2020-11-23 16:07:53 +00:00
|
|
|
programInfo.uniformLocations.viewMatrix,
|
|
|
|
false,
|
|
|
|
viewMatrix);
|
|
|
|
gl.uniformMatrix4fv(
|
|
|
|
programInfo.uniformLocations.modelMatrix,
|
2020-11-22 14:09:28 +00:00
|
|
|
false,
|
2020-11-23 16:07:53 +00:00
|
|
|
modelMatrix);
|
2020-11-25 11:43:13 +00:00
|
|
|
gl.uniformMatrix4fv(
|
|
|
|
programInfo.uniformLocations.normalModelMatrix,
|
|
|
|
false,
|
|
|
|
normalModelMatrix);
|
2020-11-22 14:09:28 +00:00
|
|
|
|
2020-11-24 18:44:32 +00:00
|
|
|
// Tell WebGL we want to affect texture unit 0
|
|
|
|
gl.activeTexture(gl.TEXTURE0);
|
|
|
|
// Bind the texture to texture unit 0
|
|
|
|
gl.bindTexture(gl.TEXTURE_2D, texture);
|
|
|
|
// Tell the shader we bound the texture to texture unit 0
|
|
|
|
gl.uniform1i(programInfo.uniformLocations.uSampler, 0);
|
|
|
|
|
2020-11-22 14:09:28 +00:00
|
|
|
{
|
2020-11-23 12:47:44 +00:00
|
|
|
const vertexCount = length;
|
2020-11-22 19:56:56 +00:00
|
|
|
const type = gl.UNSIGNED_SHORT;
|
|
|
|
const offset = 0;
|
|
|
|
gl.drawElements(gl.TRIANGLES, vertexCount, type, offset);
|
|
|
|
}
|
|
|
|
|
2020-11-22 14:16:08 +00:00
|
|
|
squareRotation += deltaTime;
|
2020-11-22 14:09:28 +00:00
|
|
|
}
|