2013-05-28 6 views
1

У меня есть следующая проблема: У меня есть координаты мыши, и у меня есть модель (точки данных), и мне нужны координаты на координатах моей мыши и мой lookAt Vector этих координат, Я могу сделать raycast с объектом, чтобы я мог видеть 3D-значения точек данных. Поэтому я хочу щелкнуть мышью, а затем я хочу увидеть координаты точки данных, на которую я нажал.Преобразование координат мыши в 3d координаты

У меня есть следующее из учебника, но оно не работает. Луч происхождение и направление луча не правильно (я рисую я линия от луча в направлении луча и луча происхождения не прав: Может кто-нибудь мне помочь Вот код:

// Move the mouse cursor coordinates into the -1 to +1 range. 
pointX = ((2.0f * (float)mouseX)/(float) screen_width) - 1.0f; 
pointY = (((2.0f * (float)mouseY)/(float) screen_height) - 1.0f) * -1.0f; 


m_D3D->GetProjectionMatrix(projectionMatrix); 

pointX = pointX/projectionMatrix._11; 
pointY = pointY/projectionMatrix._22; 

// Get the inverse of the view matrix. 
m_Camera->GetViewMatrix(viewMatrix); 
D3DXMatrixInverse(&inverseViewMatrix, NULL, &viewMatrix); 

// Calculate the direction of the picking ray in view space. 
direction.x = (pointX * inverseViewMatrix._11) + (pointY * inverseViewMatrix._21)+ 
    inverseViewMatrix._31; 
direction.y = (pointX * inverseViewMatrix._12) + (pointY * inverseViewMatrix._22) 
    + inverseViewMatrix._32; 
direction.z = (pointX * inverseViewMatrix._13) + (pointY * inverseViewMatrix._23) 
    + inverseViewMatrix._33; 

// Get the origin of the picking ray which is the position of the camera. 
origin = m_Camera->GetPosition(); 

// Get the world matrix and translate to the location of the sphere. 
m_Impact->GetWorldMatrix(worldMatrix); 
//D3DXMatrixTranslation(&translateMatrix, -5.0f, 1.0f, 5.0f); 
//D3DXMatrixMultiply(&worldMatrix, &worldMatrix, &translateMatrix); 

// Now get the inverse of the translated world matrix. 
D3DXMatrixInverse(&inverseWorldMatrix, NULL, &worldMatrix); 


D3DXVec3TransformCoord(&rayOrigin, &origin, &inverseWorldMatrix); 
D3DXVec3TransformNormal(&rayDirection, &direction, &inverseWorldMatrix); 

// Normalize the ray direction. 
D3DXVec3Normalize(&rayDirection, &rayDirection); 

//collision_object->setTransform(col_matrix); 
    collision_model->setTransform(col_matrix); 
    float collision_point[3]; 
    //bool collision_result = collision_object ->rayCollision(rayOrigin, 
      rayDirection, true); 
    bool collision_result = collision_model ->rayCollision(rayOrigin, 
      rayDirection, true); 

      if(collision_result == true) 
    { 
     intersect = true; 
     //collision_object->getCollisionPoint(collision_point, true); 
     collision_model->getCollisionPoint(collision_point, false); 
     *coordX = collision_point[0]; 
     *coordY = collision_point[1]; 
     *coordZ = collision_point[2]; 



    } 

ответ

1

Один простой способ построить луч от мыши выглядит следующим образом (псевдо-код)

Получить мышь Coords до -1 -> 1 диапазон (как вы уже делаете)

Создать вид матрицу проекции (вид * проекция) инвертировать .

Создатель e 2 вектора мышей:

near = Vector3(mousex,mousey,0); 
far = Vector3(mousex,mousey,1); 

rayorigin = transformcoord(near, inverseviewprojection); 
rayend = transformcoord(far, inverseviewprojection); 
raydir = normalize(rayend-rayorigin); 
Смежные вопросы