Advertisement

World coordinates to screen coordinates

Started by June 23, 2020 10:25 PM
4 comments, last by taby 4 years, 7 months ago

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?

taby said:
Here mat is the camera matrix.

Which? Camera to world space, world space to camera, projection matrix…?

taby said:
Is there something obvious missing?

The perspective divide.

This should help (i guess, did not read it): https://stackoverflow.com/questions/8491247/c-opengl-convert-world-coords-to-screen2d-coords

Advertisement

So basically you use (world*view)*projection matrix for that (multiple this resulting mat with point - remember that 4th coordinate of the point equals to 1.0) edit. Then you divide x and y by 4th component of resulting output to get ndc then you get coord from -1..1 then you do vec2.xy = vec2.xy 0.5 + 0.5 so you have it in screen space

World matrix is often identity

If this dont give you proper coord maybe you have different order of a mat and you have to dot(column[i], point) for i = x etc.

Thanks for the ideas guys.

It turns out that it was some semi-unrelated code was buggy. It's all working great now. Thanks again for the ideas!

This topic is closed to new replies.

Advertisement