I am trying to convert world coordinates to screen coordinates.
The problem is that it works when clicking near the centre of the window, but it becomes more and more incorrect the closer the click is toward the edge of the window.
I'm using the code:
vertex_3 get2dPoint(float(&point3D)[4], float(&mat)[16], int width, int height)
{
float out[4];
multiply_4x4_matrix_by_4_vector(mat, point3D, out);
return vertex_3(
round(((1.0f + out[0]) * 0.5) * (float)width),
round(((1.0f - out[1]) * 0.5) * (float)height),
0);
}
void multiply_4x4_matrix_by_4_vector(float(&in_a)[16], float(&in_b)[4], float(&out)[4])
{
float temp[4];
for (int i = 0; i < 4; i++)
{
temp[i] = 0.0;
for (int j = 0; j < 4; j++)
{
temp[i] += in_a[i*4 + j] * in_b[j];
}
}
for (size_t i = 0; i < 4; i++)
out[i] = temp[i];
}
Here mat is the camera matrix. Is there something obvious missing?