void TrackingCamera::Update(float deltaTime, float totalTime) { XMVECTOR vPosition = XMLoadFloat3(&position); XMVECTOR vForward = XMLoadFloat3(&forward); XMVECTOR vTarget = XMLoadFloat3(&(target->GetTranslation())); XMVECTOR vUp = XMLoadFloat3(&up); //get vector to object from camera XMVECTOR vToObject = vTarget - vPosition; //get vector projection and rejection of toObject onto forward XMVECTOR vProjection = (XMVector3Dot(vToObject, vForward) / XMVector3Dot(vForward, vForward)) * vForward; XMVECTOR vRejection = vToObject - vProjection; //multiply rejection by trackStrength vRejection = vRejection * trackStrength; //add projection and multiplied rejection XMVECTOR vDirection = vProjection + vRejection; //use new vector as look to XMMATRIX mViewMat = XMMatrixLookToLH(vPosition, vDirection, vUp); XMStoreFloat4x4(&viewMat, XMMatrixTranspose(mViewMat)); }
/** * Create view matrix using position, direction and up vectors * * @param vPos Position of the camera. * @param vDir View direction for the camera to look along. * @param vUp A vector that describes the up direction of the camera. * @return View matrix */ CFMat4x4 CFMat4x4::CreateViewDirection(CFVec3Arg vPos, CFVec3Arg vDir, CFVec3Arg vUp) { CFMat4x4 mOut; XMMATRIX& rmOut = *reinterpret_cast<XMMATRIX*>(&mOut); rmOut = XMMatrixLookToLH(*reinterpret_cast<const XMVECTOR*>(&vPos), *reinterpret_cast<const XMVECTOR*>(&vDir), *reinterpret_cast< const XMVECTOR*>(&vUp)); return mOut; }
void CascadeShadow::CascadeUpdate(){ float nearClip = 0.01f; float farClip = 100.0f; float m_Lamda = 0.8f; // 平行分割処理. float splitPositions[MAX_CASCADE + 1]; ComputeSplitPositions(MAX_CASCADE, m_Lamda, nearClip, farClip, splitPositions); float SlideBack = 50.0f; auto camera = Game::GetMainCamera(); auto campos = camera->gameObject->mTransform->WorldPosition(); auto camvec = camera->gameObject->mTransform->Forward(); float forward = splitPositions[0]; // カスケード処理. for (int i = 0; i<MAX_CASCADE; ++i) { float size = splitPositions[i + 1] - splitPositions[i + 0]; auto lightPos = campos + camvec * (forward + size / 2) + mLightVect * -SlideBack; m_LightView = XMMatrixLookToLH(lightPos, mLightVect, XMVectorSet(0, 1, 0, 1)); forward += size; float aspect = float(WindowState::mWidth) / float(WindowState::mHeight); float fov = XM_PIDIV4; float farHeight = tanf(fov) * splitPositions[i + 1]; float farWidth = farHeight * aspect; size = farWidth * 1.41421356f * 1.41421356f; m_LightProj = XMMatrixOrthographicLH(size, size, 0.01f, 100.0f); // ライトのビュー射影行列. m_ShadowMatrix[i] = m_LightView * m_LightProj; XMVECTOR min, max; // 分割した視錘台の8角をもとめて,ライトのビュー射影空間でAABBを求める. CalculateFrustum( splitPositions[i + 0], splitPositions[i + 1], m_ShadowMatrix[i], &min, &max); // クロップ行列を求める. XMMATRIX crop = CreateCropMatrix(min, max); // シャドウマップ行列と分割位置を設定. m_ShadowMatrix[i] = m_ShadowMatrix[i] * crop; m_SplitPos[i] = splitPositions[i + 1]; } }
void CineCameraClass::Render() { //Create a view matrix based on direction camera is looking XMStoreFloat4x4( &m_viewMatrix, XMMatrixLookToLH( XMLoadFloat3(&position), XMLoadFloat3(&direction), XMLoadFloat3(&upDirection)) ); //Create the projection matrix for 3D rendering. XMStoreFloat4x4(&m_projectionMatrix, XMMatrixPerspectiveFovLH(fieldOfView, screenAspectRatio, CAMERA_SCREEN_NEAR, CAMERA_SCREEN_DEPTH)); return; }
void Camera::BuildMatrices() { XMMATRIX rotation = XMMatrixRotationRollPitchYaw(phi, theta, 0); // rotation around x axis, y axis, z axis XMVECTOR look = rotation.r[2]; XMVECTOR up = {0,1,0}; view = XMMatrixLookToLH(eye, look, up); float fov = 45 / 360.0f * 2 * XM_PI; float aspect = 4/3.0f; float nearz = 0.1f; float farz = 2000; proj = XMMatrixPerspectiveFovLH(fov, aspect, nearz, farz); }
void Camera::update(float deltaTime) { //Update view matrix based on current camera's position //Load variables in for operations XMVECTOR camForward = XMLoadFloat3(&forward); XMVECTOR camUp = XMLoadFloat3(&up); XMVECTOR camLeft = XMVector3Cross(camForward, camUp); XMStoreFloat3(&left, camLeft); XMVECTOR camPos = XMLoadFloat3(&position); XMMATRIX viewMat = XMLoadFloat4x4(&viewMatrix); viewMat = XMMatrixLookToLH(camPos, camForward, camUp); XMStoreFloat4x4(&viewMatrix, XMMatrixTranspose(viewMat)); }
void Camera::LookTo(XMFLOAT3 p_EyePos, XMFLOAT3 p_EyeDir, XMFLOAT3 p_UpPos) { XMVECTOR p_VecEye = XMLoadFloat3(&p_EyePos); XMVECTOR p_VecEyeDir = XMLoadFloat3(&p_EyeDir); XMVECTOR p_VecUp = XMLoadFloat3(&p_UpPos); XMMATRIX p_View = XMMatrixLookToLH(p_VecEye, p_VecEyeDir, p_VecUp); XMStoreFloat4x4(&m_View, p_View); m_Position = p_EyePos; m_Look = p_EyeDir; m_Up = p_UpPos; XMVECTOR t_Right = XMVector3Cross(p_VecUp, p_VecEyeDir); XMStoreFloat3(&m_Right, t_Right); }
XMFLOAT4X4 Camera::GetView() { // Initialize the view matrix XMVECTOR Eye = XMLoadFloat4(&_eye); XMVECTOR At = XMLoadFloat4(&_at); XMVECTOR Up = XMLoadFloat4(&_up); if (_lookTo = true) { XMStoreFloat4x4(&_view, XMMatrixLookToLH(Eye, At, Up)); } else { XMStoreFloat4x4(&_view, XMMatrixLookAtLH(Eye, At, Up)); } return _view; }
bool Camera::Update() { bool transformed = Transform.Moved; if (Transform.Moved) { // Get position and rotation. XMVECTOR rotation = Transform.GetRotation(); XMVECTOR position = Transform.GetPosition(); // Calculate local space directions. XMVECTOR forward = XMVector3Rotate(Vector3::Forward, rotation); XMVECTOR up = XMVector3Rotate(Vector3::Up, rotation); //update view matrix _view = XMMatrixLookToLH(position, forward, up); _vp = XMMatrixMultiply(_view, _projection); Transform.Moved = false; } return transformed; }
Camera::Camera() { //initialize attributes position = XMFLOAT3(0.0f, 0.0f, -5.0f); direction = XMFLOAT3(0.0f, 0.0f, 1.0f); left = XMFLOAT3(0.0f, 0.0f, 0.0f); up = XMFLOAT3(0.0f, 1.0f, 0.0f); forward = XMFLOAT3(0.0f, 0.0f, 1.0f); pitch = 0.0f; yaw = 0.0f; XMMATRIX rotMat = XMLoadFloat4x4(&rotationMatrix); rotMat = XMMatrixIdentity(); XMStoreFloat4x4(&rotationMatrix, rotMat); //initialize viewMatrix XMVECTOR pos = XMVectorSet(0, 0, -5, 0); XMVECTOR dir = XMVectorSet(0, 0, 1, 0); XMVECTOR up = XMVectorSet(0, 1, 0, 0); XMMATRIX V = XMMatrixLookToLH( pos, // The position of the "camera" dir, // Direction the camera is looking up); // "Up" direction in 3D space (prevents roll) XMStoreFloat4x4(&viewMatrix, XMMatrixTranspose(V)); // Transpose for HLSL! }
//Updates our viewMatrix based on the camera's position void MyDemoGame::UpdateCamera() { // values used to translate and rotate the camera in response to input float translationScale = -0.001f; float rotationScale = -.01f; //Left ad right arrow keys alter X position // make all camera manipulations occur at double speed when holding spacebar if (GetAsyncKeyState(VK_SPACE) & 0x8000) { translationScale *= 2.0f; rotationScale *= 2.0f; } if (GetAsyncKeyState(VK_LEFT) & 0x8000) { gameCam.setDistanceX(translationScale); gameCam.setPosition(gameCam.getDistanceX(), gameCam.getDistanceY(), gameCam.getDistanceZ()); cameraPosition = XMVectorSet(gameCam.getPositionX(), gameCam.getPositionY(), gameCam.getPositionZ(), 0); cameraRotation = XMVectorSet(gameCam.getRotationX(), gameCam.getRotationY(), gameCam.getRotationZ(), 0); } if (GetAsyncKeyState(VK_RIGHT) & 0x8000) { gameCam.setDistanceX(-translationScale); gameCam.setPosition(gameCam.getDistanceX(), gameCam.getDistanceY(), gameCam.getDistanceZ()); cameraPosition = XMVectorSet(gameCam.getPositionX(), gameCam.getPositionY(), gameCam.getPositionZ(), 0); cameraRotation = XMVectorSet(gameCam.getRotationX(), gameCam.getRotationY(), gameCam.getRotationZ(), 0); } //Up/Down arrow keys alter Y position if (GetAsyncKeyState(VK_DOWN) & 0x8000) { gameCam.setDistanceY(translationScale); gameCam.setPosition(gameCam.getDistanceX(), gameCam.getDistanceY(), gameCam.getDistanceZ()); cameraPosition = XMVectorSet(gameCam.getPositionX(), gameCam.getPositionY(), gameCam.getPositionZ(), 0); cameraRotation = XMVectorSet(gameCam.getRotationX(), gameCam.getRotationY(), gameCam.getRotationZ(), 0); } if (GetAsyncKeyState(VK_UP) & 0x8000) { gameCam.setDistanceY(-translationScale); gameCam.setPosition(gameCam.getDistanceX(), gameCam.getDistanceY(), gameCam.getDistanceZ()); cameraPosition = XMVectorSet(gameCam.getPositionX(), gameCam.getPositionY(), gameCam.getPositionZ(), 0); cameraRotation = XMVectorSet(gameCam.getRotationX(), gameCam.getRotationY(), gameCam.getRotationZ(), 0); } //5 and 0 on the numpad alter the Z position if (GetAsyncKeyState(VK_NUMPAD0) & 0x8000) { gameCam.setDistanceZ(translationScale); gameCam.setPosition(gameCam.getDistanceX(), gameCam.getDistanceY(), gameCam.getDistanceZ()); cameraPosition = XMVectorSet(gameCam.getPositionX(), gameCam.getPositionY(), gameCam.getPositionZ(), 0); cameraRotation = XMVectorSet(gameCam.getRotationX(), gameCam.getRotationY(), gameCam.getRotationZ(), 0); } if (GetAsyncKeyState(VK_NUMPAD5) & 0x8000) { gameCam.setDistanceZ(-translationScale); gameCam.setPosition(gameCam.getDistanceX(), gameCam.getDistanceY(), gameCam.getDistanceZ()); cameraPosition = XMVectorSet(gameCam.getPositionX(), gameCam.getPositionY(), gameCam.getPositionZ(), 0); cameraRotation = XMVectorSet(gameCam.getRotationX(), gameCam.getRotationY(), gameCam.getRotationZ(), 0); } //4 and 6 on the numpad will rotate along the X axis if (GetAsyncKeyState(VK_NUMPAD4) & 0x8000) { gameCam.setRotationDistanceX(rotationScale); gameCam.setRotation(gameCam.getRotationDistanceX(), gameCam.getRotationDistanceY(), gameCam.getRotationDistanceZ()); cameraRotation = XMVectorSet(gameCam.getRotationX(), gameCam.getRotationY(), gameCam.getRotationZ(), 0); } if (GetAsyncKeyState(VK_NUMPAD6) & 0x8000) { gameCam.setRotationDistanceX(-rotationScale); gameCam.setRotation(gameCam.getRotationDistanceX(), gameCam.getRotationDistanceY(), gameCam.getRotationDistanceZ()); cameraRotation = XMVectorSet(gameCam.getRotationX(), gameCam.getRotationY(), gameCam.getRotationZ(), 0); } //8 ad 2 on the unmpad will rotate along the y axis if (GetAsyncKeyState(VK_NUMPAD8) & 0x8000) { gameCam.setRotationDistanceY(-rotationScale); gameCam.setRotation(gameCam.getRotationDistanceX(), gameCam.getRotationDistanceY(), gameCam.getRotationDistanceZ()); cameraRotation = XMVectorSet(gameCam.getRotationX(), gameCam.getRotationY(), gameCam.getRotationZ(), 0); } if (GetAsyncKeyState(VK_NUMPAD2) & 0x8000) { gameCam.setRotationDistanceY(rotationScale); gameCam.setRotation(gameCam.getRotationDistanceX(), gameCam.getRotationDistanceY(), gameCam.getRotationDistanceZ()); cameraRotation = XMVectorSet(gameCam.getRotationX(), gameCam.getRotationY(), gameCam.getRotationZ(), 0); } //reset camera back to original position if (GetAsyncKeyState('R') & 0x8000) { gameCam.reset(); cameraPosition = XMVectorSet(gameCam.getPositionX(), gameCam.getPositionY(), gameCam.getPositionZ(), 0); cameraRotation = XMVectorSet(gameCam.getRotationX(), gameCam.getRotationY(), gameCam.getRotationZ(), 0); } // return the manipulation scales to their normal values translationScale = -0.001f; rotationScale = -.01f; XMMATRIX V = XMMatrixLookToLH(cameraPosition, cameraRotation, upDirection); XMStoreFloat4x4(&viewMatrix, XMMatrixTranspose(V)); }
// Entry point _Use_decl_annotations_ int WINAPI WinMain(HINSTANCE instance, HINSTANCE, LPSTR, int) { Instance = instance; if (!Initialize()) { assert(false); return -1; } // Initialize graphics std::unique_ptr<Renderer> renderer(Renderer::Create(Window)); if (!renderer) { assert(false); return -4; } ShowWindow(Window, SW_SHOW); UpdateWindow(Window); // Timing info LARGE_INTEGER lastTime {}; LARGE_INTEGER currTime {}; LARGE_INTEGER frequency {}; QueryPerformanceFrequency(&frequency); float invFreq = 1.0f / frequency.QuadPart; // TODO: Replace with something better as needed // Camera info XMVECTOR cameraPosition = XMVectorSet(-1.f, 1.f, -3.f, 1.f); XMVECTOR cameraForward = XMVectorSet(0.f, 0.f, 1.f, 0.f); XMMATRIX worldToView = XMMatrixLookToLH(cameraPosition, cameraForward, XMVectorSet(0.f, 1.f, 0.f, 0.f)); XMMATRIX projection = XMMatrixPerspectiveFovLH( Fov, ScreenWidth / (float)ScreenHeight, // Aspect ratio of window client (rendering) area NearClip, FarClip); // Main loop MSG msg {}; while (msg.message != WM_QUIT) { if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } else { // Idle, measure time and produce a frame QueryPerformanceCounter(&currTime); if (lastTime.QuadPart == 0) { // First frame, skip so we have a good lastTime to compute from lastTime.QuadPart = currTime.QuadPart; continue; } // Compute time step from last frame until now float timeStep = (currTime.QuadPart - lastTime.QuadPart) * invFreq; // Compute fps float frameRate = 1.0f / (float)timeStep; lastTime = currTime; UNREFERENCED_PARAMETER(frameRate); // Handle input if (GetAsyncKeyState(VK_ESCAPE) & 0x8000) { // Exit break; } // TODO: Replace with something better as needed renderer->Render(cameraPosition, worldToView, projection); } } //renderer.reset(); Shutdown(); return 0; }
void CameraObj::UpdateCBuffer(UINT screenWidth, UINT screenHeight) { TransformData tDataTemp = transform->transformData; //XMVECTOR pos = XMVectorSet(tDataTemp.pos.x, tDataTemp.pos.y, tDataTemp.pos.z, 1.0f); //XMVECTOR rotTemp = XMVectorSet(tDataTemp.rot.x, tDataTemp.rot.y, tDataTemp.rot.z, tDataTemp.rot.w); ////Load the stuff XMFLOAT4 rotQuad = XMFLOAT4(tDataTemp.rot.x, tDataTemp.rot.y, tDataTemp.rot.z, tDataTemp.rot.w); //använd denna sen //XMFLOAT4 rotQuad = XMFLOAT4(0, 0, -1, 0); //hårkodad XMVECTOR rotQuadVec = XMLoadFloat4(&rotQuad); rotQuadVec = XMVector4Normalize(rotQuadVec); XMFLOAT3 pos = XMFLOAT3(tDataTemp.pos.x, tDataTemp.pos.y, tDataTemp.pos.z); //XMFLOAT3 pos = XMFLOAT3(0, 0, 200);//hårkodad XMVECTOR posVec = XMLoadFloat3(&pos); ////Load standard vectors XMFLOAT3 startUp = XMFLOAT3(0, 1, 0); XMVECTOR startUpVec = XMLoadFloat3(&startUp); XMFLOAT3 startTar = XMFLOAT3(0, 0, 1); XMVECTOR startTarVec = XMLoadFloat3(&startTar); XMMATRIX rotMatrix = XMMatrixRotationQuaternion(rotQuadVec); startUpVec = XMVector3Transform(startUpVec, rotMatrix); startTarVec = XMVector3Transform(startTarVec, rotMatrix); XMFLOAT3 derpTar; XMStoreFloat3(&derpTar, startTarVec); XMMATRIX cameraMat = XMMatrixLookToLH(posVec, startTarVec, startUpVec); //XMVECTOR rot = XMVector3Rotate(XMVectorSet(1.0f, 1.0f, 1.0f, 0.0f), rotTemp); //+ positionen eller nått sånt, se denna //XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f); //XMMATRIX view = XMMatrixLookAtRH(pos, rot, up); XMMATRIX projection; if (cameraData.isOrtho == 0) { float aspect = (float)screenWidth / (float)screenHeight; projection = XMMatrixPerspectiveFovLH( cameraData.hAngle,//cameraData.hAngle, aspect, //aspect ratio? 1.0f, 4000 ); } else { float fovinv = (float)screenHeight / (float)screenWidth; projection = XMMatrixOrthographicLH(cameraData.hAngle, cameraData.hAngle * fovinv, 1.0f, 4000.0f); } XMMATRIX view = cameraMat; XMStoreFloat4x4(&cameraCBufferData.view, XMMatrixTranspose(view)); XMStoreFloat4x4(&cameraCBufferData.projection, XMMatrixTranspose(projection)); cameraCBufferData.cameraPos[0] = transform->transformData.pos.x; cameraCBufferData.cameraPos[1] = transform->transformData.pos.y; cameraCBufferData.cameraPos[2] = transform->transformData.pos.z; cameraCBufferData.cameraPos[3] = 1; gDeviceContext->UpdateSubresource(cameraCbuffer, 0, NULL, &cameraCBufferData, 0, 0); }