FLOAT32 CFVec4::DotProduct( CFVec4Arg vArg ) const { const XMVECTOR& v4V = *reinterpret_cast<const XMVECTOR*>( this ); const XMVECTOR& v4V2 = *reinterpret_cast<const XMVECTOR*>( &vArg ); return XMVectorGetX( XMVector4Dot( v4V, v4V2 ) ); }
/** * @return A vector quantity with the scalar in each component. */ CFVec4 CFVec4::DotProductV( CFVec4Arg vArg ) const { CFVec4 v4Return; const XMVECTOR& v4V = *reinterpret_cast<const XMVECTOR*>( this ); const XMVECTOR& v4V2 = *reinterpret_cast<const XMVECTOR*>( &vArg ); XMVECTOR& v4Result = *reinterpret_cast<XMVECTOR*>( &v4Return ); v4Result = XMVector4Dot( v4V, v4V2 ); return v4Return; }
E_INTERSECT_STATE IntersectSphereFrustum(const SSphere& sphere, const SFrustum& frustum) { XMFLOAT4 c(sphere.Center.x, sphere.Center.y, sphere.Center.z, 1.0f); XMVECTOR center = XMLoadFloat4(&c); bool bIntersect = false; for (u32 i = 0; i < 6; i++) { XMVECTOR plane = XMLoadFloat4(&frustum.Planes[i]); XMVECTOR dist = XMVector4Dot(center, plane); f32 r = XMVectorGetX(dist); if (r < -sphere.Radius) return EIS_OUTSIDE; if (r < sphere.Radius) bIntersect = true; } return bIntersect ? EIS_INTERSECTING : EIS_INSIDE; }
void MyApp::editTerrain() { if (!glb_bOn || !m_LButtonDown) return; //------------------------------------------------------------ int w = width(), h = height(); XMMATRIX P = m_camera.getProjectionMatrix(); // Compute picking ray in view space. float vx = (+2.0f*m_mouseX/w - 1.0f)/P(0,0); float vy = (-2.0f*m_mouseY/h + 1.0f)/P(1,1); // Ray definition in view space. XMVECTOR rayOrigin = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f); XMVECTOR rayDir = XMVectorSet(vx, vy, 1.0f, 0.0f); // Tranform ray to local space of Mesh. XMMATRIX V = m_camera.getViewMatrix(); XMMATRIX invView = XMMatrixInverse(&XMMatrixDeterminant(V), V); rayOrigin = XMVector3TransformCoord(rayOrigin, invView); rayDir = XMVector3TransformNormal(rayDir, invView); rayDir = XMVector3Normalize(rayDir); XMFLOAT4 oo, rr; XMStoreFloat4(&oo, rayOrigin); XMStoreFloat4(&rr, rayDir); XMVECTOR a = XMVectorSet(0,0,0,1); // point on plane XMVECTOR n = XMVectorSet(0,1,0,0); // plane's normal XMVECTOR res = XMVector3Dot((a - rayOrigin), n) / XMVector4Dot(rayDir, n); float t = XMVectorGetX(res); XMVECTOR hit_point = rayOrigin + t*rayDir; XMFLOAT4 hp; XMStoreFloat4(&hp, hit_point); XMFLOAT2 coords; coords.x = XMVectorGetX(hit_point) / GRID_WIDTH + 0.5f; coords.y = XMVectorGetZ(hit_point) / GRID_WIDTH + 0.5f; coords.y = 1.0f - coords.y; XMFLOAT4 mo, md; XMStoreFloat4(&mo, rayOrigin); XMStoreFloat4(&md, rayDir); //------------------------------------------------------------ // save/set Render Target View ID3D11DepthStencilView* dsv; ID3D11RenderTargetView* rtv; _dxImmedDC->OMGetRenderTargets(1, &rtv, &dsv); D3D11_VIEWPORT vp; UINT vp_num = 1; _dxImmedDC->RSGetViewports(&vp_num, &vp); D3D11_VIEWPORT new_vp; new_vp.Height = 1024; new_vp.Width = 1024; new_vp.MaxDepth =1.0f; new_vp.MinDepth = 0.0f; new_vp.TopLeftX = new_vp.TopLeftY = 0.0f; _dxImmedDC->RSSetViewports(1, &new_vp); _dxImmedDC->OMSetRenderTargets(1, &m_rtvDynamicHmap, 0); // save input layout ID3D11InputLayout* ia; _dxImmedDC->IAGetInputLayout(&ia); // set null layout _dxImmedDC->IASetInputLayout(0); _dxImmedDC->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); setBoolVar(m_fxDynamicTerrain, glb_bAdditive, "bAdditive"); setVectorVar(m_fxDynamicTerrain, (void*)&coords, "vCoords"); setFloatVar(m_fxDynamicTerrain, glb_Range, "fRange"); // draw 4 points (-> quad) ID3DX11EffectTechnique* tech; tech = m_fxDynamicTerrain->GetTechniqueByIndex(0); tech->GetPassByIndex(0)->Apply(0, _dxImmedDC); _dxImmedDC->Draw(4, 0); // restore IA layout, rtv, dsv _dxImmedDC->OMSetRenderTargets(1, &rtv, dsv); _dxImmedDC->IASetInputLayout(ia); _dxImmedDC->OMSetBlendState(0, 0, 0xffffffff); _dxImmedDC->OMSetDepthStencilState(0, 0); _dxImmedDC->RSSetViewports(1, &vp); }
bool WorldEntity::circleAALineIntersect( XMVECTOR start, XMVECTOR end, XMVECTOR circleCenter, float circleRadius ) { //A collision function we actually understand... //Create a vector from the start to the circle position XMVECTOR cToStart = circleCenter - start; XMVECTOR cToEnd = circleCenter - end; XMVECTOR lenToStart = XMVector4Length( cToStart ); XMVECTOR lenToEnd = XMVector4Length( cToEnd ); XMFLOAT4 ans; XMStoreFloat4( &ans, lenToStart ); if( ans.x <= circleRadius ){ return true; } XMStoreFloat4( &ans, lenToEnd ); if( ans.x <= circleRadius ){ return true; } //Calculate the start to end XMVECTOR endToStart = end - start; XMVECTOR endToStartLen = XMVector4Length( endToStart ); XMFLOAT4 tmp; XMStoreFloat4(&tmp, endToStartLen); tmp.x = 1.0f / tmp.x; XMVECTOR tmpA = XMVectorScale(endToStart, tmp.x); XMVECTOR tmpB = XMVectorScale(cToStart, tmp.x); //Project it onto the start -> end vector XMVECTOR dot = XMVector4Dot( tmpA, tmpB ); //Calculate the Perpendicular point XMVECTOR per = start + ( endToStart * dot ); //If the perpendicular point is within range of the circle, there is a collision XMVECTOR perDist = XMVector4Length( circleCenter - per ); XMStoreFloat4( &ans, perDist ); if( ans.x > circleRadius ){ return false; } //Store the dot product to see how far down the line the collision happens XMStoreFloat4( &ans, dot ); //If the dot product is negative, the collision is outside the range if( ans.x < 0.0f ){ return false; } //If the dot product is bigger than 1.0f, meaning it scaled the vector passed the segment, the collision is outside the range if( ans.x > 1.0f ){ return false; } return true; }