# OpenGL Rotating around a vector (or arbitrary axis)

## Recommended Posts

I've been trying to rotate a vector about a certain axis but the functions I use implicitly rotate it about the vectors 1,0,0 (x) 0,1,0 (y) and 0,0,1 (z), the default ones. I know the openGL function glRotate() performs the rotation about a given vector. Anyone know how one would go about implementing one like that? These are the functions I'm using now

void C3DVector::RotateZ(double dDegrees_)
{

double dX = this->m_dX, dY = this->m_dY;

}// end void C3DVector::RotateZ(double dDegrees_)

void C3DVector::RotateY(double dDegrees_)
{

double dX = this->m_dX, dZ = this->m_dZ;

}// end void C3DVector::RotateY(double dDegrees_)

void C3DVector::RotateX(double dDegrees_)
{

double dY = this->m_dY, dZ = this->m_dZ;

}// end void C3DVector::RotateX(double dDegrees_)



##### Share on other sites
It's just one big conjugate transformation:

Rotate & translate everything so that the axis of rotation is mapped to one of the coordinate axes, rotate about this axis, then inverse-transform back to the original coordinate space.
You could do this in the three steps as described, or you could get out some paper, write out the overall transformation, simplify, and use this in your implementation, for a more efficient result.

At least, that's the way I'd do it, being a mathematician. I suppose the computer scientist's solution would be to Google for an existing solution and copy it [rolleyes].

Regards

##### Share on other sites
I found this through a Google search. I'm not at my work computer so I can't compare it against the code I've written before, but it looks correct.

##### Share on other sites
Check the Matrix and Quaternion FAQ on this very site.

Or just generate the appropriate quaternion from axis/angle formulation, which is very easy (calculate angle / 2, take sin/cosine, multiply axis by that much and use the consine for the angle).

##### Share on other sites
Quote:
 Original post by TheAdmiralI suppose the computer scientist's solution would be to Google for an existing solution and copy it [rolleyes].
Ouch - that's pretty cynical :-)

@The OP: The formula to rotate a vector about another (unit-length) vector is fairly straightforward. It looks like Zipster's link covers it, although the article does have a glaring error in that it confuses coordinate system handedness with vector orientation :-/

The vector rotation formula can also be expressed as a matrix-vector multiplication. This is what the OpenGL function glRotate() does - it constructs a matrix representing this operation and concatenates it with the matrix at the top of the currently active OpenGL matrix stack.

##### Share on other sites
Here is some code from my math library that returns the rotation matrix around an arbitrary axis.

Matrix4 Matrix4::MakeRotateArbitrary( Vector3& axis, float radian ) {	float fCos = Math::Cos( radian );	float fSin = Math::Sin( radian );	float fSum = 1.0f - fCos;	axis.Normalize();        Matrix4 m;	m[0][0] = ( axis.x * axis.x ) * fSum + fCos;	m[0][1] = ( axis.x * axis.y ) * fSum - ( axis.z * fSin );	m[0][2] = ( axis.x * axis.z ) * fSum + ( axis.y * fSin );	m[0][3] = 0.0f;			m[1][0] = ( axis.y * axis.x ) * fSum + ( axis.z * fSin );	m[1][1] = ( axis.y * axis.y ) * fSum + fCos;	m[1][2] = ( axis.y * axis.z ) * fSum + ( axis.x * fSin );	m[1][3] = 0.0f;	m[2][0] = ( axis.z * axis.x ) * fSum + ( axis.y * fSin );	m[2][1] = ( axis.z * axis.y ) * fSum + ( axis.x * fSin );	m[2][2] = ( axis.z * axis.z ) * fSum + fCos;	m[2][3] = 0.0f;	m[3][0] = 0.0f;	m[3][1] = 0.0f;	m[3][2] = 0.0f;	m[3][3] = 1.0f;        return m;}

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627680
• Total Posts
2978610
• ### Similar Content

• Both functions are available since 3.0, and I'm currently using glMapBuffer(), which works fine.
But, I was wondering if anyone has experienced advantage in using glMapBufferRange(), which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• 13
• 12
• 10
• 12
• 22