summaryrefslogtreecommitdiff
path: root/frontend/_shared/math/line2.js
blob: a9e72be04bae4f05cfa6b90b8315928a8d57610e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/// <reference path="vec2.js" />
/// <reference path="mat4.js" />

/**
 * Creates a new line object
 * @param {vec2} pStart 
 * @param {vec2} pEnd 
 * @param {WebGLRenderingContext} pGl
 */
function line2(pStart, pEnd, pGl, pColor, pMass) {
    const lDiffVector = subVec2(pEnd, pStart);

    const lBuffer = pGl.createBuffer();
    pGl.bindBuffer(pGl.ARRAY_BUFFER, lBuffer);

    var lBufferedData = [
        pStart.x, pStart.y, pColor.x, pColor.y, pColor.z, pColor.w,
        pEnd.x, pEnd.y, pColor.x, pColor.y, pColor.z, pColor.w
    ];

    pGl.bufferData(pGl.ARRAY_BUFFER, new Float32Array(lBufferedData), pGl.STATIC_DRAW)
    pGl.bindBuffer(pGl.ARRAY_BUFFER, undefined);

    var lSlope = (pEnd.y - pStart.y) / (pEnd.x - pStart.x);

    return {
        buffer: lBuffer,
        start: pStart,
        end: pEnd,
        slope: lSlope,
        normal: normalize2(getPerp2(lDiffVector)),
        length: length2(lDiffVector),
        mass: pMass,
        direction: normalize2(lDiffVector),
        velocity: vec2(0, 0)
    };
}

function getLine2MomentOfInertia(pLine) {
    return (1.0 / 12.0) * pLine.mass * Math.pow(pLine.length, 2);
}

function renderLine2(pGl, pProgramInfo, pLine) {
    pGl.uniformMatrix4fv(pProgramInfo.uniformLocations.model, false, mat4()); // Model on a line is always default matrix
    pGl.bindBuffer(pGl.ARRAY_BUFFER, pLine.buffer);
    {
        pGl.enableVertexAttribArray(pProgramInfo.attributeLocations.position);
        pGl.vertexAttribPointer(pProgramInfo.attributeLocations.position, 2, pGl.FLOAT, false, BYTES_PER_FLOAT * 6, 0);

        pGl.enableVertexAttribArray(pProgramInfo.attributeLocations.color);
        pGl.vertexAttribPointer(pProgramInfo.attributeLocations.color, 4, pGl.FLOAT, false, BYTES_PER_FLOAT * 6, BYTES_PER_FLOAT * 2);
    }

    pGl.drawArrays(pGl.LINES, 0, 2);
}