void x3ds_definition::ensure_camera() { if (m_file->cameras == NULL) { // Fabricate camera Lib3dsCamera* camera = lib3ds_camera_new("Perscpective"); float size; Lib3dsVector bmin, bmax; get_bounding_center(bmin, bmax, camera->target, &size); memcpy(camera->position, camera->target, sizeof(camera->position)); camera->position[0] = bmax[0] + 0.75f * size; camera->position[1] = bmin[1] - 0.75f * size; camera->position[2] = bmax[2] + 0.75f * size; // hack Lib3dsMatrix m; lib3ds_matrix_identity(m); lib3ds_matrix_rotate_z(m, 1.6f); // PI/2 lib3ds_matrix_rotate_y(m, 0.3f); lib3ds_vector_transform(camera->position, m, camera->position); camera->near_range = (camera->position[0] - bmax[0] ) * 0.5f; camera->far_range = (camera->position[0] - bmin[0] ) * 3.0f; lib3ds_file_insert_camera(m_file, camera); } }
/*! * \ingroup matrix */ void lib3ds_matrix_camera(Lib3dsMatrix matrix, Lib3dsVector pos, Lib3dsVector tgt, Lib3dsFloat roll) { Lib3dsMatrix M,R; Lib3dsVector x, y, z; lib3ds_vector_sub(y, tgt, pos); lib3ds_vector_normalize(y); z[0] = 0; z[1] = 0; z[2] = 1.0; lib3ds_vector_cross(x, y, z); lib3ds_vector_cross(z, x, y); lib3ds_vector_normalize(x); lib3ds_vector_normalize(y); lib3ds_matrix_identity(M); M[0][0] = x[0]; M[1][0] = x[1]; M[2][0] = x[2]; M[0][1] = y[0]; M[1][1] = y[1]; M[2][1] = y[2]; M[0][2] = z[0]; M[1][2] = z[1]; M[2][2] = z[2]; lib3ds_matrix_identity(R); lib3ds_matrix_rotate_y(R, roll); lib3ds_matrix_mul(matrix, R,M); lib3ds_matrix_translate_xyz(matrix, -pos[0],-pos[1],-pos[2]); }
/*! * Compute a camera matrix based on position, target and roll. * * Generates a translate/rotate matrix that maps world coordinates * to camera coordinates. Resulting matrix does not include perspective * transform. * * \param matrix Destination matrix. * \param pos Camera position * \param tgt Camera target * \param roll Roll angle * * \ingroup matrix */ void lib3ds_matrix_camera(Lib3dsMatrix matrix, Lib3dsVector pos, Lib3dsVector tgt, Lib3dsFloat roll) { Lib3dsMatrix M; Lib3dsVector x, y, z; lib3ds_vector_sub(y, tgt, pos); lib3ds_vector_normalize(y); if (y[0] != 0. || y[1] != 0) { z[0] = 0; z[1] = 0; z[2] = 1.0; } else { /* Special case: looking straight up or down z axis */ z[0] = -1.0; z[1] = 0; z[2] = 0; } lib3ds_vector_cross(x, y, z); lib3ds_vector_cross(z, x, y); lib3ds_vector_normalize(x); lib3ds_vector_normalize(z); lib3ds_matrix_identity(M); M[0][0] = x[0]; M[1][0] = x[1]; M[2][0] = x[2]; M[0][1] = y[0]; M[1][1] = y[1]; M[2][1] = y[2]; M[0][2] = z[0]; M[1][2] = z[1]; M[2][2] = z[2]; lib3ds_matrix_identity(matrix); lib3ds_matrix_rotate_y(matrix, roll); lib3ds_matrix_mult(matrix, M); lib3ds_matrix_translate_xyz(matrix, -pos[0],-pos[1],-pos[2]); }