void Picking::CalculatePickingRay(int x, int y, Ray* ray) { D3DVIEWPORT9 viewport; m_pDevice->GetViewport(&viewport); D3DXMATRIX proj_matrix; m_pDevice->GetTransform(D3DTS_PROJECTION, &proj_matrix); float px = ((( 2.0f * x) / viewport.Width) - 1.0f) / proj_matrix(0, 0); float py = (((-2.0f * y) / viewport.Height) + 1.0f) / proj_matrix(1, 1); ray->m_vOrigin = D3DXVECTOR3(0.0f, 0.0f, 0.0f); ray->m_vDirection = D3DXVECTOR3(px, py, 1.0f); }
void Camera3D::updateMatricies () { Matrix4F basis; Vector3DF temp; // compute camera direction vectors --- MATCHES OpenGL's gluLookAt function (DO NOT MODIFY) dir_vec = to_pos; // f vector in gluLookAt docs dir_vec -= from_pos; // eye = from_pos in gluLookAt docs dir_vec.Normalize (); side_vec = dir_vec; side_vec.Cross ( up_dir ); side_vec.Normalize (); up_vec = side_vec; up_vec.Cross ( dir_vec ); up_vec.Normalize(); dir_vec *= -1; // construct view matrix rotate_matrix.Basis (side_vec, up_vec, dir_vec ); view_matrix = rotate_matrix; //Matrix4F trans; //trans.Translate ( -from_pos.x, -from_pos.y, -from_pos.z ); // !efficiency view_matrix.PreTranslate ( Vector3DF(-from_pos.x, -from_pos.y, -from_pos.z ) ); // construct projection matrix --- MATCHES OpenGL's gluPerspective function (DO NOT MODIFY) float sx = tan ( mFov * 0.5 * DEGtoRAD ) * mNear; float sy = sx / mAspect; proj_matrix = 0.0; proj_matrix(0,0) = 2.0*mNear / sx; // matches OpenGL definition proj_matrix(1,1) = 2.0*mNear / sy; proj_matrix(2,2) = -(mFar + mNear)/(mFar - mNear); // C proj_matrix(2,3) = -(2.0*mFar * mNear)/(mFar - mNear); // D proj_matrix(3,2) = -1.0; // construct tile projection matrix --- MATCHES OpenGL's glFrustum function (DO NOT MODIFY) float l, r, t, b; l = -sx + 2.0*sx*mTile.x; // Tile is in range 0 <= x,y <= 1 r = -sx + 2.0*sx*mTile.z; t = sy - 2.0*sy*mTile.y; b = sy - 2.0*sy*mTile.w; tileproj_matrix = 0.0; tileproj_matrix(0,0) = 2.0*mNear / (r - l); tileproj_matrix(1,1) = 2.0*mNear / (t - b); tileproj_matrix(0,2) = (r + l) / (r - l); // A tileproj_matrix(1,2) = (t + b) / (t - b); // B tileproj_matrix(2,2) = proj_matrix(2,2); // C tileproj_matrix(2,3) = proj_matrix(2,3); // D tileproj_matrix(3,2) = -1.0; // construct inverse rotate and inverse projection matrix Vector3DF tvz(0, 0, 0); //invrot_matrix.InverseView ( rotate_matrix.GetDataF(), Vector3DF(0,0,0) ); // Computed using rule: "Inverse of a basis rotation matrix is its transpose." (So long as translation is taken out) invrot_matrix.InverseView ( rotate_matrix.GetDataF(), tvz ); // Computed using rule: "Inverse of a basis rotation matrix is its transpose." (So long as translation is taken out) invproj_matrix.InverseProj ( tileproj_matrix.GetDataF() ); // Computed using rule: updateFrustum (); }