Cache Staheli - 4 months ago 25

C++ Question

I am writing an OpenGL program, and for that program, I am trying to write some rotation matrices by hand (I know about

`glm::rotate`

I am drawing a scene with a parking lot and car with tires drawn separately, and the tires are moving with the camera instead of staying put:

As you can see, the tires are rotated slightly in towards the camera, it looks like.

Notice the highly skewed building.

As far as I understand, this is how rotation matrices are supposed to look:

And this is what I have done in code (parts about translation and scaling left out for brevity):

`//glm::mat4 rotXMatrix = glm::rotate(rotation.x, glm::vec3(1, 0, 0));`

glm::mat4 rotXMatrix = glm::mat4(1.0f);

rotXMatrix[1][1] = cos(rotation.x);

rotXMatrix[1][2] = sin(rotation.x);

rotXMatrix[2][1] = -sin(rotation.x);

rotXMatrix[2][2] = cos(rotation.x);

//glm::mat4 rotYMatrix = glm::rotate(rotation.y, glm::vec3(0, 1, 0));

glm::mat4 rotYMatrix = glm::mat4(1.0f);

rotXMatrix[0][0] = cos(rotation.y);

rotXMatrix[0][2] = -sin(rotation.y);

rotXMatrix[2][0] = sin(rotation.y);

rotXMatrix[2][2] = cos(rotation.y);

//glm::mat4 rotZMatrix = glm::rotate(rotation.z, glm::vec3(0, 0, 1));

glm::mat4 rotZMatrix = glm::mat4(1.0f);

rotXMatrix[0][0] = cos(rotation.z);

rotXMatrix[0][1] = sin(rotation.z);

rotXMatrix[1][0] = -sin(rotation.z);

rotXMatrix[1][1] = cos(rotation.z);

glm::mat4 rotMatrix = rotZMatrix * rotYMatrix * rotXMatrix;

return posMatrix * rotMatrix * scaleMatrix;

It works correctly when I don't do it by hand (using

`glm::rot`

`[]`

Answer

You are modifying the wrong matrices

```
glm::mat4 rotYMatrix = glm::mat4(1.0f);
rotXMatrix[0][0] = cos(rotation.y);
rotXMatrix[0][2] = -sin(rotation.y);
rotXMatrix[2][0] = sin(rotation.y);
rotXMatrix[2][2] = cos(rotation.y);
```

You are constructing `rotYMatrix`

but modify `rotXMatrix`

.