Кто -нибудь знает, почему моя матрица вращения искажает представление о оси z?C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Кто -нибудь знает, почему моя матрица вращения искажает представление о оси z?

Сообщение Anonymous »


< /p>
Все входы - это нормализованные векторы, а Проекция лучей работает просто отлично, если смотреть вдоль оси x или y, но, глядя вдоль оси z, вид полностью искажается, я нахожусь в конце этого, полная кодовая база находится в
https: // github .com/fuushi/trumbone-renderer

Код: Выделить всё

// Rodrigues' rotation formula: Rotates `v` around axis `u` by `theta` radians
std::vector rotate_vector_u(const std::vector& v, double theta) {
// Compute rotation axis (perpendicular to the vector and z-axis)
std::vector z_axis = {0.0, 0.0, 1.0};
std::vector rotation_axis = vector_cross_product(v, z_axis);

// Handle edge case where vector is perfectly aligned with z (avoid zero vector)
if (rotation_axis[0] == 0 && rotation_axis[1] == 0 && rotation_axis[2] == 0) {
rotation_axis = {1.0, 0.0, 0.0};  // Z perpendicular axis
}

// Normalize the rotation axis
rotation_axis = normalize_vector(rotation_axis);

double cos_theta = std::cos(theta);
double sin_theta = std::sin(theta);
double dot = v[0] * rotation_axis[0] + v[1] * rotation_axis[1] + v[2] * rotation_axis[2];

return {
v[0] * cos_theta + (rotation_axis[1] * v[2] - rotation_axis[2] * v[1]) * sin_theta + rotation_axis[0] * dot * (1 - cos_theta),
v[1] * cos_theta + (rotation_axis[2] * v[0] - rotation_axis[0] * v[2]) * sin_theta + rotation_axis[1] * dot * (1 - cos_theta),
v[2] * cos_theta + (rotation_axis[0] * v[1] - rotation_axis[1] * v[0]) * sin_theta + rotation_axis[2] * dot * (1 - cos_theta)
};
}

std::vector rotate_vector_z(std::vector vec, double rotation_rad) {

// Define z-axis rotation matrix
std::vector r_z = {
{cos(rotation_rad), -sin(rotation_rad), 0.0},
{sin(rotation_rad), cos(rotation_rad), 0.0},
{0.0, 0.0, 1.0}
};

// Apply z-axis rotation first
std::vector z_corrected_matrix = matrix_vector_multiplication(vec, r_z);

return z_corrected_matrix;
};

std::vector rotate_vector_wrapper(std::vector vec, double z_rad, double u_rad) {
std::vector z_corrected_matrix = rotate_vector_z(vec, z_rad);
std::vector rotated_vector = rotate_vector_u(z_corrected_matrix, u_rad);
return rotated_vector;
};

std::vector rotation_matrix_degrees(std::vector input_vector, double fov, std::vector uv) {
// Compute horizontal (z-axis) rotation angle

//remap coordinates
std::vector uv2 = {uv[1],1-uv[0]};

//calculate z rotation from UV
double theta_z_deg = (uv2[0] * fov) - (fov / 2);
double theta_z_rad = theta_z_deg * (3.141592 / 180.0);

//calculate u rotation from UV
double theta_up_deg = (uv2[1] * fov) - (fov / 2);
double theta_up_rad = theta_up_deg * (3.141592 / 180.0);

// Apply rotation using Rodrigues' formula
std::vector rotated_vector = rotate_vector_wrapper(
input_vector,
theta_z_rad,
theta_up_rad
);

// Normalize the final vector
return normalize_vector(rotated_vector);
};
При просмотре z+ все 3 цветных кубика должны быть видны, но проекция полностью искажает представление, я считаю, что мои матрицы вращения могут быть неправильными, но я не уверен

Подробнее здесь: https://stackoverflow.com/questions/794 ... the-z-axis
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»