adityatan's picture

2D Zooming

Hi there,

I'm so lost. I'm trying to do 2D zooming using GL.Ortho and GL.Viewport.

Suppose I have a current game window and four zooming points--namely x1, y1, x2 and y2--that would be the new, zoomed game window. I'm not sure how to implement this. Below is my code.

        private void glControl1_Load(object sender, EventArgs e)
        {
            ...
            int w = glControl1.Width;
            int h = glControl1.Height;
            SetupViewport(offsetX, offsetY, w, h);
        }
 
        private void SetupViewport(int inputx1, int inputy1, int inputx2, int inputy2)
        {            
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
 
            GL.Ortho(0, w, 0, h, -1, 1); 
            GL.Viewport(inputx1, inputy1, inputx2, inputy2); // should I change the input of GL.Viewport or GL.Ortho?
        }
 
        private void glControl1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            ...
 
            else if (zoomToolStripMenuItem.Checked == true)
            {
                if (e.Button == MouseButtons.Left)
                {
                    // not sure how to implement and/or change the input of GL.Ortho or GL.Viewport? 
                }
            }
            glControl1.Invalidate();
        }

I have attached a picture of my game window. There's a red rectangle which represents the graphical input by the user.

Thanks! Please let me know if the question is not clear! THank you very much!

Aditya

Inline Images

Comments

Comment viewing options

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

The easiest way to zoom an orthographic projection is to use... GL.Scale(zoom, zoom, zoom), where 'zoom' is the scale factor (i.e. 1.0 is the neutral zoom level).

adityatan's picture

Thanks for the reply.

Okay, I have a follow up question if you don't mind.

How do I project the object inside the red box into GL.Control1? Should I play around with the GL.Ortho and/or GL.Viewport, or something else? I don't quite get it.

Thanks!

Aditya

adityatan's picture

Please help me again.

I don't know how to implement GL.Scale. Where do I call it? As I load the glControl, i.e. glControl_Load, I manage to scale it. But I can't scale it afterwards. What sort of commands should I use prior and/or posterior to the command GL.Scale(x, y, z)?

        private void glControl1_Load(object sender, EventArgs e)
        {
            ...
            SetupViewport(1, 1, 1);
            ...
        }
 
        private void SetupViewport(double scaleX, double scaleY, double scaleZ)
        {
            int w = glControl1.Width;
            int h = glControl1.Height;
 
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
 
            GL.Scale(scaleX, scaleY, scaleZ);
            GL.Ortho(0, w, 0, h, -1, 1); 
            GL.Viewport(offsetX, offsetY, w, h);        
        }
 
        private void glControl1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            ...
            else if (zoomToolStripMenuItem.Checked == true)
            {
                if (e.Button == MouseButtons.Left)
                {
                    int w = glControl1.Width;
                    int h = glControl1.Height;
 
                    scaleX = Math.Abs((zoomx2 - zoomx1) / (w * xmag - zoomx1));
                    scaleY = Math.Abs((zoomy2 - zoomy1) / (h * ymag - zoomy1));
 
                    translate.X = Convert.ToSingle(-zoomx1 * xmag);
                    translate.Y = Convert.ToSingle(-zoomy1 * ymag);
 
                    // GL.Scale???
                }
            }
        }

My algorithm is: (1) translate the red box into the origin and (2) scale it so the red box is as big as glControl. The problem with translation is that I'm using the Projection, while I'm using ModelView for the scaling. So I'm not sure how to do this.

Thank you very much.

Aditya

adityatan's picture

No one helps :( but fortunately I got it :)

I must scale first then translate!

Thanks!

the Fiddler's picture

The OpenGL wiki is an invaluable resource on OpenGL topics and 3d rendering in general. Keep it in your bookmarks, it covers almost all common OpenGL questions and issues.

adityatan's picture

thanks!