void mat4x4_rotate_quaternion(float pMatrix[16], float w, float x, float y, float z) {
Mat4x4 pRotMatrix = {
1 - 2 * y * y - 2 * z * z, 2 * x * y - 2 * w * z, 2 * x * z + 2 * w * y, 0.0f,
2 * x * y + 2 * w * z, 1 - 2 * x * x - 2 * z * z, 2 * y * z - 2 * w * x, 0.0f,
2 * x * z - 2 * w * y, 2 * y * z + 2 * w * x, 1 - 2 * x * x - 2 * y * y, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
code
mulMat4x4Mat4x4(pMatrix, pRotMatrix, pMatrix);
}
Can you correct this code?
Assistant
The provided code seems to be implementing functions for converting Euler angles to quaternions and for rotating a 4x4 matrix using a quaternion. However, since there is no context or specific explanation of what is wrong with the code, it is challenging to identify any issues or suggest corrections.
If you could provide more information about the errors you are experiencing or the specific problem you are trying to solve, I would be happy to assist you further.
User
It's causing incorrect deformations when rotating on the Z axis. I'm using it in OpenGL.
Assistant
One issue I can identify is that the conversion from Euler angles to quaternions is using the order of rotations: Roll (X), Pitch (Y), Yaw (Z). However, in OpenGL and many other graphics libraries, the convention is typically Yaw, Pitch, Roll. This could be causing the incorrect deformations when rotating on the Z axis.
To correct this, you can modify the order of the Euler angles used in the calculation of the quaternion. Here's a modified version of the code considering the Yaw, Pitch, Roll order:
Additionally, make sure that the multiplication of matrices in the mat4x4_rotate_quaternion function is implemented correctly (mulMat4x4Mat4x4). If there are any issues with that function, it may also contribute to incorrect deformations.
If you're still facing issues, providing more details about how you're using these functions and any specific problems you're encountering would be helpful for further troubleshooting.
In the mat4x4_rotate_quaternion function, the parameter float pMatrix[16] is not being used. This function assumes that the pMatrix is already a 4x4 matrix, so there is no need to pass it as a parameter. You can remove the parameter and modify the function to operate directly on the pMatrix array.
In the main code, the functions mat4x4_identity and mat4x4_rotate_quaternion are not defined. You need to include their definitions or make sure they are declared properly.
The order of matrix multiplication in the main code seems to be incorrect. Typically, the order should be: model * view * projection. You are currently multiplying model with view and then the result with projection, which may lead to incorrect transformations.
Here's an updated version of the code to address these issues:
cpp
// Define the required matrices and vectors
Mat4x4 p_model_mat, p_view_mat, p_projection_mat, p_mvp_mat;
Vec3 euler = { 0.0f, 0.0f, deg_to_rad(360 * sin(glfwGetTime())) };
Vec4 quaternion;
// Compute the quaternion from Euler angles
euler_to_quaternion(euler, quaternion);
// Set the model matrix to identity
mat4x4_identity(p_model_mat);
// Rotate the model matrix using the quaternion
mat4x4_rotate_quaternion(p_model_mat, quaternion[0], quaternion[1], quaternion[2], quaternion[3]);
// Set the view matrix to identity (no translation or rotation)
mat4x4_identity(p_view_mat);
// Set up the projection matrix
mat4x4_identity(p_projection_mat);
mat4x4_orthographic(p_projection_mat, -1.0f, 1.0f, -1.0f, 1.0f, -0.1f, -1.0f);
// Compute the final MVP matrix by multiplying the model-view-projection matrices
mat4x4_identity(p_mvp_mat);
mulMat4x4Mat4x4(p_projection_mat, p_view_mat, p_mvp_mat);
mulMat4x4Mat4x4(p_mvp_mat, p_model_mat, p_mvp_mat);
Make sure that the missing functions (mat4x4_identity, mat4x4_rotate_quaternion, etc.) are defined correctly and accessible in your code. Additionally, ensure that the matrix multiplication and other utility functions (mulMat4x4Mat4x4, deg_to_rad, etc.) are implemented correctly as well.
If you're still experiencing issues or have any specific errors or expected results, please provide more details so that I can assist you further.
User
That's all the related code. I'm not sure what's wrong.
Assistant
Based on the provided code, it seems that you are setting up an MVP (Model-View-Projection) matrix for rendering using OpenGL. However, without additional information on what exactly is not working or what incorrect deformations you are experiencing, it's difficult to pinpoint the exact issue.
Here are a few things to check and consider:
Verify that the transformations (rotation, translation, scaling) applied to the model matrix (p_model_mat) are correct. Ensure that the rotation quaternion is computed properly and the order of operations matches your desired transformation sequence.
Confirm that the orthographic projection parameters (left, right, bottom, top, zNear, zFar) are set correctly for your scene. Pay attention to the signs and ensure that objects are within the specified frustum.
Check if other parts of your rendering pipeline (vertex shaders, fragment shaders, vertex attributes, etc.) are implemented correctly and compatible with the transformations applied in the code.
Consider any specific requirements or conventions of your rendering framework or graphics API (such as OpenGL) that might affect the behavior of the matrix transformations. Pay attention to coordinate systems, matrix multiplication order, and any transformations applied by the framework itself.
If you can provide more specific information or examples of the expected and observed results, I may be able to assist you further in identifying the issue.
User
Can you check for bugs in the rotation matrix? Changing Z causes it to shear.
Assistant
Yes, it seems that there is an issue with the rotation matrix pRotMatrix in the mat4x4_rotate_quaternion function. When rotating around the Z-axis, the matrix should be constructed as follows:
Make sure to use the correct sine and cosine values by passing the z angle to the cos and sin functions respectively. This will ensure a proper rotation around the Z-axis without any shearing effects.
void mat4x4_rotate_quaternion(float pMatrix[16], float w, float x, float y, float z) {
Mat4x4 pRotMatrix = {
1 - 2 * y * y - 2 * z * z, 2 * x * y - 2 * w * z, 2 * x * z + 2 * w * y, 0.0f,
2 * x * y + 2 * w * z, 1 - 2 * x * x - 2 * z * z, 2 * y * z - 2 * w * x, 0.0f,
2 * x * z - 2 * w * y, 2 * y * z + 2 * w * x, 1 - 2 * x * x - 2 * y * y, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};