ligne et histo
This commit is contained in:
parent
4de675fb59
commit
6a2b71cb4d
387
index.html
387
index.html
@ -1,143 +1,278 @@
|
|||||||
<!DOCTYPE html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html>
|
||||||
<head>
|
<body>
|
||||||
<meta charset="UTF-8">
|
<canvas width = "300" height = "300" id = "my_Canvas"></canvas>
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>WebGL Line Strip</title>
|
|
||||||
<style>
|
|
||||||
canvas { width: 100%; height: 100%; }
|
|
||||||
</style>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<canvas id="glCanvas"></canvas>
|
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
// Vertex Shader Source
|
|
||||||
const vertexShaderSource = `
|
|
||||||
attribute vec2 a_position;
|
|
||||||
uniform vec2 u_resolution;
|
|
||||||
|
|
||||||
void main() {
|
// Mapping function
|
||||||
// Convert the position from pixels to clipspace
|
function map(smin, smax, val, omin, omax) {
|
||||||
vec2 zeroToOne = a_position / u_resolution;
|
const percent = (val - smin) / (smax - smin);
|
||||||
vec2 zeroToTwo = zeroToOne * 2.0;
|
return (omax - omin) * percent + omin;
|
||||||
vec2 clipSpace = zeroToTwo - 1.0;
|
|
||||||
|
|
||||||
gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1); // Flip Y axis
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
// Fragment Shader Source
|
|
||||||
const fragmentShaderSource = `
|
|
||||||
precision mediump float;
|
|
||||||
uniform vec4 u_color;
|
|
||||||
void main() {
|
|
||||||
gl_FragColor = u_color;
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
function createShader(gl, type, source) {
|
|
||||||
const shader = gl.createShader(type);
|
|
||||||
gl.shaderSource(shader, source);
|
|
||||||
gl.compileShader(shader);
|
|
||||||
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
|
|
||||||
console.error("Shader compile error:", gl.getShaderInfoLog(shader));
|
|
||||||
gl.deleteShader(shader);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return shader;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createProgram(gl, vertexShader, fragmentShader) {
|
/*======= Creating a canvas =========*/
|
||||||
const program = gl.createProgram();
|
|
||||||
gl.attachShader(program, vertexShader);
|
var canvas = document.getElementById('my_Canvas');
|
||||||
gl.attachShader(program, fragmentShader);
|
var gl = canvas.getContext('webgl');
|
||||||
gl.linkProgram(program);
|
|
||||||
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
|
/*======= Filling values =========*/
|
||||||
console.error("Program link error:", gl.getProgramInfoLog(program));
|
var nc = 3; // Number of curves
|
||||||
return null;
|
var courbe = [
|
||||||
|
[5, 0.1, 0.4, 0.7, 0.9, 1.0], // Example data
|
||||||
|
[5, 0.2, 0.3, 0.6, 0.8, 0.95],
|
||||||
|
[5, 0.05, 0.15, 0.5, 0.75, 0.85],
|
||||||
|
];
|
||||||
|
var valmin = [0.1, 0.2, 0.05];
|
||||||
|
var valmax = [1.0, 0.95, 0.85];
|
||||||
|
|
||||||
|
// Drawing modes
|
||||||
|
const HORIZONTAL = 0;
|
||||||
|
const VERTICAL = 1;
|
||||||
|
const SUPERPOSER = 2;
|
||||||
|
|
||||||
|
// Drawing types
|
||||||
|
const LIGNE = 0;
|
||||||
|
const LIGNE_AXES = 1;
|
||||||
|
const HISTOGRAMME = 2;
|
||||||
|
|
||||||
|
let mode_affichage = HORIZONTAL;
|
||||||
|
let type_dessin = HISTOGRAMME;
|
||||||
|
|
||||||
|
function toVertices(index)
|
||||||
|
{
|
||||||
|
const size = courbe[index][0];
|
||||||
|
var vertices = new Array(size*3).fill(0);
|
||||||
|
|
||||||
|
let j = 0;
|
||||||
|
for (let i = 1; i <= size; i++) {
|
||||||
|
vertices[j] = map(1, size, i, -1, 1);
|
||||||
|
|
||||||
|
if(mode_affichage == SUPERPOSER)
|
||||||
|
{
|
||||||
|
vertices[j+1] = map(valmin[index], valmax[index], courbe[index][i], -1, 1);
|
||||||
|
}else
|
||||||
|
{
|
||||||
|
var start = map(0, nc, index, -1, 1);
|
||||||
|
var end = map(0, nc, index+1, -1, 1);
|
||||||
|
vertices[j+1] = map(valmin[index], valmax[index], courbe[index][i], start, end);
|
||||||
|
if(mode_affichage == VERTICAL)
|
||||||
|
{
|
||||||
|
[vertices[j], vertices[j+1]] = [vertices[j+1], vertices[j]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
vertices[j+2] = 0;
|
||||||
|
j += 3;
|
||||||
}
|
}
|
||||||
return program;
|
return vertices;
|
||||||
}
|
}
|
||||||
|
|
||||||
function main() {
|
function getCurveVertices(index)
|
||||||
const canvas = document.getElementById("glCanvas");
|
{
|
||||||
const gl = canvas.getContext("webgl");
|
const size = courbe[index][0];
|
||||||
if (!gl) {
|
var vertices = toVertices(index);
|
||||||
console.error("WebGL not supported");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Resize canvas to full screen
|
// Create an empty buffer object
|
||||||
canvas.width = window.innerWidth;
|
var vertex_buffer = gl.createBuffer();
|
||||||
canvas.height = window.innerHeight;
|
|
||||||
|
|
||||||
// Create shaders
|
// Bind appropriate array buffer to it
|
||||||
const vertexShader = createShader(gl, gl.VERTEX_SHADER, vertexShaderSource);
|
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
|
||||||
const fragmentShader = createShader(gl, gl.FRAGMENT_SHADER, fragmentShaderSource);
|
|
||||||
|
|
||||||
|
// Pass the vertex data to the buffer
|
||||||
|
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
|
||||||
|
|
||||||
// Create program
|
// Unbind the buffer
|
||||||
const program = createProgram(gl, vertexShader, fragmentShader);
|
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
||||||
|
|
||||||
// Look up attributes and uniforms
|
console.log(vertices);
|
||||||
const positionLocation = gl.getAttribLocation(program, "a_position");
|
|
||||||
const resolutionUniformLocation = gl.getUniformLocation(program, "u_resolution");
|
|
||||||
const colorUniformLocation = gl.getUniformLocation(program, "u_color");
|
|
||||||
|
|
||||||
// Create buffer and bind
|
return vertex_buffer;
|
||||||
const positionBuffer = gl.createBuffer();
|
|
||||||
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
|
|
||||||
|
|
||||||
// Create some points for the curve (You would populate this with your data)
|
|
||||||
const points = [
|
|
||||||
100, 150,
|
|
||||||
200, 250,
|
|
||||||
300, 50,
|
|
||||||
400, 300
|
|
||||||
];
|
|
||||||
|
|
||||||
// Send points to buffer
|
|
||||||
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(points), gl.STATIC_DRAW);
|
|
||||||
|
|
||||||
// Resize the viewport to match canvas size
|
|
||||||
gl.viewport(0, 0, gl.canvas.width, gl.canvas.height);
|
|
||||||
|
|
||||||
// Use the program
|
|
||||||
gl.useProgram(program);
|
|
||||||
|
|
||||||
// Set the resolution
|
|
||||||
gl.uniform2f(resolutionUniformLocation, gl.canvas.width, gl.canvas.height);
|
|
||||||
|
|
||||||
// Set the color (white)
|
|
||||||
gl.uniform4f(colorUniformLocation, 1, 1, 1, 1);
|
|
||||||
|
|
||||||
// Enable the position attribute
|
|
||||||
gl.enableVertexAttribArray(positionLocation);
|
|
||||||
|
|
||||||
// Bind the position buffer
|
|
||||||
gl.bindBuffer(gl.ARRAY_BUFFER, positionBuffer);
|
|
||||||
|
|
||||||
// Tell the attribute how to get data out of positionBuffer
|
|
||||||
const size = 2; // 2 components per iteration (x, y)
|
|
||||||
const type = gl.FLOAT; // the data is 32bit floats
|
|
||||||
const normalize = false; // don't normalize the data
|
|
||||||
const stride = 0; // move forward size * sizeof(type) each iteration
|
|
||||||
const offset = 0; // start at the beginning of the buffer
|
|
||||||
gl.vertexAttribPointer(positionLocation, size, type, normalize, stride, offset);
|
|
||||||
|
|
||||||
// Clear the canvas
|
|
||||||
gl.clearColor(0, 0, 0, 0);
|
|
||||||
gl.clear(gl.COLOR_BUFFER_BIT);
|
|
||||||
|
|
||||||
// Draw the curve as a line strip
|
|
||||||
const primitiveType = gl.LINE_STRIP;
|
|
||||||
const offsetIndex = 0;
|
|
||||||
const count = points.length / 2; // 2D points
|
|
||||||
gl.drawArrays(primitiveType, offsetIndex, count);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
window.onload = main;
|
/*=================== Shaders ====================*/
|
||||||
</script>
|
function setShader()
|
||||||
</body>
|
{
|
||||||
|
// Vertex shader source code
|
||||||
|
var vertCode =
|
||||||
|
'attribute vec3 coordinates;' +
|
||||||
|
'void main(void) {' +
|
||||||
|
' gl_Position = vec4(coordinates, 1.0);' +
|
||||||
|
'}';
|
||||||
|
|
||||||
|
// Create a vertex shader object
|
||||||
|
var vertShader = gl.createShader(gl.VERTEX_SHADER);
|
||||||
|
|
||||||
|
// Attach vertex shader source code
|
||||||
|
gl.shaderSource(vertShader, vertCode);
|
||||||
|
|
||||||
|
// Compile the vertex shader
|
||||||
|
gl.compileShader(vertShader);
|
||||||
|
|
||||||
|
// Fragment shader source code
|
||||||
|
var fragCode =
|
||||||
|
'void main(void) {' +
|
||||||
|
'gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);' +
|
||||||
|
'}';
|
||||||
|
|
||||||
|
// Create fragment shader object
|
||||||
|
var fragShader = gl.createShader(gl.FRAGMENT_SHADER);
|
||||||
|
|
||||||
|
// Attach fragment shader source code
|
||||||
|
gl.shaderSource(fragShader, fragCode);
|
||||||
|
|
||||||
|
// Compile the fragmentt shader
|
||||||
|
gl.compileShader(fragShader);
|
||||||
|
|
||||||
|
// Create a shader program object to store
|
||||||
|
// the combined shader program
|
||||||
|
var shaderProgram = gl.createProgram();
|
||||||
|
|
||||||
|
// Attach a vertex shader
|
||||||
|
gl.attachShader(shaderProgram, vertShader);
|
||||||
|
|
||||||
|
// Attach a fragment shader
|
||||||
|
gl.attachShader(shaderProgram, fragShader);
|
||||||
|
|
||||||
|
// Link both the programs
|
||||||
|
gl.linkProgram(shaderProgram);
|
||||||
|
|
||||||
|
// Use the combined shader program object
|
||||||
|
gl.useProgram(shaderProgram);
|
||||||
|
|
||||||
|
return shaderProgram;
|
||||||
|
}
|
||||||
|
|
||||||
|
var shaderProgram = setShader();
|
||||||
|
|
||||||
|
function draw(index)
|
||||||
|
{
|
||||||
|
var vertex_buffer = getCurveVertices(index);
|
||||||
|
|
||||||
|
// Bind vertex buffer object
|
||||||
|
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
|
||||||
|
|
||||||
|
// Get the attribute location
|
||||||
|
var coord = gl.getAttribLocation(shaderProgram, "coordinates");
|
||||||
|
|
||||||
|
// Point an attribute to the currently bound VBO
|
||||||
|
gl.vertexAttribPointer(coord, 3, gl.FLOAT, false, 0, 0);
|
||||||
|
|
||||||
|
// Enable the attribute
|
||||||
|
gl.enableVertexAttribArray(coord);
|
||||||
|
|
||||||
|
// Set the view port
|
||||||
|
gl.viewport(0,0,canvas.width,canvas.height);
|
||||||
|
|
||||||
|
// Draw the triangle
|
||||||
|
gl.drawArrays(gl.LINE_STRIP, 0, courbe[index][0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function square(index, squ)
|
||||||
|
{
|
||||||
|
const size = courbe[index][0];
|
||||||
|
|
||||||
|
x_start = map(1, size, squ, -1, 1);
|
||||||
|
x_end = map(1, size, squ+1, -1, 1);
|
||||||
|
|
||||||
|
y_start = -1;
|
||||||
|
y_end = map(valmin[index], valmax[index], courbe[index][squ], -1, 1);
|
||||||
|
if(mode_affichage != SUPERPOSER)
|
||||||
|
{
|
||||||
|
y_start = map(0, nc, index, -1, 1);
|
||||||
|
var end = map(0, nc, index+1, -1, 1);
|
||||||
|
y_end = map(valmin[index], valmax[index], courbe[index][squ], y_start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mode_affichage == VERTICAL)
|
||||||
|
{
|
||||||
|
[x_start, y_start] = [y_start, x_start];
|
||||||
|
[x_end, y_end] = [y_end, x_end];
|
||||||
|
}
|
||||||
|
|
||||||
|
var vertices = [
|
||||||
|
x_start, y_start, 0,
|
||||||
|
x_end, y_start, 0,
|
||||||
|
x_end, y_end, 0,
|
||||||
|
x_start, y_end, 0
|
||||||
|
]
|
||||||
|
|
||||||
|
return vertices;
|
||||||
|
}
|
||||||
|
|
||||||
|
function drawHisto(index)
|
||||||
|
{
|
||||||
|
const size = courbe[index][0];
|
||||||
|
for(let i = 1; i < size; i++)
|
||||||
|
{
|
||||||
|
var vertices = square(index, i);
|
||||||
|
|
||||||
|
console.log(i);
|
||||||
|
console.log(vertices);
|
||||||
|
|
||||||
|
indices = [0, 2, 1, 0, 3, 2];
|
||||||
|
|
||||||
|
// Create an empty buffer object to store vertex buffer
|
||||||
|
var vertex_buffer = gl.createBuffer();
|
||||||
|
|
||||||
|
// Bind appropriate array buffer to it
|
||||||
|
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
|
||||||
|
|
||||||
|
// Pass the vertex data to the buffer
|
||||||
|
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
|
||||||
|
|
||||||
|
// Unbind the buffer
|
||||||
|
gl.bindBuffer(gl.ARRAY_BUFFER, null);
|
||||||
|
|
||||||
|
// Create an empty buffer object to store Index buffer
|
||||||
|
var Index_Buffer = gl.createBuffer();
|
||||||
|
|
||||||
|
// Bind appropriate array buffer to it
|
||||||
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Index_Buffer);
|
||||||
|
|
||||||
|
// Pass the vertex data to the buffer
|
||||||
|
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(indices), gl.STATIC_DRAW);
|
||||||
|
|
||||||
|
// Unbind the buffer
|
||||||
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null);
|
||||||
|
|
||||||
|
// Bind vertex buffer object
|
||||||
|
gl.bindBuffer(gl.ARRAY_BUFFER, vertex_buffer);
|
||||||
|
|
||||||
|
// Bind index buffer object
|
||||||
|
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, Index_Buffer);
|
||||||
|
|
||||||
|
// Get the attribute location
|
||||||
|
var coord = gl.getAttribLocation(shaderProgram, "coordinates");
|
||||||
|
|
||||||
|
// Point an attribute to the currently bound VBO
|
||||||
|
gl.vertexAttribPointer(coord, 3, gl.FLOAT, false, 0, 0);
|
||||||
|
|
||||||
|
// Enable the attribute
|
||||||
|
gl.enableVertexAttribArray(coord);
|
||||||
|
|
||||||
|
// Set the view port
|
||||||
|
gl.viewport(0,0,canvas.width,canvas.height);
|
||||||
|
|
||||||
|
// Draw the triangle
|
||||||
|
gl.drawElements(gl.TRIANGLES, indices.length, gl.UNSIGNED_SHORT,0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
gl.clearColor(0, 0, 0, 0.5);
|
||||||
|
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
|
||||||
|
for(var i = 0; i < nc; i++)
|
||||||
|
{
|
||||||
|
if(type_dessin == HISTOGRAMME)
|
||||||
|
{
|
||||||
|
drawHisto(i);
|
||||||
|
}else if(type_dessin == LIGNE){
|
||||||
|
draw(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in New Issue
Block a user