Created
November 17, 2020 16:03
-
-
Save MinaGabriel/01a47b95dc6161ccbbb07c0c0518b78a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Translate a Triangle</title> | |
<body onload="main()"> | |
<canvas id="webgl" width="400" height="400"> | |
Please use a browser that supports "canvas" | |
</canvas> | |
</body> | |
<script src="https://rawgit.com/huningxin/webglbook_examples/master/lib/webgl-utils.js"></script> | |
<script src="https://rawgit.com/huningxin/webglbook_examples/master/lib/webgl-debug.js"></script> | |
<script src="https://rawgit.com/huningxin/webglbook_examples/master/lib/cuon-utils.js"></script> | |
<script src="https://rawgit.com/huningxin/webglbook_examples/master/lib/cuon-matrix.js"></script> | |
<script type="text/javascript"> | |
// MultiPoint.js (c) 2012 matsuda | |
// Vertex shader program | |
var VSHADER_SOURCE = | |
'attribute vec4 a_Position;\n' + | |
'uniform mat4 u_ModelMatrix;\n' + | |
'void main() {\n' + | |
' gl_Position = u_ModelMatrix * a_Position;\n' + | |
' gl_PointSize = 10.0;\n' + | |
'}\n'; | |
// Fragment shader program | |
var FSHADER_SOURCE = | |
'void main() {\n' + | |
' gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n' + | |
'}\n'; | |
function main() { | |
// Retrieve <canvas> element | |
var canvas = document.getElementById('webgl'); | |
// Get the rendering context for WebGL | |
var gl = getWebGLContext(canvas); | |
if (!gl) { | |
console.log('Failed to get the rendering context for WebGL'); | |
return; | |
} | |
// Initialize shaders | |
if (!initShaders(gl, VSHADER_SOURCE, FSHADER_SOURCE)) { | |
console.log('Failed to intialize shaders.'); | |
return; | |
} | |
// Write the positions of vertices to a vertex shader | |
var n = initVertexBuffers(gl); | |
if (n < 0) { | |
console.log('Failed to set the positions of the vertices'); | |
return; | |
} | |
// Specify the color for clearing <canvas> | |
gl.clearColor(0.0, 0.0, 0.0, 1.0); | |
var u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix'); | |
if (!u_ModelMatrix) { | |
console.log('Failed to get the storage location of u_ModelMatrix'); | |
return; | |
} | |
var modelMatrix = new Matrix4(); | |
var x = 0.0, y = 0.0 | |
var x_t = 0.005, y_t = 0.005, x_new= 0.0, y_new = 0.0 | |
var tick = function () { | |
modelMatrix.translate(x_t, y_t, 0); | |
// This is important to init the canvas color | |
gl.clear(gl.COLOR_BUFFER_BIT); | |
gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements); | |
gl.drawArrays(gl.POINTS, 0, n); | |
//where x should go | |
x_new = x + get_random(); | |
y_new = y + get_random(); | |
if(x_new >= 0.9){ | |
x_new = x_new - 0.1 | |
}else if (x_new <= -0.9 ){ | |
x_new = x_new + 0.1 | |
}else if (y_new >= 0.9){ | |
y_new = y_new - 0.9 | |
}else if (y_new <= -0.9){ | |
y_new = y_new + 0.1 | |
} | |
x_t = x_new - x | |
y_t = y_new - y | |
x = x_new | |
y = y_new | |
requestAnimationFrame(tick, canvas) | |
}; | |
tick(); | |
} | |
function get_random(){ | |
const options = [-0.1, 0, 0.1]; | |
return options[Math.floor(Math.random() * (3))]; | |
} | |
function initVertexBuffers(gl) { | |
var vertices = new Float32Array([ | |
0.0, 0.0 | |
]); | |
var n = 1; // The number of vertices | |
// Create a buffer object | |
var vertexBuffer = gl.createBuffer(); | |
if (!vertexBuffer) { | |
console.log('Failed to create the buffer object'); | |
return -1; | |
} | |
// Bind the buffer object to target | |
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer); | |
// Write date into the buffer object | |
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW); | |
var a_Position = gl.getAttribLocation(gl.program, 'a_Position'); | |
if (a_Position < 0) { | |
console.log('Failed to get the storage location of a_Position'); | |
return -1; | |
} | |
// Assign the buffer object to a_Position variable | |
gl.vertexAttribPointer(a_Position, 2, gl.FLOAT, false, 0, 0); | |
// Enable the assignment to a_Position variable | |
gl.enableVertexAttribArray(a_Position); | |
return n; | |
} | |
</script> | |
</head> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment