Scale bar - Computation of a good initial value


I developed a program which models the dynamics of a stellar disk. Rendering is done in 3D with rotation and zoom functionalities. Here’s an image at the start of the simulation :

You can see on top the different statistics. My problem is about the initial value of the scale (100 kpc). Indeed, I use for initialization a perspective projection this way :

 if (isDisplayFirst)
    isDisplayFirst = false;

    // Initialize View
    glViewport(0, 0, w_width, w_height);

    glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
    glLoadIdentity(); // Reset The Projection Matrix

    glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
    glLoadIdentity(); // Reset The Modelview Matrix

    /* perspective */
    gluPerspective(45.0f, (float)w_width / w_height, g_nearPlane, g_farPlane); 

    /* distance z = 3 */
    gluLookAt (0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

   /* zoom out = 100/3 */
    glScalef(0.03f, 0.03f, 0.03f);

My widget has the following dimensions :

w_width = w_height = 600

So, from a simple computation, I think that I can have :

Scale_value_Init = tan(45) * distance * zoom_out = 3*100/3 = 100


distance = 3 and zoom_out = 100/3

To display the HUD (Head Up Display), I use a orthogonal projection and I draw a line of 100 pixels.

When I compare the “Scale_value_init” value with the average position values of each particle, I can see this initial value is a little too big.

There must be an arror in this initial value computation for scale bar.

Unities used in this program are “kpc” (kilo parsec) and I have set : 1 pixel = 1 kpc.

I just want to get a good value which gives a distance scale in (xy) plane.

Could you tell me why this calculus is wrong and give me some clues to get a good estimation ?

I don’t understand what you are trying to achieve. Are you trying to draw a fixed line on the screen and then say how many kpc that represents?

yes, I draw a fixed line of 100 pixel and I would like to put the distance that it represents in kpc

Given that you are rendering a 3D object in perspective view, this length only has meaning at a certain distance from the eye point so 100 pixels on the screen can similtaneously be 2 meters close to the camera and 10000 meters a long way from it; so it is never “right” or “wrong”.

I would like this scale bar (located at z=100) represents the distance in (xy) plane for the objects at z=0, could you help me please

objects at z=0

Do you mean at the camera eye point or do you mean at the near plane. The scale of something at the camera eye point cannot really be calculated. Have you looked at what the prespective matrix actually is see OpenGL Transformation