COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### rotate vector around vector using GLSL

• Follow

```
Hi all,

i try to rotate a vector using another vector
as the axis by a certain amount a.

both vectors are warped, so i cant use one
of those well-known matrices ... and i am
doing this with GLSL.

i found that i can rotate a vector using a
rotation matrix, created with

mat3 rotation( vector1 , cross(vector1,vector2) , vector2 )

and rotate my new vector using

vec3 new = old * rotation;

this will rotate around cross(vector1,vector2)
but ... where is the amount of my rotation?
is this the angle between vector1 and vector2?

thank you all for any hint,
hendrik

```
 0

```Hendrik Wendler wrote:
>
>
> Hi all,
>
> i try to rotate a vector using another vector
> as the axis by a certain amount a.
>

1. Make a transformation matrix using the axis vector
x,y,z = axis vector
angle = rotation angle in radians

GLfloat Mat[16];

// set identity matrix
memset(Mat, 0, sizeof(GLfloat) * 16);
Mat[0]  = 1;
Mat[5]  = 1;
Mat[10] = 1;
Mat[15] = 1;

float c = cos(angle);
float s = sin(angle);
float u = 1 - c;

Mat[0]  = (x * x * u) + c;
Mat[4]  = (y * x * u) - (z * s);
Mat[8]  = (z * x * u) + (y * s);

Mat[1]  = (x * y * u) + (z * s);
Mat[5]  = (y * y * u) + c;
Mat[9]  = (z * y * u) - (x * s);

Mat[2]  = (x * z * u) - (y * s);
Mat[6]  = (y * z * u) + (x * s);
Mat[10] = (z * z * u) + c;

2. Apply the matrix to the rotation vector
rx,ry,rz = rotating vector
dx,dy,dz = result vector

dx = (Mat[0] * rx) + (Mat[1] * ry) + (Mat[2] * rz);
dy = (Mat[4] * rx) + (Mat[5] * ry) + (Mat[6] * rz);
dz = (Mat[8] * rx) + (Mat[9] * ry) + (Mat[10] * rz);

Once you have build the rotation matrix, you can
rotate as many vectors as you want.

Hope that helps!
Best regards, Martin
```
 0

```
hey thank you, martin!

i converted this to GLSL:

float satangle = sat * 6.282;
float csat = cos(satangle);
float ssat = sin(satangle);
float usat = 1.0 - chue;

mat3 rotmat;
rotmat[0][0] = satvec.x*satvec.x*usat + csat;			//	Mat[0]  = (x * x *
u) + c;
rotmat[1][0] = satvec.y*satvec.x*usat - (satvec.z*ssat);//	Mat[4]  = (y
* x * u) - (z * s);
rotmat[2][0] = satvec.z*satvec.x*usat + (satvec.y*ssat);//	Mat[8]  = (z
* x * u) + (y * s);

rotmat[0][1] = satvec.x*satvec.y*usat + (satvec.z*ssat);//	Mat[1]  = (x
* y * u) + (z * s);
rotmat[1][1] = satvec.y*satvec.y*usat + csat;			//	Mat[5]  = (y * y *
u) + c;
rotmat[2][1] = satvec.z*satvec.y*usat - (satvec.x*ssat);//	Mat[9]  = (z
* y * u) - (x * s);

rotmat[0][2] = satvec.x*satvec.z*usat - (satvec.y*ssat);//	Mat[2]  = (x
* z * u) - (y * s);
rotmat[1][2] = satvec.x*satvec.z*usat - (satvec.y*ssat);//	Mat[6]  = (y
* z * u) + (x * s);
rotmat[2][2] = satvec.z*satvec.z*usat + csat;			//	Mat[10] = (z * z *
u) + c;
col.xyz *=  rotmat;

All the best,
hendrik

Martin Steen schrieb:
> Hendrik Wendler wrote:
>
>>
>>
>> Hi all,
>>
>> i try to rotate a vector using another vector
>> as the axis by a certain amount a.
>>
>
> 1. Make a transformation matrix using the axis vector
>    x,y,z = axis vector
>    angle = rotation angle in radians
>
>
> GLfloat Mat[16];
>
> // set identity matrix
> memset(Mat, 0, sizeof(GLfloat) * 16);
> Mat[0]  = 1;
> Mat[5]  = 1;
> Mat[10] = 1;
> Mat[15] = 1;
>
> float c = cos(angle);
> float s = sin(angle);
> float u = 1 - c;
>
> Mat[0]  = (x * x * u) + c;
> Mat[4]  = (y * x * u) - (z * s);
> Mat[8]  = (z * x * u) + (y * s);
>
> Mat[1]  = (x * y * u) + (z * s);
> Mat[5]  = (y * y * u) + c;
> Mat[9]  = (z * y * u) - (x * s);
>
> Mat[2]  = (x * z * u) - (y * s);
> Mat[6]  = (y * z * u) + (x * s);
> Mat[10] = (z * z * u) + c;
>
> 2. Apply the matrix to the rotation vector
>    rx,ry,rz = rotating vector
>    dx,dy,dz = result vector
>
> dx = (Mat[0] * rx) + (Mat[1] * ry) + (Mat[2] * rz);
> dy = (Mat[4] * rx) + (Mat[5] * ry) + (Mat[6] * rz);
> dz = (Mat[8] * rx) + (Mat[9] * ry) + (Mat[10] * rz);
>
> Once you have build the rotation matrix, you can
> rotate as many vectors as you want.
>
> Hope that helps!
> Best regards, Martin
```
 0

2 Replies
755 Views

Similiar Articles:

7/21/2012 6:11:38 PM