coordinate systems and image paint

1
tshuva
7/13/2010 8:53 AM

Hello,

I have a couple of questions - I think these are simple ones.

1. How can I Get screen coordinates for a specified world location.
I have objects locations in world coordinates and I have to paint it on the map (from world coordinates to screen coordinates), but I fail to do that .

I managed to do the opposite:
public Point2D WorldPntToScreen(Point3D worldPnt)
    {
     Matrix4D modelViewTransform = GetModelViewTransform();
     Matrix4D projectionTransform = GetProjectionTransform();
     Matrix4D mat = projectionTransform * modelViewTransform;
     Point3D retPnt = mat.Transform(worldPnt);

     double clientWidth = (double)ClientSize.Width;
     double clientHeight = (double)ClientSize.Height;
     Point3D ScreenPoint = new Point3D((double)-retPnt.X * clientWidth /2d + 1d,
        (double)retPnt.Y *clientHeight / 2d - 1d, 0);


     return new Point2D(ScreenPoint.X, ScreenPoint.Y);
    }

and I tried to do the same in the opposite direction (with mat.Inverse) but I think I fail in the normalization process of the received screen point.

public Point2D ScreenPntToWorld(Point2D pnt)
    {
     Matrix4D modelViewTransform = GetModelViewTransform();
     Matrix4D projectionTransform = GetProjectionTransform();
     double clientWidth = (double)ClientSize.Width;
     double clientHeight = (double)ClientSize.Height;
     Point3D startPoint = new Point3D((double)pnt.X / clientWidth * 2d - 1d,
        -(double)pnt.Y / clientHeight * 2d + 1d, 0);

     Matrix4D mat = projectionTransform * modelViewTransform;
     Point3D retPnt = mat.GetInverse().Transform(startPoint);
     return new Point2D(retPnt.X, retPnt.Y);
    }


2. Another thing is when I try to draw an image.ico with a transparent part in the paint event, the transparent part appears white.
I'm doing it like this (same way I did it with the GDI):
private void ViewControl_Paint(object sender, PaintEventArgs e)
{
glGraphics.Paint(ClientSize.Width, ClientSize.Height);
Graphics g = e.Graphics;
Point2D screenPnt = WorldPntToScreen(new Point3D(788033.658798808, 2960313.2685174546, 14.129180746096438));//FAILS
Image img = Image.FromFile('Operational.ICO');
g.DrawImage(img,
new Rectangle((int)screenPnt.X - 12, (int)screenPnt.Y - 12, 24, 24), new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
}


Your quick help would be truly appreciated.
Thanks in advance

Shlomit

Wout
7/15/2010 7:16 PM

Hi Shlomit,

The OpenGL coordinate space is:
- Bottom left: (-1, -1),
- Top right: (1, 1).

These should map to the GDI coordinate space:
- Bottom left: (0, ClientHeight),
- Top right: (ClientWidth, 0).

So your final transformation from OpenGL to GDI space looks incorrect, it should probably be:

     Point3D ScreenPoint = new Point3D((retPnt.X + 1d) * clientWidth /2d,
        (1d - retPnt.Y) *clientHeight / 2d, 0);

About your second question, I just tried it, but the transparency thing has to do with the combination of OpenGL and GDI+. The image is drawing using GDI+, but it has no knowledge of what was drawn with OpenGL. As far as GDI+ is concerned, the background is white,

Let me know if that worked,

Wout

1