void Scene::initialize(const UINT &w, const UINT &h, const HWND &window, const HINSTANCE &inst) { DirectX11Core::initialize(w, h, window, 1); t.initialize(); s.initialize(); terrain.initialize("res/terrain.aaa"); box.initialize("res/crate.aaa"); box.setWorldMatrix(XMMatrixTranspose(XMMatrixTranslation(-2.0f,0.5f,0.0f))); sphere.initialize("res/teapot.aaa"); sphere.setWorldMatrix(XMMatrixTranspose(XMMatrixRotationX(-XM_PIDIV2) * XMMatrixTranslation(5.0f, 0.0f, 0.0f))); car.initialize("res/car.aaa"); terrain.setWorldMatrix(XMMatrixTranspose(XMMatrixRotationX(XM_PIDIV2))); cbl.lightCol = XMFLOAT4(1, 1, 1, 1); cbl.lightPosW = XMFLOAT3(0, 0, -1); light = DirectX11Core::createConstantBuffer(sizeof(CBLight), 1, 0, &cbl); c = new FirstPersonCamera; c->initialize(DirectX11Core::mWindowWidth, DirectX11Core::mWindowHeight); Input::initialize(window, inst); }
void Scene(ConstantBuffer& cb) { float t = rotationBoxValue; //頂点シェーダーとピクセルシェーダーのセット g_pImmediateContext->VSSetShader(g_pVertexShader, NULL, 0); g_pImmediateContext->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); g_pImmediateContext->PSSetShader(g_pPixelShader, NULL, 0); g_pImmediateContext->PSSetShaderResources(0, 1, &g_pTextureRV); g_pImmediateContext->PSSetSamplers(0, 1, &g_pSamplerLinear); //頂点バッファとインデックスバッファのセット UINT stride = sizeof(SimpleVertex), offset = 0; g_pImmediateContext->IASetVertexBuffers(0, 1, &g_pVertexBuffer, &stride, &offset); g_pImmediateContext->IASetIndexBuffer(g_pIndexBuffer, DXGI_FORMAT_R16_UINT, 0); //インプットレイアウトのセット g_pImmediateContext->IASetInputLayout(g_pVertexLayout); //キューブの描画 //ワールド行列を変えることでボックスを適当に配置 //1つめ (0,0,5)の位置 g_World = XMMatrixRotationY(t) * XMMatrixRotationX(t / 5) * XMMatrixTranslation(0, 0, 5); cb.mWorld = XMMatrixTranspose(g_World); g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, NULL, &cb, 0, 0); g_pImmediateContext->DrawIndexed(36, 0, 0); //2つめ (0,4,0)の位置 g_World = XMMatrixRotationY(t) * XMMatrixRotationX(t / 5) * XMMatrixTranslation(0, 4, 0); cb.mWorld = XMMatrixTranspose(g_World); g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, NULL, &cb, 0, 0); g_pImmediateContext->DrawIndexed(36, 0, 0); //3つめ (0,-4,0)の位置 g_World = XMMatrixRotationY(t) * XMMatrixRotationX(t / 5) * XMMatrixTranslation(0, -4, 0); cb.mWorld = XMMatrixTranspose(g_World); g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, NULL, &cb, 0, 0); g_pImmediateContext->DrawIndexed(36, 0, 0); //4つめ (-4,0,0)の位置 g_World = XMMatrixRotationY(t) * XMMatrixRotationX(t / 5) * XMMatrixTranslation(-4, 0, 0); cb.mWorld = XMMatrixTranspose(g_World); g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, NULL, &cb, 0, 0); g_pImmediateContext->DrawIndexed(36, 0, 0); //5つめ (4,0,0)の位置 g_World = XMMatrixRotationY(t) * XMMatrixRotationX(t / 5) * XMMatrixTranslation(4, 0, 0); cb.mWorld = XMMatrixTranspose(g_World); g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, NULL, &cb, 0, 0); g_pImmediateContext->DrawIndexed(36, 0, 0); }
void SceneNode::execute(XMMATRIX *world, XMMATRIX* view, XMMATRIX* projection) { // the local_world matrix will be used to calc the local transformations for this node XMMATRIX local_world = XMMatrixIdentity(); local_world = XMMatrixRotationX(XMConvertToRadians(m_xangle)); local_world *= XMMatrixRotationY(XMConvertToRadians(m_yangle)); local_world *= XMMatrixRotationZ(XMConvertToRadians(m_zangle)); local_world *= XMMatrixScaling(m_scale, m_scale, m_scale); local_world *= XMMatrixTranslation(m_x, m_y, m_z); // the local matrix is multiplied by the passed in world matrix that contains the concatenated // transformations of all parent nodes so that this nodes transformations are relative to those local_world *= *world; // only draw if there is a model attached if (m_p_model) m_p_model->draw(world, view, projection); // traverse all child nodes, passing in the concatenated world matrix for (int i = 0; i< m_children.size(); i++) { m_children[i]->execute(&local_world, view, projection); } }
void Aircraft::SetRotation(float x, float y, float z) { _rotation.x = x; _rotation.y = y; _rotation.z = z; XMStoreFloat4x4(&_rotate, XMMatrixRotationX(_rotation.x) * XMMatrixRotationY(_rotation.y) * XMMatrixRotationZ(_rotation.z)); }
XMMATRIX CreateWorldMatrix( float x, float y, float z, float fSize ) // ワールド行列の生成 { float fAngleY; // y軸周り回転角 static float fAngleX = 0.0f; // x軸周り回転角 XMMATRIX matRot_Y; // y軸周り回転行列 XMMATRIX matRot_X; // x軸周り回転行列 XMMATRIX matScaleTrans; // 拡大縮小平行移動行列 // 強制回転 fAngleY = 2.0f * PI * ( float )( timeGetTime() % 3000 ) / 3000.0f; // 行列作成 matRot_Y = XMMatrixRotationY( fAngleY ); matRot_X = XMMatrixRotationX( fAngleX ); matScaleTrans= XMMatrixIdentity(); matScaleTrans._11 = fSize; matScaleTrans._22 = fSize; matScaleTrans._33 = fSize; matScaleTrans._41 = x; matScaleTrans._42 = y; matScaleTrans._43 = z; return matRot_Y * matRot_X * matScaleTrans; // 変換の合成 }
void Entity::SetRot(float x, float y, float z) { XMMATRIX rotX = XMMatrixRotationX(XM_PI*x); XMMATRIX rotY = XMMatrixRotationY(XM_PI*y); XMMATRIX rotZ = XMMatrixRotationZ(XM_PI*z); XMStoreFloat4x4(&mWorld, rotX * rotY * rotZ); }
void Camera::RotateX(float angle){ XMMATRIX rotation = XMMatrixRotationX(angle); XMStoreFloat3(&m_up, XMVector3TransformNormal(XMLoadFloat3(&m_up), rotation)); XMStoreFloat3(&m_look, XMVector3TransformNormal(XMLoadFloat3(&m_look), rotation)); XMStoreFloat3(&m_right, XMVector3TransformNormal(XMLoadFloat3(&m_right), rotation)); }
void Engine::Model::genMatModel(void) const { *_modelMatrix = XMMatrixScaling(_scale->x, _scale->y, _scale->z) * XMMatrixRotationZ(_rotation->z) * XMMatrixRotationY(_rotation->y) * XMMatrixRotationX(_rotation->x) * XMMatrixTranslation(_position->x, _position->y, _position->z); }
/** * Creates a rotation matrix about the X axis. * Angles is clockwise when looking along the axis towards the origin. * * @param fRadians The angle to rotate by. * @return The resulting matrix. */ CFMat4x4 CFMat4x4::CreateRotationX( FLOAT32 fRadians ) { CFMat4x4 matReturn; XMMATRIX& matResult = *reinterpret_cast<XMMATRIX*>( &matReturn ); matResult = XMMatrixRotationX( fRadians ); return matReturn; }
void Transform::UpdateMatrices() { m_Centroid_matrix = XMMatrixTranslation(m_Centroid.x, m_Centroid.y, m_Centroid.z); m_CentroidNeg_matrix = XMMatrixTranslation(-m_Centroid.x, -m_Centroid.y, -m_Centroid.z); m_Translation_matrix = XMMatrixTranslation(m_Position.x, m_Position.y, m_Position.z); m_Rotation_matrix = XMMatrixRotationX(m_Rotation.x) * XMMatrixRotationY(m_Rotation.y) * XMMatrixRotationZ(m_Rotation.z); m_Scale_matrix = XMMatrixScaling(m_Scale.x, m_Scale.y, m_Scale.z); m_World_matrix = XMMatrixTranspose(m_Centroid_matrix * m_Scale_matrix * m_Rotation_matrix * m_Translation_matrix); }
void Camera::UpdateLookAt() { XMMATRIX rotationMatrix = XMMatrixRotationX(m_CameraRotation.x) * XMMatrixRotationY(m_CameraRotation.y); XMVECTOR lookAtOffset = XMVector3Transform(XMVECTOR{0.0f, 0.0f, 1.0f}, rotationMatrix); XMVECTOR cameraPosition = XMLoadFloat3(&m_CameraPosition); XMVECTOR cameraLookAt = cameraPosition + lookAtOffset; XMStoreFloat3(&m_CameraLookAt, cameraLookAt); }
void Entity::RotateX(float angle) { //rotationY = angle; // Rotate the basis vectors about the world y-axis. XMMATRIX R = XMMatrixRotationX(angle); XMStoreFloat3(&mRight, XMVector3TransformNormal(XMLoadFloat3(&mRight), R)); XMStoreFloat3(&mUp, XMVector3TransformNormal(XMLoadFloat3(&mUp), R)); XMStoreFloat3(&mLook, XMVector3TransformNormal(XMLoadFloat3(&mLook), R)); }
void camera::update() { m_rotationX = XMMatrixRotationX(m_rotateDeltaY); m_rotationY = XMMatrixRotationY(m_rotateDeltaX); m_rotateMatrix = m_rotationX*m_rotationY; dir = XMFLOAT3(m_rotateMatrix.r->m128_f32[8], m_rotateMatrix.r->m128_f32[9], m_rotateMatrix.r->m128_f32[10]); originPos.x += dir.x*m_deltaZ; originPos.y += dir.y*m_deltaZ; originPos.z += dir.z*m_deltaZ; dir = XMFLOAT3(m_rotateMatrix.r->m128_f32[4], m_rotateMatrix.r->m128_f32[5], m_rotateMatrix.r->m128_f32[6]); originPos.x += dir.x*m_deltaY; originPos.y += dir.y*m_deltaY; originPos.z += dir.z*m_deltaY; dir = XMFLOAT3(m_rotateMatrix.r->m128_f32[0], m_rotateMatrix.r->m128_f32[1], m_rotateMatrix.r->m128_f32[2]); originPos.x += dir.x*m_deltaX; originPos.y += dir.y*m_deltaX; originPos.z += dir.z*m_deltaX; /*eyePos[0].x += dir.x*2; eyePos[1].x -= dir.x*2;*/ m_translation = XMMatrixTranslation(originPos.x, originPos.y, originPos.z); m_MultiplyMatrix = m_rotateMatrix*m_translation; //calculate mirror right float Xadjust = 5.0f; float Zadjust = 5.0f; m_translation = XMMatrixTranslation(originPos.x - Xadjust, originPos.y + 0.8f, originPos.z - Zadjust); m_mirrorRightViewMatrix = (m_rotateYAxisBack*m_rotateMatrix)*m_translation; m_mirrorRightViewMatrix = XMMatrixInverse(NULL, m_mirrorRightViewMatrix); //calculate mirror left m_translation = XMMatrixTranslation(originPos.x + Xadjust, originPos.y + 0.8f, originPos.z - Zadjust); m_mirrorLeftViewMatrix = (m_rotateYAxisBack*m_rotateMatrix)*m_translation; m_mirrorLeftViewMatrix = XMMatrixInverse(NULL, m_mirrorLeftViewMatrix); //calculate god camera pos m_liftYAxisGod = XMMatrixTranslation(originPos.x, 10.0, originPos.z); m_godCamera = m_rotateXAxisGod*m_liftYAxisGod; //m_godCamera.r->m128_f32[12] = originPos.x; //m_godCamera.r->m128_f32[13] = 0; //m_godCamera.r->m128_f32[14] = originPos.z; m_godCamera = XMMatrixInverse(NULL, m_godCamera); m_viewMatrix = XMMatrixInverse(NULL,m_MultiplyMatrix); }
Skeleton::Skeleton() : numBones( 0 ) { /* This fix is kinda crappy. AssImp has some kind of correction factor that forces the first bone with weights to be rotated to the identity matrix. It means that no matter how you rotate the model it will always be wrong. These numbers are taken directly from the modeling package. The rotation correction seems to be on a node that is the child of the root node in the assimp scene but I could not figure out how to use it or turn it off. */ XMMATRIX rotX = XMMatrixRotationX( ((0.f) * (XM_PI/180.0f)) ); XMMATRIX rotY = XMMatrixRotationY( ((0.f) * (XM_PI/180.0f)) ); XMMATRIX rotZ = XMMatrixRotationZ( ((0.f) * (XM_PI/180.0f)) ); XMMATRIX correctionMatrix = rotX*rotY*rotZ; XMStoreFloat4x4( &rootCorrection, correctionMatrix ); }
// // UpdateBunnyConstantsVS // void UpdateBunnyConstantsVS() { XMMATRIX SpinX = XMMatrixRotationX(XMConvertToRadians(g_SpinY)); XMMATRIX SpinY = XMMatrixRotationY(XMConvertToRadians(g_SpinX)); XMMATRIX Pivot = XMMatrixMultiply(SpinY, SpinX); XMMATRIX Trans = XMMatrixTranslation(0.0f, 0.1f, 0.0f); // Lift slightly up XMMATRIX World = XMMatrixMultiply(Pivot, Trans); XMMATRIX WorldView = XMMatrixMultiply(World, g_View); XMMATRIX WorldViewProj = XMMatrixMultiply(WorldView, g_Proj); glUniformMatrix4fv(g_PhongUniforms.Mpivot, 1, GL_FALSE, (const GLfloat *)&Pivot); glUniformMatrix4fv(g_PhongUniforms.Mworld, 1, GL_FALSE, (const GLfloat *)&World); glUniformMatrix4fv(g_PhongUniforms.Mwvp, 1, GL_FALSE, (const GLfloat *)&WorldViewProj); }
void UIModel::SetUIPosition(Camera & camera) { auto cameraPos = camera.GetPosition(); auto normalviewVec = camera.GetNormalNiewDIr(); float nearPlaneDist = 1.0f; XMFLOAT3 centerPos = { cameraPos.x + normalviewVec.x / nearPlaneDist, cameraPos.y + normalviewVec.y / nearPlaneDist, cameraPos.z + normalviewVec.z / nearPlaneDist, }; float deltaX = (m_uiPosx * camera.GetViewSizeWidth() - camera.GetViewSizeWidth() / 2.0f); float deltaY = (m_uiPosy * camera.GetViewSizeHeight() - camera.GetViewSizeHeight() / 2.0f); XMVECTOR v = { centerPos.x, centerPos.y, centerPos.z}; //xz평면으로 돌리는 행렬 XMMATRIX mat = XMMatrixRotationZ(-m_zRot); mat *= XMMatrixRotationY(-m_yRot); mat *= XMMatrixRotationX(-m_xRot); //XZ평면에서 움직임 mat *= XMMatrixTranslation(deltaX, deltaY, 0.0f); //다시 원래 각도로 돌리는 행렬 mat *= XMMatrixRotationX(m_xRot); mat *= XMMatrixRotationY(m_yRot); mat *= XMMatrixRotationZ(m_zRot); //위치이동 XMFLOAT3 resultPos; auto resultVec = XMVector3Transform(v, mat); XMStoreFloat3(&resultPos, resultVec); SetPosition(resultPos); }
void BoidManager::updateWorldMatrix(birds &bird) { XMMATRIX mRotX, mRotY, mRotZ, mTrans, WorldMatrix; XMFLOAT4 iPos = bird.getPosition(); XMFLOAT4 iRot = bird.getRotation(); mRotX = XMMatrixRotationX(XMConvertToRadians(iRot.x)); mRotY = XMMatrixRotationY(XMConvertToRadians(iRot.y)); mRotZ = XMMatrixRotationZ(XMConvertToRadians(iRot.z)); mTrans = XMMatrixTranslation(iPos.x, iPos.y, iPos.z); WorldMatrix = mRotZ * mRotX * mRotY * mTrans; bird.setWorldMatrix(WorldMatrix); }
// // UpdateSphereConstantsVS // void UpdateSphereConstantsVS(XMFLOAT3 Position) { // Sphere radius is 1 by default - too large for us, // scale as appropriate const float SCALE = 1.0f / 80.0f; const XMMATRIX Scale = XMMatrixScaling(SCALE, SCALE, SCALE); XMMATRIX Spin90 = XMMatrixRotationX(XMConvertToRadians(90.0f)); // Need to rotate here because of initial mesh rotation XMMATRIX Trans = XMMatrixTranslation(Position.x, Position.y, Position.z); XMMATRIX World = XMMatrixMultiply(Spin90, Trans); XMMATRIX ScaledWorld = XMMatrixMultiply(Scale, World); XMMATRIX WorldView = XMMatrixMultiply(ScaledWorld, g_View); XMMATRIX WorldViewProj = XMMatrixMultiply(WorldView, g_Proj); glUniformMatrix4fv(g_WvpAndColorUniforms.Mwvp, 1, GL_FALSE, (const GLfloat *)&WorldViewProj); }
void Transform::Update(float t) { // Calculate world matrix XMMATRIX scale = XMMatrixScaling(_scale.x, _scale.y, _scale.z); XMMATRIX rotation = XMMatrixRotationX(_rotation.x) * XMMatrixRotationY(_rotation.y) * XMMatrixRotationZ(_rotation.z); XMMATRIX translation = XMMatrixTranslation(_position.x, _position.y, _position.z); XMStoreFloat4x4(&_world, scale * rotation * translation); if (_parent != nullptr) { XMStoreFloat4x4(&_world, this->GetWorldMatrix() * _parent->GetWorldMatrix()); } _lPosition = GetPosition(); }
void EndOfDirectX11::Update( float dt ) { keyboardDevice_->GetDeviceState( sizeof( keyboardKeys_ ), ( LPVOID )&keyboardKeys_ ); // Button press event. if( GetAsyncKeyState( VK_ESCAPE ) ) { PostQuitMessage( 0 ); } static float y = 0.0; static float x = 0.0; XMMATRIX changeX, changeY; XMMATRIX translationMat = XMMatrixTranslation( 0.0f, 0.0f, 6.0f ); if( KEYDOWN( keyboardKeys_, DIK_UP ) ) { x += 0.01; } if( KEYDOWN( keyboardKeys_, DIK_DOWN ) ) { x -= 0.01; } // Button up event. if( KEYDOWN( keyboardKeys_, DIK_LEFT ) ) { y += 0.01; } // Button up event. if( KEYDOWN( keyboardKeys_, DIK_RIGHT ) ) { y -= 0.01; } changeY = XMMatrixRotationY( XM_PIDIV4 * y ); changeX = XMMatrixRotationX( XM_PIDIV4 * x ); worldMat_ = changeX * changeY * translationMat; memcpy( prevKeyboardKeys_, keyboardKeys_, sizeof( keyboardKeys_ ) ); }
void CActorPlane::Update() { static float fRotationX; static float fRotationY; if (Input->IsKeyDown(VK_RIGHT)) fRotationX += .1f; if (Input->IsKeyDown(VK_LEFT)) fRotationX -= .1f; if (Input->IsKeyDown(VK_UP)) fRotationY += .1f; if (Input->IsKeyDown(VK_DOWN)) fRotationY -= .1f; this->mxWorld = XMMatrixRotationY( fRotationY ) * XMMatrixRotationX( fRotationX ); }
LRESULT CALLBACK WindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ){ switch( message ){ case WM_DESTROY: PostQuitMessage( 0 ); return 0; break; case WM_KEYDOWN: { switch( wParam ){ case( VK_DOWN ): gAngle -= 0.20f; UpdateConstantBuffer(); break; case( VK_UP ): gAngle += 0.20f; UpdateConstantBuffer(); break; case( VK_RIGHT ): gYAngle -= 0.20f; UpdateConstantBuffer(); break; case( VK_LEFT ): gYAngle += 0.20f; UpdateConstantBuffer(); break; } gRotationMatrix = XMMatrixMultiply( XMMatrixRotationX( gAngle ), XMMatrixRotationY( gYAngle ) ); } } return DefWindowProc( hWnd, message, wParam, lParam ); }
void SceneNode::update_collision_tree(XMMATRIX* world, float scale) { // the m_local_world_matrix matrix will be used to calculate the local transformations for this node XMMATRIX m_local_world_matrix = XMMatrixIdentity(); m_local_world_matrix = XMMatrixRotationX(XMConvertToRadians(m_xangle)); m_local_world_matrix *= XMMatrixRotationY(XMConvertToRadians(m_yangle)); m_local_world_matrix *= XMMatrixRotationZ(XMConvertToRadians(m_zangle)); m_local_world_matrix *= XMMatrixScaling(m_scale, m_scale, m_scale); m_local_world_matrix *= XMMatrixTranslation(m_x, m_y, m_z); // the local matrix is multiplied by the passed in world matrix that contains the concatenated // transformations of all parent nodes so that this nodes transformations are relative to those m_local_world_matrix *= *world; // calc the world space scale of this object, is needed to calculate the // correct bounding sphere radius of an object in a scaled hierarchy m_world_scale = scale * m_scale; XMVECTOR v; if (m_p_model) { v = XMVectorSet(m_p_model->GetBoundingSphere_x(), m_p_model->GetBoundingSphere_y(), m_p_model->GetBoundingSphere_z(), 0.0); } else v = XMVectorSet(0, 0, 0, 0); // no model, default to 0 // find and store world space bounding sphere centre v = XMVector3Transform(v, m_local_world_matrix); m_world_centre_x = XMVectorGetX(v); m_world_centre_y = XMVectorGetY(v); m_world_centre_z = XMVectorGetZ(v); // traverse all child nodes, passing in the concatenated world matrix and scale for (int i = 0; i< m_children.size(); i++) { m_children[i]->update_collision_tree(&m_local_world_matrix, m_world_scale); } }
void Frame() { // Update our time static float t = 0.0f; if (g_driverType == D3D_DRIVER_TYPE_REFERENCE) { t += (float)XM_PI * 0.0125f; } else { static ULONGLONG timeStart = 0; ULONGLONG timeCur = GetTickCount64(); if (timeStart == 0) timeStart = timeCur; t = (timeCur - timeStart) / 1000.0f; } // 1st Cube: Rotate around the origin g_World = XMMatrixRotationX(t * -0.1f) * XMMatrixRotationY(t * 1.2f) * XMMatrixRotationZ(t * 0.69f); // Clear the back buffer g_pImmediateContext->ClearRenderTargetView(g_pRenderTargetView, Colors::MidnightBlue); // Clear the depth buffer to 1.0 (max depth) g_pImmediateContext->ClearDepthStencilView(g_pDepthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0); // Update variables for the first cube ConstantBuffer cb; cb.mWorld = XMMatrixTranspose(g_World); cb.mView = XMMatrixTranspose(g_View); cb.mProjection = XMMatrixTranspose(g_Projection); g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, nullptr, &cb, 0, 0); // Render the first cube g_pImmediateContext->VSSetShader(g_pVertexShader, nullptr, 0); g_pImmediateContext->VSSetConstantBuffers(0, 1, &g_pConstantBuffer); g_pImmediateContext->PSSetShader(g_pPixelShader, nullptr, 0); g_pImmediateContext->DrawIndexed(36, 0, 0); // Present our back buffer to our front buffer g_pSwapChain->Present(1, 0); }
XMVECTOR LimitAngle(const XMVECTOR& quat, const XMVECTOR& rotmin, const XMVECTOR& rotmax) { XMVECTOR rot_xyz = GetAngle(quat); /* XMMATRIX mtx = XMMatrixRotationQuaternion(quat); //ZYX Y=-90〜90°Y軸=ねじり方向 float rx = -atan2f(XMVectorGetY(mtx.r[2]),XMVectorGetZ(mtx.r[2])); float ry = asinf(XMVectorGetX(mtx.r[2])); float rz = -atan2f(XMVectorGetX(mtx.r[1]),XMVectorGetX(mtx.r[0])); XMVECTOR rot_xyz = {rx,ry,rz,0}; *rotang_before = rot_xyz; */ rot_xyz = XMVectorMax(rot_xyz, rotmin); rot_xyz = XMVectorMin(rot_xyz, rotmax); XMMATRIX mtx = XMMatrixRotationZ(XMVectorGetZ(rot_xyz)); mtx = XMMatrixMultiply(mtx, XMMatrixRotationY(XMVectorGetY(rot_xyz))); mtx = XMMatrixMultiply(mtx, XMMatrixRotationX(XMVectorGetX(rot_xyz))); return XMQuaternionRotationMatrix(mtx); }
void BoidManager::initialisePositions() { for (int i = 0; i < maxBirds; ++i) { XMMATRIX mRotX, mRotY, mRotZ, mTrans, WorldMatrix; XMFLOAT4 iPos = m_birds.at(i)->getPosition(); XMFLOAT4 iRot = m_birds.at(i)->getRotation(); mRotX = XMMatrixRotationX(XMConvertToRadians(iRot.x)); mRotY = XMMatrixRotationY(XMConvertToRadians(iRot.y)); mRotZ = XMMatrixRotationZ(XMConvertToRadians(iRot.z)); mTrans = XMMatrixTranslation(iPos.x, iPos.y, iPos.z); WorldMatrix = mRotZ * mRotX * mRotY * mTrans; m_birds.at(i)->setWorldMatrix(WorldMatrix); m_birds.at(i)->setVector(XMVector4Normalize(WorldMatrix.r[2])); } }
void Camera::OrbitVertical(float angle) { //orbit back to the starting position //this orbit angle is not quite right. float orbitAngle = XMVectorGetX(XMVector3AngleBetweenVectors(XMLoadFloat3(&XMFLOAT3(0.0, 0.0, 1.0)), XMVector3Cross(GetRightXM(), XMVectorSet(0.0, 1.0, 0.0, 1.0)))); //special case for when you pass 180 if (mPosition.x < 0) orbitAngle = 180 + (90 - orbitAngle); OrbitHorizontal(orbitAngle); //get the angle between the right? of the camera and the right? of the world float check = XMVectorGetX(XMVector3AngleBetweenVectors(XMLoadFloat3(&XMFLOAT3(0.0, 0.0, 1.0)), XMLoadFloat3(&mLook))); if (check < MathHelper::Pi * 0.45 || ((mPosition.y > 0 && angle < 0 ) || ((mPosition.y < 0 && angle > 0))) ) { pitch += angle; //pitch the camaera by that angle XMVECTOR offsetAngle = XMVector3AngleBetweenVectors(XMLoadFloat3(&XMFLOAT3(1.0, 0.0, 0.0)), XMLoadFloat3(&mRight)); RotateY(XMVectorGetX(offsetAngle) + 0.001f); XMMATRIX matrixRot = XMMatrixRotationX(angle); XMStoreFloat3(&mPosition, XMVector3TransformNormal(XMLoadFloat3(&mPosition), matrixRot)); XMStoreFloat3(&mRight, XMVector3TransformNormal(XMLoadFloat3(&mRight), matrixRot)); } //move back OrbitHorizontal(-orbitAngle); //look at the center XMFLOAT3 zero(0.01, 0.01, 0.01); LookAt(mPosition, zero, XMFLOAT3(0.0, 1.0, 0.0)); }
// ワールド行列の生成 XMMATRIX CreateWorldMatrix( void ) { float fAngleY; // y軸周り回転角 static float fAngleX = 0.0f; // x軸周り回転角 XMMATRIX matRot_Y; // y軸周り回転行列 XMMATRIX matRot_X; // x軸周り回転行列 // 強制回転 fAngleY = 2.0f * PI * ( float )( timeGetTime() % 2000 ) / 2000.0f; // キー入力で回転 if ( GetAsyncKeyState( VK_UP ) ) { fAngleX += ROT_SPEED; } if ( GetAsyncKeyState( VK_DOWN ) ) { fAngleX -= ROT_SPEED; } // 行列作成 matRot_Y = XMMatrixRotationY( fAngleY ); matRot_X = XMMatrixRotationX( fAngleX ); return matRot_Y * matRot_X; // 回転の合成 }
void AnimationController::Interpolate( float dt ) { timeSinceStart += dt; Anim anim = *anims[currentAnim]; float animCurrentTime = fmod( timeSinceStart, anim.totalTime ); for( Bone* bone:anim.boneSet ) { XMMATRIX rotMat, scaleMat, translateMat; // Interpolate Rotation auto rotSetIt = anim.rotChannels.find( bone ); if( rotSetIt==anim.rotChannels.end() ) { rotMat = XMMatrixIdentity(); } else { keySet_t rotKeySet = rotSetIt->second; auto itLow = rotKeySet.lower_bound( animCurrentTime ); if( itLow==rotKeySet.begin() ) { itLow = rotKeySet.end(); } --itLow; auto itHigh = rotKeySet.upper_bound( animCurrentTime ); if( itHigh==rotKeySet.end() ) { itHigh = rotKeySet.begin(); } float factor = (animCurrentTime-itLow->first)/(itHigh->first-itLow->first); XMVECTOR low = XMLoadFloat4( &itLow->second ); XMVECTOR high = XMLoadFloat4( &itHigh->second ); XMVECTOR interp = XMQuaternionSlerp( low, high, factor ); XMVECTOR normalized = XMQuaternionNormalize( interp ); rotMat = XMMatrixRotationQuaternion( interp ); } // Interpolate Scale auto scaleSetIt = anim.scaleChannels.find( bone ); if( scaleSetIt==anim.scaleChannels.end() ) { scaleMat = XMMatrixIdentity(); } else { keySet_t scaleKeySet = scaleSetIt->second; auto itLow = scaleKeySet.lower_bound( animCurrentTime ); if( itLow==scaleKeySet.begin() ) { itLow = scaleKeySet.end(); } --itLow; auto itHigh = scaleKeySet.upper_bound( animCurrentTime ); if( itHigh==scaleKeySet.end() ) { itHigh = scaleKeySet.begin(); } float factor = (animCurrentTime-itLow->first)/(itHigh->first-itLow->first); XMFLOAT4 lowVec = itLow->second; XMFLOAT4 highVec = itHigh->second; scaleMat = XMMatrixScaling( lowVec.x+factor*(highVec.x-lowVec.x), lowVec.y+factor*(highVec.y-lowVec.y), lowVec.z+factor*(highVec.z-lowVec.z) ); } // Interpolate Position auto posSetIt = anim.posChannels.find( bone ); if( posSetIt==anim.posChannels.end() ) { translateMat = XMMatrixIdentity(); } else { keySet_t posKeySet = posSetIt->second; auto itLow = posKeySet.lower_bound( animCurrentTime ); if( itLow==posKeySet.begin() ) { itLow = posKeySet.end(); } --itLow; auto itHigh = posKeySet.upper_bound( animCurrentTime ); if( itHigh==posKeySet.end() ) { itHigh = posKeySet.begin(); } float factor = (animCurrentTime-itLow->first)/(itHigh->first-itLow->first); XMFLOAT4 lowVec = itLow->second; XMFLOAT4 highVec = itHigh->second; translateMat = XMMatrixTranslation( lowVec.x+factor*(highVec.x-lowVec.x), lowVec.y+factor*(highVec.y-lowVec.y), lowVec.z+factor*(highVec.z-lowVec.z) ); } // Conversion Matrix - this converts imported coords to DirectX XMMATRIX reflectX = XMMatrixReflect( XMLoadFloat3( &XMFLOAT3( 1.f, 0.f, 0.f ) ) ); XMMATRIX reflectY = XMMatrixReflect( XMLoadFloat3( &XMFLOAT3( 0.f, 1.f, 0.f ) ) ); XMMATRIX reflectZ = XMMatrixReflect( XMLoadFloat3( &XMFLOAT3( 0.f, 0.f, 1.f ) ) ); XMMATRIX rotX = XMMatrixRotationX( XM_PIDIV2 ); XMMATRIX rotY = XMMatrixRotationY( XM_PIDIV2 ); XMMATRIX rotZ = XMMatrixRotationZ( XM_PIDIV2 ); int foo = 17; //DELETEME XMMATRIX flip = XMLoadFloat4x4( &XMFLOAT4X4( 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f, 0.f, 0.f, 0.f, 0.f, 1.f ) ); XMMATRIX finalMat = scaleMat * rotMat * translateMat; XMFLOAT4X4 transform; XMStoreFloat4x4( &transform, finalMat ); finalTransform[bone] = transform; } }
// Fill the command list with all the render commands and dependent state. void D3D12PipelineStateCache::PopulateCommandList() { // Command list allocators can only be reset when the associated // command lists have finished execution on the GPU; apps should use // fences to determine GPU execution progress. ThrowIfFailed(m_commandAllocators[m_frameIndex]->Reset()); // However, when ExecuteCommandList() is called on a particular command // list, that command list can then be reset at any time and must be before // re-recording. ThrowIfFailed(m_commandList->Reset(m_commandAllocators[m_frameIndex].Get(), nullptr)); // Set necessary state. m_commandList->SetGraphicsRootSignature(m_rootSignature.Get()); ID3D12DescriptorHeap* ppHeaps[] = { m_srvHeap.Get() }; m_commandList->SetDescriptorHeaps(_countof(ppHeaps), ppHeaps); m_commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST); m_commandList->RSSetViewports(1, &m_viewport); m_commandList->RSSetScissorRects(1, &m_scissorRect); CD3DX12_CPU_DESCRIPTOR_HANDLE rtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart(), m_frameIndex, m_rtvDescriptorSize); CD3DX12_CPU_DESCRIPTOR_HANDLE intermediateRtvHandle(m_rtvHeap->GetCPUDescriptorHandleForHeapStart(), FrameCount, m_rtvDescriptorSize); CD3DX12_GPU_DESCRIPTOR_HANDLE srvHandle(m_srvHeap->GetGPUDescriptorHandleForHeapStart()); m_commandList->OMSetRenderTargets(1, &intermediateRtvHandle, FALSE, nullptr); // Record commands. m_commandList->ClearRenderTargetView(intermediateRtvHandle, IntermediateClearColor, 0, nullptr); // Draw the scene as normal into the intermediate buffer. PIXBeginEvent(m_commandList.Get(), 0, L"Draw cube"); { static float rot = 0.0f; DrawConstantBuffer* drawCB = (DrawConstantBuffer*)m_dynamicCB.GetMappedMemory(m_drawIndex, m_frameIndex); drawCB->worldViewProjection = XMMatrixTranspose(XMMatrixRotationY(rot) * XMMatrixRotationX(-rot) * m_camera.GetViewMatrix() * m_projectionMatrix); rot += 0.01f; m_commandList->IASetVertexBuffers(0, 1, &m_cubeVbv); m_commandList->IASetIndexBuffer(&m_cubeIbv); m_psoLibrary.SetPipelineState(m_device.Get(), m_rootSignature.Get(), m_commandList.Get(), BaseNormal3DRender, m_frameIndex); m_commandList->SetGraphicsRootConstantBufferView(RootParameterCB, m_dynamicCB.GetGpuVirtualAddress(m_drawIndex, m_frameIndex)); m_commandList->DrawIndexedInstanced(36, 1, 0, 0, 0); m_drawIndex++; } PIXEndEvent(m_commandList.Get()); // Set up the state for a fullscreen quad. m_commandList->IASetVertexBuffers(0, 1, &m_quadVbv); m_commandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); // Indicate that the back buffer will be used as a render target and the // intermediate render target will be used as a SRV. D3D12_RESOURCE_BARRIER barriers[] = { CD3DX12_RESOURCE_BARRIER::Transition(m_renderTargets[m_frameIndex].Get(), D3D12_RESOURCE_STATE_PRESENT, D3D12_RESOURCE_STATE_RENDER_TARGET), CD3DX12_RESOURCE_BARRIER::Transition(m_intermediateRenderTarget.Get(), D3D12_RESOURCE_STATE_RENDER_TARGET, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE) }; m_commandList->ResourceBarrier(_countof(barriers), barriers); m_commandList->SetGraphicsRootDescriptorTable(RootParameterSRV, m_srvHeap->GetGPUDescriptorHandleForHeapStart()); const float black[] = { 0.0f, 0.0f, 0.0f, 0.0f }; m_commandList->ClearRenderTargetView(rtvHandle, black, 0, nullptr); m_commandList->OMSetRenderTargets(1, &rtvHandle, FALSE, nullptr); // Draw some quads using the rendered scene with some effect shaders. PIXBeginEvent(m_commandList.Get(), 0, L"Post-processing"); { UINT quadCount = 0; static const UINT quadsX = 3; static const UINT quadsY = 3; // Cycle through all of the effects. for (UINT i = PostBlit; i < EffectPipelineTypeCount; i++) { if (m_enabledEffects[i]) { D3D12_VIEWPORT viewport = {}; viewport.TopLeftX = (quadCount % quadsX) * (m_viewport.Width / quadsX); viewport.TopLeftY = (quadCount / quadsY) * (m_viewport.Height / quadsY); viewport.Width = m_viewport.Width / quadsX; viewport.Height = m_viewport.Height / quadsY; viewport.MinDepth = 0.0f; viewport.MaxDepth = 0.0f; PIXBeginEvent(m_commandList.Get(), 0, g_cEffectNames[i]); m_commandList->RSSetViewports(1, &viewport); m_psoLibrary.SetPipelineState(m_device.Get(), m_rootSignature.Get(), m_commandList.Get(), static_cast<EffectPipelineType>(i), m_frameIndex); m_commandList->DrawInstanced(4, 1, 0, 0); PIXEndEvent(m_commandList.Get()); } quadCount++; } } PIXEndEvent(m_commandList.Get()); // Revert resource states back to original values. barriers[0].Transition.StateBefore = D3D12_RESOURCE_STATE_RENDER_TARGET; barriers[0].Transition.StateAfter = D3D12_RESOURCE_STATE_PRESENT; barriers[1].Transition.StateBefore = D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE; barriers[1].Transition.StateAfter = D3D12_RESOURCE_STATE_RENDER_TARGET; m_commandList->ResourceBarrier(_countof(barriers), barriers); ThrowIfFailed(m_commandList->Close()); }