PDA

View Full Version : OpenGL coordinate mapping troubles



stanigator
04-18-2009, 12:11 AM
I have the following code that attempts to convert from the coordinate of the right view to coordinate with respect to the top left corner of the box that I have indicated in the picture below:

Picture Link (http://imagebin.org/index.php?mode=image&id=45985)

While in the ConvertCoordinates function, I have been able to convert the x coordinate from the actual number to the desired mapping. However, for the vertical mapping, I have been having some trouble b/c of the viewport array. viewport is always {0, offset, 472, 408}. The offset keeps changing whenever I resize the window or a view in the window. I don't know what I'm missing to map the y coordinate properly. It would be much appreciated to hear some pointers. I used what I have found on gluUnProject on Google in general in getting the object coordinates.



void USview::ConvertCoordinates( float oldX, float oldY, float *newX, float *newY, int *viewport )
{
BrachySimApp* theApp = (BrachySimApp*)AfxGetApp();

// For the USview limits, find out how to get the values without hard-coding
float USviewXLimit = viewport[2]/2;
float USviewXoffset = viewport[0];
float USviewYoffset = viewport[1]; // need a better way than using this
//float USviewYoffset = viewport[1];
float USviewYLimit = viewport[3]/2;

// 3D FEM model ultrasound slice limits
float FEMviewXLimit = theApp->activeUSslicer->usW/2;
float FEMviewYLimit = theApp->activeUSslicer->usD;

// Conversion code
*newX = oldX - (-USviewXoffset - USviewXLimit);
*newY = oldY - (-USviewYoffset - USviewYLimit);
}

void USview::OnLButtonDown(UINT nFlags, CPoint point)
{
BrachySimApp* theApp = (BrachySimApp*)AfxGetApp();

GLdouble position[3] = {0};
GLenum error_code = 0;

wglMakeCurrent(m_chdc->GetSafeHdc(), m_hglrc);
glLoadIdentity();

// Left mouse button
if (nFlags & MK_LBUTTON) {
GLdouble modelMatrix[16] = {0};
glGetDoublev(GL_MODELVIEW_MATRIX, &modelMatrix[0]);
GLdouble projMatrix[16] = {0};
glGetDoublev(GL_PROJECTION_MATRIX, &projMatrix[0]);
int viewport[4] = {0};
glGetIntegerv(GL_VIEWPORT, &viewport[0]);
if(gluUnProject(point.x, point.y, 0,
modelMatrix, projMatrix, viewport,
&position[0], &position[1], &position[2]) == GL_TRUE)
{
// check if the user clicked in the right area
printf("USview coordinates clicked: %f %f %f\n", position[0], position[1], position[2]);
ConvertCoordinates(position[0], position[1], &x_mapped, &y_mapped, viewport);
printf("Corresponding FEM coordinates: %f %f\n", x_mapped, y_mapped);

// use the converted target points to display the needle base
// position
mouseclick = true;
theApp->activeUserControl->OnEnUpdateTBX();
theApp->activeUserControl->OnEnUpdateTBZ();
mouseclick = false;
}
}

wglMakeCurrent(NULL, NULL);

CsplitterView::OnLButtonDown(nFlags, point);
}

stanigator
04-19-2009, 03:41 PM
I would like to make my question clearer. I was trying to use gluUnProject to perform the ray tracing calculations that I wanted. Basically, I would like to get a coordinate system with respect to a known reference point on the black rectangle on the top right view. I have found some tutorials online on ray-sphere intersection as well as ray-triangle intersection, but have some trouble figuring out what input variables I need in the first place to make it happen. Any tips? Thanks.