Last active
November 7, 2024 14:29
-
-
Save MinaGabriel/e4f9e3b45c7db7f0b9670620742916f4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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"> | |
// Rotation angle (degrees/second) | |
var ANGLE_STEP = 45.0; | |
// LookAtTrianglesWithKeys.js (c) 2012 matsuda | |
// Vertex shader program | |
var VSHADER_SOURCE = | |
'attribute vec4 a_Position;\n' + | |
'attribute vec4 a_Color;\n' + | |
'uniform mat4 u_ViewMatrix;\n' + | |
'uniform mat4 u_ModelMatrix;\n' + | |
'uniform mat4 u_ProjMatrix;\n' + | |
'varying vec4 v_Color;\n' + | |
'void main() {\n' + | |
' gl_Position = u_ProjMatrix * u_ViewMatrix * u_ModelMatrix * a_Position;\n' + | |
' v_Color = a_Color;\n' + | |
'}\n'; | |
// Fragment shader program | |
var FSHADER_SOURCE = | |
'#ifdef GL_ES\n' + | |
'precision mediump float;\n' + | |
'#endif\n' + | |
'varying vec4 v_Color;\n' + | |
'void main() {\n' + | |
' gl_FragColor = v_Color;\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; | |
} | |
// Set the vertex coordinates and color (the blue triangle is in the front) | |
var n = initVertexBuffers(gl); | |
if (n < 0) { | |
console.log('Failed to set the vertex information'); | |
return; | |
} | |
// Specify the color for clearing <canvas> | |
gl.clearColor(0, 0, 0, 1); | |
// Get the storage location of u_ViewMatrix | |
var u_ViewMatrix = gl.getUniformLocation(gl.program, 'u_ViewMatrix'); | |
if(!u_ViewMatrix) { | |
console.log('Failed to get the storage locations of u_ViewMatrix'); | |
return; | |
} | |
// Get storage location of u_ModelMatrix | |
var u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix'); | |
if (!u_ModelMatrix) { | |
console.log('Failed to get the storage location of u_ModelMatrix'); | |
return; | |
} | |
// get the storage location of u_ProjMatrix | |
var u_ProjMatrix = gl.getUniformLocation(gl.program, 'u_ProjMatrix'); | |
if (!u_ProjMatrix) { | |
console.log('Failed to get the storage location of u_ProjMatrix'); | |
return; | |
} | |
// Current rotation angle | |
var currentAngle = 0.0; | |
// rotation Model matrix | |
var modelMatrix = new Matrix4(); | |
// Create the matrix to set the eye point, and the line of sight | |
var projMatrix = new Matrix4(); | |
// Create the view matrix | |
var viewMatrix = new Matrix4(); | |
// Register the event handler to be called on key press | |
document.onkeydown = function(ev){ | |
keydown(ev, gl, n, u_ViewMatrix, viewMatrix, currentAngle, modelMatrix, u_ModelMatrix, projMatrix, u_ProjMatrix); | |
console.log("keydown"); | |
}; | |
// Start drawing | |
var tick = function() { | |
currentAngle = animate(currentAngle); // Update the rotation angle | |
draw(gl, n, u_ViewMatrix, viewMatrix, currentAngle, modelMatrix, u_ModelMatrix, projMatrix, u_ProjMatrix); // Draw the triangle | |
requestAnimationFrame(tick, canvas); // Request that the browser calls tick | |
}; | |
tick(); | |
//draw(gl, n, u_ViewMatrix, viewMatrix, currentAngle, modelMatrix, u_ModelMatrix); // Draw | |
} | |
function initVertexBuffers(gl) { | |
var verticesColors = new Float32Array([ | |
// Vertex coordinates and color | |
0.0, 0.5, -0.4, 0.4, 1.0, 0.4, // The back green one | |
-0.5, -0.5, -0.4, 0.4, 1.0, 0.4, | |
0.5, -0.5, -0.4, 1.0, 0.4, 0.4, | |
]); | |
var n = 3; | |
// Create a buffer object | |
var vertexColorbuffer = gl.createBuffer(); | |
if (!vertexColorbuffer) { | |
console.log('Failed to create the buffer object'); | |
return -1; | |
} | |
// Write the vertex information and enable it | |
gl.bindBuffer(gl.ARRAY_BUFFER, vertexColorbuffer); | |
gl.bufferData(gl.ARRAY_BUFFER, verticesColors, gl.STATIC_DRAW); | |
var FSIZE = verticesColors.BYTES_PER_ELEMENT; | |
//Get the storage location of a_Position, assign and enable buffer | |
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; | |
} | |
gl.vertexAttribPointer(a_Position, 3, gl.FLOAT, false, FSIZE * 6, 0); | |
gl.enableVertexAttribArray(a_Position); | |
// Enable the assignment of the buffer object | |
// Get the storage location of a_Position, assign buffer and enable | |
var a_Color = gl.getAttribLocation(gl.program, 'a_Color'); | |
if(a_Color < 0) { | |
console.log('Failed to get the storage location of a_Color'); | |
return -1; | |
} | |
// Assign the buffer object to a_Color variable | |
gl.vertexAttribPointer(a_Color, 3, gl.FLOAT, false, FSIZE * 6, FSIZE * 3); | |
gl.enableVertexAttribArray(a_Color); | |
// Enable the assignment of the buffer object | |
return n; | |
} | |
var g_eyeX = 0.20, g_eyeY = 0.25, g_eyeZ = 0.25; // Eye position | |
function keydown(ev, gl, n, u_ViewMatrix, viewMatrix, currentAngle, modelMatrix, u_ModelMatrix,projMatrix, u_ProjMatrix) { | |
if(ev.keyCode == 39) { // The right arrow key was pressed | |
g_eyeX += 0.01; | |
} else | |
if (ev.keyCode == 37) { // The left arrow key was pressed | |
g_eyeX -= 0.01; | |
} else { return; } | |
draw(gl, n, u_ViewMatrix, viewMatrix, currentAngle, modelMatrix, u_ModelMatrix, projMatrix, u_ProjMatrix); | |
} | |
function draw(gl, n, u_ViewMatrix, viewMatrix, currentAngle, modelMatrix, u_ModelMatrix, projMatrix, u_ProjMatrix) { | |
// Specify the viewing volume | |
projMatrix.setOrtho(-1.5, 1.5, -1.5, 1.0, 0, 1.5); | |
// Pass the projection matrix to u_ProjMatrix | |
gl.uniformMatrix4fv(u_ProjMatrix, false, projMatrix.elements); | |
// Set the rotation matrix | |
modelMatrix.setRotate(-currentAngle, 0, 0, 1); // Rotation angle, rotation axis (0, 0, 1) | |
// Set the matrix to be used for to set the camera view | |
viewMatrix.setLookAt(g_eyeX, g_eyeY, g_eyeZ, 0, 0, 0, 0, 1, 0); | |
// Pass the view projection matrix | |
gl.uniformMatrix4fv(u_ViewMatrix, false, viewMatrix.elements); | |
// Pass the rotation matrix to the vertex shader | |
gl.uniformMatrix4fv(u_ModelMatrix, false, modelMatrix.elements); | |
gl.clear(gl.COLOR_BUFFER_BIT); // Clear <canvas> | |
gl.drawArrays(gl.TRIANGLES, 0, n); // Draw the rectangle | |
} | |
// Last time that this function was called | |
var g_last = Date.now(); | |
function animate(angle) { | |
// Calculate the elapsed time | |
var now = Date.now(); | |
var elapsed = now - g_last; | |
g_last = now; | |
// Update the current rotation angle (adjusted by the elapsed time) | |
var newAngle = angle + (ANGLE_STEP * elapsed) / 1000.0; | |
return newAngle %= 360; | |
} | |
</script> | |
</head> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment