lid6j86's picture

Scrolling and Mouse capabilities

I'm trying to create a map editor using a GLControl context. I put it in a split container, and I'd like the GLControl to be able to render the map (64x64 tile based). I'm trying to figure out two things:

The first is being able to scroll around in the world. Does it make more sense to make the glcontrol the size of the map and use the form controls to scroll, or does it make more sense to create an in-context scrollbar capability? Is there some other better way around this?

the second is I'd like to get the World coordinates of where the mouse is pointing, not the form pixel coordinates. any suggestions on that? I saw a couple things but I honestly don't know if they are correct so i wanted to see what everyone's opinion was.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
lid6j86's picture

I've been reading on Screen to World and World to Screen coordinates, and came across a lot of 'Project/Un-Project' stuff, specifically these 2 implimentations

  //pulled from http://www.opentk.com/node/1276, needed to implement world to screen and screen to world
        public static Vector4 UnProject(ref Matrix4 projection, Matrix4 view, Size viewport, Vector2 mouse)
        {
            Vector4 vec;
 
            vec.X = 2.0f * mouse.X / (float)viewport.Width - 1;
            vec.Y = -(2.0f * mouse.Y / (float)viewport.Height - 1);
            vec.Z = 0;
            vec.W = 1.0f;
 
            Matrix4 viewInv = Matrix4.Invert(view);
            Matrix4 projInv = Matrix4.Invert(projection);
 
            Vector4.Transform(ref vec, ref projInv, out vec);
            Vector4.Transform(ref vec, ref viewInv, out vec);
 
            if (vec.W > float.Epsilon || vec.W < float.Epsilon)
            {
                vec.X /= vec.W;
                vec.Y /= vec.W;
                vec.Z /= vec.W;
            }
 
            return vec;
        }
 
        //pulled from http://www.opentk.com/node/1276, needed to implement world to screen and screen to world
        public static Vector4 Project(OpenTK.Vector4 objPos, Matrix4 projection, Matrix4 view, Size viewport)
        {
            Vector4 vec = objPos;
 
            vec = Vector4.Transform(vec, Matrix4.Mult(projection, view));
 
            vec.X = (vec.X + 1) * (viewport.Width / 2);
            vec.Y = (vec.Y + 1) * (viewport.Height / 2);
 
            return vec;
        }

could I please get some help in understanding how these work? (I figured out how to implement them, I'm just trying to understand what the code itself is doing here geometrically / mathematically)