PDA

View Full Version : PAN is looking like rotating please check my code



AeroSujan
10-04-2011, 01:00 AM
Dear Friends
I am doing perspective projection for my 3D model. I have implemented PAN like this.
Please check this code if this is correct, when I am panning it's looking like rotating. Give me some idea how better I can do pan, zoom and rotate. Thanks a lot in advance :p


void CRevolutionProjView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
STATE = PAN;
oldP = point;
CView::OnLButtonDown(nFlags, point);
}

void CRevolutionProjView::OnMouseMove(UINT nFlags, CPoint point)
{
newP = point;
// TODO: Add your message handler code here and/or call default
int dx = oldP.x - newP.x;
int dy = newP.y - oldP.y;
switch(STATE)
{
case PAN:
{
trans[0] -= dx/10.0f;
trans[1] -= dy/10.0f;
Invalidate();
}
break;
case ZOOM:
{
trans[2] -= (dx+dy);
Invalidate();
}
break;
case ROTATE:
{
rot[0] += (dy * 180.0f) / 500.0f;
rot[1] -= (dx * 180.0f) / 500.0f;
#define clamp(x) x = x > 360.0f ? x-360.0f : x < -360.0f ? x+=360.0f : x
clamp(rot[0]);
clamp(rot[1]);
Invalidate();

}
break;
}

oldP = newP;

CView::OnMouseMove(nFlags, point);
}

carsten neumann
10-04-2011, 07:35 AM
Please take a moment to read posting guideline suggestions (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Number=302436#Post3024 36), then think a moment about what you are asking for:
Your post shows a couple of variables that you update in reaction to input events, but there is no indication of what these variables mean or which OpenGL matrices are derived from them. Since I can't tell how these variables influence your camera position it's just not possible to say what the problem might be.

doug65536
10-18-2011, 09:07 PM
What you posted looks right. I'm going to take a wild guess that trans and rot eventually end up as parameters to glTranslatef and glRotatef. Try reversing the order of the calls. If you translate then rotate, the result is a lot different from rotating then translating.

AeroSujan
10-19-2011, 11:00 AM
void CRevolutionProjView::DrawScene(CDC *pDC)
{
wglMakeCurrent(pDC->m_hDC, m_hrc);
//---------------------------------
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glLoadIdentity ();
glPushMatrix();
glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
glRotatef(rot[2], 0.0f, 0.0f, 1.0f);
glTranslatef(trans[0], trans[1], trans[2]);
glCallList(axes);
glCallList(object);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Pulsing Colors Based On Text Position
// glColor3f(1.0f*float(cos(cnt1)),1.0f*float(sin(cnt 2)),1.0f-0.5f*float(cos(cnt1+cnt2)));
// Position The Text On The Screen
// glRasterPos2f(-0.45f+0.05f*float(cos(cnt1)), 0.32f*float(sin(cnt2)));
// glPrint("Active OpenGL Text With NeHe - %7.2f", cnt1); // Print GL Text To The Screen
// cnt1+=0.051f; // Increase The First Counter
// cnt2+=0.005f; // Increase The First Counter
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
glPrintX("X");
glPrintY("Y");
glPrintZ("Z");
glPopMatrix();
glFlush();
SwapBuffers(pDC->m_hDC);
wglMakeCurrent(NULL, NULL);
}


This is my drawscene....As per your suggestions i have reveresed the order of call first rotate and then translate but still i can see the rotation effect on pan. is this because of the perspective projection. should i use orthographis instead. please give me some suggestions. Thanks sujan