TaB's picture

How to move camera in 2D with mouse

Hello I'm writing a 2d designer application and i thought about using GLControl for drawing.
I'm using this code to setup the view, so that the 0,0 coordinate is at the upper left corner of the control.

GL.ClearColor(1f, 1f, 1f, 1f);
            GL.Clear(ClearBufferMask.ColorBufferBit);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Ortho(0, Width, Height, 0, 0, 1);
            GL.MatrixMode(MatrixMode.Modelview);

I want to be able to move the camera using mouse.
I tried to do it like that, but it doesnt work to good :P

GL.ClearColor(1f, 1f, 1f, 1f);
            GL.Clear(ClearBufferMask.ColorBufferBit);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            GL.Ortho(0, Width, Height, 0, 0, 1);
            if (MoveCamera) CameraHelper.CalcMovement();
            GL.Translate(CameraHelper.GetMovmentX, CameraHelper.GetMovmentY, 0);
            GL.MatrixMode(MatrixMode.Modelview);
 
public static class CameraHelper
    {
        private static int OldX = 0, OldY = 0, X = 0, Y = 0;
        private static int MovementX = 0, MovementY = 0, MovementIncrement = 15;
 
        public static int SetX { set { OldX = X; X = value; } }
        public static int SetY { set { OldY = Y; Y = value; } }
        public static int GetMovmentX { get { return MovementX; } }
        public static int GetMovmentY { get { return MovementY; } }
 
        public static void CalcMovement()
        {
            if ((Y - OldY) > 1) MovementY += MovementIncrement;
            else if ((Y - OldY) < -1) MovementY -= MovementIncrement;
            if ((X - OldX) > 1) MovementX += MovementIncrement;
            else if ((X - OldX) < -1) MovementX -= MovementIncrement;
        }
    }

Can someone provide a sample code for moving the camera with mouse in 2d space.
Soz for my English and OpenGL skills I just started learning :D