void ReflRender::BeginRT(Engine& engine, const RtFlags& flags) { const DWORD cClipPlanes[6] = {D3DCLIPPLANE0, D3DCLIPPLANE1, D3DCLIPPLANE2, D3DCLIPPLANE3, D3DCLIPPLANE4, D3DCLIPPLANE5}; _MyBase::BeginRT(engine, flags); CameraDesc desc = engine.GetContext().GetCamera().GetDesc(); D3DXVec3TransformCoord(&desc.pos, &desc.pos, &_reflMat); D3DXVec3TransformNormal(&desc.dir, &desc.dir, &_reflMat); D3DXVec3TransformNormal(&desc.up, &desc.up, &_reflMat); desc.up = -desc.up; _reflCamera.SetDesc(desc); //reflCamera.AdjustFarPlane(sceneBox, maxFarDist); engine.GetContext().ApplyCamera(&_reflCamera); DWORD enableClipPlanes = cClipPlanes[0]; engine.GetDriver().GetDevice()->SetClipPlane(0, _reflPlane); LSL_ASSERT(_clipPlanes.size() <= 5); for (unsigned i = 0; i < _clipPlanes.size(); ++i) { enableClipPlanes |= cClipPlanes[i + 1]; engine.GetDriver().GetDevice()->SetClipPlane(i + 1, _clipPlanes[i]); } engine.GetContext().SetRenderState(rsClipPlaneEnable, enableClipPlanes); ApplyRT(engine, flags); }
void CElementManager::CreateCircle(const D3DXVECTOR3& pos, FLOAT intensity, const SMap *map, FLOAT lastTime, FLOAT length, INT max, DWORD color, BOOL isPlayer) { D3DXVECTOR3 dir; D3DXMATRIX trans; if (intensity > 1.f) intensity = 1.f; INT cnt = static_cast<INT>(max*intensity+0.5f); D3DXMatrixRotationZ(&trans, 2*D3DX_PI/cnt); UINT t; do { rand_s(&t); dir.x = t%10-5.f; rand_s(&t); dir.y = t%10-5.f; } while (dir.x == 0.f && dir.y == 0.f); dir.z = 0.f; D3DXVec3Normalize(&dir, &dir); if (isPlayer) for (INT i(0); i < cnt; ++i) { D3DXVec3TransformNormal(&dir, &dir, &trans); m_pEcho.push_back(new CLine(pos, dir, intensity, map, intensity <= 2.f/9, lastTime, length, color)); } else for (INT i(0); i < cnt; ++i) { D3DXVec3TransformNormal(&dir, &dir, &trans); m_pAmbience.push_back(new CLine(pos, dir, intensity, map, intensity <= 2.f/9, lastTime, length, color)); } }
void BoundingBox::Transform(const D3DXMATRIX& m) { D3DXVec3TransformCoord(¢er, ¢er, &m); D3DXVec3TransformNormal(&vx, &vx, &m); D3DXVec3TransformNormal(&vy, &vy, &m); D3DXVec3TransformNormal(&vz, &vz, &m); }
//just rotate lights a little in place void Scene::UpdateLights(double fTime) { D3DXVECTOR3 offAngleVec3 = D3DXVECTOR3(1, -4, 0); D3DXMATRIX rotY; D3DXVECTOR3 dir; D3DXVec3Normalize(&offAngleVec3, &offAngleVec3); D3DXMatrixRotationY(&rotY, (float)(fTime / 4.f) * 3.14159265f); D3DXVec3TransformNormal(&dir, &offAngleVec3, &rotY); m_lights[0].vLightDir = dir; D3DXVec3Normalize(&offAngleVec3, &offAngleVec3); D3DXMatrixRotationY(&rotY, (float)(-fTime / 3.f) * 3.14159265f); D3DXVec3TransformNormal(&dir, &offAngleVec3, &rotY); m_lights[1].vLightDir = dir; D3DXVec3Normalize(&offAngleVec3, &offAngleVec3); D3DXMatrixRotationY(&rotY, (float)(fTime / 2.f) * 3.14159265f); D3DXVec3TransformNormal(&dir, &offAngleVec3, &rotY); m_lights[2].vLightDir = dir; D3DXVec3Normalize(&offAngleVec3, &offAngleVec3); D3DXMatrixRotationY(&rotY, (float)(-fTime / 4.f) * 3.14159265f); D3DXVec3TransformNormal(&dir, &offAngleVec3, &rotY); m_lights[3].vLightDir = dir; }
void Camera::Pitch( float angle ) { D3DXMATRIX R; D3DXMatrixRotationAxis(&R, &mRight, angle); D3DXVec3TransformNormal(&mUp, &mUp, &R); D3DXVec3TransformNormal(&mLook, &mLook, &R); }
void Camera::Pitch(float _angle) { D3DXMATRIX p; D3DXMatrixRotationAxis(&p, &right, _angle); D3DXVec3TransformNormal(&up, &up, &p); D3DXVec3TransformNormal(&look, &look, &p); }
void Camera::RotateY( float angle ) { D3DXMATRIX R; D3DXMatrixRotationY(&R, angle); D3DXVec3TransformNormal(&mRight, &mRight, &R); D3DXVec3TransformNormal(&mUp, &mUp, &R); D3DXVec3TransformNormal(&mLook, &mLook, &R); }
void Camera::rotateY(float angle) { D3DXMATRIX rotation; D3DXMatrixRotationY(&rotation,angle); D3DXVec3TransformNormal(&right,&right,&rotation); D3DXVec3TransformNormal(&up,&up,&rotation); D3DXVec3TransformNormal(&look,&look,&rotation); }
void Camera::pitch(float angle) { D3DXMATRIX rotation; D3DXMatrixRotationAxis(&rotation,&right,angle); D3DXVec3TransformNormal(&up,&up,&rotation); D3DXVec3TransformNormal(&look,&look,&rotation); }
void Camera::RotateY(float _angle) { D3DXMATRIX r; D3DXMatrixRotationY(&r, _angle); D3DXVec3TransformNormal(&right, &right, &r); D3DXVec3TransformNormal(&up, &up, &r); D3DXVec3TransformNormal(&look, &look, &r); }
Ray OBB::GetContactPoint(Ray &ray) { D3DXMATRIX p, r, world, invWorld; D3DXMatrixTranslation(&p, m_pos.x, m_pos.y, m_pos.z); D3DXMatrixRotationQuaternion(&r, &m_rot); D3DXMatrixMultiply(&world, &r, &p); D3DXMatrixInverse(&invWorld, NULL, &world); D3DXVECTOR3 org, dir; D3DXVec3TransformCoord(&org, &ray.m_org, &invWorld); D3DXVec3TransformNormal(&dir, &ray.m_dir, &invWorld); D3DXPLANE planes[] = {D3DXPLANE(0.0f, 0.0f, -1.0f, -m_size.z), D3DXPLANE(0.0f, 0.0f, 1.0f, -m_size.z), D3DXPLANE(0.0f, -1.0f, 0.0f, -m_size.y), D3DXPLANE(0.0f, 1.0f, 0.0f, -m_size.y), D3DXPLANE(-1.0f, 0.0f, 0.0f, -m_size.x), D3DXPLANE(1.0f, 0.0f, 0.0f, -m_size.x) }; D3DXVECTOR3 result, normal; int numPlanes = 0; int numIntersections = 0; for (int i=0; i<6; i++) { float d = org.x * planes[i].a + org.y * planes[i].b + org.z * planes[i].c; if (d > -planes[i].d) { D3DXVECTOR3 r; if (D3DXPlaneIntersectLine(&r, &planes[i], &org, &(org + dir * 1000.0f)) != NULL) { numPlanes++; if (abs(r.x) <= m_size.x && abs(r.y) <= m_size.y && abs(r.z) <= m_size.z) { D3DXVec3TransformCoord(&r, &r, &world); result = r; normal = D3DXVECTOR3(planes[i].a, planes[i].b, planes[i].c); numIntersections++; } } } } if (numIntersections == 0) { //Warning! OBB No Intersections! return Ray(ray.m_org, -ray.m_dir); } D3DXVec3Normalize(&normal, &normal); D3DXVec3TransformNormal(&normal, &normal, &world); return Ray(result, normal); }
void CPlayer::Rotate(float x, float y, float z) { D3DXMATRIX mtxRotate; DWORD nCurrentCameraMode = m_pCamera->GetMode(); // 1인칭 카메라 또는 3인칭 카메라의 경우 플레이어의 회전은 약간의 제약이 따름 if ((nCurrentCameraMode == FIRST_PERSON_CAMERA) || (nCurrentCameraMode == THIRD_PERSON_CAMERA)) { // 로컬 x축을 중심으로 회전 회전각도는 -89.0 ~ 89.0 사이로 제한. //if (x != 0.0f) //{ // m_fPitch += x; // if (m_fPitch > +89.0f) { x -= (m_fPitch - 89.0f); m_fPitch = +89.0f; } // if (m_fPitch < -89.9f) { x -= (m_fPitch + 89.0f); m_fPitch = -89.0f; } //} //// 로컬 y축을 중심으로 회전 회전각도 제한없음 //if (y != 0.0f) //{ // //m_fYaw += y; // if (m_fYaw > 360.0f) m_fYaw -= 360.0f; // if (m_fYaw < 0.0f) m_fYaw += 360.0f; //} //// 로컬 z축 회전 몸통을 좌우로 기울이는 것 회전각도는 -20 ~ 20 사이로 제한 //// z는 현재 m_fRoll에서 실제 회전하는 각도 z만큼 회전 //if (z != 0.0f) //{ // m_fRoll += z; // if (m_fRoll > 20.0f) { z -= (m_fRoll - 20.0f); m_fRoll = +20.0f; } // if (m_fRoll < -20.0f) { z -= (m_fRoll + 20.0f); m_fRoll = -20.0f; } //} // 카메라를 x y z 만큼 회전. 플레이어가 회전하면 카메라가 회전 m_pCamera->Rotate(x, y, z); if (y != 0.0f) { D3DXMatrixRotationAxis(&mtxRotate, &m_d3dxvUp, (float)D3DXToRadian(y)); D3DXVec3TransformNormal(&m_d3dxvLook, &m_d3dxvLook, &mtxRotate); D3DXVec3TransformNormal(&m_d3dxvRight, &m_d3dxvRight, &mtxRotate); } } // 회전으로 인해 플레이어의 로컬 축이 서로 직교하지 않을수 잇으므로 z축을 기준으로 // 서로 직교하고 단위벡터가 되도록 한다. D3DXVec3Normalize(&m_d3dxvLook, &m_d3dxvLook); D3DXVec3Cross(&m_d3dxvRight, &m_d3dxvUp, &m_d3dxvLook); D3DXVec3Normalize(&m_d3dxvRight, &m_d3dxvRight); D3DXVec3Cross(&m_d3dxvUp, &m_d3dxvLook, &m_d3dxvRight); D3DXVec3Normalize(&m_d3dxvUp, &m_d3dxvUp); RegenerateWorldMatrix(); }
void DX10_Camera_FirstPerson::Process(float _dt) { // Get mouse input //m_pDirectInput->DetectMouseInput(&m_yawChange, &m_pitchChange); m_yaw += m_yawChange * _dt; m_pitch += m_pitchChange * _dt; // Prevent Gimbal Lock if (m_pitch > m_maxRotation) { m_pitch = m_maxRotation; } if (m_pitch < m_minRotation) { m_pitch = m_minRotation; } // Create a full rotation matrix D3DXMatrixRotationYawPitchRoll(&m_rotationMatrix, m_yaw, m_pitch, 0); D3DXVec3TransformCoord(&m_target, &m_defaultForward, &m_rotationMatrix); D3DXVec3Normalize(&m_target, &m_target); // Calculate a Yaw rotation matrix D3DXMATRIX RotateYTempMatrix; D3DXMatrixRotationY(&RotateYTempMatrix, m_yaw); // Update the Local camera Axis around the Y axis D3DXVec3TransformNormal(&m_right, &m_defaultRight, &RotateYTempMatrix); D3DXVec3TransformNormal(&m_up, &m_up, &RotateYTempMatrix); D3DXVec3TransformNormal(&m_forward, &m_defaultForward, &RotateYTempMatrix); // Adjust the position m_position += m_moveStrafe * m_right * _dt; m_position += m_moveForwards * m_forward * _dt; m_position += m_moveFly * m_up * _dt; // reset the the movement values m_moveStrafe = 0.0f; m_moveForwards = 0.0f; m_moveFly = 0.0f; m_yawChange = 0.0f; m_pitchChange = 0.0f; // Pick a target in front of the camera m_target = m_position + m_target; // Create the View matrix D3DXMatrixLookAtLH(&m_matView, &m_position, &m_target, &m_up); m_pRenderer->SetViewMatrix(m_matView); m_pRenderer->SetEyePosition(m_position); }
void FpsCamera::rotate(float x, float y) { Matrix rotateX, rotateY; D3DXMatrixRotationAxis(&rotateX, &m_right, x * m_delta); D3DXMatrixRotationY(&rotateY, y * m_delta); Matrix total; D3DXMatrixMultiply(&total, &rotateX, &rotateY); D3DXVec3TransformNormal(&m_right, &m_right, &total); D3DXVec3TransformNormal(&m_up, &m_up, &total); D3DXVec3TransformNormal(&m_view, &m_view, &total); normalize(); buildView(); }
void RayIntersection::intersect(engine::IAtomic* atomic, engine::CollisionCallBack callBack, void* data) { _callBack = callBack; _callBackData = data; _bsp = NULL; _bspSector = NULL; _atomic = dynamic_cast<Atomic*>( atomic ); assert( _atomic ); assert( _atomic->_geometry->getOcTreeRoot() ); _geometry = _atomic->_geometry; _vertices = _geometry->getVertices(); _triangles = _geometry->getTriangles(); if( _atomic->_frame->isDirtyHierarchy() ) { _atomic->_frame->synchronizeSafe(); } // transform ray to atomic space Matrix iLTM; D3DXMatrixInverse( &iLTM, NULL, &_atomic->_frame->LTM ); D3DXVec3TransformCoord( &_asRay.start, &_ray.start, &iLTM ); D3DXVec3TransformNormal( &_asRay.end, &_ray.end, &iLTM ); collideAtomicOcTreeSector( _geometry->getOcTreeRoot() ); }
/** Intersects the whole primitive. If hit, it returns a distance other than -1 */ float EditorLinePrimitive::IntersectPrimitive(D3DXVECTOR3* RayOrigin, D3DXVECTOR3* RayDirection, float Epsilon) { UINT i; float Shortest = -1; // Bring the ray into object space D3DXMATRIX invWorld; D3DXMatrixInverse(&invWorld, NULL, &WorldMatrix); D3DXVECTOR3 Origin; D3DXVECTOR3 Dir; D3DXVec3TransformCoord(&Origin, RayOrigin, &invWorld); D3DXVec3TransformNormal(&Dir, RayDirection, &invWorld); // Go through all line segments and intersect them for(i = 0; i < NumVertices; i+=2) { D3DXVECTOR3 vx[2]; D3DXVec3TransformCoord(&vx[0], Vertices[i].Position.toD3DXVECTOR3(), &WorldMatrix); D3DXVec3TransformCoord(&vx[1], Vertices[i+1].Position.toD3DXVECTOR3(), &WorldMatrix); //float Dist = IntersectLineSegment(&Origin, &Dir, Vertices[i].Position.toD3DXVECTOR3(), Vertices[i+1].Position.toD3DXVECTOR3(), Epsilon); float Dist = IntersectLineSegment(RayOrigin, RayDirection, &vx[0], &vx[1], Epsilon); if(Dist < Shortest || Shortest == -1) { Shortest = Dist / Scale.x; } } if(NumSolidVertices>0) { FLOAT fBary1, fBary2; FLOAT fDist; int NumIntersections=0; Shortest = FLT_MAX; for( DWORD i = 0; i < NumSolidVertices; i+=3 ) { D3DXVECTOR3 v0 = *SolidVertices[i + 0].Position.toD3DXVECTOR3(); D3DXVECTOR3 v1 = *SolidVertices[i + 1].Position.toD3DXVECTOR3(); D3DXVECTOR3 v2 = *SolidVertices[i + 2].Position.toD3DXVECTOR3(); // Check if the pick ray passes through this point if( IntersectTriangle( &Origin, &Dir, v0, v1, v2, &fDist, &fBary1, &fBary2 ) ) { if( fDist < Shortest || Shortest == -1) { NumIntersections++; Shortest=0; //if( NumIntersections == MAX_INTERSECTIONS ) // break; } } } } return Shortest; }
void CWallPiece3D::render( const SMatrix4x4& matrix, TPieceVertex* vb, unsigned short* ib, int baseIndex, int& vbcount, int& ibcount, BYTE alpha ) const { int i; // VB const SVertexXyzNormal* srcVB = &mVB[0]; vbcount = mVB.size(); for( i = 0; i < vbcount; ++i ) { SVector3 p, n; D3DXVec3TransformCoord( &p, &srcVB->p, &matrix ); D3DXVec3TransformNormal( &n, &srcVB->n, &matrix ); vb->p = p; DWORD ncol = gVectorToColor( n ); ncol |= (alpha<<24); vb->diffuse = ncol; ++srcVB; ++vb; } // IB const int* srcIB = &mIB[0]; ibcount = mIB.size(); for( i = 0; i < ibcount; ++i ) { int idx = *srcIB + baseIndex; assert( idx >= 0 && idx < 64000 ); *ib = idx; ++srcIB; ++ib; } }
void CEntity::Update() { if(m_pMesh ) { if(m_pMesh->GetColliderType() == MT_UI) { SetVisiable(true); _SINGLE(CObjectManager)->Push_RenderList(this); } else if (_SINGLE(CFrustum)->isInFrustum( m_vPos, GetSize() ) ) { SetVisiable(true); _SINGLE(CObjectManager)->Push_RenderList(this); } else SetVisiable(false); } memcpy(&m_matTrans._41, &m_vPos, sizeof(D3DXVECTOR3)); if(m_bTransformUpdate) { Rotation(); Move(); for(int i = 0; i < AT_MAX; ++i) { //Ãà ¼¼Æà D3DXVec3TransformNormal(&m_vWorldAxis[i], &m_vLocalAxis[i], &m_matRot); D3DXVec3Normalize(&m_vWorldAxis[i], &m_vWorldAxis[i]); } m_bTransformUpdate = false; } }
int CBoss::Update(void) { m_fSeverTime += CTimeMgr::GetInstance()->GetTime(); SetSeverPosMove(); ChangeDir(); D3DXVec3TransformNormal(&m_pInfo->m_vDir, &g_vLook, &m_pInfo->m_matWorld); m_pTerrainCol->CollisionTerrain(&m_pInfo->m_vPos, m_pVerTex); if (m_eBossState != BOSS_IDLE) { if (dynamic_cast<CDynamicMesh*>(m_pBuffer)->m_bAniEnd == true) m_eBossState = BOSS_IDLE; } //SetCurrling(); CObj::Update(); if (!m_bDeath) return 0; else return 100; }
//transforms ray from screen space to world space, used for picking models in the game with the mouse void GetWorldPickingRay(D3DXVECTOR3& vOrigin, D3DXVECTOR3& vDirection) { //TR level:Picking POINT pCursorPosition; GetCursorPos(&pCursorPosition); //transforms the position of the cursor to be relative to the client's window ScreenToClient(pApp->GetMainWindow(), &pCursorPosition); float w = static_cast<float>(pApp->GetPresentParameters().BackBufferWidth); float h = static_cast<float>(pApp->GetPresentParameters().BackBufferHeight); D3DXMATRIX ProjMatrix = camera->GetProjMatrix(); //transforms from screen space to view space float x = (2.0f * pCursorPosition.x/w - 1.0f) / ProjMatrix(0,0); float y = (-2.0f*pCursorPosition.y/h + 1.0f) / ProjMatrix(1,1); //so far the cursor position is transformed from screen space to view space //our models are in world space, though, so we must make one more transformation - from view space to world space D3DXVECTOR3 origin(0.0f, 0.0f, 0.0f); D3DXVECTOR3 direction(x, y, 1.0f); //makes inverse of the view matrix, to transform the coordinates to world space D3DXMATRIX InverseViewMatrix; D3DXMatrixInverse(&InverseViewMatrix, 0, &(camera->GetViewMatrix())); D3DXVec3TransformCoord(&vOrigin, &origin, &InverseViewMatrix); D3DXVec3TransformNormal(&vDirection, &direction, &InverseViewMatrix); D3DXVec3Normalize(&vDirection, &vDirection); }
RAY MOUSE::GetRay() { try { D3DXMATRIX projectionMatrix, viewMatrix, worldViewInverse, worldMatrix; m_pDevice->GetTransform(D3DTS_PROJECTION, &projectionMatrix); m_pDevice->GetTransform(D3DTS_VIEW, &viewMatrix); m_pDevice->GetTransform(D3DTS_WORLD, &worldMatrix); float width = (float)(m_viewport.right - m_viewport.left); float height = (float)(m_viewport.bottom - m_viewport.top); float angle_x = (((2.0f * x) / width) - 1.0f) / projectionMatrix(0,0); float angle_y = (((-2.0f * y) / height) + 1.0f) / projectionMatrix(1,1); RAY ray; ray.org = D3DXVECTOR3(0.0f, 0.0f, 0.0f); ray.dir = D3DXVECTOR3(angle_x, angle_y, 1.0f); D3DXMATRIX m = worldMatrix * viewMatrix; D3DXMatrixInverse(&worldViewInverse, 0, &m); D3DXVec3TransformCoord(&ray.org, &ray.org, &worldViewInverse); D3DXVec3TransformNormal(&ray.dir, &ray.dir, &worldViewInverse); D3DXVec3Normalize(&ray.dir, &ray.dir); return ray; } catch(...) { debug.Print("Error in MOUSE::GetRay()"); } return RAY(); }
void ApplicationClass::TestIntersection(int mouseX, int mouseY) { float pointX, pointY; D3DXMATRIX projectionMatrix, viewMatrix, inverseViewMatrix, worldMatrix, translateMatrix, inverseWorldMatrix; D3DXVECTOR3 direction, origin, rayOrigin, rayDirection; bool intersect, result; // Move the mouse cursor coordinates into the -1 to +1 range. pointX = ((2.0f * (float)mouseX) / (float)m_screenWidth) - 1.0f; pointY = (((2.0f * (float)mouseY) / (float)m_screenHeight) - 1.0f) * -1.0f; // Adjust the points using the projection matrix to account for the aspect ratio of the viewport. m_D3D->GetProjectionMatrix(projectionMatrix); pointX = pointX / projectionMatrix._11; pointY = pointY / projectionMatrix._22; // Get the inverse of the view matrix. m_Camera->GetViewMatrix(viewMatrix); D3DXMatrixInverse(&inverseViewMatrix, NULL, &viewMatrix); // Calculate the direction of the picking ray in view space. direction.x = (pointX * inverseViewMatrix._11) + (pointY * inverseViewMatrix._21) + inverseViewMatrix._31; direction.y = (pointX * inverseViewMatrix._12) + (pointY * inverseViewMatrix._22) + inverseViewMatrix._32; direction.z = (pointX * inverseViewMatrix._13) + (pointY * inverseViewMatrix._23) + inverseViewMatrix._33; // Get the origin of the picking ray which is the position of the camera. origin = m_Camera->GetPosition(); // Get the world matrix and translate to the location of the sphere. m_D3D->GetWorldMatrix(worldMatrix); D3DXMatrixTranslation(&translateMatrix, -5.0f, 1.0f, 5.0f); D3DXMatrixMultiply(&worldMatrix, &worldMatrix, &translateMatrix); // Now get the inverse of the translated world matrix. D3DXMatrixInverse(&inverseWorldMatrix, NULL, &worldMatrix); // Now transform the ray origin and the ray direction from view space to world space. D3DXVec3TransformCoord(&rayOrigin, &origin, &inverseWorldMatrix); D3DXVec3TransformNormal(&rayDirection, &direction, &inverseWorldMatrix); // Normalize the ray direction. D3DXVec3Normalize(&rayDirection, &rayDirection); // Now perform the ray-sphere intersection test. intersect = RaySphereIntersect(rayOrigin, rayDirection, 1.0f); if(intersect == true) { // If it does intersect then set the intersection to "yes" in the text string that is displayed to the screen. result = m_Text->SetIntersection(true, m_D3D->GetDeviceContext()); } else { // If not then set the intersection to "No". result = m_Text->SetIntersection(false, m_D3D->GetDeviceContext()); } return; }
//将屏幕上的点转换为世界坐标系的射线 RAY START::CalcPickingRay(int x, int y) { float px = 0.0f; float py = 0.0f; D3DVIEWPORT9 vp; _device->GetViewport(&vp); D3DXMATRIX proj; _device->GetTransform(D3DTS_PROJECTION, &proj); px = ((( 2.0f*x) / vp.Width) - 1.0f) / proj(0, 0); py = (((-2.0f*y) / vp.Height) + 1.0f) / proj(1, 1); RAY ray; ray._origin = D3DXVECTOR3(0.0f, 0.0f, 0.0f); ray._direction = D3DXVECTOR3(px, py, 1.0f); //将射线平移至世界坐标系,详情可以参照矩阵的平移以及摄像机类的取景变换矩阵 //下面的代码可以用下一个函数代替 D3DXMATRIX view; _device->GetTransform(D3DTS_VIEW,&view); D3DXMatrixInverse(&view, NULL, &view); D3DXVec3TransformCoord(&ray._origin,&ray._origin,&view); D3DXVec3TransformNormal(&(ray._direction),&(ray._direction),&view); D3DXVec3Normalize(&(ray._direction),&(ray._direction)); return ray; }
void Sun::HandleMessage(const D3DXMATRIX *pViewMatrix) { render::Interface *pInterface = render::Interface::GetInstance(); int iW = pInterface->GetWndWidth(); int iH = pInterface->GetWndHeight(); ui::Manager::_tagINPUTINFO *pInputInfo = ui::Manager::GetInstance()->GetInputInfoBuffer(); switch(pInputInfo->eType) { case UIMT_MS_BTNDOWN: if (pInputInfo->dwData == MK_RBUTTON && (pInputInfo->byKeyBuffer[DIK_LALT] & 0x80)) { m_bDrag = TRUE; m_qOld = m_qNow; ScreenToVector(pInputInfo->ptMouse.x,pInputInfo->ptMouse.y,iW,iH,1.0f,&m_vOldVector); } break; case UIMT_MS_MOVE: if (m_bDrag) { ScreenToVector(pInputInfo->ptMouse.x,pInputInfo->ptMouse.y,iW,iH,1.0f,&m_vCurVector); m_qNow = m_qOld * QuatFromBallPoints( m_vOldVector, m_vCurVector ); D3DXMATRIX mInvView; D3DXMatrixInverse(&mInvView, NULL, pViewMatrix); mInvView._41 = mInvView._42 = mInvView._43 = 0; D3DXMATRIX mLastRotInv; D3DXMatrixInverse(&mLastRotInv, NULL, &m_mRotSnapshot); D3DXMATRIX mRot; D3DXMatrixRotationQuaternion(&mRot, &m_qNow); m_mRotSnapshot = mRot; // Accumulate the delta of the arcball's rotation in view space. // Note that per-frame delta rotations could be problematic over long periods of time. m_mRot *= *pViewMatrix * mLastRotInv * mRot * mInvView; // Since we're accumulating delta rotations, we need to orthonormalize // the matrix to prevent eventual matrix skew D3DXVECTOR3* pXBasis = (D3DXVECTOR3*) &m_mRot._11; D3DXVECTOR3* pYBasis = (D3DXVECTOR3*) &m_mRot._21; D3DXVECTOR3* pZBasis = (D3DXVECTOR3*) &m_mRot._31; D3DXVec3Normalize( pXBasis, pXBasis ); D3DXVec3Cross( pYBasis, pZBasis, pXBasis ); D3DXVec3Normalize( pYBasis, pYBasis ); D3DXVec3Cross( pZBasis, pXBasis, pYBasis ); //Transform the default direction vector by the light's rotation matrix D3DXVec3TransformNormal( &m_vCurDirection, &m_vDefDirection , &m_mRot ); } break; } if (!(pInputInfo->dwMouseButtonState & MK_RBUTTON)) { m_bDrag = FALSE; } }
void MirrorDemo::drawReflectedTeapot() { HR(gd3dDevice->SetRenderState(D3DRS_STENCILENABLE, true)); HR(gd3dDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS)); HR(gd3dDevice->SetRenderState(D3DRS_STENCILREF, 0x1)); HR(gd3dDevice->SetRenderState(D3DRS_STENCILMASK, 0xffffffff)); HR(gd3dDevice->SetRenderState(D3DRS_STENCILWRITEMASK, 0xffffffff)); HR(gd3dDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_KEEP)); HR(gd3dDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_KEEP)); HR(gd3dDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE)); // Disable writes to the depth and back buffers HR(gd3dDevice->SetRenderState(D3DRS_ZWRITEENABLE, false)); HR(gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, true)); HR(gd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO)); HR(gd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE)); // Draw mirror to stencil only. drawMirror(); // Re-enable depth writes HR(gd3dDevice->SetRenderState( D3DRS_ZWRITEENABLE, true )); // Only draw reflected teapot to the pixels where the mirror // was drawn to. HR(gd3dDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_EQUAL)); HR(gd3dDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_KEEP)); // Build Reflection transformation. D3DXMATRIX R; D3DXPLANE plane(0.0f, 0.0f, 1.0f, 0.0f); // xy plane D3DXMatrixReflect(&R, &plane); // Save the original teapot world matrix. D3DXMATRIX oldTeapotWorld = mTeapotWorld; // Add reflection transform. mTeapotWorld = mTeapotWorld * R; // Reflect light vector also. D3DXVECTOR3 oldLightVecW = mLightVecW; D3DXVec3TransformNormal(&mLightVecW, &mLightVecW, &R); HR(mFX->SetValue(mhLightVecW, &mLightVecW, sizeof(D3DXVECTOR3))); // Disable depth buffer and render the reflected teapot. HR(gd3dDevice->SetRenderState(D3DRS_ZENABLE, false)); HR(gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, false)); // Finally, draw the reflected teapot HR(gd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW)); drawTeapot(); mTeapotWorld = oldTeapotWorld; mLightVecW = oldLightVecW; // Restore render states. HR(gd3dDevice->SetRenderState(D3DRS_ZENABLE, true)); HR(gd3dDevice->SetRenderState( D3DRS_STENCILENABLE, false)); HR(gd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW)); }
void CPlayer::Turn( float fRadians ) { D3DXMATRIX mRot; D3DXMatrixRotationY( &mRot, fRadians ); D3DXVECTOR3 vNewDir; D3DXVec3TransformNormal( &vNewDir, &m_vDirection, &mRot ); D3DXVec3Normalize( &m_vDirection, &vNewDir ); }
void DXCamera::Update(float _dt) { D3DXMatrixRotationYawPitchRoll(&m_matrixRotation, m_yaw, m_pitch, 0); D3DXVec3TransformCoord(&m_vec3Target, &m_vec3DefaultForward, &m_matrixRotation); D3DXVec3Normalize(&m_vec3Target, &m_vec3Target); switch (m_type) { case eCamera::ECAMERA_DEFAULT: { D3DXVec3TransformNormal(&m_vec3Right, &m_vec3DefaultRight, &m_matrixRotation); D3DXVec3TransformNormal(&m_vec3Forward, &m_vec3DefaultForward, &m_matrixRotation); D3DXVec3Cross(&m_vec3Up, &m_vec3Forward, &m_vec3Right); } break; case eCamera::ECAMERA_FPS: { D3DXMATRIX RotateYTempMatrix; D3DXMatrixRotationY(&RotateYTempMatrix, m_yaw); D3DXVec3TransformNormal(&m_vec3Right, &m_vec3DefaultRight, &RotateYTempMatrix); D3DXVec3TransformNormal(&m_vec3Up, &m_vec3DefaultUp, &RotateYTempMatrix); D3DXVec3TransformNormal(&m_vec3Forward, &m_vec3DefaultForward, &RotateYTempMatrix); } break; case eCamera::ECAMERA_THIRD: { // TODO } break; } // Update position m_vec3Position += m_moveLR * m_vec3Right * _dt; m_vec3Position += m_moveFB * m_vec3Forward * _dt; // Reset the movement m_moveLR = 0.0f; m_moveFB = 0.0f; // Adjust the target m_vec3Target = m_vec3Position + m_vec3Target; D3DXMatrixLookAtLH(&m_matrixView, &m_vec3Position, &m_vec3Target, &m_vec3Up); }
void BoundingBox::TransformRayToLocalSpace(Vector3f *rayOrigin, Vector3f *rayDir)const { Transform *pParentTransform = (Transform*)pParent->GetComponent(TRANSFORM); D3DXMATRIX inverseW; D3DXMATRIX mObjMatrix; pParentTransform->GetTransformMatrix(mObjMatrix); D3DXMatrixInverse(&inverseW, 0, &mObjMatrix); D3DXVec3TransformCoord(rayOrigin, rayOrigin, &inverseW); D3DXVec3TransformNormal(rayDir, rayDir, &inverseW); }
void CTourist::MoveToward(const float fElapsedTime) { D3DXVECTOR3 vTrans(0, 0, m_fSpeed*fElapsedTime); D3DXVec3TransformNormal(&vTrans, &vTrans, &m_mOrientation); m_vPos += vTrans; //平移调整物体世界坐标方向矩阵 D3DXMATRIX mTrans; D3DXMatrixTranslation(&mTrans, vTrans.x, vTrans.y, vTrans.z); D3DXMatrixMultiply(&m_mOrientation, &m_mOrientation, &mTrans); }
void Picking::TransformRay(Ray* ray, D3DXMATRIX* matrix) { //转化射线的起始点, w = 1. D3DXVec3TransformCoord(&ray->m_vOrigin, &ray->m_vOrigin, matrix); //转化射线的方向向量, w = 0. D3DXVec3TransformNormal(&ray->m_vDirection, &ray->m_vDirection, matrix); //单位化方向向量 D3DXVec3Normalize(&ray->m_vDirection, &ray->m_vDirection); }