Archived

This topic is now archived and is closed to further replies.

Mouse Coordinates in GL-Ortho Space

Recommended Posts

NTense    102
Hi, I''ve got a MDI app, and I''m trying to get the GL mouse coordinates to display in the status bar. I''ve got the status bar fields working, and the horizontal coordinates work correctly, but something is off on my vertical coordinates. I was hoping someone might help me spot the error, and or let me know if there''s an easier way to get these values. The way I''ve implemented it, I''m getting the window''s mouse location, and interpolating it''s percentage relation to the center of the view (viewCam.position.(x or y)). Then I''m taking that percentage and multiplying it by the orthoWidth or orthoHeight values. The orthoWidth produces accurate x values, but orthoHeight gives me values that are off by 30 - 50%. Can anyone see what''s wrong, or is there an easier way to get my ortho Mouse coordinates? here''s the code that interpolates the mouse coordinates.
void CMotionXGLView::OnMouseMove(UINT nFlags, CPoint point)
{
int xdiff, ydiff;
static CPoint lastpoint;
CMotionXApp* pApp = (CMotionXApp*)AfxGetApp();

// Variables for mouse interp coordinates

double percent;
int decimal, sign;
static CString bufferx, buffery,bufferz;
static double cX, cY, cZ;
.
.
.
.
\ /

// DISPLAY INTERPOLATED MOUSE COORDINATES

switch(m_viewportMode)
{
case PERSPECTIVE:
cX = cY = cZ = 0.0;
break;

case FRONT:
// calculate mouse position as a percentage of the screen

// in relation to the camera''s centered position

percent = (double)((point.x - (width * .5)) / (width * .5));
if(point.x == (width * .5))
{
cX = viewCam.position.x;
}
else
{
cX = viewCam.position.x + (percent * orthoWidth);
}

percent = (double)((point.y - (height * .5)) / (height * .5));
if(point.y == (height * .5))
{
cY = viewCam.position.y;
}
else
{
cY = viewCam.position.y + (-percent * orthoHeight);
}

}

// Convert cX, cY, and cZ to strings and display in status bar;

bufferx = _fcvt(cX, 9, &decimal, &sign);
if (cX < 1.0 && cX > -1.0)
{
bufferx.Insert(0,"0.");
}
else
{
bufferx.Insert(decimal,".");
}
if (sign != 0)
{
bufferx.Insert(0,"-");
}

buffery = _fcvt(cY, 9, &decimal, &sign);
if (cY < 1.0 && cY > -1.0)
{
buffery.Insert(0,"0.");
}
else
{
buffery.Insert(decimal,".");
}
if (sign != 0)
{
buffery.Insert(0,"-");
}

bufferz = _fcvt(cZ, 9, &decimal, &sign);
if (cZ < 1.0 && cZ > -1.0)
{
bufferz.Insert(0,"0.");
}
else
{
bufferz.Insert(decimal,".");
}
if (sign != 0)
{
bufferz.Insert(0,"-");
}

CStatusBar *pStatusBar = (CStatusBar*)AfxGetMainWnd()->GetDescendantWindow(AFX_IDW_STATUS_BAR,TRUE);
pStatusBar->SetPaneText(1, bufferx);
pStatusBar->SetPaneText(2, buffery);
pStatusBar->SetPaneText(3, bufferz);

CView::OnMouseMove(nFlags, point);
}

(double)orthoWidth is defined as the width from the center of the viewport to the left or right edge; width and height are the client window''s width and height values. where (double)orthoHeight is defined as:
orthoHeight = orthoWidth/aspectRatio;


Share this post

Share on other sites
urayami    122
When you create your window and setup your projection, make sure you''re setting up ortho mode with the height as 2*orthoHeight because you said orthoWidth is the width from the center to the edge. Also, I wouldn''t recommend using camera position for the center. Why not just use the center of the screen as the center?

~Urayami

• 9
• 10
• 9
• 14
• 10