OpenGL, MFC, координаты. Срочно!
void CTraceView::OnLButtonDown(UINT nFlags, CPoint point)
{
CRect clientRect;
GetClientRect(&clientRect);
// TODO: Add your message handler code here and/or call default
MouseX1=(float)point.x/clientRect.right;
MouseY1=(float)point.y/clientRect.bottom-0.5;
CView::OnLButtonDown(nFlags, point);
}
Надо получить координаты OpenGL. Например рисую линию,
glBegin (GL_LINES);
glVertex3f (-1,-1,0);
glVertex3f (1,1,0);
glEnd();
кликаю мышью на край линии и в MouseY1 и в MouseX1 должны записаться координаты либо -1, -1 или 1,1 в зависимости от конца линии. Этого не происходит. Использование gluLookAt(9 параметров) эту проблему усугубляет. Что делать???? Заранее благодарен.
{
CRect clientRect;
GetClientRect(&clientRect);
MouseX1=-1+(float)point.x/(float)clientRect.Width()*2;
MouseY1=-1+(float)point.y/(float)clientRect.Height()*2;
}
Все это не совсем так. Получается маленько не то. Надо еще учесть gluLookAt. И даже эти значения не правильные. Но спасибо
В Builder есть функция LPtoDP.
The LPtoDP function converts logical coordinates into device coordinates. The conversion depends on the mapping mode of the device context, the settings of the origins and extents for the window and viewport, and the world transformation.
BOOL LPtoDP(
HDC hdc, // handle of device context
LPPOINT lpPoints, // array of points
int nCount // count of points
);
Parameters
hdc
Identifies the DC.
lpPoints
Points to an array of POINT structures. The x-coordinates and y-coordinates contained in each of the POINT structures will be transformed.
nCount
Specifies the number of points in the array.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero.
Remarks
This function fails if the logical coordinates exceed 32 bits, or if the converted device coordinates exceed 27 bits. In the case of such an overflow, the results for all the points are undefined.
See Also
DPtoLP, POINT
Все это не совсем так. Получается маленько не то. Надо еще учесть gluLookAt. И даже эти значения не правильные. Но спасибо
Если левая верхняя точка в координатах OpenGL (-1,1), тогда
MouseX1=-1+(float)point.x/(float)clientRect.Width()*2;
MouseY1=1-(float)point.y/(float)clientRect.Height()*2;
Но а, gluLookAt, это уже тебе нужно учесть.
Если левая верхняя точка в координатах OpenGL (-1,1), тогда
MouseX1=-1+(float)point.x/(float)clientRect.Width()*2;
MouseY1=1-(float)point.y/(float)clientRect.Height()*2;
Но а, gluLookAt, это уже тебе нужно учесть.
Да это вроде работает (по краайней мере на бумаге), спасибо. Но мож кто знает как это работает с gluLookAt. Я изменяю в ней третий параметр (удаление глаза по оси Z). Естественно, приведенные формулы будут уже не верны.
Спасибо, это должно было помочь, но на самом деле новые проблемы. НЕ могу понять в чем ошибка. В режиме отладки просматриваю переменные &wx, &wy, &wz в них храняться совершенно дикие значения.
void CTraceView::OnLButtonDown(UINT nFlags, CPoint point)
{
CRect clientRect;
GetClientRect(&clientRect);
InvalidateRect(NULL, FALSE);
GLint viewport[4];
GLdouble projection[16];
GLdouble modelview[16];
GLdouble vx,vy,vz;
GLdouble wx,wy,wz;
glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_PROJECTION_MATRIX,projection);
glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
vx = point.x;
vy = clientRect.bottom - point.y - 1;
vz = -1;
gluUnProject((GLdouble)vx, (GLdouble)vy, 0.0, odelview, projection, viewport, &wx, &wy, &wz);
CView::OnLButtonDown(nFlags, point);
}
Уже обшарил много сайтов на эту тему, но "догнать" не могу нифига. Файл прикрепил. Посмотрите пожалуйста в чем дело. Это принципиально важная для меня проблема.