# OpenGL Rotation Matrix about point

This topic is 2671 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, would you help me a bit? I'm struggling with making matrices to rotate about point. My rotation part of the matrix construction looks like that:

 boneMatrices[offs + 0] = cos(rx) * cos(ry); boneMatrices[offs + 1] = -cos(rz) * sin(rx) - cos(rx)*sin(ry)*sin(rz); boneMatrices[offs + 2] = cos(rx) * cos(rz) * sin(ry) + sin(rx) * sin(rz); boneMatrices[offs + 4] = cos(ry) * sin(rx); boneMatrices[offs + 5] = cos(rx) * cos(rz) - sin(rx)*sin(ry)*sin(rz); boneMatrices[offs + 6] = cos(rz) * sin(rx) * sin(ry) - cos(rx) * sin(rz); boneMatrices[offs + 8] = -sin(ry); boneMatrices[offs + 9] = cos(ry) * sin(rz); boneMatrices[offs + 10] = cos(ry) * cos(rz); 

I pass origin for rotation instead of scaling part of the matrix, and then, in vertex shader i do:

1) subtract origin from vertex

2) rotate vertex

It works, but it sucks. too much operations, ugly code;

I want to pass to my shader matrices that already do rotation about specific point. but really i need someone's help here. how can i make above matrix to rotate about specific point? i couldn't find any descriprion adoptable in my case.

i use opengl, if it matters.

##### Share on other sites
Assuming that you use column vectors, if T(c) is the translation matrix to the center of rotation and R is the rotation matrix, then the product
M := T(c) * R * T(-c)
defines the desired new rotation matrix. It translates by -c, rotates, and translates back by c like you do now separately.

EDIT:
You can take advantage from knowing the structures of the matrices when you compute the above product. Think of M having a 3x3 sub-marix M[sub]R[/sub] on the upper left and a 1x3 sub-matrix M[sub]T[/sub] on the upper right, and doing so with R and T as well, then
M[sub]R[/sub] := R[sub]R[/sub]
M[sub]T[/sub] := R[sub]R[/sub] * T[sub]T[/sub](-c) + T[sub]T[/sub](c)
means the minimum computations to be done for yielding in M.

##### Share on other sites

1) subtract origin from vertex

2) rotate vertex

I know you might not want to hear this, but the way you listed above is the correct way. If you want your code to still look good, you can write it like this (writen in psuedocode of course):

 function RotateAboutPoint(Point,Rotation){ return MoveToAxis * Rotate * MoveFromAxis; } 

It might even perform better than your "beautiful" code since matrix multiplication is accelerated while multiplying every element in your matrix separately is not... (Not sure about this one though...)

##### Share on other sites

Well i've tried to do it like haegarr described:

 //Origins oMat.make_identity(); oMat2.make_identity(); oMat.element(0, 3) = -ox; oMat.element(1, 3) = -oy; oMat.element(2, 3) = -oz; oMat2.element(0, 3) = ox; oMat2.element(1, 3) = oy; oMat2.element(2, 3) = oz; //Rotation(tested) bMat.make_identity(); bMat.element(0, 0) = cos(rx) * cos(ry); bMat.element(1, 0) = -cos(rz) * sin(rx) - cos(rx)*sin(ry)*sin(rz); bMat.element(2, 0) = cos(rx) * cos(rz) * sin(ry) + sin(rx) * sin(rz);; bMat.element(0, 1) = cos(ry) * sin(rx); bMat.element(1, 1) = cos(rx) * cos(rz) - sin(rx)*sin(ry)*sin(rz); bMat.element(2, 1) = cos(rz) * sin(rx) * sin(ry) - cos(rx) * sin(rz); bMat.element(0, 2) = -sin(ry); bMat.element(1, 2) = cos(ry) * sin(rz); bMat.element(2, 2) = cos(ry) * cos(rz); //Bone translation bMat.element(0, 3) = translation.x; bMat.element(1, 3) = translation.y; bMat.element(2, 3) = translation.z; //M := R * T(-c) + T© ?? bMat *= oMat; bMat += oMat2;

and rotation\translation is now rigth, but for some reason i[color="#8b0000"]t's about 2x weaker than it should be and it looks like it scales vertices a bit then rotating

##### Share on other sites

 bMat *= oMat; bMat += oMat2;

Why are you adding? All matrix chaining transformations should be multiplications...

##### Share on other sites
Ok, fixed by

 bMat *= oMat; oMat2 *= bMat; bMat = oMat2;//(or just pass oMat2 to shader)

(equialent of -T * R * T)

but i'm not sure if it's ok to use such a matrix for tangent and normal? looks ok, but just want to know.

##### Share on other sites

[quote name='SingularOne' timestamp='1313485221' post='4849759']
 bMat *= oMat; bMat += oMat2;

Why are you adding? All matrix chaining transformations should be multiplications...
[/quote]
[font="arial, verdana, tahoma, sans-serif"]Please let me clarify what
[color="#1C2837"]M[sub]T[/sub] := R[sub]R[/sub] * T[sub]T[/sub](-c) + T[sub]T[/sub](c)[/font]
actually means. As mentioned my post above, [color="#1C2837"]R[sub]R[/sub] is a 3x3 matrix and [color="#1C2837"]T[sub]T[/sub] is a 1x3 matrix (a.k.a. column vector). Multiplying a 3x3 matrix on the left and a 1x3 matrix on the right gives you a 1x3 matrix. Adding a 1x3 matrix onto a 1x3 matrix gives you a 1x3 matrix.

So notice that the result [color="#1C2837"]M[sub]T[/sub] is a 1x3 matrix (and that [color="#1C2837"]M[sub]R[/sub] is a 3x3 matrix), while M itself is a usual homogeneous 4x4 matrix. The correct assembly then looks like
 bMat.make_identity(); // MR bMat.element(0, 0) = cos(rx) * cos(ry); bMat.element(1, 0) = -cos(rz) * sin(rx) - cos(rx)*sin(ry)*sin(rz); bMat.element(2, 0) = cos(rx) * cos(rz) * sin(ry) + sin(rx) * sin(rz);; bMat.element(0, 1) = cos(ry) * sin(rx); bMat.element(1, 1) = cos(rx) * cos(rz) - sin(rx)*sin(ry)*sin(rz); bMat.element(2, 1) = cos(rz) * sin(rx) * sin(ry) - cos(rx) * sin(rz); bMat.element(0, 2) = -sin(ry); bMat.element(1, 2) = cos(ry) * sin(rz); bMat.element(2, 2) = cos(ry) * cos(rz); // MT = MR * TT(-c) + TT(c) bMat.element(0, 3) = bMat.element(0, 0) * (-ox) + bMat.element(0, 1) * (-oy) + bMat.element(0, 2) * (-oz) + ox; bMat.element(1, 3) = bMat.element(1, 0) * (-ox) + bMat.element(1, 1) * (-oy) + bMat.element(1, 2) * (-oz) + oy; bMat.element(2, 3) = bMat.element(2, 0) * (-ox) + bMat.element(2, 1) * (-oy) + bMat.element(2, 2) * (-oz) + oz; 
if I have interpreted the indexing scheme correctly.

EDIT: It is for sure possible to compose the desired rotation simply by computing [color="#1C2837"]T(c) * R * T(-c). The above way just shows (as mentioned) the minimal computational effort to do; it avoids all that nasty scalar products with 0 and 1. However, this kind of optimization will probably not be noticeable.

##### Share on other sites
[color="#1C2837"]
i don't know how to thank you for your effort, your method works fine and it's much more efficient
[color="#1c2837"]

thank you too for pointing out the part i misunderstood.

[color="#1c2837"]and yeah, i've alredy noticed, that it's not really good to rotate normals.

##### Share on other sites

...
(equialent of -T * R * T)

but i'm not sure if it's ok to use such a matrix for tangent and normal? looks ok, but just want to know.

Well, please notice that -T is not the same as T(-c), because the elements on the main diagonal will be negated in -T but not in T(-c)!

However, you can apply T(-c) * R * T(c) to a normal / tangent because
a) normals and tangents are direction vectors and are hence invariant to translations, and
b) there is no scaling or shearing in this formula.
Hence for normals and tangents the above formula does the same as the lonely R does: It simply rotates the vector.

##### Share on other sites
yes it looks like problem is in my shader.

here shader that i found in nvidia example of hardware skinning:

 attribute vec4 position; attribute vec3 normal; attribute vec4 weight; attribute vec4 index; attribute float numBones; uniform mat4 boneMatrices[30]; uniform vec4 color; uniform vec4 lightPos; void main() { vec4 transformedPosition = vec4(0.0); vec3 transformedNormal = vec3(0.0); vec4 curIndex = index; vec4 curWeight = weight; for (int i = 0; i < int(numBones); i++) { mat4 m44 = boneMatrices[int(curIndex.x)]; // transform the offset by bone i transformedPosition += m44 * position * curWeight.x; mat3 m33 = mat3(m44[0].xyz, m44[1].xyz, m44[2].xyz); // transform normal by bone i transformedNormal += m33 * normal * curWeight.x; // shift over the index/weight variables, this moves the index and // weight for the current bone into the .x component of the index // and weight variables curIndex = curIndex.yzwx; curWeight = curWeight.yzwx; } gl_Position = gl_ModelViewProjectionMatrix * transformedPosition; transformedNormal = normalize(transformedNormal); gl_FrontColor = dot(transformedNormal, lightPos.xyz) * color; }

and significant part of my adoption(maximum 2 bones affecting vertex, 1st one is always most effective, so 2nd affecting bone might exist only if 1st one is):

 V = gl_Vertex; vec3 n2 = gl_Normal; vec3 t2 = Tangent; if(Bones.x >= 0.0)//Bone1 ID { mat4 tmat = BonesMat[int(Bones.x)]; //Bone matrix mat3 nmat = mat3(tmat[0].xyz, tmat[1].xyz, tmat[2].xyz); V = tmat * gl_Vertex * Bones.z; //Bones.z - Bone 1 weight n2 = nmat * gl_Normal * Bones.z; t2 = nmat * Tangent * Bones.z; if(Bones.y >= 0.0)//Bone2 ID { tmat = BonesMat[int(Bones.y)]; nmat = mat3(tmat[0].xyz, tmat[1].xyz, tmat[2].xyz); V += tmat * gl_Vertex * Bones.w; //Bones.w - Bone2 weight n2 += nmat * gl_Normal * Bones.w; t2 += nmat * Tangent * Bones.w; } } 
Further - using V,N,T as regular;

result: rotation\translation is alright.
problem: lighting glitches. then i move camera away from object - vertices that affected by 2 bones become dark with the distance(lambert decreasing).

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633665
• Total Posts
3013246
×