-1

In my SharpGL project (C#) I have used the Unproject function in order to get the world coordinates from mouse coordinates.

This procedure, quite trivial, fails when the drawing is scaled. I found many articles about this issue, but no one suited me.

When I say scaled means that in draw main proc i apply this code:

_gl.Scale(_params.ScaleFactor, _params.ScaleFactor, _params.ScaleFactor);

Then, when I intercept the mouse move I want to visualize the world coords. These coordinates are precise when the scale factor is 1, but when I change it these are wrong.

for example: a world point (10, 10) scaled 1 is detected (10, 10) scaled 1,25 is detected (8, 8) scaled 1,25 is detected (6.65, 6.65)

This is my simple code, consider that scale_factor is just passed for debugging.

public static XglVertex GetWorldCoords(this OpenGL gl, int x, int y, float scale_factor)
    {
        double worldX = 0;
        double worldY = 0;
        double worldZ = 0;
        int[] viewport = new int[4];
        double[] modelview = new double[16];
        double[] projection = new double[16];

        gl.GetDouble(OpenGL.GL_MODELVIEW_MATRIX, modelview); //get the modelview info
        gl.GetDouble(OpenGL.GL_PROJECTION_MATRIX, projection); //get the projection matrix info
        gl.GetInteger(OpenGL.GL_VIEWPORT, viewport); //get the viewport info

        float winX = (float)x;
        float winY = (float)viewport[3] - (float)y;
        float winZ = 0;

        //get the world coordinates from the screen coordinates
        gl.UnProject(winX, winY, winZ, modelview, projection, viewport, ref worldX, ref worldY, ref worldZ);
        XglVertex vres = new XglVertex((float)worldX, (float)worldY, (float)worldZ);
        Debug.Print(string.Format("World Coordinate: x = {0}, y = {1}, z = {2}, sf = {3}", vres.X, vres.Y, vres.Z, scale_factor));

        return vres;
    }

enter image description here

Tostone
  • 95
  • 1
  • 9
  • How is the drawing scaled in first place? – BDL Jul 02 '20 at 14:58
  • *"This procedure, quite trivial, fails [...]"* - You've to be more specific. What does "fail" mean in this context. What is the expected behavior and what is the actual behavior? – Rabbid76 Jul 02 '20 at 16:11
  • So you arer supplying an additional function parameter `scale_factor`, but do not use it at all, so why would you expect that it affects the output? – derhass Jul 02 '20 at 17:31
  • Scaled means that in draw main proc i apply this:_gl.Scale(_params.ScaleFactor, _params.ScaleFactor, _params.ScaleFactor); _gl.Translate(_params.X, _params.Y, _params.Z); – Tostone Jul 03 '20 at 06:51
  • I was more precise now in my main comment. Check it there. – Tostone Jul 03 '20 at 07:01

1 Answers1

0

I found a solution!

there were in the main draw procedure a portion of code that alterate results of UnProject function.

gl.PushMatrix();
.Translate(_mouse.CursorPosition.X * _params.ScaleFactor, _mouse.CursorPosition.Y * _params.ScaleFactor, _mouse.CursorPosition.Z * _params.ScaleFactor);


foreach (var el in _cursor.Elements) //objects to be drawn
{
    gl.LineWidth(1f);
    gl.Begin(el.Mode);
    foreach (var v in el.Vertex)
    {
        gl.Color(v.Color.R, v.Color.G, v.Color.B, v.Color.A);
        gl.Vertex(v.X, v.Y, v.Z);
    }
    gl.End();
 
}
gl.PopMatrix();
Tostone
  • 95
  • 1
  • 9