Computer Graphics Asked by user13665 on August 27, 2021

*For context:*

I’m absolutely new to graphics programming and I’ve always had trouble getting the math.

I had the task to write code for a crystal ball interface using OpenGL.

There’s an object in the center of the screen and by using arrow keys, the user can rotate around it.

The code below is working and passed the grader.

*The problem:*

I don’t get why this code is working. I’m reading up on linear algebra (vectors, matrices…) and I understand the operations that can be performed on those structures. But when it comes to applying, it all doesn’t seem to make sense anymore.

Maybe someone here can explain to me in simple terms why this part works:

```
mat3 xmat(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
mat3 ymat(x * x, x * y, x * z, x * y, y * y, y * z, x * z, y * z, z * z);
mat3 zmat(0.0, z, -y, -z, 0.0, x, y, -x, 0.0);
// This part I "understand"
mat3 rotMat((cos(radians) * xmat) + ((1 - cos(radians)) * ymat) + (sin(radians) * (zmat)));
```

I feel like someone would have to explain me all of it with lots of pictures.

But I hope that the above part will make the rest click for me.

Wouldn’t this way be equivalent – it looks like it-?

```
mat3 xmat(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
mat3 zmat(0.0, z, -y, -z, 0.0, x, y, -x, 0.0);
mat3 rotMat(xmat + zmat * sin(radians) + (zmat * zmat) * (1-cos(radians)));
```

```
// Helper rotation function. Please implement this.
mat3 Transform::rotate(const float degrees, const vec3& axis)
{
// YOUR CODE HERE
vec3 normalAxis = glm::normalize(axis);
float radians = ((2 * pi) / 360) * degrees;
float x = axis.x;
float y = axis.y;
float z = axis.z;
mat3 xmat(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0);
mat3 ymat(x * x, x * y, x * z, x * y, y * y, y * z, x * z, y * z, z * z);
mat3 zmat(0.0, z, -y, -z, 0.0, x, y, -x, 0.0);
mat3 rotMat((cos(radians) * xmat) + ((1 - cos(radians)) * ymat) + (sin(radians) * (zmat)));
return rotMat;
}
void Transform::left(float degrees, vec3& eye, vec3& up) {
mat3 rotMat = rotate(degrees, up);
eye = rotMat * eye;
}
// Transforms the camera up around the "crystal ball" interface
void Transform::up(float degrees, vec3& eye, vec3& up) {
vec3 unit = glm::cross(eye, up);
vec3 unitNormal = glm::normalize(unit);
mat3 rotMat = rotate(degrees, unitNormal);
eye = (rotMat * eye);
up = glm::normalize(rotMat * up);
int dot1 = glm::dot(eye, up);
}
```

Thanks a lot and stay healthy,

Cheers.

0 Asked on December 14, 2021

1 Asked on December 9, 2021

0 Asked on November 15, 2021 by ethan-ma

1 Asked on November 13, 2021 by daniel-cooke

1 Asked on November 10, 2021 by jummit

1 Asked on August 27, 2021 by cemklkn

1 Asked on August 27, 2021

1 Asked on August 27, 2021 by endanke

0 Asked on August 27, 2021 by hafid-boukhoulda

0 Asked on August 27, 2021 by mrjab

0 Asked on August 27, 2021 by user13665

2 Asked on August 27, 2021 by arjan-singh

Get help from others!

Recent Questions

Recent Answers

- Lex on Does Google Analytics track 404 page responses as valid page views?
- Jon Church on Why fry rice before boiling?
- haakon.io on Why fry rice before boiling?
- Joshua Engel on Why fry rice before boiling?
- Peter Machado on Why fry rice before boiling?

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir