Solved

Zoom to fit function implementation problem

Posted on 2006-06-29
11
2,040 Views
Last Modified: 2013-12-06
I think this is done/asked millions of time but I couldnt find any useful information about it.
I am rendering some points scattered through the scene. I want to implement zoom to fit function, for example when the user presses "F" program will automatically align the model so all of the model appears on the view and as maximum.

The steps I took:
1. I have calculated the bounding sphere
2. And I am trying to compute some parameters according to it, here the problem occurs.

Here are some code I have written (in MFC):
NOTE: m_xpos, m_ypos, m_zoom, m_xrot, m_yrot and m_zrot are variables that changes with user interaction.

void COrthographic::OnDraw(CDC *pDC)
{      
      CspetrexDoc *doc = (CspetrexDoc*) GetDocument();
      
      SetContext();
      glLoadIdentity();
      glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

      // Position the camera
      glTranslatef( m_xpos, -m_ypos, -m_zoom );

      // Adjust viewport for md3 models which
      // use a different coordinate system -
      // 3DSMAX format.
      glRotatef( -90.0f, 1.0f, 0.0f, 0.0f );
      glRotatef( -90.0f, 0.0f, 0.0f, 1.0f );

      // Rotate the camera
      glRotatef( m_xrot, 1.0f, 0.0f, 0.0f );
      glRotatef( m_yrot, 0.0f, 1.0f, 0.0f );
      glRotatef( m_zrot, 0.0f, 0.0f, 1.0f );
      
      doc->drawScene(pDC);
      
      SwapGLBuffers();
}

void COrthographic::OnSize(UINT nType, int cx, int cy)
{
      COpenGLWnd::OnSize(nType, cx, cy);
      
      if ( 0 >= cx || 0 >= cy || nType == SIZE_MINIMIZED )
            return;

      // Change the orthographic viewing volume to
      // reflect the new dimensions of the window
      // and the zoom and position of the viewport.
      SetContext();
      glViewport( 0, 0, cx, cy );
      glMatrixMode( GL_PROJECTION );
      glLoadIdentity();
      glOrtho( (float)(cx)/(float)(cy)*-m_zoom-m_xpos, (float)(cx)/(float)(cy)*m_zoom-m_xpos,      
            -m_zoom+m_ypos, m_zoom+m_ypos, -200.0f, 200.0f );
            
      glMatrixMode( GL_MODELVIEW );
}

// HERE!! this is zoomToFit function!!!
void COrthographic::zoomToFit()
{
      Sphere boundingSphere;
      Point3D center;
      CspetrexDoc *doc = (CspetrexDoc*) GetDocument();
      CRect cr;
      GetClientRect( &cr );

      boundingSphere = doc->getBoundingSphere();
      
      center = boundingSphere.getCenter();

      m_xpos = -center.getX();
      m_ypos = center.getY();
      m_zoom = (double)cr.Height()/cr.Width()*boundingSphere.getDiameter();

      OnSize( SIZE_MAXIMIZED, cr.Width(), cr.Height() );
      OnDraw(NULL);
}


I will be glad if anyone helps.
0
Comment
Question by:bitkidoku
  • 4
  • 2
  • 2
  • +2
11 Comments
 

Author Comment

by:bitkidoku
ID: 17031306
I think this is a hard question, lets make it more attractive (raised value to 300)
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 17132615
What output are you actually getting?
0
 
LVL 1

Expert Comment

by:nargov
ID: 17165636
Observation: Since you're using an Orthographic projection, it is impossible to change the dispursal of the points, except if you change their location. Moving the camera will do no good (no 'zoom' in Orthographic projection). What you can do, is move the points to the center of the screen (as much as possible) by creating a bounding box/sphere and setting the look-at vector of the camera to the center of it (you should actually move the points using glTranslate).
0
 

Author Comment

by:bitkidoku
ID: 17170130
@nargov: Do you mean it is impossible to implement "zoom to fit" function using glOrtho() ? If you mean that, i dont think so.
0
 
LVL 25

Expert Comment

by:InteractiveMind
ID: 17170299
You could scale the scene using glScale*() by a factor of w/(2d). Where w is the screen width, and d is the distance of the furthest horizontal point within the scene.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Accepted Solution

by:
JoseParrot earned 300 total points
ID: 17195805
Hi, bitkidoku,

As far I am understanding the objective, I see the following procedures:

1. Find the model's projection center.
Finding the bounding sphere is just an intermediate approach, because give us the model's center, not the projection center. The model can have points so far from the center, that force the radius to be bigger than the projection area. So, we make C = bounding sphere center as a first step.

2. When looking to C, discover the Top, Bottom, Leftmost and Rightmost points of the model's projection in the projection plane.

3. Calculate the middle point M of the rectangle defined by that points.
Mx = (Lx+Rx)/2; My = (Ty+By)/2. Not necessarely M is equal to the projection of point C. If model is a cube, they are equal, but will be different if the model is a not a symetrical one.
Example: Lx = -10, Rx = 38, Cx projection = 0. So the projection center x is 14, not 0. This is something different to the code you show us.

4. Rotate the camera such that the projection of C is equal to M. The angle can be computed by reverse calculation, or find by repeating 3 or 4 times the steps 3 and 4, thus refining the exact position of the camera.

5. Now we can zoom in (or out) until left and right (or top and bottom) points are in the view clipping boundaries. You can also add a 5% of left-right (and top-bottom) distance to create a safe viewing area slight bigger than the extreme points. Let's remember that zoom is determined by the distance between the plane and the camera.

Of course, this is true only if we use a conical projection, as stated by nargov. In parallel projection, the distance between projection plane and camera does't matter, so the way is to scaling, as stated by InteractiveMind.

Jose
0
 

Author Comment

by:bitkidoku
ID: 17199244
@Jose: I am on vacation right now, I will consider your answer when I return.
0
 
LVL 11

Expert Comment

by:dbkruger
ID: 17347045
If you have computed the center of the points and a bounding sphere, then you have a radius outside of which there are no points.

If that's so, then simply:

glOrtho(x-r, x+r, y-r, y+r, z-r, z+r);

and the points will go to the edge. If you want them not to go quite to the edge, increase r by 5 or 10%
0
 
LVL 18

Expert Comment

by:JoseParrot
ID: 17547185
Hi,

I understand that all participants have contributed with correct comments toward a solution.

Jose
0
 

Author Comment

by:bitkidoku
ID: 17549974
I already forgat that I posted this here, thanks for reminding. Jose you got the point :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
any impressive Python code for a child? 3 113
What is size of MIT, UC artificial intelligence? 2 57
How would I store peoples passwords 5 79
Beginner to Unreal Engine 4 5 57
What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now