|
72 | 72 | let meshOn = true;
|
73 | 73 | let clearColor = [0, 0, 0, 1];
|
74 | 74 |
|
| 75 | + let legend; |
| 76 | + let legendtex; |
| 77 | + let legendprogram; |
| 78 | + let legendvertexbuffer; |
| 79 | + let legendtexcoordsbuffer; |
| 80 | + |
75 | 81 | const frequencyArray = [];
|
76 | 82 |
|
77 | 83 | const Vec3 = VemaLib.Vec3; // eslint-disable-line no-undef
|
|
307 | 313 | }
|
308 | 314 | }
|
309 | 315 | }
|
| 316 | + |
| 317 | + if(legendtex !== undefined) drawLegend(gl); |
310 | 318 | checkGlError();
|
311 | 319 | }
|
312 | 320 |
|
|
1011 | 1019 | pointcount = dataLength;
|
1012 | 1020 | histogramvisible = true;
|
1013 | 1021 | console.log('READY OR NOT');
|
| 1022 | + updateLegend(maxv); |
1014 | 1023 | } else if (dataType === 1) {
|
1015 | 1024 | const testfreq = splitStringBuffer[datastart].split('\t')[1];
|
1016 | 1025 | let k = 0;
|
|
1202 | 1211 | histogramvisible = true;
|
1203 | 1212 | cf = frequencyArray[0];
|
1204 | 1213 | console.log('READY OR NOT2');
|
| 1214 | + updateLegend(maxv2[0],minv2[0]); |
1205 | 1215 | }
|
1206 | 1216 | };
|
1207 | 1217 | reader.onerror = function () {
|
|
1422 | 1432 | clearColor = [rgb.r / 255, rgb.g / 255, rgb.b / 255, alpha / 255];
|
1423 | 1433 | };
|
1424 | 1434 |
|
| 1435 | + function updateLegend(maxv) { |
| 1436 | + let ctx = legend.getContext('2d'); |
| 1437 | + let h = legend.height; |
| 1438 | + let w = legend.width; |
| 1439 | + for(let i=0; i<w; i++){ |
| 1440 | + const color = getPseudoColor(i/w); |
| 1441 | + ctx.strokeStyle = 'rgb('+Math.ceil(color.red*255)+','+Math.ceil(color.green*255)+','+Math.ceil(color.blue*255),+')'; |
| 1442 | + ctx.beginPath(); |
| 1443 | + ctx.moveTo(i,0); |
| 1444 | + ctx.lineTo(i,h); |
| 1445 | + ctx.stroke(); |
| 1446 | + } |
| 1447 | + //ctx.font = "30px Comic Sans MS"; |
| 1448 | + ctx.font = "8px" |
| 1449 | + ctx.fillStyle = 'white'; |
| 1450 | + ctx.textAlign = "center"; |
| 1451 | + ctx.rotate(-Math.PI/2); |
| 1452 | + ctx.fillText(""+maxv.toFixed(2),-h/2,w); |
| 1453 | + |
| 1454 | + legendtex = makeTexture(legend); |
| 1455 | + } |
| 1456 | + |
| 1457 | + function initLegend(gl){ |
| 1458 | + let legendvertex=` |
| 1459 | +precision highp float; |
| 1460 | +attribute vec2 pos; |
| 1461 | +attribute vec2 texCoord; |
| 1462 | +varying vec2 v_TexCoordinate; |
| 1463 | +void main() { |
| 1464 | + v_TexCoordinate = vec2(texCoord.x,1.0-texCoord.y); |
| 1465 | + gl_Position = vec4(pos,0.0,1.0); |
| 1466 | +} |
| 1467 | +`; |
| 1468 | + let legendfragment=` |
| 1469 | +precision highp float; |
| 1470 | +uniform sampler2D uTexture0; |
| 1471 | +varying vec2 v_TexCoordinate; |
| 1472 | +void main () { |
| 1473 | + gl_FragColor = texture2D(uTexture0, v_TexCoordinate); |
| 1474 | +} |
| 1475 | +`; |
| 1476 | + |
| 1477 | + legendprogram = ShaderLib.makeProgram2(gl, legendvertex, legendfragment); |
| 1478 | + gl.useProgram(legendprogram); |
| 1479 | + legendprogram.vertexCoords = gl.getAttribLocation(legendprogram, 'pos'); |
| 1480 | + legendprogram.texCoords = gl.getAttribLocation(legendprogram, 'texCoord'); |
| 1481 | + legendprogram.uTexture0 = gl.getUniformLocation(legendprogram, 'uTexture0'); |
| 1482 | + legendvertexbuffer =gl.createBuffer(); |
| 1483 | + |
| 1484 | + gl.bindBuffer(gl.ARRAY_BUFFER, legendvertexbuffer); |
| 1485 | + const ratio= 16/9; |
| 1486 | + const height = 1/10; |
| 1487 | + const width = height*300/80*ratio; |
| 1488 | + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([1-width, 1-height, 1, 1-height, 1-width, 1, 1, 1 |
| 1489 | + ]), gl.STATIC_DRAW); |
| 1490 | + |
| 1491 | + legendtexcoordsbuffer =gl.createBuffer(); |
| 1492 | + gl.bindBuffer(gl.ARRAY_BUFFER, legendtexcoordsbuffer); |
| 1493 | + gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([0, 0, 1, 0, 0, 1, 1, 1 |
| 1494 | + ]), gl.STATIC_DRAW); |
| 1495 | + |
| 1496 | + } |
| 1497 | + |
| 1498 | + function drawLegend(gl){ |
| 1499 | + |
| 1500 | + if(legendtex === undefined) return; |
| 1501 | + gl.useProgram(legendprogram); |
| 1502 | + |
| 1503 | + gl.activeTexture(gl.TEXTURE0); |
| 1504 | + gl.bindTexture(gl.TEXTURE_2D, legendtex); |
| 1505 | + gl.uniform1i(legendprogram.uTexture0, 0); |
| 1506 | + |
| 1507 | + gl.bindBuffer(gl.ARRAY_BUFFER, legendvertexbuffer); |
| 1508 | + gl.enableVertexAttribArray(legendprogram.vertexCoords); |
| 1509 | + gl.vertexAttribPointer(legendprogram.vertexCoords, 2, gl.FLOAT, false, 0, 0); |
| 1510 | + |
| 1511 | + gl.bindBuffer(gl.ARRAY_BUFFER, legendtexcoordsbuffer); |
| 1512 | + gl.enableVertexAttribArray(legendprogram.texCoords); |
| 1513 | + gl.vertexAttribPointer(legendprogram.texCoords, 2, gl.FLOAT, false, 0, 0); |
| 1514 | + |
| 1515 | + gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); |
| 1516 | + } |
| 1517 | + |
| 1518 | + function makeTexture(textCanvas){ |
| 1519 | + var textWidth = textCanvas.width; |
| 1520 | + var textHeight = textCanvas.height; |
| 1521 | + var textTex = gl.createTexture(); |
| 1522 | + gl.bindTexture(gl.TEXTURE_2D, textTex); |
| 1523 | + gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, textCanvas); |
| 1524 | + // make sure we can render it even if it's not a power of 2 |
| 1525 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR); |
| 1526 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); |
| 1527 | + gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); |
| 1528 | + return textTex; |
| 1529 | + } |
1425 | 1530 |
|
1426 | 1531 | PlotterLib.startdice = function startdice() {
|
1427 | 1532 | gravity = new Vec3(0, -9.81, 0);
|
|
1435 | 1540 | }
|
1436 | 1541 |
|
1437 | 1542 | canvas = document.getElementById('glcanvas'); // eslint-disable-line no-undef
|
| 1543 | + legend = document.getElementById('legend'); |
| 1544 | + |
1438 | 1545 | const tresholdslider = document.getElementById('tresholdslider'); // eslint-disable-line no-undef
|
1439 | 1546 | const colorinput = document.getElementById('colorinput'); // eslint-disable-line no-undef
|
1440 | 1547 | const alphaslider = document.getElementById('alphaslider'); // eslint-disable-line no-undef
|
|
1575 | 1682 | // Initialize the GL context
|
1576 | 1683 | gl = initWebGL(canvas);
|
1577 | 1684 |
|
| 1685 | + initLegend(gl); |
| 1686 | + |
1578 | 1687 | // stream = canvas.captureStream(); // frames per second
|
1579 | 1688 |
|
1580 | 1689 | modelTrans.translate(modelPosition.f[0], modelPosition.f[1], modelPosition.f[2]);
|
|
0 commit comments