Skip to content

Commit f440a48

Browse files
+ 龙书第10章基础代码(拷贝第九章)
1 parent 5c54a00 commit f440a48

File tree

266 files changed

+29160
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

266 files changed

+29160
-0
lines changed

Chapter 10 Blending/Chapter 10 Blending.vcxproj

Lines changed: 448 additions & 0 deletions
Large diffs are not rendered by default.

Chapter 10 Blending/Chapter 10 Blending.vcxproj.filters

Lines changed: 853 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// This code is licensed under the MIT License (MIT).
4+
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
5+
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
6+
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
7+
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
8+
//
9+
// Developed by Minigraph
10+
//
11+
// Author: James Stanard
12+
//
13+
14+
#include "pch.h"
15+
#include "CameraController.h"
16+
#include "Camera.h"
17+
#include "GameInput.h"
18+
19+
using namespace Math;
20+
using namespace GameCore;
21+
22+
CameraController::CameraController( Camera& camera, Vector3 worldUp ) : m_TargetCamera( camera )
23+
{
24+
m_WorldUp = Normalize(worldUp);
25+
m_WorldNorth = Normalize(Cross(m_WorldUp, Vector3(kXUnitVector)));
26+
m_WorldEast = Cross(m_WorldNorth, m_WorldUp);
27+
28+
m_HorizontalLookSensitivity = 2.0f;
29+
m_VerticalLookSensitivity = 2.0f;
30+
m_MoveSpeed = 1000.0f;
31+
m_StrafeSpeed = 1000.0f;
32+
m_MouseSensitivityX = 1.0f;
33+
m_MouseSensitivityY = 1.0f;
34+
35+
m_CurrentPitch = Sin(Dot(camera.GetForwardVec(), m_WorldUp));
36+
37+
Vector3 forward = Normalize(Cross(m_WorldUp, camera.GetRightVec()));
38+
m_CurrentHeading = ATan2(-Dot(forward, m_WorldEast), Dot(forward, m_WorldNorth));
39+
40+
m_FineMovement = false;
41+
m_FineRotation = false;
42+
m_Momentum = true;
43+
44+
m_LastYaw = 0.0f;
45+
m_LastPitch = 0.0f;
46+
m_LastForward = 0.0f;
47+
m_LastStrafe = 0.0f;
48+
m_LastAscent = 0.0f;
49+
}
50+
51+
namespace Graphics
52+
{
53+
extern EnumVar DebugZoom;
54+
}
55+
56+
void CameraController::Update( float deltaTime )
57+
{
58+
(deltaTime);
59+
60+
float timeScale = Graphics::DebugZoom == 0 ? 1.0f : Graphics::DebugZoom == 1 ? 0.5f : 0.25f;
61+
62+
if (GameInput::IsFirstPressed(GameInput::kLThumbClick) || GameInput::IsFirstPressed(GameInput::kKey_lshift))
63+
m_FineMovement = !m_FineMovement;
64+
65+
if (GameInput::IsFirstPressed(GameInput::kRThumbClick))
66+
m_FineRotation = !m_FineRotation;
67+
68+
float speedScale = (m_FineMovement ? 0.1f : 1.0f) * timeScale;
69+
float panScale = (m_FineRotation ? 0.5f : 1.0f) * timeScale;
70+
71+
float yaw = GameInput::GetTimeCorrectedAnalogInput( GameInput::kAnalogRightStickX ) * m_HorizontalLookSensitivity * panScale;
72+
float pitch = GameInput::GetTimeCorrectedAnalogInput( GameInput::kAnalogRightStickY ) * m_VerticalLookSensitivity * panScale;
73+
float forward = m_MoveSpeed * speedScale * (
74+
GameInput::GetTimeCorrectedAnalogInput( GameInput::kAnalogLeftStickY ) +
75+
(GameInput::IsPressed( GameInput::kKey_w ) ? deltaTime : 0.0f) +
76+
(GameInput::IsPressed( GameInput::kKey_s ) ? -deltaTime : 0.0f)
77+
);
78+
float strafe = m_StrafeSpeed * speedScale * (
79+
GameInput::GetTimeCorrectedAnalogInput( GameInput::kAnalogLeftStickX ) +
80+
(GameInput::IsPressed( GameInput::kKey_d ) ? deltaTime : 0.0f) +
81+
(GameInput::IsPressed( GameInput::kKey_a ) ? -deltaTime : 0.0f)
82+
);
83+
float ascent = m_StrafeSpeed * speedScale * (
84+
GameInput::GetTimeCorrectedAnalogInput( GameInput::kAnalogRightTrigger ) -
85+
GameInput::GetTimeCorrectedAnalogInput( GameInput::kAnalogLeftTrigger ) +
86+
(GameInput::IsPressed( GameInput::kKey_e ) ? deltaTime : 0.0f) +
87+
(GameInput::IsPressed( GameInput::kKey_q ) ? -deltaTime : 0.0f)
88+
);
89+
90+
if (m_Momentum)
91+
{
92+
ApplyMomentum(m_LastYaw, yaw, deltaTime);
93+
ApplyMomentum(m_LastPitch, pitch, deltaTime);
94+
ApplyMomentum(m_LastForward, forward, deltaTime);
95+
ApplyMomentum(m_LastStrafe, strafe, deltaTime);
96+
ApplyMomentum(m_LastAscent, ascent, deltaTime);
97+
}
98+
99+
// don't apply momentum to mouse inputs
100+
yaw += GameInput::GetAnalogInput(GameInput::kAnalogMouseX) * m_MouseSensitivityX;
101+
pitch += GameInput::GetAnalogInput(GameInput::kAnalogMouseY) * m_MouseSensitivityY;
102+
103+
m_CurrentPitch += pitch;
104+
m_CurrentPitch = XMMin( XM_PIDIV2, m_CurrentPitch);
105+
m_CurrentPitch = XMMax(-XM_PIDIV2, m_CurrentPitch);
106+
107+
m_CurrentHeading -= yaw;
108+
if (m_CurrentHeading > XM_PI)
109+
m_CurrentHeading -= XM_2PI;
110+
else if (m_CurrentHeading <= -XM_PI)
111+
m_CurrentHeading += XM_2PI;
112+
113+
Matrix3 orientation = Matrix3(m_WorldEast, m_WorldUp, -m_WorldNorth) * Matrix3::MakeYRotation( m_CurrentHeading ) * Matrix3::MakeXRotation( m_CurrentPitch );
114+
Vector3 position = orientation * Vector3( strafe, ascent, -forward ) + m_TargetCamera.GetPosition();
115+
m_TargetCamera.SetTransform( AffineTransform( orientation, position ) );
116+
m_TargetCamera.Update();
117+
}
118+
119+
void CameraController::ApplyMomentum( float& oldValue, float& newValue, float deltaTime )
120+
{
121+
float blendedValue;
122+
if (Abs(newValue) > Abs(oldValue))
123+
blendedValue = Lerp(newValue, oldValue, Pow(0.6f, deltaTime * 60.0f));
124+
else
125+
blendedValue = Lerp(newValue, oldValue, Pow(0.8f, deltaTime * 60.0f));
126+
oldValue = blendedValue;
127+
newValue = blendedValue;
128+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// This code is licensed under the MIT License (MIT).
4+
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
5+
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
6+
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
7+
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
8+
//
9+
// Developed by Minigraph
10+
//
11+
// Author: James Stanard
12+
//
13+
14+
#pragma once
15+
16+
#include "GameCore.h"
17+
#include "VectorMath.h"
18+
19+
namespace Math
20+
{
21+
class Camera;
22+
}
23+
24+
namespace GameCore
25+
{
26+
using namespace Math;
27+
28+
class CameraController
29+
{
30+
public:
31+
// Assumes worldUp is not the X basis vector
32+
CameraController( Camera& camera, Vector3 worldUp );
33+
34+
void Update( float dt );
35+
36+
void SlowMovement( bool enable ) { m_FineMovement = enable; }
37+
void SlowRotation( bool enable ) { m_FineRotation = enable; }
38+
39+
void EnableMomentum( bool enable ) { m_Momentum = enable; }
40+
41+
Vector3 GetWorldEast() { return m_WorldEast; }
42+
Vector3 GetWorldUp() { return m_WorldUp; }
43+
Vector3 GetWorldNorth() { return m_WorldNorth; }
44+
float GetCurrentHeading() { return m_CurrentHeading; }
45+
float GetCurrentPitch() { return m_CurrentPitch; }
46+
47+
void SetCurrentHeading(float heading) { m_CurrentHeading = heading; }
48+
void SetCurrentPitch(float pitch) { m_CurrentPitch = pitch; }
49+
50+
51+
private:
52+
CameraController& operator=( const CameraController& ) {return *this;}
53+
54+
void ApplyMomentum( float& oldValue, float& newValue, float deltaTime );
55+
56+
Vector3 m_WorldUp;
57+
Vector3 m_WorldNorth;
58+
Vector3 m_WorldEast;
59+
Camera& m_TargetCamera;
60+
float m_HorizontalLookSensitivity;
61+
float m_VerticalLookSensitivity;
62+
float m_MoveSpeed;
63+
float m_StrafeSpeed;
64+
float m_MouseSensitivityX;
65+
float m_MouseSensitivityY;
66+
67+
float m_CurrentHeading;
68+
float m_CurrentPitch;
69+
70+
bool m_FineMovement;
71+
bool m_FineRotation;
72+
bool m_Momentum;
73+
74+
float m_LastYaw;
75+
float m_LastPitch;
76+
float m_LastForward;
77+
float m_LastStrafe;
78+
float m_LastAscent;
79+
};
80+
}

0 commit comments

Comments
 (0)