I am writing graphical 2d editor and i am using OpenTK as rendering engine.
I wandering how could it possible to move and scroll the camera with the mouse to look like it is done in Photoshop.
Here is the code i have for now.
GL.Enable(EnableCap.Texture2D);
GL.Enable(EnableCap.Blend);
GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha);
…
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.Viewport(0, 0, 1024, 768);
…
///Part od drawing function
ClassNodeField nField = NodeFieldsManager.GetByID(ID);
int Rows = 4000 / 128 + 1;
int Columns = 4000 / 128 + 1;
GL.ClearColor(Color.Silver);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.Color3(200f, 200f, 200f);
GL.Begin(BeginMode.Lines);
for (int i = 0; i < Rows; i++)
{
GL.Vertex3(4000, 128 * i, 0);
GL.Vertex3(4000, 128 * i, 0);
}
for (int i = 0; i < Columns; i++)
{
GL.Vertex3(128 * i, 4000, 0);
GL.Vertex3(128 * i, 4000, 0);
}
GL.End();
/// end part of drawing function
it is working fine a have the field 4000 by 4000 pixels for tests. I am planing to use 45000 by 45000. And now i need to navigate across this field and scroling with mouse wheel.
Help me with that what should i call from OpenTK to move my camera and scroll it.
3
Answers
Ok thanks to everyone i solved this task. The triangle always moves with the mouse cursor - but i think it i not a problem to make it only when the button is pressed (with two variables of old translate and new translate).
Try it. It works.
If I understand openGL enough, there is no call to move the camera. As a result, you create a transformation matrix that does all the work for you. You should look into a world matrix.
Its a little mind bending for certain, so I will try to simplify it for you. Since openGL doesn’t allow the camera to be moved (technically, there is no camera), have to move the world. You can think of it as moving a slide under a microscope, you can’t move the microscope for the sake of the slide. I haven’t used OpenTK directly, but I have used Monogame which uses OpenTK.
So there are 2 options before you, 1) try to convert this call to work with OpenTK:
where location refers to the center of where you want to look. The other option, is to review any relevant documentation about openGL and OpenTK.
Try to replace code
with this one
and you will see how it works more clearly: