(내가 일반적으로 코드가 아닌 자바에서 설명 하겠지만, 구문 번역하기 쉽습니다)
B * M * P,
동안 :
B = Base (or View/Camera) matrix
M = Model (or objects, like the enemies or the player spaceship) matrix
P = Projection matrix
매트릭스 현장에있는 모든 개체가에 의해 렌더링
업데이트주기에서 렌더링주기 전에 플레이어의 우주선 모델 행렬을 업데이트 한 직후에 따라 기본 행렬을 변경하십시오. 예를 들어 카메라를 우주선 뒤에 배치하려면 먼저 우주선의 방향 벡터와 위쪽을 찾습니다. 우주선의 벡터 :
/// set the offsets between the camera and the spaceship ///
float distCameraToShip = 2.0;
float cameraElevation = 2.0;
// find the ship position ///
Vector3 vShipTranslation = shipMatrix.GetTranslation();
/// or Vector3 vShipTranslation = shipMatrix * Vector3(0.0,0.0,0.0); ///
/// calculate the camera transformation ///
Vector3 vCameraPos = vShipTranslation - vHeading * distCameraToShip + vShipUp * cameraElevation;
Matrix4x4 cameraMatrix = LookAt(vCameraPos, vShipTranslation, vShipUp);
바라보기 구현 :
Vector3 vHeading = shipMatrix * Vector3(0.0,0.0,1.0);
Vector3 vShipUp = shipMatrix * Vector3(0.0,1.0,0.0);
는 카메라 행렬 우주선 뒤에 배치하고 바라 계산을 이용하여 우주선보고를 생성
CMatrix4x4 LookAt(Vector3 vEye, Vector3 vObject, Vector3 vUp)
{
Vector3 n = normalize(vEye - vObject);
Vector3 u = cross(vUp , n);
Vector3 v = cross(n , u);
float m[16] = { u[0], v[0], n[0], 0.0f,
u[1], v[1], n[1], 0.0f,
u[2], v[2], n[2], 0.0f,
(u * -1.0 * vEye) ,
(v * -1.0 * vEye) ,
(n * -1.0 * vEye),
1.0f };
return m;
}
모든 렌더링 사이클이 카메라 행렬을 사용하고 쉐이더 예에 전달 : 거리 사이의 설정처럼,
////// Vertex shader ///
/// it is recommended to do the multiplication on the CPU and pass the ModelViewMatrix to the shader, here is just to example ///
uniform mat4 u_BaseMatrix;
uniform mat4 u_ModelMatrix;
uniform mat4 u_ProjectionMatrix;
in vec3 a_VerAttrib;
void main()
{
gl_Position = u_ProjectionMatrix * u_BaseMatrix * u_ModelMatrix * vec4(a_VertAttrib, 1.0);
}
이제 카메라를 조작하기 시작하고 시원한 보간의 모든 종류를 제공 할 수 있습니다
float distCameraToShip = 2.0 + pow(shipSpeed,2.0) * 0.1;
당신은 또한 그것을 효과 다음과 같은 멋진주는 시간을 부드러운 필터를 사용할 수 있습니다 :
/// dt = time diff between updating cycles, or 1/FrameRate ///
float ct = 1.0/(1.0 + dt);
cameraMatrix = cameraMatrix + (previousCameraMatrix - cameraMatrix) * ct;
0123을 배의 속도에 따라 카메라와 배
카메라를 임의의 위치로 향하게하면 그 임의의 위치를 단순히 "플레이어"로 만들 수 있습니다. –
나는 말하려고했다 : cameraPos = playersPos. 그러나 이것은 여전히 플레이어가 화면을 궁금해하게하는 것처럼 보입니다. (모든 게임 업데이트). – BinaryMatrix
그러나 카메라는 여전히 움직이거나 움직이지만 플레이어는 천천히 카메라에서 멀어 지나요? 두 위치를 모두 인쇄하고 값을 확인하십시오. 몇 가지 코드가 없으면 우리는 예언 할 수 있습니다. – Mars