从D3DXIntersectTri获取世界坐标 [英] Get world coordinates from D3DXIntersectTri

查看:74
本文介绍了从D3DXIntersectTri获取世界坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正方形区域,必须确定鼠标指向的位置。

使用D3DXIntersectTri可以告诉我鼠标是否指向其上,但是我在计算x,y,z时遇到麻烦

I have a square area on which I have to determine where the mouse pointing.
With D3DXIntersectTri I can tell IF the mouse pointing on it, but I have trouble calculating the x,y,z coordinates.

来自顶点缓冲区的图形,并使用vertices数组进行了初始化:

The drawing from vertex buffer, which initialized with the vertices array:

vertices[0].position = D3DXVECTOR3(-10, 0,  -10);
vertices[1].position = D3DXVECTOR3(-10, 0,   10);
vertices[2].position = D3DXVECTOR3( 10, 0,  -10);
vertices[3].position = D3DXVECTOR3( 10, 0,  -10);
vertices[4].position = D3DXVECTOR3(-10, 0,   10);
vertices[5].position = D3DXVECTOR3( 10, 0,   10);

到目前为止,我有此方法,但这不能给我正确的坐标(仅适用于小区域的一部分,靠近两个边缘,内部更不准确):

I have this method so far, this is not giving me the right coordinates (works only on a small part of the area, near two of the edges and more less accurate inside):

BOOL Area::getcoord( Ray& ray, D3DXVECTOR3& coord)
{
    D3DXVECTOR3 rayOrigin, rayDirection;
    rayDirection = ray.direction;
    rayOrigin = ray.origin;

    float d;

    D3DXMATRIX matInverse;
    D3DXMatrixInverse(&matInverse, NULL, &matWorld);

    // Transform ray origin and direction by inv matrix
    D3DXVECTOR3 rayObjOrigin,rayObjDirection;

    D3DXVec3TransformCoord(&rayOrigin, &rayOrigin, &matInverse);
    D3DXVec3TransformNormal(&rayDirection, &rayDirection, &matInverse);
    D3DXVec3Normalize(&rayDirection,&rayDirection);

    float u, v;
    BOOL isHit1, isHit2;

    D3DXVECTOR3 p1, p2, p3;
    p1 = vertices[3].position;
    p2 = vertices[4].position;
    p3 = vertices[5].position;

    isHit1 = D3DXIntersectTri(&p1, &p2, &p3, &rayOrigin, &rayDirection, &u, &v, &d);
    isHit2 = FALSE;

    if(!isHit1)
    {
        p1 = vertices[0].position;
        p2 = vertices[1].position;
        p3 = vertices[2].position;
        isHit2 = D3DXIntersectTri(&p1, &p2, &p3, &rayOrigin, &rayDirection, &u, &v, &d);
    }

    if(isHit1) 
    {
        coord.x = 1 * ((1-u-v)*p3.x + u*p3.y + v*p3.z);
        coord.y = 0.2f;
        coord.z = -1 * ((1-u-v)*p1.x + u*p1.y + v*p1.z);
        D3DXVec3TransformCoord(&coord, &coord, &matInverse);
    }

    if(isHit2) 
    {
        coord.x = -1 * ((1-u-v)*p3.x + u*p3.y + v*p3.z);
        coord.y = 0.2f;
        coord.z = 1 * ((1-u-v)*p1.x + u*p1.y + v*p1.z);
        D3DXVec3TransformCoord(&coord, &coord, &matWorld);
    }
    return isHit1 || isHit2;
}


推荐答案

重心坐标不起作用您使用它们的方式。 u和v定义源向量的权重。因此,如果要计算命中点,则必须计算

Barycentric coordinates don't work the way you used them. u and v define the weight of the source vectors. So if you want to calculate the hit point, you will have to compute

coord = u * p1 + v * p2 + (1 - u - v) * p3

也可以使用d ray参数:

Alternatively you can use the d ray parameter:

coord = rayOrigin + d * rDirection

两种方式都应产生相同的坐标。

Both ways should result in the same coordinate.

这篇关于从D3DXIntersectTri获取世界坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆