I'm using OpenGL ES 2.0 to render only 2D shapes, since it's OpenGL ES 2.0 I have to create my own transformaton matrices.
Being only 2D shapes I'd like to keep all my matrices as 3x3 and my vectors with only 2 coordinates.
I also need a projection matrix as I would like to have my coordinates mapped to the screen, I don't want to specify positions from [-1.0; 1.0].
From what I've seen people still use 4x4 matrices for 2D and vectors with z set to 1, and an orthographic projection matrix.
My question is: How can I do all my transformations and the projection with only 3x3 matrices? I have all but the projection matrix set up, but the translation doesn't work.
So far I've done my translation matrix like this:
m[0][0] = 1; m[0][1] = 0; m[0][2] = tx;
m[1][0] = 0; m[1][2] = 1; m[1][2] = ty;
m[2][0] = 0; m[2][3] = 0; m[2][2] = 1;
And from what I can tell this should work, even with NDC coordinates, when multiplied by a vector it should result in
x+tx y+ty 1.0
But when I try to translate this triangle that goes from -1 to 1 with tx = 2f it gets squashed and doesn't move.
Before translation:
After translation:
for reference here is how I draw the triangle:
GLES20.glUniformMatrix3fv(shader.GetUniform("u_transform_mat"), 1, false, Transform.GetTransformationMatrix().ToFloatBuffer());
GLES20.glEnableVertexAttribArray(shader.GetAttribute("a_vert_position"));
GLES20.glEnableVertexAttribArray(shader.GetAttribute("a_vert_color"));
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, VBO[0]);
GLES20.glVertexAttribPointer(shader.GetAttribute("a_vert_position"), 2, GLES20.GL_FLOAT, false, Vertex.SIZE, 0);
GLES20.glVertexAttribPointer(shader.GetAttribute("a_vert_color"), 4, GLES20.GL_FLOAT, false, Vertex.SIZE, Vertex.POS_SIZE);
GLES20.glBindBuffer(GLES20.GL_ELEMENT_ARRAY_BUFFER, IBO[0]);
GLES20.glDrawElements(primitive, size, GLES20.GL_UNSIGNED_INT, 0);
GLES20.glDisableVertexAttribArray(shader.GetAttribute("a_vert_color"));
GLES20.glDisableVertexAttribArray(shader.GetAttribute("a_vert_position"));
and my vertex shader
uniform mat3 u_transform_mat;
attribute vec3 a_vert_position;
attribute vec4 a_vert_color;
varying vec4 v_vert_color;
void main()
{
v_vert_color = a_vert_color;
gl_Position = vec4(u_transform_mat * a_vert_position, 1.0);
}