I am new to WebGL. I am experimenting with the matrix operations to try various operations like rotation, reflection, scaling and translation. I am using the book "Interactive Computer Graphics, A top-down approach with WebGL (Seventh Edition)" and helper libraries to load the shaders.
All of these work fine except translation. I am drawing a simple 2D triangle (using clip coordinates for now to keep things simple) and trying to translate the triangle away from the origin -0.5 in the x-axis and -0.5 in the y-axis.
This is the code in the vertex shader:
<script id="vertex-shader" type="x-shader/x-vertex">
attribute vec4 vPosition;
void main()
{
mat4 translationMatrix = mat4(
1, 0, 0, -0.5,
0, 1, 0, -0.5,
0, 0, 1, 0,
0, 0, 0, 1 );
gl_Position = translationMatrix * vPosition;
}
</script>
However, the triangle is skewed and not translated as I expected.
This is the code in my js file: var gl;
window.onload = function init()
{
var canvas = document.getElementById( "gl-canvas" );
gl = WebGLUtils.setupWebGL( canvas );
if ( !gl ) {
alert( "WebGL isn't available" );
}
var vertices = [-0.2, -0.2, 0, 0.2, 0.2, -0.2];
gl.viewport( 0, 0, canvas.width, canvas.height );
gl.clearColor( 1.0, 1.0, 1.0, 1.0 );
// Load shaders and initialize attribute buffers
var program = initShaders( gl, "vertex-shader", "fragment-shader" );
gl.useProgram( program );
// Load the data into the GPU
var bufferId = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, bufferId );
gl.bufferData( gl.ARRAY_BUFFER, flatten(vertices), gl.STATIC_DRAW );
// Associate out shader variables with our data buffer
var vPosition = gl.getAttribLocation( program, "vPosition" );
gl.vertexAttribPointer( vPosition, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray( vPosition );
render();
};
function render()
{
gl.clear( gl.COLOR_BUFFER_BIT );
gl.drawArrays( gl.TRIANGLES, 0, 3 );
}
This approach works:
Vertex shader:
<script id="vertex-shader" type="x-shader/x-vertex">
attribute vec4 vPosition;
uniform mat4 translationMatrix;
void main()
{
gl_Position = translationMatrix * vPosition;
}
</script>
... and js file:
var gl;
var translationMatrix;
var translationMatrixLoc;
window.onload = function init()
{
var canvas = document.getElementById( "gl-canvas" );
gl = WebGLUtils.setupWebGL( canvas );
if ( !gl ) {
alert( "WebGL isn't available" );
}
var vertices = [-0.2, -0.2, 0, 0.2, 0.2, -0.2];
gl.viewport( 0, 0, canvas.width, canvas.height );
gl.clearColor( 1.0, 1.0, 1.0, 1.0 );
// Load shaders and initialize attribute buffers
var program = initShaders( gl, "vertex-shader", "fragment-shader" );
gl.useProgram( program );
// Load the data into the GPU
var bufferId = gl.createBuffer();
gl.bindBuffer( gl.ARRAY_BUFFER, bufferId );
gl.bufferData( gl.ARRAY_BUFFER, flatten(vertices), gl.STATIC_DRAW );
// Associate out shader variables with our data buffer
var vPosition = gl.getAttribLocation( program, "vPosition" );
gl.vertexAttribPointer( vPosition, 2, gl.FLOAT, false, 0, 0 );
gl.enableVertexAttribArray( vPosition );
// Works for translation
translationMatrix = mat4();
var d = vec3(-0.5, -0.5, 0.0);
translationMatrix = mult(translationMatrix, translate(d));
// Debug print the matrix contents
matToStr(translationMatrix);
/*
// It prints out as
1, 0, 0, -0.5
0, 1, 0, -0.5
0, 0, 1, 0
0, 0, 0, 1
*/
translationMatrixLoc = gl.getUniformLocation(program, "translationMatrix");
gl.uniformMatrix4fv(translationMatrixLoc, false, flatten(translationMatrix));
render();
};
function render()
{
gl.clear( gl.COLOR_BUFFER_BIT );
gl.drawArrays( gl.TRIANGLES, 0, 3 );
}
function matToStr(matrix) {
var output = "";
for (var i = 0; i<4; i++) {
output = output + matrix[i] + "\r\n";
}
console.log(output);
}
Why is this happening? When I create the translation matrix in the .js file and send it through to the vertex shader as a uniform variable it works.

