2014-07-27 1 views
0

OpenGL es 2.0을 사용하여 충돌 감지로 3D 입자 시뮬레이션을 작성하려고하는데 화면 가장자리와의 충돌을 감지하는 방법을 알 수 없습니다.다른 깊이의 OpenGL 화면 제한

절두체를 적절하게 조정하고 카메라 위치를 조정하면 입자가 0.0 z 깊이에있을 때 X 및 Y 화면 제한을 -1.0에서 1.0 사이로 다소 높일 수 있지만 문제는 화면 가장자리를 찾는 것입니다. 입자 z 위치가 0이 아닌 경우.

본질적으로 입자가 모든 깊이에서 화면 경계 내에 있는지 확인해야합니다. 누구든지이 작업을 수행하는 방법을 알고 있습니까?

감사합니다.

+0

답변은 카메라의 각도와 위치 (영사)에 따라 달라집니다. 보낼 매개 변수를 "GLU.gluPerspective"에 게시 할 수 있습니까? –

답변

0

답변은 절두 평면입니다. 월드 스페이스 입자의 좌표를 사용합니다 절두체 평면의 교차점을 계산하기

class Frustum 
{ 
public: 
    Plane plane[6]; // array of planes 
public: 
    // default constructor 
    Frustum() 
    { 
     for(unsigned int i=0; i<6; i++) plane[i] = Plane(0,0,0,0); 
    } 
    // perspective projection frustum 
    Frustum(Matrix* matrix, 
      unsigned int width, unsigned int height, 
      float fov, float nearClip, float farClip, 
      float xFactor, float yFactor) 
    { 
    // frustrum planes 
    Vector r_origin(matrix->_41, matrix->_42, matrix->_43); 
    Vector vpn(matrix->_31, matrix->_32, matrix->_33); 
    Vector vright(matrix->_11, matrix->_12, matrix->_13); 
    Vector vup(matrix->_21, matrix->_22, matrix->_23); 

    // for right-handed coordinate system 
    vpn *= - 1, vright *= -1, vup *= -1; 

    // somewhat an offset 
    float orgOffset = D3DXVec3Dot(&r_origin, &vpn); 

    // far plane 
    plane[4].a = -vpn.x; 
    plane[4].b = -vpn.y; 
    plane[4].c = -vpn.z; 
    plane[4].d = -farClip - orgOffset; 

    // near plane 
    plane[5].a = vpn.x; 
    plane[5].b = vpn.y; 
    plane[5].c = vpn.z; 
    plane[5].d = nearClip + orgOffset; 

    // field of view 
    float fovx = fov * xFactor; 
    float fovy = fovx * height/width * yFactor; 
    fovx *= 0.5f, fovy *= 0.5f; 
    float tanx = tan(D3DXToRadian(fovx)); 
    float tany = tan(D3DXToRadian(fovy)); 

    // left plane 
    Vector n = (vpn * tanx) + vright; 
    D3DXVec3Normalize(&n, &n); 
    plane[0].a = n.x; 
    plane[0].b = n.y; 
    plane[0].c = n.z; 

    // right plane 
    n = (vpn * tanx) - vright; 
    D3DXVec3Normalize(&n, &n); 
    plane[1].a = n.x; 
    plane[1].b = n.y; 
    plane[1].c = n.z; 

    // bottom plane 
    n = (vpn * tany) + vup; 
    D3DXVec3Normalize(&n, &n); 
    plane[2].a = n.x; 
    plane[2].b = n.y; 
    plane[2].c = n.z; 

    // top plane 
    n = (vpn * tany) - vup; 
    D3DXVec3Normalize(&n, &n); 
    plane[3].a = n.x; 
    plane[3].b = n.y; 
    plane[3].c = n.z; 

    for(unsigned int i=0; i < 4; i++) 
    { 
     n.x = plane[i].a; 
     n.y = plane[i].b; 
     n.z = plane[i].c; 
     plane[i].d = D3DXVec3Dot(&n, &r_origin); 
    } 
    } 
}; 

참고 : 다음은 유용하게 사용할 수 있다는 C + +를/Direct3D를 예제가있다.