void MainLoop() { Layer[0] = new VRLayer(Session); while (HandleMessages()) { //Need to check we're visible, before proceeding with velocity changes, //otherwise it does this a lot of times, and we //end up miles away from our start point from the sheer number of iterations. ovrSessionStatus sessionStatus; ovr_GetSessionStatus(Session, &sessionStatus); if (sessionStatus.IsVisible) { // Take out manual yaw rotation (leaving button move for now) ActionFromInput(1, false); ovrTrackingState trackingState = Layer[0]->GetEyePoses(); // Set various control methods into camera MainCam->Pos = XMVectorAdd(MainCam->Pos, FindVelocityFromTilt(this, Layer[0], &trackingState)); MainCam->Pos = XMVectorSet(XMVectorGetX(MainCam->Pos), GetAccelJumpPosY(this, &trackingState), XMVectorGetZ(MainCam->Pos), 0); MainCam->Rot = GetAutoYawRotation(Layer[0]); // If tap side of Rift, then fire a bullet bool singleTap = WasItTapped(trackingState.HeadPose.LinearAcceleration); static XMVECTOR bulletPos = XMVectorZero(); static XMVECTOR bulletVel = XMVectorZero(); if (singleTap) { XMVECTOR eye0 = ConvertToXM(Layer[0]->EyeRenderPose[0].Position); XMVECTOR eye1 = ConvertToXM(Layer[0]->EyeRenderPose[1].Position); XMVECTOR midEyePos = XMVectorScale(XMVectorAdd(eye0, eye1), 0.5f); XMVECTOR totalRot = XMQuaternionMultiply(ConvertToXM(Layer[0]->EyeRenderPose[0].Orientation), MainCam->Rot); XMVECTOR posOfOrigin = XMVectorAdd(MainCam->Pos, XMVector3Rotate(midEyePos, MainCam->Rot)); XMVECTOR unitDirOfMainCamera = XMVector3Rotate(XMVectorSet(0, 0, -1, 0), totalRot); bulletPos = XMVectorAdd(posOfOrigin, XMVectorScale(unitDirOfMainCamera, 2.0f)); bulletVel = XMVectorScale(unitDirOfMainCamera, 0.3f); } // Move missile on, and set its position bulletPos = XMVectorAdd(bulletPos, bulletVel); XMStoreFloat3(&RoomScene->Models[1]->Pos, bulletPos); for (int eye = 0; eye < 2; ++eye) { Layer[0]->RenderSceneToEyeBuffer(MainCam, RoomScene, eye); } Layer[0]->PrepareLayerHeader(); DistortAndPresent(1); } } }
_Use_decl_annotations_ __forceinline void __vectorcall FindBarycentricCoordinates(FXMVECTOR a, FXMVECTOR b, FXMVECTOR c, FXMVECTOR p, float* s, float* r, float* t) { XMVECTOR u = b - a; XMVECTOR v = c - a; XMVECTOR w = p - a; XMVECTOR vCrossW = XMVector3Cross(v, w); XMVECTOR vCrossU = XMVector3Cross(v, u); // Validate r is positive (should be if p is in triangle) assert(XMVector2GreaterOrEqual(XMVector3Dot(vCrossW, vCrossU), XMVectorZero())); XMVECTOR uCrossW = XMVector3Cross(u, w); XMVECTOR uCrossV = XMVector3Cross(u, v); // Validate t is positive (should be if p is in triangle) assert(XMVector2GreaterOrEqual(XMVector3Dot(uCrossW, uCrossV), XMVectorZero())); XMVECTOR denom = XMVector3Length(uCrossV); XMVECTOR R = XMVector3Length(vCrossW) / denom; XMVECTOR T = XMVector3Length(uCrossW) / denom; assert(XMVector2LessOrEqual(R + T, XMVectorSet(1, 1, 1, 1))); *r = XMVectorGetX(R); *t = XMVectorGetX(T); *s = 1 - (*r) - (*t); }
_Use_decl_annotations_ bool __vectorcall ShapeCast(const XMFLOAT3& posA, const XMFLOAT3& moveA, SupportMapping* supA, const XMFLOAT3& posB, const XMFLOAT3& moveB, SupportMapping* supB, XMFLOAT3* normal, float* distance) { XMVECTOR s = XMLoadFloat3(&posA); XMVECTOR r = XMVectorSubtract(XMLoadFloat3(&moveA), XMLoadFloat3(&moveB)); XMVECTOR lambda = XMVectorZero(); XMVECTOR x = s; XMVECTOR v = x - XMLoadFloat3(&posB); XMVECTOR vDotR; XMVECTOR p; XMVECTOR w; XMVECTOR simplexP[5] = {}; uint32_t bits = 0; *distance = 0.0f; *normal = XMFLOAT3(0, 0, 0); XMVECTOR e2 = g_XMEpsilon * g_XMEpsilon; uint32_t iterations = 0; while (XMVector2Greater(XMVector3LengthSq(v), e2) && iterations++ < 1000) { v = XMVector3Normalize(v); p = supA->GetSupportPoint(v) + supB->GetSupportPoint(v); w = x - p; if (XMVector2Greater(XMVector3Dot(v, w), XMVectorZero())) { vDotR = XMVector3Dot(v, r); if (XMVector2GreaterOrEqual(vDotR, XMVectorZero())) { return false; } lambda = lambda - XMVector3Dot(v, w) / vDotR; if (XMVector2Greater(lambda, XMVectorSet(1, 1, 1, 1))) { return false; } x = s + lambda * r; XMStoreFloat3(normal, v); } AddPoint(simplexP, p, &bits); v = FindSupportVectorAndReduce(simplexP, x, &bits); } *distance = XMVectorGetX(lambda); return true; }
void World::moveEntity( WorldEntity* entity, XMVECTOR moveVec, float dist ) { //Try to move the entity in the world along the moveVec XMVECTOR pos = XMLoadFloat4( &entity->getPosition() ); XMVECTOR wall = XMVectorZero(); XMVECTOR vel = XMVectorScale( moveVec, dist ); XMVECTOR desiredPos; XMFLOAT4 check; XMFLOAT4 negativeCheck; int checks = 0; int maxChecks = 10; float saveDist = dist; float distInterval = dist / static_cast<float>(maxChecks); XMStoreFloat4( &check, XMVector3Length(moveVec) ); //Don't bother doing anything if there is no movement direction if( check.x <= 0.0f ){ return; } //Check collision at where we plan to be if( checkEntityCollision( entity, pos, &wall ) ){ XMStoreFloat4( &check, wall ); XMStoreFloat4( &negativeCheck, vel ); //Check the negative bit of the x and z values and see if they are equal, if they are, then stop movement bool negativeXWall = *(int*)(&check.x) < 0; bool negativeZWall = *(int*)(&check.z) < 0; bool negativeXVel = *(int*)(&negativeCheck.x) < 0; bool negativeZVel = *(int*)(&negativeCheck.z) < 0; //If we are not in a corner, collide with the wall, otherwise stop movement if(check.w <= 1.5f || ( negativeXWall != negativeXVel || negativeZWall != negativeZVel ) ){ XMVECTOR invWall = XMVectorNegate( wall ); wall = wall * XMVector3Length( moveVec * invWall ); vel = (moveVec - wall) * dist; }else{ vel = XMVectorZero(); } } desiredPos = pos + vel; XMStoreFloat4( &entity->getPosition(), desiredPos ); }
XMVECTOR MathHelper::RandHemisphereUnitVec3(XMVECTOR n) { XMVECTOR One = XMVectorSet(1.0f, 1.0f, 1.0f, 1.0f); XMVECTOR Zero = XMVectorZero(); // Keep trying until we get a point on/in the hemisphere. while(true) { // Generate random point in the cube [-1,1]^3. XMVECTOR v = XMVectorSet(MathHelper::RandF(-1.0f, 1.0f), MathHelper::RandF(-1.0f, 1.0f), MathHelper::RandF(-1.0f, 1.0f), 0.0f); // Ignore points outside the unit sphere in order to get an even distribution // over the unit sphere. Otherwise points will clump more on the sphere near // the corners of the cube. if( XMVector3Greater( XMVector3LengthSq(v), One) ) continue; // Ignore points in the bottom hemisphere. if( XMVector3Less( XMVector3Dot(n, v), Zero ) ) continue; return XMVector3Normalize(v); } }
bool RayIntersectTriangle( XMVECTOR xmvOrigin , XMVECTOR xmvDirection , XMVECTOR xmvP0 , XMVECTOR xmvP1 , XMVECTOR xmvP2 , float *pfU , float *pfV , float *pfRayToTriangle ) { XMVECTOR xmvEdge1 = xmvP1 - xmvP0; XMVECTOR xmvEdge2 = xmvP2 - xmvP0; XMVECTOR xmvP, xmvQ; xmvP = XMVector3Cross(xmvDirection, xmvEdge2); XMVECTOR xmvA = XMVector3Dot(xmvEdge1, xmvP); if (XMVector3Equal(xmvA, XMVectorZero())) return(false); float f = 1.0f / XMVectorGetX(xmvA); XMVECTOR d3dxvP0ToOrigin = xmvOrigin - xmvP0; *pfU = f * XMVectorGetX(XMVector3Dot(d3dxvP0ToOrigin, xmvP)); if ((*pfU < 0.0f) || (*pfU > 1.0f)) return(false); xmvQ = XMVector3Cross(d3dxvP0ToOrigin, xmvEdge1); *pfV = f * XMVectorGetX(XMVector3Dot(xmvDirection, xmvQ)); if ((*pfV < 0.0f) || ((*pfU + *pfV) > 1.0f)) return(false); *pfRayToTriangle = f * XMVectorGetX(XMVector3Dot(xmvEdge2, xmvQ)); return(*pfRayToTriangle >= 0.0f); }
void Ball::Update(float dt) { //d = v*t //v = a*t; //d = v*t*t+c //dp = v*dt //pos = pos + a XMVECTOR force_vec = XMVectorAdd(force,impulse); XMVECTOR mass_vec = XMVectorReplicate(mass); XMVECTOR time_vec = XMVectorReplicate(dt*0.001f); impulse = XMVectorZero(); acceleration = XMVectorDivide(force_vec,mass_vec); velocity = XMVectorMultiplyAdd(acceleration,time_vec,velocity); position = XMVectorMultiplyAdd(velocity,time_vec,position); // auto coll = IntersectCircleAxisAlignedBox(this,&system->table); //if (coll == 0) { //velocity = XMVectorNegate(velocity); //} object_time += dt; }
void TextureDemo::UpdateScene(float dt) { float x = m_radius * sinf(m_phi) * cosf(m_theta); float z = m_radius * sinf(m_phi) * sinf(m_theta); float y = m_radius * cosf(m_phi); XMVECTOR pos = XMVectorSet(x, y, z, 1.0f); XMVECTOR target = XMVectorZero(); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); m_view = XMMatrixLookAtLH(pos, target, up); if (GetAsyncKeyState('1') & 0x8000) { m_currentSamplerState = m_wrapModeSampler.Get(); } if (GetAsyncKeyState('2') & 0x8000) { m_currentSamplerState = m_boderColorModeSampler.Get(); } if (GetAsyncKeyState('3') & 0x8000) { m_currentSamplerState = m_mirrorModeSampler.Get(); } if (GetAsyncKeyState('4') & 0x8000) { m_currentSamplerState = m_clampModeSampler.Get(); } }
//-------------------------------------------------------------------------------------- // Calculate the separation force and add it to the total force. // Param1: The entity will try to move away from entities that are at this distance or closer. // Param2: The maximal size of this force. //-------------------------------------------------------------------------------------- void EntityMovementManager::Separate(float separationRadius, float maximalForce) { XMVECTOR separationVector = XMVectorZero(); // The separation force to prevent overlapping of moving entities // Find the moving entities that are in close proximity to this one (check both teams) std::multimap<float, CollidableObject*> nearbySoldiers; m_pEnvironment->GetNearbyObjects(m_pEntity->GetPosition(), separationRadius, GroupAllSoldiers, nearbySoldiers); // The entity itself will be included in the list of soldiers -> check if there are others as well if(nearbySoldiers.size() > 1) { for(std::multimap<float, CollidableObject*>::const_iterator it = nearbySoldiers.begin(); it != nearbySoldiers.end(); ++it) { if(it->second->GetId() != m_pEntity->GetId()) { // Scale the force according to the proximity of the nearby entity. Thus the push from close objects will be // stronger than that from objects that are farther away. // Avoid possible division by zero float proximity = (it->first != 0) ? it->first : 0.01f; separationVector += (XMLoadFloat2(&m_pEntity->GetPosition()) - XMLoadFloat2(&it->second->GetPosition())) / proximity; } } // Truncate the force according to the maximally allowed separation force. separationVector = XMVector2Normalize(separationVector) * maximalForce; // Add the separation force to the accumulated steering force XMStoreFloat2(&m_steeringForce, XMLoadFloat2(&m_steeringForce) + separationVector); } }
//-------------------------------------------------------------------------------------- // Calculate the wall avoidance force and add it to the total force. This force // is used to push the entity away from walls (objects) that it is getting too close to. // Param1: Obstacles within this radius around the entity will be avoided. // Param2: The maximal size of this force. //-------------------------------------------------------------------------------------- void EntityMovementManager::StayAwayFromWalls(float avoidWallsRadius, float maximalForce) { // Get nearby obstacles std::multimap<float, CollidableObject*> nearbyObjects; m_pEnvironment->GetNearbyObjects(m_pEntity->GetPosition(), avoidWallsRadius, GroupObstacles, nearbyObjects); if(!nearbyObjects.empty()) { XMVECTOR avoidanceForce = XMVectorZero(); for(std::multimap<float, CollidableObject*>::iterator it = nearbyObjects.begin(); it != nearbyObjects.end(); ++it) { // Scale the force according to the proximity of the nearby entity. Thus the push from close objects will be // stronger than that from objects that are farther away. avoidanceForce += (XMLoadFloat2(&m_pEntity->GetPosition()) - XMLoadFloat2(&(it->second->GetPosition()))) / it->first; } // Truncate the force according to the maximally allowed wall avoidance force. avoidanceForce = XMVector2Normalize(avoidanceForce) * maximalForce; // Add the collision avoidance force to the accumulated steering force XMStoreFloat2(&m_steeringForce, XMLoadFloat2(&m_steeringForce) + avoidanceForce); } }
void TexColumnApp::UpdateScene(float dt) { // Convert Spherical to Cartesian coordinates. float x = mRadius*sinf(mPhi)*cosf(mTheta); float z = mRadius*sinf(mPhi)*sinf(mTheta); float y = mRadius*cosf(mPhi); mEyePosW = XMFLOAT3(x, y, z); // Build the view matrix. XMVECTOR pos = XMVectorSet(x, y, z, 1.0f); XMVECTOR target = XMVectorZero(); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMMATRIX V = XMMatrixLookAtLH(pos, target, up); XMStoreFloat4x4(&mView, V); // // Switch the number of lights based on key presses. // if( GetAsyncKeyState('0') & 0x8000 ) mLightCount = 0; if( GetAsyncKeyState('1') & 0x8000 ) mLightCount = 1; if( GetAsyncKeyState('2') & 0x8000 ) mLightCount = 2; if( GetAsyncKeyState('3') & 0x8000 ) mLightCount = 3; }
SH9Color SH9Color::Zero() { SH9Color sh; for(UINT_PTR i = 0; i < 9; ++i) sh.c[i] = XMVectorZero(); return sh; }
//-------------------------------------------------------------------------------------- // Name: Reset() // Desc: Resets the coordinate system. This method should be called if this class // is to be used for a new player skeleton. //-------------------------------------------------------------------------------------- VOID SpineRelativeCameraSpaceCoordinateSystem::Reset( ) { m_vAverageNormalToGravity = XMVectorZero(); m_dwLastTrackingID = 0; m_fAverageSpineHeadLength = 0.0f; m_vAverageSpine = XMVectorSet( 0.0f, 0.0f, 0.0f, 1.0f ); m_vRightHandRelative = XMVectorSet( 0.0f, 0.0f, 0.0f, 0.0f ); m_vLeftHandRelative = XMVectorSet( 0.0f, 0.0f, 0.0f, 0.0f ); }
XMVECTOR HydraManager::getPosition(int controllerIndex) const { if (sixenseIsControllerEnabled(controllerIndex)) return XMLoadFloat3(&XMFLOAT3(-mAcd.controllers[controllerIndex].pos[0], mAcd.controllers[controllerIndex].pos[1] + 900.0f, //Table height offset in mm -mAcd.controllers[controllerIndex].pos[2])) / 1000.0f; return XMVectorZero(); }
float ObjectToCamera(XMFLOAT4X4* _objMatrix, XMFLOAT3 _cameraPos) { XMVECTOR obj = XMVectorZero(); obj = XMVector3Transform(obj, XMLoadFloat4x4(_objMatrix)); float ObjtoCameraX = XMVectorGetX(obj) - _cameraPos.x; float ObjtoCameraY = XMVectorGetY(obj) - _cameraPos.y; float ObjtoCameraZ = XMVectorGetZ(obj) - _cameraPos.z; return ObjtoCameraX*ObjtoCameraX + ObjtoCameraY*ObjtoCameraY + ObjtoCameraZ*ObjtoCameraZ; }
void TextureWave::UpdateScene(float dt) { m_eyePos.x = m_radius * sinf(m_phi) * cosf(m_theta); m_eyePos.z = m_radius * sinf(m_phi) * sinf(m_theta); m_eyePos.y = m_radius * cosf(m_phi); XMVECTOR pos = DirectX::XMVectorSet(m_eyePos.x, m_eyePos.y, m_eyePos.z, 1.0f); XMVECTOR target = XMVectorZero(); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); m_constBufferPerFrameReflect.GlobalEyePos = m_eyePos; m_constBufferPerFrameReflect.GlobalFogStart = 10.0f; m_constBufferPerFrameReflect.GlobalFogRange = 170.f; m_constBufferPerFrameReflect.GlobalFogColor = (XMFLOAT4&)(Colors::Silver); m_view = XMMatrixLookAtLH(pos, target, up); if (GetAsyncKeyState('1') & 0x8000) { m_currentRenderState = m_renderStateManager->GetFrameRenderState(); } if (GetAsyncKeyState('2') & 0x8000) { m_currentRenderState = m_renderStateManager->GetSolidRenderState(); } m_wave.Update(dt); D3D11_MAPPED_SUBRESOURCE waveMapedResource; D3DHelper::ThrowIfFailed(m_d3dContext->Map(m_waveVB.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &waveMapedResource)); D3DHelper::CustomVertexWithTexture* v = reinterpret_cast<D3DHelper::CustomVertexWithTexture*>(waveMapedResource.pData); for (UINT i = 0; i < m_wave.VertexCount(); ++i) { v[i].Position = m_wave[i]; v[i].Normal = m_wave.Normal(i); v[i].TexCoord.x = 0.5f + m_wave[i].x / m_wave.Width(); v[i].TexCoord.y = 0.5f - m_wave[i].z / m_wave.Depth(); } m_d3dContext->Unmap(m_waveVB.Get(), 0); m_waveTexOffset.y += 0.004f *dt; m_waveTexOffset.x += 0.02f *dt; auto translation = XMMatrixTranslation(m_waveTexOffset.x, m_waveTexOffset.y, 0.0f); auto waveScaling = XMMatrixScaling(5.0f, 5.0f, 0.0f); m_waveTextureTransform = translation * waveScaling; m_waveTextureTransform = XMMatrixTranspose(m_waveTextureTransform); }
void MirrorApp::UpdateScene(float dt) { // Convert Spherical to Cartesian coordinates. float x = mRadius*sinf(mPhi)*cosf(mTheta); float z = mRadius*sinf(mPhi)*sinf(mTheta); float y = mRadius*cosf(mPhi); mEyePosW = XMFLOAT3(x, y, z); // Build the view matrix. XMVECTOR pos = XMVectorSet(x, y, z, 1.0f); XMVECTOR target = XMVectorZero(); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMMATRIX V = XMMatrixLookAtLH(pos, target, up); XMStoreFloat4x4(&mView, V); // // Switch the render mode based in key input. // if( GetAsyncKeyState('1') & 0x8000 ) mRenderOptions = RenderOptions::Lighting; if( GetAsyncKeyState('2') & 0x8000 ) mRenderOptions = RenderOptions::Textures; if( GetAsyncKeyState('3') & 0x8000 ) mRenderOptions = RenderOptions::TexturesAndFog; // // Allow user to move box. // if( GetAsyncKeyState('A') & 0x8000 ) mSkullTranslation.x -= 1.0f*dt; if( GetAsyncKeyState('D') & 0x8000 ) mSkullTranslation.x += 1.0f*dt; if( GetAsyncKeyState('W') & 0x8000 ) mSkullTranslation.y += 1.0f*dt; if( GetAsyncKeyState('S') & 0x8000 ) mSkullTranslation.y -= 1.0f*dt; // Don't let user move below ground plane. mSkullTranslation.y = MathHelper::Max(mSkullTranslation.y, 0.0f); // Update the new world matrix. XMMATRIX skullRotate = XMMatrixRotationY(0.5f*MathHelper::Pi); XMMATRIX skullScale = XMMatrixScaling(0.45f, 0.45f, 0.45f); XMMATRIX skullOffset = XMMatrixTranslation(mSkullTranslation.x, mSkullTranslation.y, mSkullTranslation.z); XMStoreFloat4x4(&mSkullWorld, skullRotate*skullScale*skullOffset); }
SpineRelativeCameraSpaceCoordinateSystem::SpineRelativeCameraSpaceCoordinateSystem( ) : m_dwLastTrackingID( 0 ), m_fAverageSpineHeadLength( 0.0f ), m_vAverageNormalToGravity( XMVectorZero() ) { m_vAverageSpine = XMVectorSet( 0.0f, 0.0f, 0.0f, 1.0f ); m_vRightHandRelative = XMVectorSet( 0.0f, 0.0f, 0.0f, 0.0f ); m_vLeftHandRelative = XMVectorSet( 0.0f, 0.0f, 0.0f, 0.0f ); // Call the updates function with default parameters SetUpdateRates(); }
mxUNDONE #if 0 void BatchRenderer::DrawRing( const XMFLOAT3& Origin, const XMFLOAT3& MajorAxis, const XMFLOAT3& MinorAxis, const FColor& Color ) { static const DWORD dwRingSegments = 32; XMFLOAT3 verts[ dwRingSegments + 1 ]; XMVECTOR vOrigin = XMLoadFloat3( &Origin ); XMVECTOR vMajor = XMLoadFloat3( &MajorAxis ); XMVECTOR vMinor = XMLoadFloat3( &MinorAxis ); FLOAT fAngleDelta = XM_2PI / ( float )dwRingSegments; // Instead of calling cos/sin for each segment we calculate // the sign of the angle delta and then incrementally calculate sin // and cosine from then on. XMVECTOR cosDelta = XMVectorReplicate( cosf( fAngleDelta ) ); XMVECTOR sinDelta = XMVectorReplicate( sinf( fAngleDelta ) ); XMVECTOR incrementalSin = XMVectorZero(); static const XMVECTOR initialCos = { 1.0f, 1.0f, 1.0f, 1.0f }; XMVECTOR incrementalCos = initialCos; for( DWORD i = 0; i < dwRingSegments; i++ ) { XMVECTOR Pos; Pos = XMVectorMultiplyAdd( vMajor, incrementalCos, vOrigin ); Pos = XMVectorMultiplyAdd( vMinor, incrementalSin, Pos ); XMStoreFloat3( ( XMFLOAT3* )&verts[i], Pos ); // Standard formula to rotate a vector. XMVECTOR newCos = incrementalCos * cosDelta - incrementalSin * sinDelta; XMVECTOR newSin = incrementalCos * sinDelta + incrementalSin * cosDelta; incrementalCos = newCos; incrementalSin = newSin; } verts[ dwRingSegments ] = verts[0]; // Copy to vertex buffer assert( (dwRingSegments+1) <= MAX_VERTS ); XMFLOAT3* pVerts = NULL; HRESULT hr; V( g_pVB->Lock( 0, 0, (void**)&pVerts, D3DLOCK_DISCARD ) ) memcpy( pVerts, verts, sizeof(verts) ); V( g_pVB->Unlock() ) // Draw ring D3DXCOLOR clr = Color; g_pEffect9->SetFloatArray( g_Color, clr, 4 ); g_pEffect9->CommitChanges(); pd3dDevice->DrawPrimitive( D3DPT_LINESTRIP, 0, dwRingSegments ); }
void D3DTextureDemo::UpdateScene(float dt) { float x = mRadius * sinf(mPhi) * cosf(mTheta); float z = mRadius * sinf(mPhi) * sinf(mTheta); float y = mRadius * cosf(mPhi); mEyePosW = XMFLOAT3(x, y, z); XMVECTOR pos = XMVectorSet(x, y, z, 1.0f); XMVECTOR target = XMVectorZero(); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMMATRIX view = XMMatrixLookAtLH(pos, target, up); XMStoreFloat4x4(&mView, view); static float t_base = 0.0f; if ((m_Timer.TotalTime() - t_base) >= 0.25f) { t_base += 0.25f; DWORD i = 5 + rand() % (mWaves.RowCount() - 10); DWORD j = 5 + rand() % (mWaves.ColumnCount() - 10); float r = MathHelper::RandF(1.0f, 2.0f); mWaves.Disturb(i, j, r); } mWaves.Update(dt); D3D11_MAPPED_SUBRESOURCE mappedData; HR(m_d3dImmediateContext->Map( mWaveVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData), L"Context Map"); Vertex* v = reinterpret_cast<Vertex*>(mappedData.pData); for(UINT i = 0; i < mWaves.VertexCount(); ++i) { v[i].Pos = mWaves[i]; v[i].Normal = mWaves.Normal(i); v[i].Tex.x = 0.5f + mWaves[i].x / mWaves.Width(); v[i].Tex.y = 0.5f - mWaves[i].z / mWaves.Depth(); } m_d3dImmediateContext->Unmap(mWaveVB, 0); XMMATRIX wavesScale = XMMatrixScaling(5.0f, 5.0f, 0.0f); mWaterTexOffset.y += 0.05f * dt; mWaterTexOffset.x += 0.1f * dt; XMMATRIX wavesOffset = XMMatrixTranslation(mWaterTexOffset.x, mWaterTexOffset.y, 0.0f); XMStoreFloat4x4(&mWaterTexTransform, wavesScale*wavesOffset); }
void Box::UpdateScene(float delta) { float x = m_radius * sinf(m_phi) * cosf(m_theta); float z = m_radius * sinf(m_phi) * sinf(m_theta); float y = m_radius * cosf(m_phi); // build the view matrix XMVECTOR pos = XMVectorSet(x, y, z, 1.0f); XMVECTOR target = XMVectorZero(); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); m_view = XMMatrixLookAtLH(pos, target, up); }
void Camera::Rotate(XMFLOAT3 axis, float degrees) { if (XMVector3Equal(to(axis), XMVectorZero()) || degrees == 0.0f) return; // rotate vectors XMFLOAT3 look_at_target = to(to(mTarget) - to(mPosition)); XMFLOAT3 look_at_up = to(to(mUp) - to(mPosition)); look_at_target = to(XMVector3Transform(to(look_at_target), XMMatrixRotationAxis(to(axis), XMConvertToRadians(degrees)))); look_at_up = to(XMVector3Transform(to(look_at_up), XMMatrixRotationAxis(to(axis), XMConvertToRadians(degrees)))); // restore vectors's end points mTarget and mUp from new rotated vectors mTarget = to(to(mPosition) + to(look_at_target)); mUp = to(to(mPosition) + to(look_at_up)); this->initViewMatrix(); XMVECTOR w = XMVectorZero(); }
void Shape::UpdateScence(float dt) { float x = mRadius*sinf(mPhi)*cosf(mTheta); float z = mRadius*sinf(mPhi)*sinf(mTheta); float y = mRadius*cosf(mPhi); XMVECTOR pos = XMVectorSet(x, y, z, 1.0f); XMVECTOR target = XMVectorZero(); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMMATRIX V = XMMatrixLookAtLH(pos, target, up); XMStoreFloat4x4(&mView, V); }
Camera::Camera() : position_(XMVectorSet( 0.0f, 0.0f, -20.0f, 0.0f )), lookat_(XMVectorZero()), up_(XMVectorSet( 0.0f, 1.0f, 0.0f, 0.0f )), view_(XMMatrixIdentity()), projection_(XMMatrixIdentity()), aspectRatio_(1), fovY_(XM_PIDIV4), nearZ_(0.1f), farZ_(1000.0f), updated_(true), resized_(true) { }
void overlay::init_buffers(int window_width, int window_height) { D3D11_BUFFER_DESC vertex_desc, index_desc, const_desc; D3D11_SUBRESOURCE_DATA vertex_data, index_data; std::array<overlay::vertex_t, 6> vertices; std::array<uint32_t, 6> indices; zero_memory(vertex_desc); zero_memory(index_desc); zero_memory(const_desc); zero_memory(vertex_data); zero_memory(index_data); zero_memory(vertices); auto device = d3d_device::instance()->raw(); auto context = d3d_device::instance()->get_context(); for (uint32_t i = 0; i < vertex_count; i++) indices[i] = i; vertex_desc.Usage = D3D11_USAGE_DYNAMIC; vertex_desc.ByteWidth = sizeof(overlay::vertex_t) * vertex_count; vertex_desc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vertex_desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; vertex_data.pSysMem = vertices.data(); index_desc.ByteWidth = sizeof(uint32_t) * vertex_count; index_desc.BindFlags = D3D11_BIND_INDEX_BUFFER; index_data.pSysMem = indices.data(); const_desc.ByteWidth = sizeof(overlay::matrix_t); const_desc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; device->CreateBuffer(&vertex_desc, &vertex_data, &vertex_buffer); device->CreateBuffer(&index_desc, &index_data, &index_buffer); device->CreateBuffer(&const_desc, nullptr, &const_buffer); overlay::matrix_t matrix_buffer; cxmatrix world = XMMatrixIdentity(); cxmatrix view = XMMatrixLookAtLH(XMVectorZero(), XMVectorSet(0.f, 0.f, 1.f, 0.f), XMVectorSet(0.f, 1.f, 0.f, 0.f)); cxmatrix ortho = XMMatrixOrthographicLH(static_cast<float>(window_width), static_cast<float>(window_height), 0.1f, 1000.f); XMStoreFloat4x4(&matrix_buffer.world, XMMatrixTranspose(world)); XMStoreFloat4x4(&matrix_buffer.view, XMMatrixTranspose(view)); XMStoreFloat4x4(&matrix_buffer.ortho, XMMatrixTranspose(ortho)); context->UpdateSubresource(const_buffer.Get(), 0, nullptr, &matrix_buffer, 0, 0); }
void BoxApp::UpdateScene(float dt) { // Convert Spherical to Cartesian coordinates. float x = mRadius*sinf(mPhi)*cosf(mTheta); float z = mRadius*sinf(mPhi)*sinf(mTheta); float y = mRadius*cosf(mPhi); // Build the view matrix. XMVECTOR pos = XMVectorSet(x, y, z, 1.0f); XMVECTOR target = XMVectorZero(); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMMATRIX V = XMMatrixLookAtLH(pos, target, up); XMStoreFloat4x4(&mView, V); }
void WavesApp::UpdateScene(float dt) { // Convert Spherical to Cartesian coordinates. float x = m_fRadius * sinf(m_fPhi) * cosf(m_fTheta); float z = m_fRadius * sinf(m_fPhi) * sinf(m_fTheta); float y = m_fRadius*cosf(m_fPhi); XMVECTOR pos = XMVectorSet(x, y, z, 1.0f); XMVECTOR target = XMVectorZero(); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMMATRIX V = XMMatrixLookAtLH(pos, target, up); XMStoreFloat4x4(&m_viewMatrix, V); // // Every quarter second, generate a random wave. // static float s_baseT = 0.0f; if ((mTimer.GetTotalTime() - s_baseT)>=0.25f) { s_baseT += 0.25f; UINT i = 5 + rand() % 190; UINT j = 5 + rand() % 190; float r = MathHelper::RandF(1.0f, 2.0f); m_waves.Disturb(i, j, r);//jingz 假设这个是生成浪花,数据变化时更新了什么内容呢? } m_waves.Update(dt); // // Update the wave vertex buffer with the new solution. // D3D11_MAPPED_SUBRESOURCE mappedData; HR(m_pD3dImmediateContext->Map(m_pWavesVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData)); Vertex * vertices = reinterpret_cast<Vertex *>(mappedData.pData); for (UINT i = 0; i < m_waves.GetVertexCount();++i) { vertices[i].Pos = m_waves[i]; vertices[i].Color = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f); } m_pD3dImmediateContext->Unmap(m_pWavesVB,0); }
void BoxApp::UpdateScene(float dt) { static float deltax = 0.001f; static float deltar = 45.0f; // Convert Spherical to Cartesian coordinates. float x = mRadius*sinf(mPhi)*cosf(mTheta); float z = mRadius*sinf(mPhi)*sinf(mTheta); float y = mRadius*cosf(mPhi); // Build the view matrix. XMVECTOR pos = XMVectorSet(0.0f, 0.0f, -6.0f, 1.0f); XMVECTOR target = XMVectorZero(); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMMATRIX V = XMMatrixLookAtLH(pos, target, up); XMStoreFloat4x4(&mView, V); // Move the block float var = figures[0]->GetPosition().y; if(figures[0]->GetPosition().y > 1.0f) { figures[0]->GetPosition().y = 0.98f; deltax = -0.001f; } else if(figures[0]->GetPosition().y < -1.0f) { figures[0]->GetPosition().y = -0.98f; deltax = 0.001f; } figures[0]->move(D3DXVECTOR3(0.00f, deltax, 0.0f)); figures[1]->move(D3DXVECTOR3(0.00f, deltax, 0.0f)); figures[2]->move(D3DXVECTOR3(0.00f, deltax, 0.0f)); figures[3]->move(D3DXVECTOR3(0.00f, deltax, 0.0f)); blocks[0]->ApplyTransform(); }
void LightingApp::UpdateScene(float dt) { float x = mRadius*sinf(mPhi)*cosf(mTheta); float z = mRadius*sinf(mPhi)*sinf(mTheta); float y = mRadius*cosf(mPhi); mEyePosW = XMFLOAT3(x, y, z); XMVECTOR pos = XMVectorSet(x, y, z, 1.0f); XMVECTOR target = XMVectorZero(); XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); XMMATRIX V = XMMatrixLookAtLH(pos, target, up); XMStoreFloat4x4(&mView, V); static float t_base = 0.0f; if ((mTimer.TotalTime() - t_base) >= 0.25f) { t_base += 0.25f; DWORD i = 5 + rand() % (mWaves.RowCount() - 10); DWORD j = 5 + rand() % (mWaves.ColumnCount() - 10); float r = MathHelper::RandF(1.0f, 2.0f); mWaves.Disturb(i, j, r); } mWaves.Update(dt); D3D11_MAPPED_SUBRESOURCE mappedData; HR(md3dImmediateContext->Map(mWavesVB, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedData)); Vertex* v = reinterpret_cast<Vertex *>(mappedData.pData); for (UINT i = 0; i < mWaves.VertexCount(); ++i) { v[i].Pos = mWaves[i]; v[i].Normal = mWaves.Normal(i); } md3dImmediateContext->Unmap(mWavesVB, 0); mPointLight.Position.x = 70.0f*cosf(0.2f*mTimer.TotalTime()); mPointLight.Position.z = 70.0f*sinf(0.2f*mTimer.TotalTime()); mPointLight.Position.y = MathHelper::Max(GetHillHeight(mPointLight.Position.x, mPointLight.Position.z) , -3.0f) + 10.0f; mSpotLight.Position = mEyePosW; XMStoreFloat3(&mSpotLight.Direction, XMVector3Normalize(target - pos)); }
void GCamera::Rotate(XMFLOAT3 axis, float degrees) { if (XMVector3Equal(GMathFV(axis), XMVectorZero()) || degrees == 0.0f) return; // rotate vectors XMFLOAT3 look_at_target = GMathVF(GMathFV(mTarget) - GMathFV(mPosition)); XMFLOAT3 look_at_up = GMathVF(GMathFV(mUp) - GMathFV(mPosition)); look_at_target = GMathVF(XMVector3Transform(GMathFV(look_at_target), XMMatrixRotationAxis(GMathFV(axis), XMConvertToRadians(degrees)))); look_at_up = GMathVF(XMVector3Transform(GMathFV(look_at_up), XMMatrixRotationAxis(GMathFV(axis), XMConvertToRadians(degrees)))); // restore vectors's end points mTarget and mUp from new rotated vectors mTarget = GMathVF(GMathFV(mPosition) + GMathFV(look_at_target)); mUp = GMathVF(GMathFV(mPosition) + GMathFV(look_at_up)); this->lookatViewMatrix(); }