Public Group

## Recommended Posts

Posted (edited)

It is quickly dawning upon me that understanding how rotations work in OpenGL is a rather different beast to anything that I have encountered before. The mathematics involved is complex and has easily overwhelmed me as a beginner.

Where does one start from when trying to gain an understanding of the mathematics that goes into rotation calculations? There are lots of resources online for beginners to learn about rotation from, but they all assume a base level of understanding which I certainly do not posses at this point. Where should the starting point be for someone like me who only has an average understanding of secondary/high school mathematics?

Edited by calioranged

##### Share on other sites
Posted (edited)

Easiest entry point i know is this (with examples to hack in and try out immediately):

Aside from that:

The Red Book (OpenGL Programming Guide 9th edition, must have)) and the Blue Book (OpenGL Superbible, 7th edition, should have i'd say) each have beginner level chapters in them.

And this:

If you're more the interactive type:

If you're only after using rotations, that's simple: just use for example glm's rotate() function, give it a matrix to rotate, an angle and the axis about which to rotate and that's it. Example: glm::mat4 modelMatrix{ glm::mat4{ 1.0f}, 90.0f, glm::vec3{ 0.0f, 0.0f, 1.0f } } to rotate something 90° around the z-axis. You get back the rotation matrix that can be combined with other rotations, transformations or scaling observing the correct sequence.

Edited by Green_Baron

##### Share on other sites
Posted (edited)
19 minutes ago, Green_Baron said:

If you're only after using rotations, that's simple﻿

The idea of rotating by x amount of radians/degrees around a given axis is easy enough to grasp, but my understanding completely breaks down when the scalar values for rotation are not 1.0F or 0.0F. See here regarding a topic I posted on Stack Overflow: https://stackoverflow.com/questions/56313743/cross-product-confusion

Thanks for the resources you have provided.

Edited by calioranged

##### Share on other sites
Posted (edited)

The cross product gives you a vector perpendicular to two given ones. In the camera case, if you want to rotate around the vertical axis but you only have the front and the right vector, you can obtain the vertical vector (the one to rotate around, aka yaw) via the cross product of the front- and the right-vector. Or rotate around x (aka pitch) via the cross of front and up vector, or rotate around z via the cross of up- and right vector.

Just keep in mind that these vectors must be world space vectors. It is best to store camera vectors separately, as they are needed later for other purposes.

Just one example:

void Camera::updateCameraVectors() {
if( ORBITING == m_mode ) {
......
} else {
// fps mode
glm::vec3 posf{ glm::vec3{ m_position } };
// flip yaw and pitch because intuition (at least mine ;-))
float yaw{ glm::radians( m_pitch ) };
float pitch{ glm::radians( m_yaw ) };
// Calculate the new front and right vectors. Camera position is set by movement.
glm::vec3 front{ -std::cos( yaw ) * std::sin( pitch ),
std::sin( yaw ),
std::cos( yaw ) * std::cos( pitch ) };
m_front = glm::normalize( front );
m_viewMatrix = glm::lookAt( posf, posf + front, m_up );
m_frustum.setCameraVectors( posf, posf + front, m_up );
}
// Also re-calculate the right vector and view matrices
m_right = glm::normalize( glm::cross( m_front, m_up ) );
......
}

In this case the up-vector is always world up, camera rotation around the longitudinal axis is not (yet) implemented.

Edited by Green_Baron
Correction in remarks of the code

##### Share on other sites
Posted (edited)
23 minutes ago, Green_Baron said:

The cross product gives you a vector perpendicular to two given ones. In the camera case, if you want to rotate around the vertical axis but you only have the front and the right v﻿ector, you can obtain the vertical vector (the one to rotate around, aka yaw) via the cross product of the front- and the right-vector.

Not really sure what you mean by 'front' and 'right' vector (the front of what vector..? the right of what vector..?). From my understanding, the view matrix (matrix produced by glm::lookAt()) is composed of the 3D vectors; position, view direction and orientation. In the example there it is the cross product of the view direction and orientation that is used as a scalar for camera rotation. I just cannot grasp the concept of this. I probably need to gain a more fundamental understanding of the mathematics behind this because when I listen to people explain the concepts it just goes straight over my head to be honest.

I have found that whenever I start reading an article or watching a video which explains a particular concept, they assume a base level of understanding about certain related concepts which I don't really have. For example in the introduction of http://immersivemath.com/ila/index.html (section 1.1) there are already notational conventions that I am having to look elsewhere for explanations of. In section 1.2 trigonometry is discussed where the authors assume that the reader already understands the basics. Because of this I then have to research the related concepts where prior knowledge was assumed by the tutorial, and then I end up down a rabbit hole further and further away from the actual subject I want to learn about.

This may all be necessary but finding a starting point for learning about these concepts from my experience is rather difficult.

Edited by calioranged

##### Share on other sites

front = view direction

The cross product is not a scalar. It is a vector. Looks like that's what is confusing you ...

##### Share on other sites
9 minutes ago, calioranged said:

I end up down a rabbit hole further and further away from the actual subject I want to learn about.﻿

If the rabbit is your subject then you're headed in the right direction. The devil is in the details.

##### Share on other sites

Maybe you should start by reading a book or some tutorials about basic linear algebra. I know, that this is boring if you want to do the fun stuff that is graphics programming, but not understanding the basics will just lead to more and more confusion and you might end up totally frustrated.

13 minutes ago, calioranged said:

Because of this I then have to research the related concepts where prior knowledge was assumed by the tutorial, and then I end up down a rabbit hole further and further away from the actual subject I want to learn about.

Well, that's a common problem and I can understand your frustration, but learning the basics is necessary.

##### Share on other sites
Posted (edited)
18 minutes ago, Green_Baron said:

The cross product is not a scalar. It is a vector. Looks like that's what is confusing you ...

Yes sorry I have my terminology wrong there.

What I mean is that if you have the following rotate matrix:

glm::mat4 rotator { glm::rotate(glm::mat4(1.0F),
glm::vec3(rotation.x, rotation.y, rotation.z)) };

MVP.view.direction = rotator * MVP.view.direction;

Where:

degrees = 90
rotation.x = 1.0F
rotation.y = 0.5F
rotation.z = 0.0F

Then the view direction will be rotated by:

90° on the x-axis (90 * 1.0)
45° on the y-axis (90 * 0.5)
0°   on the z-axis (90 * 0.0)

[Please correct me if the above is wrong] So rotation is not happening by the specified amount on one axis, it is happening by the specified amount on each axis.

6 minutes ago, DerTroll said:

Well, that's a common problem and I can understand your frustration, but learning the basics is necessary.

Of course I agree. I am just trying to select a starting point for learning the basics of the relevant mathematics.

Edited by calioranged

##### Share on other sites
Posted (edited)
29 minutes ago, calioranged said:

degrees = 90
rotation.x = 1.0F
rotation.y = 0.5F
rotation.z = 0.0F

Then the view direction will be rotated by:

90° on the x-axis (90 * 1.0)
45° on the y-axis (90 * 0.5)
0°   on the z-axis (90 * 0.0)

[Please correct me if the above is wrong] So rotation is not happening by the specified amount on one axis, it is happening by the specified amount on each axis.

Of course I agree. I am just trying to select a starting point for learning the basics of the relevant mathematics.

I just grabbed behind me on the shelf. Must correct me that the red book has no dedicated math chapter, but the blue book has ! The math in the tutorial linked above also is a basic introduction. I had to read it several times (archaeologist with a natural aversion against math). I even took the blue book with me to bed for the math chapter. Don't tell anyone ... but if i can dig it, you can as well.

Your examples rotates a given matrix by 90° around the axis defined by the vector (1/0.5/0), that is parallel to x, tilt 45° to the left, through the center of the coord. system. I think it rotates towards the viewer in the left handed version. Valid until correction 🙂

Edited by Green_Baron

• 9
• 12
• 15
• 12
• 24