8Observer8 Posted November 24, 2017 Author Share Posted November 24, 2017 WebGL 1.0. Drawing multiple lines using gl.LINES https://jsfiddle.net/8Observer8/e5hoqo3v/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 1.0. Drawing multiple lines using gl.LINES</title> <style> #renderCanvas { border: 5px solid #41af66; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = ` attribute vec2 a_Position; void main() { gl_Position = vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = ` void main() { gl_FragColor = vec4(0.411, 0.372, 0.670, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.9, 0.3, 0.9, 0.3, -0.9, -0.3, 0.9, -0.3, -0.3, 0.9, -0.3, -0.9, 0.3, 0.9, 0.3, -0.9 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); gl.clearColor(0.925, 0.952, 0.905, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.LINES, 0, 8); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 24, 2017 Author Share Posted November 24, 2017 WebGL 2.0. Drawing multiple lines using gl.LINES https://jsfiddle.net/8Observer8/j387w1t1/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 2.0. Drawing multiple lines using gl.LINES</title> <style> #renderCanvas { border: 5px solid #41af66; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `#version 300 es in vec2 a_Position; void main() { gl_Position = vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `#version 300 es precision mediump float; out vec4 fragColor; void main() { fragColor = vec4(0.411, 0.372, 0.670, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl2"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.9, 0.3, 0.9, 0.3, -0.9, -0.3, 0.9, -0.3, -0.3, 0.9, -0.3, -0.9, 0.3, 0.9, 0.3, -0.9 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); gl.clearColor(0.925, 0.952, 0.905, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.LINES, 0, 8); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 24, 2017 Author Share Posted November 24, 2017 WebGL 1.0. Drawing lines with different colors https://jsfiddle.net/8Observer8/sy2n6Lho/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 1.0. Drawing lines with different colors</title> <style> #renderCanvas { border: 5px solid #9dbdda; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = ` attribute vec2 a_Position; attribute vec3 a_Color; varying vec3 v_Color; void main() { gl_Position = vec4(a_Position, 0.0, 1.0); v_Color = a_Color; }`; var fragmentShaderSource = ` precision mediump float; varying vec3 v_Color; void main() { gl_FragColor = vec4(v_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var verticesAndColors = new Float32Array([ // First line -0.9, 0.0, // Coordinate 0.850, 0.109, 0.184, // Color 0.9, 0.0, // Coordinate 0.850, 0.109, 0.184, // Color // Second line 0.0, 0.9, // Coordinate 0.298, 0.850, 0.109, // Color 0.0, -0.9, // Coordinate 0.298, 0.850, 0.109 // Color ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, verticesAndColors, gl.STATIC_DRAW); var FSIZE = verticesAndColors.BYTES_PER_ELEMENT; var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 5 * FSIZE, 0); gl.enableVertexAttribArray(a_Position); var a_Color = gl.getAttribLocation(program, "a_Color"); gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, 5 * FSIZE, 2 * FSIZE); gl.enableVertexAttribArray(a_Color); gl.clearColor(0.968, 0.968, 0.972, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.LINES, 0, 4); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 24, 2017 Author Share Posted November 24, 2017 WebGL 2.0. Drawing lines with different colors https://jsfiddle.net/8Observer8/epvLrjta/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 2.0. Drawing lines with different colors</title> <style> #renderCanvas { border: 5px solid #9dbdda; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `#version 300 es in vec2 a_Position; in vec3 a_Color; out vec3 v_Color; void main() { gl_Position = vec4(a_Position, 0.0, 1.0); v_Color = a_Color; }`; var fragmentShaderSource = `#version 300 es precision mediump float; in vec3 v_Color; out vec4 fragColor; void main() { fragColor = vec4(v_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl2"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var verticesAndColors = new Float32Array([ // First line -0.9, 0.0, // Coordinate 0.850, 0.109, 0.184, // Color 0.9, 0.0, // Coordinate 0.850, 0.109, 0.184, // Color // Second line 0.0, 0.9, // Coordinate 0.298, 0.850, 0.109, // Color 0.0, -0.9, // Coordinate 0.298, 0.850, 0.109 // Color ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, verticesAndColors, gl.STATIC_DRAW); var FSIZE = verticesAndColors.BYTES_PER_ELEMENT; var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 5 * FSIZE, 0); gl.enableVertexAttribArray(a_Position); var a_Color = gl.getAttribLocation(program, "a_Color"); gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, 5 * FSIZE, 2 * FSIZE); gl.enableVertexAttribArray(a_Color); gl.clearColor(0.968, 0.968, 0.972, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.LINES, 0, 4); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 25, 2017 Author Share Posted November 25, 2017 WebGL 1.0. Drawing a polygonal chain using gl.LINE_STRIP https://jsfiddle.net/8Observer8/4ckevug1/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 1.0. Drawing a polygonal chain using gl.LINE_STRIP</title> <style> #renderCanvas { border: 5px solid #b9ddc7; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = ` attribute vec2 a_Position; void main() { gl_Position = vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = ` precision mediump float; uniform vec3 u_Color; void main() { gl_FragColor = vec4(u_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.8, -0.8, -0.6, 0.0, -0.4, -0.6, -0.2, 0.2, 0.0, 0.5, 0.2, -0.7, 0.4, 0.7, 0.6, -0.4, 0.8, 0.8 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.278, 0.270, 0.768); gl.clearColor(0.984, 0.988, 0.972, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.LINE_STRIP, 0, 9); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 25, 2017 Author Share Posted November 25, 2017 WebGL 2.0. Drawing a polygonal chain using gl.LINE_STRIP https://jsfiddle.net/8Observer8/9eohrfcr/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 2.0. Drawing a polygonal chain using gl.LINE_STRIP</title> <style> #renderCanvas { border: 5px solid #b9ddc7; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `#version 300 es in vec2 a_Position; void main() { gl_Position = vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `#version 300 es precision mediump float; uniform vec3 u_Color; out vec4 fragColor; void main() { fragColor = vec4(u_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl2"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.8, -0.8, -0.6, 0.0, -0.4, -0.6, -0.2, 0.2, 0.0, 0.5, 0.2, -0.7, 0.4, 0.7, 0.6, -0.4, 0.8, 0.8 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.278, 0.270, 0.768); gl.clearColor(0.984, 0.988, 0.972, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.LINE_STRIP, 0, 9); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 25, 2017 Author Share Posted November 25, 2017 WebGL 1.0. Drawing a closed polygonal chain using gl.LINE_LOOP https://jsfiddle.net/8Observer8/unptervr/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 1.0. Drawing a closed polygonal chain using gl.LINE_LOOP</title> <style> #renderCanvas { border: 5px solid #8f84e9; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = ` attribute vec2 a_Position; void main() { gl_Position = vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = ` precision mediump float; uniform vec3 u_Color; void main() { gl_FragColor = vec4(u_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.7, -0.4, -0.8, 0.3, 0.0, 0.9, 0.6, 0.5, 0.7, 0.0, 0.2, -0.7, -0.3, -0.8 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.407, 0.768, 0.270); gl.clearColor(0.956, 0.968, 0.890, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.LINE_LOOP, 0, 7); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 25, 2017 Author Share Posted November 25, 2017 WebGL 2.0. Drawing a closed polygonal chain using gl.LINE_LOOP https://jsfiddle.net/8Observer8/ab58qw7m/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 2.0. Drawing a closed polygonal chain using gl.LINE_LOOP</title> <style> #renderCanvas { border: 5px solid #8f84e9; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `#version 300 es in vec2 a_Position; void main() { gl_Position = vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `#version 300 es precision mediump float; uniform vec3 u_Color; out vec4 fragColor; void main() { fragColor = vec4(u_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl2"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.7, -0.4, -0.8, 0.3, 0.0, 0.9, 0.6, 0.5, 0.7, 0.0, 0.2, -0.7, -0.3, -0.8 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.407, 0.768, 0.270); gl.clearColor(0.956, 0.968, 0.890, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.LINE_LOOP, 0, 7); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 25, 2017 Author Share Posted November 25, 2017 WebGL 1.0. Drawing a polygon using gl.TRIANGLE_STRIP https://jsfiddle.net/8Observer8/586ss160/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 1.0. Drawing a polygon using gl.TRIANGLE_STRIP</title> <style> #renderCanvas { border: 5px solid #8f84e9; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = ` attribute vec2 a_Position; attribute vec3 a_Color; varying vec3 v_Color; void main() { gl_Position = vec4(a_Position, 0.0, 1.0); v_Color = a_Color; }`; var fragmentShaderSource = ` precision mediump float; varying vec3 v_Color; void main() { gl_FragColor = vec4(v_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.6, 0.3, 1.0, 0.0, 0.0, // v0 (x, y), (r, g, b) -0.4, -0.5, 0.0, 1.0, 0.0, // v1 -0.2, 0.7, 0.0, 0.0, 1.0, // v2 0.0, -0.5, 1.0, 0.0, 0.0, // v3 0.2, 0.7, 0.0, 1.0, 0.0, // v4 0.4, -0.5, 0.0, 0.0, 1.0, // v5 0.6, 0.3, 1.0, 0.0, 0.0, // v6 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var FSIZE = vertices.BYTES_PER_ELEMENT; var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 5 * FSIZE, 0); gl.enableVertexAttribArray(a_Position); var a_Color = gl.getAttribLocation(program, "a_Color"); gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, 5 * FSIZE, 2 * FSIZE); gl.enableVertexAttribArray(a_Color); gl.clearColor(0.956, 0.968, 0.890, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 7); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 25, 2017 Author Share Posted November 25, 2017 WebGL 2.0. Drawing a polygon using gl.TRIANGLE_STRIP https://jsfiddle.net/8Observer8/6ccLwmwo/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 2.0. Drawing a polygon using gl.TRIANGLE_STRIP</title> <style> #renderCanvas { border: 5px solid #8f84e9; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `#version 300 es in vec2 a_Position; in vec3 a_Color; out vec3 v_Color; void main() { gl_Position = vec4(a_Position, 0.0, 1.0); v_Color = a_Color; }`; var fragmentShaderSource = `#version 300 es precision mediump float; in vec3 v_Color; out vec4 fragColor; void main() { fragColor = vec4(v_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl2"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.6, 0.3, 1.0, 0.0, 0.0, // v0 (x, y), (r, g, b) -0.4, -0.5, 0.0, 1.0, 0.0, // v1 -0.2, 0.7, 0.0, 0.0, 1.0, // v2 0.0, -0.5, 1.0, 0.0, 0.0, // v3 0.2, 0.7, 0.0, 1.0, 0.0, // v4 0.4, -0.5, 0.0, 0.0, 1.0, // v5 0.6, 0.3, 1.0, 0.0, 0.0, // v6 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var FSIZE = vertices.BYTES_PER_ELEMENT; var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 5 * FSIZE, 0); gl.enableVertexAttribArray(a_Position); var a_Color = gl.getAttribLocation(program, "a_Color"); gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, 5 * FSIZE, 2 * FSIZE); gl.enableVertexAttribArray(a_Color); gl.clearColor(0.956, 0.968, 0.890, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 7); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 25, 2017 Author Share Posted November 25, 2017 WebGL 1.0. Drawing a polygon using gl.TRIANGLE_FAN https://jsfiddle.net/8Observer8/y95djnh7/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 1.0. Drawing a polygon using gl.TRIANGLE_FAN</title> <style> #renderCanvas { border: 5px solid #8f84e9; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = ` attribute vec2 a_Position; attribute vec3 a_Color; varying vec3 v_Color; void main() { gl_Position = vec4(a_Position, 0.0, 1.0); v_Color = a_Color; }`; var fragmentShaderSource = ` precision mediump float; varying vec3 v_Color; void main() { gl_FragColor = vec4(v_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.6, 0.3, 1.0, 0.0, 0.0, // v0 (x, y), (r, g, b) -0.4, -0.5, 0.0, 1.0, 0.0, // v1 0.0, -0.7, 1.0, 0.0, 0.0, // v2 0.4, -0.5, 0.0, 0.0, 1.0, // v3 0.6, 0.3, 1.0, 0.0, 0.0, // v4 0.2, 0.7, 0.0, 1.0, 0.0, // v5 -0.2, 0.7, 0.0, 0.0, 1.0, // v6 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var FSIZE = vertices.BYTES_PER_ELEMENT; var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 5 * FSIZE, 0); gl.enableVertexAttribArray(a_Position); var a_Color = gl.getAttribLocation(program, "a_Color"); gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, 5 * FSIZE, 2 * FSIZE); gl.enableVertexAttribArray(a_Color); gl.clearColor(0.956, 0.968, 0.890, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_FAN, 0, 7); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 25, 2017 Author Share Posted November 25, 2017 WebGL 2.0. Drawing a polygon using gl.TRIANGLE_FAN https://jsfiddle.net/8Observer8/zpktzLdt/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 2.0. Drawing a polygon using gl.TRIANGLE_FAN</title> <style> #renderCanvas { border: 5px solid #8f84e9; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `#version 300 es in vec2 a_Position; in vec3 a_Color; out vec3 v_Color; void main() { gl_Position = vec4(a_Position, 0.0, 1.0); v_Color = a_Color; }`; var fragmentShaderSource = `#version 300 es precision mediump float; in vec3 v_Color; out vec4 fragColor; void main() { fragColor = vec4(v_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl2"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.6, 0.3, 1.0, 0.0, 0.0, // v0 (x, y), (r, g, b) -0.4, -0.5, 0.0, 1.0, 0.0, // v1 0.0, -0.7, 1.0, 0.0, 0.0, // v2 0.4, -0.5, 0.0, 0.0, 1.0, // v3 0.6, 0.3, 1.0, 0.0, 0.0, // v4 0.2, 0.7, 0.0, 1.0, 0.0, // v5 -0.2, 0.7, 0.0, 0.0, 1.0, // v6 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var FSIZE = vertices.BYTES_PER_ELEMENT; var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 5 * FSIZE, 0); gl.enableVertexAttribArray(a_Position); var a_Color = gl.getAttribLocation(program, "a_Color"); gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, 5 * FSIZE, 2 * FSIZE); gl.enableVertexAttribArray(a_Color); gl.clearColor(0.956, 0.968, 0.890, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_FAN, 0, 7); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 26, 2017 Author Share Posted November 26, 2017 WebGL 1.0. Translate a square using glMatrix https://jsfiddle.net/8Observer8/t2q4rmnm/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 1.0. Translate a square using glMatrix</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.4.0/gl-matrix-min.js"></script> <style> #renderCanvas { border: 5px solid #aaaaaa; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `attribute vec2 a_Position; uniform mat4 u_ModelMatrix; void main() { gl_Position = u_ModelMatrix * vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `precision mediump float; uniform vec3 u_Color; void main() { gl_FragColor = vec4(u_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.635, 0.450, 0.125); var modelMatrix = mat4.create(); mat4.translate(modelMatrix, modelMatrix, vec3.fromValues(0.3, 0.3, 0.0)); var u_ModelMatrix = gl.getUniformLocation(program, "u_ModelMatrix"); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix); gl.clearColor(0.898, 0.984, 0.905, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 26, 2017 Author Share Posted November 26, 2017 WebGL 2.0. Translate a square using glMatrix https://jsfiddle.net/8Observer8/o7bmuaaj/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 2.0. Translate a square using glMatrix</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.4.0/gl-matrix-min.js"></script> <style> #renderCanvas { border: 5px solid #aaaaaa; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `#version 300 es in vec2 a_Position; uniform mat4 u_ModelMatrix; void main() { gl_Position = u_ModelMatrix * vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `#version 300 es precision mediump float; uniform vec3 u_Color; out vec4 fragColor; void main() { fragColor = vec4(u_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl2"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.635, 0.450, 0.125); var modelMatrix = mat4.create(); mat4.translate(modelMatrix, modelMatrix, vec3.fromValues(0.3, 0.3, 0.0)); var u_ModelMatrix = gl.getUniformLocation(program, "u_ModelMatrix"); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix); gl.clearColor(0.898, 0.984, 0.905, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 26, 2017 Author Share Posted November 26, 2017 WebGL 1.0. Rotate a square using glMatrix https://jsfiddle.net/8Observer8/cqyu7zx9/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 1.0. Rotate a square using glMatrix</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.4.0/gl-matrix-min.js"></script> <style> #renderCanvas { border: 5px solid #e0aeae; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `attribute vec2 a_Position; uniform mat4 u_ModelMatrix; void main() { gl_Position = u_ModelMatrix * vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `precision mediump float; uniform vec3 u_Color; void main() { gl_FragColor = vec4(u_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.207, 0.635, 0.125); var modelMatrix = mat4.create(); mat4.rotateZ(modelMatrix, modelMatrix, 20 * Math.PI / 180); var u_ModelMatrix = gl.getUniformLocation(program, "u_ModelMatrix"); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix); gl.clearColor(0.894, 0.976, 0.886, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 26, 2017 Author Share Posted November 26, 2017 WebGL 2.0. Rotate a square using glMatrix https://jsfiddle.net/8Observer8/v4atjmfh/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 2.0. Rotate a square using glMatrix</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.4.0/gl-matrix-min.js"></script> <style> #renderCanvas { border: 5px solid #e0aeae; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `#version 300 es in vec2 a_Position; uniform mat4 u_ModelMatrix; void main() { gl_Position = u_ModelMatrix * vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `#version 300 es precision mediump float; uniform vec3 u_Color; out vec4 fragColor; void main() { fragColor = vec4(u_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl2"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.207, 0.635, 0.125); var modelMatrix = mat4.create(); mat4.rotateZ(modelMatrix, modelMatrix, 20 * Math.PI / 180); var u_ModelMatrix = gl.getUniformLocation(program, "u_ModelMatrix"); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix); gl.clearColor(0.894, 0.976, 0.886, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 26, 2017 Author Share Posted November 26, 2017 WebGL 1.0. Scale a square using glMatrix https://jsfiddle.net/8Observer8/xuz9wcxm/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 1.0. Scale a square using glMatrix</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.4.0/gl-matrix-min.js"></script> <style> #renderCanvas { border: 5px solid #7cc1d3; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `attribute vec2 a_Position; uniform mat4 u_ModelMatrix; void main() { gl_Position = u_ModelMatrix * vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `precision mediump float; uniform vec3 u_Color; void main() { gl_FragColor = vec4(u_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.486, 0.372, 0.749); var modelMatrix = mat4.create(); mat4.scale(modelMatrix, modelMatrix, vec3.fromValues(0.5, 0.5, 0.5)); var u_ModelMatrix = gl.getUniformLocation(program, "u_ModelMatrix"); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix); gl.clearColor(0.941, 0.960, 0.819, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 26, 2017 Author Share Posted November 26, 2017 WebGL 2.0. Scale a square using glMatrix https://jsfiddle.net/8Observer8/6pLyzwaj/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 2.0. Scale a square using glMatrix</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.4.0/gl-matrix-min.js"></script> <style> #renderCanvas { border: 5px solid #7cc1d3; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `#version 300 es in vec2 a_Position; uniform mat4 u_ModelMatrix; void main() { gl_Position = u_ModelMatrix * vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `#version 300 es precision mediump float; uniform vec3 u_Color; out vec4 fragColor; void main() { fragColor = vec4(u_Color, 1.0); }`; var gl = document.getElementById("renderCanvas").getContext("webgl2"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.486, 0.372, 0.749); var modelMatrix = mat4.create(); mat4.scale(modelMatrix, modelMatrix, vec3.fromValues(0.5, 0.5, 0.5)); var u_ModelMatrix = gl.getUniformLocation(program, "u_ModelMatrix"); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix); gl.clearColor(0.941, 0.960, 0.819, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 27, 2017 Author Share Posted November 27, 2017 WebGL 1.0. Rotation before translation on mouse click https://jsfiddle.net/4fxmLhmc/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 1.0. Rotation before translation on mouse click</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.4.0/gl-matrix-min.js"></script> <style> #renderCanvas { border: 5px solid #e0aeae; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `attribute vec2 a_Position; uniform mat4 u_ModelMatrix; void main() { gl_Position = u_ModelMatrix * vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `precision mediump float; uniform vec3 u_Color; void main() { gl_FragColor = vec4(u_Color, 1.0); }`; var canvas = document.getElementById("renderCanvas"); var gl = canvas.getContext("webgl"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.207, 0.635, 0.125); var modelMatrix = mat4.create(); var u_ModelMatrix = gl.getUniformLocation(program, "u_ModelMatrix"); var angle = 0; var angleStep = 10; draw(angle); canvas.onmousedown = function () { draw(angle); angle += angleStep; } function draw(angle) { mat4.identity(modelMatrix); mat4.translate(modelMatrix, modelMatrix, vec3.fromValues(0.6, 0.0, 0.0)); mat4.rotateZ(modelMatrix, modelMatrix, angle * Math.PI / 180); mat4.scale(modelMatrix, modelMatrix, vec3.fromValues(0.6, 0.6, 0.6)); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix); gl.clearColor(0.894, 0.976, 0.886, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); } </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 27, 2017 Author Share Posted November 27, 2017 WebGL 2.0. Rotation before translation on mouse click https://jsfiddle.net/cLxuo6jx/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 2.0. Rotation before translation on mouse click</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.4.0/gl-matrix-min.js"></script> <style> #renderCanvas { border: 5px solid #e0aeae; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `#version 300 es in vec2 a_Position; uniform mat4 u_ModelMatrix; void main() { gl_Position = u_ModelMatrix * vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `#version 300 es precision mediump float; uniform vec3 u_Color; out vec4 fragColor; void main() { fragColor = vec4(u_Color, 1.0); }`; var canvas = document.getElementById("renderCanvas"); var gl = canvas.getContext("webgl2"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.207, 0.635, 0.125); var modelMatrix = mat4.create(); var u_ModelMatrix = gl.getUniformLocation(program, "u_ModelMatrix"); var angle = 0; var angleStep = 10; draw(angle); canvas.onmousedown = function () { draw(angle); angle += angleStep; } function draw(angle) { mat4.identity(modelMatrix); mat4.translate(modelMatrix, modelMatrix, vec3.fromValues(0.6, 0.0, 0.0)); mat4.rotateZ(modelMatrix, modelMatrix, angle * Math.PI / 180); mat4.scale(modelMatrix, modelMatrix, vec3.fromValues(0.6, 0.6, 0.6)); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix); gl.clearColor(0.894, 0.976, 0.886, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); } </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 27, 2017 Author Share Posted November 27, 2017 WebGL 1.0. Rotation after translation on mouse click https://jsfiddle.net/ue911jao/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 1.0. Rotation after translation on mouse click</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.4.0/gl-matrix-min.js"></script> <style> #renderCanvas { border: 5px solid #e0aeae; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `attribute vec2 a_Position; uniform mat4 u_ModelMatrix; void main() { gl_Position = u_ModelMatrix * vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `precision mediump float; uniform vec3 u_Color; void main() { gl_FragColor = vec4(u_Color, 1.0); }`; var canvas = document.getElementById("renderCanvas"); var gl = canvas.getContext("webgl"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.207, 0.635, 0.125); var modelMatrix = mat4.create(); var u_ModelMatrix = gl.getUniformLocation(program, "u_ModelMatrix"); var angle = 0; var angleStep = 10; draw(angle); canvas.onmousedown = function () { draw(angle); angle += angleStep; } function draw(angle) { mat4.identity(modelMatrix); mat4.rotateZ(modelMatrix, modelMatrix, angle * Math.PI / 180); mat4.translate(modelMatrix, modelMatrix, vec3.fromValues(0.6, 0.0, 0.0)); mat4.scale(modelMatrix, modelMatrix, vec3.fromValues(0.6, 0.6, 0.6)); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix); gl.clearColor(0.894, 0.976, 0.886, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); } </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 27, 2017 Author Share Posted November 27, 2017 WebGL 2.0. Rotation after translation on mouse click https://jsfiddle.net/55xx969b/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 2.0. Rotation after translation on mouse click</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.4.0/gl-matrix-min.js"></script> <style> #renderCanvas { border: 5px solid #e0aeae; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `#version 300 es in vec2 a_Position; uniform mat4 u_ModelMatrix; void main() { gl_Position = u_ModelMatrix * vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `#version 300 es precision mediump float; uniform vec3 u_Color; out vec4 fragColor; void main() { fragColor = vec4(u_Color, 1.0); }`; var canvas = document.getElementById("renderCanvas"); var gl = canvas.getContext("webgl2"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.207, 0.635, 0.125); var modelMatrix = mat4.create(); var u_ModelMatrix = gl.getUniformLocation(program, "u_ModelMatrix"); var angle = 0; var angleStep = 10; draw(angle); canvas.onmousedown = function () { draw(angle); angle += angleStep; } function draw(angle) { mat4.identity(modelMatrix); mat4.rotateZ(modelMatrix, modelMatrix, angle * Math.PI / 180); mat4.translate(modelMatrix, modelMatrix, vec3.fromValues(0.6, 0.0, 0.0)); mat4.scale(modelMatrix, modelMatrix, vec3.fromValues(0.6, 0.6, 0.6)); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix); gl.clearColor(0.894, 0.976, 0.886, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); } </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
JackFalcon Posted November 28, 2017 Share Posted November 28, 2017 @8Observer8 -> I like this very much. 8Observer8 1 Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 28, 2017 Author Share Posted November 28, 2017 WebGL 1.0. Scaling on mouse click https://jsfiddle.net/m86tq8t2/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 1.0. Scaling on mouse click</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.4.0/gl-matrix-min.js"></script> <style> #renderCanvas { border: 5px solid #e0aeae; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `attribute vec2 a_Position; uniform mat4 u_ModelMatrix; void main() { gl_Position = u_ModelMatrix * vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `precision mediump float; uniform vec3 u_Color; void main() { gl_FragColor = vec4(u_Color, 1.0); }`; var canvas = document.getElementById("renderCanvas"); var gl = canvas.getContext("webgl"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.207, 0.635, 0.125); var modelMatrix = mat4.create(); var u_ModelMatrix = gl.getUniformLocation(program, "u_ModelMatrix"); var maxScale = 1.5; var minScale = 0.5; var currentscale = minScale; var scaleStep = 0.1; draw(currentscale); canvas.onmousedown = function () { currentscale += scaleStep; if (currentscale > maxScale) { scaleStep *= -1; currentscale = maxScale; } else if (currentscale < minScale) { scaleStep *= -1; currentscale = minScale; } draw(currentscale); } function draw(scale) { mat4.identity(modelMatrix); mat4.scale(modelMatrix, modelMatrix, vec3.fromValues(scale, scale, scale)); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix); gl.clearColor(0.894, 0.976, 0.886, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); } </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
8Observer8 Posted November 28, 2017 Author Share Posted November 28, 2017 WebGL 2.0. Scaling on mouse click https://jsfiddle.net/kffdhsvw/ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>WebGL 2.0. Scaling on mouse click</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.4.0/gl-matrix-min.js"></script> <style> #renderCanvas { border: 5px solid #e0aeae; } </style> </head> <body> <canvas id="renderCanvas" width="250" height="250"></canvas> <script> var vertexShaderSource = `#version 300 es in vec2 a_Position; uniform mat4 u_ModelMatrix; void main() { gl_Position = u_ModelMatrix * vec4(a_Position, 0.0, 1.0); }`; var fragmentShaderSource = `#version 300 es precision mediump float; uniform vec3 u_Color; out vec4 fragColor; void main() { fragColor = vec4(u_Color, 1.0); }`; var canvas = document.getElementById("renderCanvas"); var gl = canvas.getContext("webgl2"); var vShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vShader, vertexShaderSource); gl.compileShader(vShader); var fShader = gl.createShader(gl.FRAGMENT_SHADER); gl.shaderSource(fShader, fragmentShaderSource); gl.compileShader(fShader); var program = gl.createProgram(); gl.attachShader(program, vShader); gl.attachShader(program, fShader); gl.linkProgram(program); gl.useProgram(program); var vertices = new Float32Array([ -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, -0.5 ]); var vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); var a_Position = gl.getAttribLocation(program, "a_Position"); gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); gl.enableVertexAttribArray(a_Position); var u_Color = gl.getUniformLocation(program, "u_Color"); gl.uniform3f(u_Color, 0.207, 0.635, 0.125); var modelMatrix = mat4.create(); var u_ModelMatrix = gl.getUniformLocation(program, "u_ModelMatrix"); var maxScale = 1.5; var minScale = 0.5; var currentscale = minScale; var scaleStep = 0.1; draw(currentscale); canvas.onmousedown = function () { currentscale += scaleStep; if (currentscale > maxScale) { scaleStep *= -1; currentscale = maxScale; } else if (currentscale < minScale) { scaleStep *= -1; currentscale = minScale; } draw(currentscale); } function draw(scale) { mat4.identity(modelMatrix); mat4.scale(modelMatrix, modelMatrix, vec3.fromValues(scale, scale, scale)); gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix); gl.clearColor(0.894, 0.976, 0.886, 1.0); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLE_STRIP, 0, 4); } </script> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.