I'm trying to understand and learn WebGL and computer graphics from button up, that is why I started to work on my own little library for that. I've spent couple of days looking for right answer and I can't make it work.
I have column major matrices and I'm just trying to render basic triangle but for some reason no matter what I do after multiplification by perspective matrix my Z of vertex is always out of bounds.
I have an object set in space at position 0,0,0 with vertex positions of =
[
-0.5, -0.5, 0,
0.5, -0.5, 0,
0.5, 0.5, 0
]
my camera is set with 60 degree angle fov, aspect of canvas.width / canvas.height
, near plane is 1/1000
and far plane of 50
. And is positioned on (0,0,-10)
looking at my object.
On render time I supply to my vertex shader:
Unifrom Matrix4 u_model
[1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1]
so basically Identity matrix
Uniform Matrix4 u_view
[-1 0 0 0
0 1 0 0
0 0 -1 -10
0 0 0 1]
and Uniform Matrix4 u_projection
0.0003282401348280833 0 -0.3129605393123332 0
0 0.0003282401348280833 -0.3129605393123332 0
0 0 -1.0000400008000159 -0.002000040000800016
0 0 -1 0
My Matrix model is
[n11, n12, n13, n14
n21, n22, n23, n24,
n31, n32, n33, n34,
n41, n42, n43, n44 ]
my perspective matrix calculation :
static perspective(fov, aspect, near, far) {
const r = fov * aspect;
const l = -4;
const t = r;
const b = l;
const matrix = new Matrix4();
matrix.n11 = (2 * near) / (r - l);
matrix.n12 = 0;
matrix.n13 = (r+l)/(r-l);
matrix.n14 = 0;
matrix.n21 = 0;
matrix.n22 = (2 * near) / (t - b);
matrix.n23 = (t+b)/(t-b);
matrix.n24 = 0;
matrix.n31 = 0;
matrix.n32 = 0;
matrix.n33 = (near + far) / (near - far);
matrix.n34 = (2 * near * far) / (near - far);
matrix.n41 = 0;
matrix.n42 = 0;
matrix.n43 = -1;
matrix.n44 = 0;
return matrix;
}
my vertex shader
this.vertexShaderScript =
'\r\n' +
'precision highp float;\r\n' +
'uniform mat4 u_model;\r\n' +
'uniform mat4 u_view;\r\n' +
'uniform mat4 u_projection;\r\n' +
'attribute vec3 a_position;\r\n' +
'attribute vec4 a_color;\r\n' +
'varying vec4 v_color;\r\n' +
'void main(void) {\r\n' +
' v_color = a_color;\r\n' +
' gl_Position = u_projection * u_view * u_model * vec4(a_position, 1.0);\r\n' +
'}\r\n';
And fragment shader
this.fragmentShaderScript = '\r\n' +
'precision highp float;\r\n' +
'varying vec4 v_color;\r\n' +
'void main(void) {\r\n' +
' gl_FragColor = v_color;\r\n' +
'}\r\n';
I have checked the view matrix, tryied to transpose projection, checked with spector js if I get matrices to shader and none of it worked. I also checked other answers but none works for me.
Which of the matrices is wrong?
rest of the code can be found on my github: https://github.com/barteq100/webgl