darkprinze's picture

GLControl zoomin, zoomout and rotate using mouse

I am successfully rendered the stl file and display the points in the cloud. I am trying to implement the smooth zooming function using mouse. Currently i am modifying FOV value to zoom in but its very slow and laggy. How to implement the smooth zooming and rotation like google earth.

 var projection = Matrix4.CreatePerspectiveFieldOfView(viewAngle * zoomFactor, float.Parse(aspect_ratio.ToString()),
                                                          nearClip, farClip);

In mouse wheel event i am changing value.

 private void glSurface_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Delta > 0)
            {
                zoomFactor += 0.1f;
            }
            else
            {
                zoomFactor -= 0.1f;
            }            
        }

Is it better to modify Lookat function for zooming or i have to use scale function? It would be great if i have some sample code or point me in right direction.

Also, i am using immediate mode. which is better? immediate mode or VBO?

Inline Images
Model

Comments

Comment viewing options

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

Since you have a perspective projection, you should modify your modelview matrix to implement zoom. Imagine your camera in 3d space moving closer to the earth.

VBOs are strictly better than immediate mode and can perform orders of magnitude faster. Immediate mode is only useful for quick prototyping (and even in that case, setting up a VBO is just a few lines of code.)

darkprinze's picture

Thanks. I modified the code and now i am modifying the model view lookat function for zooming. In the lookat function i am modifying z eye co ordinates to zoom. However, its working smoothly for small stl files and little laggy and acting weird when deep zoomed(kinda model is rotating) in for big stl files. Also, rotation is weird for foot model, and instead of rotating around center its rotating around origin. My modified code is below.

The lag is appear only for big stl files which has 7242 vertices and 14150 faces. I am also having difficulty to set the camera to center of the screen

 var modelview = Matrix4.LookAt(0f, 20f, zoomFactor, 0, 0, 0, 0.0f, 1.0f, 0.0f);
            var aspect_ratio = Width / (float)Height;
            var projection = Matrix4.CreatePerspectiveFieldOfView(viewAngle, float.Parse(aspect_ratio.ToString()),
                                                          nearClip, farClip);
 
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref projection);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref modelview);
            GL.Rotate(angleY, 1.0f, 0, 0);
            GL.Rotate(angleX, 0, 1.0f, 0);
            //GL.Translate(-boxOffset);

Mouse coordinates

 private void glControl_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {            
            System.Windows.Forms.MouseEventArgs ev = (e as System.Windows.Forms.MouseEventArgs);
            _mouseStartX = ev.X;
            _mouseStartY = ev.Y;          
        }
 
        private void glControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
 
        }
 
        private void glControl_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            System.Windows.Forms.MouseEventArgs ev = (e as System.Windows.Forms.MouseEventArgs);
            if (ev.Button == MouseButtons.Left)
            {               
                angleX += (e.X - _mouseStartX);
                angleY -= (e.Y - _mouseStartY);
               glControl.Invalidate();
            }         
            _mouseStartX = ev.X;
            _mouseStartY = ev.Y;            
        }
 
        private void glControl_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
        {           
            if (e.Delta > 0)
            {
                zoomFactor += 7f;
            }
            else
            {
                zoomFactor -= 7f;
            }           
            glControl.Invalidate();
        }
darkprinze's picture

I am still try to figure out how to implement the proper zoom. These are the following things i am trying to implementation.

1. Fit the 3D model into the screen
2. Smooth zoom and rotate (rotate arounf center point) function (as of now Its lag and slow).

What i am trying to do is create the similar UI like Meshlab (Only zoom and rotate function). I have the size and center position of model.

I starting to grasp how Lookat function and rendering works, but i am having difficulty to implement them properly. I have attached my output screenshot.

Is there any example available for reference?