At least eye position is correctly retrieved from the matrix.

master
Martin Felis 2021-10-19 23:00:09 +02:00
parent eb635162c8
commit 0b12991f10
2 changed files with 22 additions and 7 deletions

View File

@ -15,10 +15,6 @@ inline void CameraState_Init(CameraState* camera_state) {
}
void CameraState_CalcFromMatrix(CameraState* camera_state, simd4x4f* mtx) {
camera_state->pos[0] = mtx->w[0];
camera_state->pos[1] = mtx->w[1];
camera_state->pos[2] = mtx->w[2];
float right[3] = {mtx->x[0], mtx->y[0], mtx->z[0]};
float up[3] = {mtx->x[1], mtx->y[1], mtx->z[1]};
float forward[3] = {-mtx->x[2], -mtx->y[2], -mtx->z[2]};
@ -27,6 +23,25 @@ void CameraState_CalcFromMatrix(CameraState* camera_state, simd4x4f* mtx) {
camera_state->pitch = asin(-forward[1]);
gLog("eye: %f %f %f", camera_state->pos[0], camera_state->pos[1], camera_state->pos[2]);
simd4x4f rot_mat = *mtx;
rot_mat.w = simd4f_create(0.f, 0.f, 0.f, 1.f);
simd4x4f_transpose_inplace(&rot_mat);
simd4f eye;
simd4x4f_matrix_point3_mul(&rot_mat, &mtx->w, &eye);
gLog ("eye: %f, %f, %f", simd4f_get_y(eye), simd4f_get_z(eye));
camera_state->pos[0] = -simd4f_get_x(eye);
camera_state->pos[1] = -simd4f_get_y(eye);
camera_state->pos[2] = -simd4f_get_z(eye);
// gLog ("ViewMat");
// gLog ("%f, %f, %f, %f", mtx->x[0], mtx->x[1], mtx->x[2], mtx->x[3]);
// gLog ("%f, %f, %f, %f", mtx->y[0], mtx->y[1], mtx->y[2], mtx->y[3]);
// gLog ("%f, %f, %f, %f", mtx->z[0], mtx->z[1], mtx->z[2], mtx->z[3]);
// gLog ("%f, %f, %f, %f", mtx->w[0], mtx->w[1], mtx->w[2], mtx->w[3]);
}
void CameraState_CalcToMatrix(CameraState* camera_state, simd4x4f* mtx) {
@ -38,10 +53,10 @@ void CameraState_CalcToMatrix(CameraState* camera_state, simd4x4f* mtx) {
const float d = 10.0f;
simd4f eye = simd4f_create (camera_state->pos[0], camera_state->pos[1], camera_state->pos[2], 1.f);
simd4f forward = simd4f_create (-cp * ch * d, -sp * d, cp * sh * d, 1.f);
simd4f forward = simd4f_create (-cp * ch, -sp, cp * sh, 0.f);
simd4f right = simd4f_cross3 (forward, simd4f_create (0.f, 1.f, 0.f, 1.f));
simd4f up = simd4f_cross3(right, forward);
simd4f center = simd4f_add(eye, forward);
simd4f center = simd4f_add(simd4f_mul(forward, simd4f_splat(d)), eye);
simd4x4f_lookat(mtx, eye, center, up);
}

View File

@ -589,7 +589,7 @@ int main(void) {
changed = changed || ImGui::SliderFloat("Pitch", &gCameraState->pitch, -M_PI * 0.98, M_PI * 0.98);
if (changed) {
//CameraState_CalcToMatrix(gCameraState, &gCameraState->mtxView);
CameraState_CalcToMatrix(gCameraState, &gCameraState->mtxView);
}
ImGui::End();