3
\$\begingroup\$

Up to know I have used this functions to create a 2D scene (for showing images) and to pan on it ( in fact it's a customization of the class COpenGLControl here in codeguru) :

void COpenGLControl::OnSize(UINT nType, int cx, int cy)
{
wglMakeCurrent(hdc, hrc);
CWnd::OnSize(nType, cx, cy);

if (0 >= cx || 0 >= cy || nType == SIZE_MINIMIZED) return;
oglWindowWidth = cx;
oglWindowHeight = cy;
// Map the OpenGL coordinates.
glViewport(0, 0, oglWindowWidth, oglWindowHeight);

// Projection view
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set our current view perspective
glOrtho(-oglWindowWidth/2, oglWindowWidth/2, oglWindowHeight/2,-oglWindowHeight/2, -1, 1);
// Model view
glMatrixMode(GL_MODELVIEW);
wglMakeCurrent(NULL, NULL);
}  

void COpenGLControl::OnDraw(CDC *pDC)
{
wglMakeCurrent(hdc,hrc);
// If the current view is perspective...
glLoadIdentity();
glScalef(m_fZoom,m_fZoom,1.0);
glTranslatef(m_fPosX, m_fPosY, 0.0f);
wglMakeCurrent(NULL, NULL);
}  

void COpenGLControl::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int diffX = (int)(point.x - m_fLastX);
int diffY = (int)(point.y - m_fLastY);
m_fLastX = (float)point.x;
m_fLastY = (float)point.y;
if (nFlags & MK_LBUTTON)
{
    m_fPosX += (float)0.5f * diffX;
    m_fPosY -= (float)0.5f * diffY;
}
OnDraw(NULL);
CWnd::OnMouseMove(nFlags, point);
}  

pressing left mouse button and draging on the window, will cause the image to pan
In fact the COpenGLControl class is for 3D scenes and the disadvantage of this way for 2D scenes is that when I zoom in, I feel the speed of pan is more than the time that I zoom out?
I mean zooming in increases the speed of pan while zooming out decreases it.
Do you know of any way better for continuous panning in a 2D OpenGL scene?

\$\endgroup\$
2
\$\begingroup\$

I can think of two ways to solve the bug.

1) Instead of simply adding the difference to the current position you add a modifier, that is dependent on the zoom value. The modifier decreases, when you zoom in and increases when you zoom out. The modifier should be related to the inverse of the zoom factor, if not the inverse itself. Reason: the pan speed increases when you zoom in, so you simply counteract on that behavior.

2) Instead of using a real 2D scene with ortho you could actually make a 3D scene. The camera looks at a 2D plane, which is your scene. Panning is still just adding the difference to the translation. For zooming, however, you move the camera nearer or further away from the 2D plane. This should avoid the bug, which stems from the fact that you actually scale the scene when zooming instead of moving the camera.

ad 2) Placing items in the scene and so on should still be as easy as in real 2D. You can place the scene on the z=0 plane, and use the usual 2D coordinates for positions and so on.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.