Lab8,9
Lab8,9
Translation
Moving an object from one position to another position on the screen is called Translation. In 2D
graphics, we can translate an object by adding translation coordinate (Tx,Ty) to each coordinate
in the original object.
Steps
// Fragment Shader
<script id="fragment-shader" type="x-shader/x-fragment">
precision mediump float;
uniform vec4 u_FragColor;
void main() {
gl_FragColor = u_FragColor;
}
</script>
</head>
<body onload="main()">
<canvas id="gl-canvas" width="512" height="512">
Oops ... your browser doesn't support the HTML5 canvas element
</canvas>
</body>
</html>
In the vertex shader, the u_Translation is defined so that it can carry the translation
coordinates.
Hence, using the vertex shader the translation coordinates will be added to any point we draw
gl_Position = a_Position + u_Translation;
Translation.js
function main() {
gl = WebGLUtils.setupWebGL( canvas );
if ( !gl ) {
alert( "WebGL isn't available" );
}
var n = initVertexBuffers(gl);
if ( n < 0 ) {
console.log('/Failed to set the positions of the vertices');
return;
}
gl.vertexAttrib1f(a_PointSize, 10.0);
function initVertexBuffers(gl) {
var vertices = new Float32Array([
0.0, 0.5,
-0.5, -0.5,
0.5, -0.5
]);
var n = 3;
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
if (a_Position < 0) {
console.log('Failed to get the storage location of a_Position.');
return;
}
return n;
}
Since the values of Tx , Ty , and Tz are fixed (uniform) all vertices, we use the uniform variable
for the u_Translation to pass them to the vertex shader.
This line of code is used for retrieving the storage location of the uniform variable
u_Translation
var u_Translation = gl.getUniformLocation(gl.program, 'u_Translation');
This line of code is used to assign the translation values from JavaScript to the u_Transalation
variable in the vertex shader.
gl.uniform4f(u_Translation, Tx, Ty, Tz, 0.0);
Exercise
Modify the sample program for object translation to use translation matrix instead of
mathematical expression.
Rotation
Any object can be rotated at a particular angle θ (theta) from its origin. As illustrated in the
figure below the triangle P is located at angle φ from the horizontal X coordinate with distance r
from the origin. Let us suppose you want to rotate it at the angle θ. After rotating it to a new
location, you will get a new triangle P’.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
<title>2D Rotation</title>
</head>
<body onload="main()">
<canvas id="gl-canvas" width="512" height="512">
Oops ... your browser doesn't support the HTML5 canvas element
</canvas>
</body>
</html>
var ANGLE = 90.0;
function main() {
gl = WebGLUtils.setupWebGL( canvas );
if ( !gl ) {
alert( "WebGL isn't available" );
}
var n = initVertexBuffers(gl);
if ( n < 0 ) {
console.log('/Failed to set the positions of the vertices');
return;
}
gl.vertexAttrib1f(a_PointSize, 10.0);
// rotation
var u_xformMatrix = gl.getUniformLocation(gl.program, 'u_xformMatrix');
if (! u_xformMatrix) {
console.log('Failed to get u_xformMatrix variable');
return;
}
//
var u_FragColor = gl.getUniformLocation(gl.program, 'u_FragColor');
if (! u_FragColor) {
console.log('Failed to get u_FragColor variable');
return;
}
function initVertexBuffers(gl) {
var vertices = new Float32Array([
0.0, 0.5,
-0.5, -0.5,
0.5, -0.5
]);
var n = 3;
gl.bindBuffer(gl.ARRAY_BUFFER, vertexBuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
return n;
}
Scaling
Scaling means to change the size of an object. In the scaling process, we can either increase or
decrease the dimensions of the object. This process can be achieved by multiplying the original
coordinates of the object with the scaling factor to get the intended result.
Exercise:
Based on the sample codes provided for translation and rotation, write a program to perform the
scaling operation.
Reference