View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1SingularOne  Members

Posted 16 August 2011 - 12:23 AM

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.

### #2haegarr  Members

Posted 16 August 2011 - 12:56 AM

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 MR on the upper left and a 1x3 sub-matrix MT on the upper right, and doing so with R and T as well, then
MR := RR
MT := RR * TT(-c) + TT(c)
means the minimum computations to be done for yielding in M.

### #3SillyCow  Members

Posted 16 August 2011 - 12:57 AM

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...)

My browser game: Vitrage - A game of stained glass

My android games : Enemies of the Crown &  Killer Bees

### #4SingularOne  Members

Posted 16 August 2011 - 03:00 AM

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 it's about 2x weaker than it should be and it looks like it scales vertices a bit then rotating

### #5SillyCow  Members

Posted 16 August 2011 - 08:21 AM


bMat *= oMat;
bMat += oMat2;

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

My browser game: Vitrage - A game of stained glass

My android games : Enemies of the Crown &  Killer Bees

### #6SingularOne  Members

Posted 16 August 2011 - 08:55 AM

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.

### #7haegarr  Members

Posted 16 August 2011 - 09:01 AM

 bMat *= oMat;
bMat += oMat2;

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

MT := RR * TT(-c) + TT(c)

actually means. As mentioned my post above, RR is a 3x3 matrix and TT 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 MT is a 1x3 matrix (and that MR 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 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.

### #8SingularOne  Members

Posted 16 August 2011 - 09:11 AM

haegarr,
i don't know how to thank you for your effort, your method works fine and it's much more efficient

SillyCow,
thank you too for pointing out the part i misunderstood.

and yeah, i've alredy noticed, that it's not really good to rotate normals.

### #9haegarr  Members

Posted 16 August 2011 - 01:06 PM

...
(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.

### #10SingularOne  Members

Posted 16 August 2011 - 10:15 PM

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).

### #11haegarr  Members

Posted 17 August 2011 - 04:29 AM

IMHO the shown adopted code snippet is missing a normalization of both n2 and t2 (assuming that "using V,N,T as regular" doesn't include it). The code snippet probably only works well if Bones.z == 1 and Bones.y < 0. In all other cases the lengths of n2 and t2 may be anything but are later expected to be 1.

### #12SingularOne  Members

Posted 17 August 2011 - 05:05 AM

yes, i've noticed before, it works ok if bone1 weight = 1.0 and bone2 is ineffective, but i do normalize resulting normal and tangent


vec3 T = normalize(gl_NormalMatrix * normalize(t2));
vec3 N = normalize(gl_NormalMatrix * normalize(n2));
vec3 B = cross(N, T);

and even if i remove normal rotation, problem doesn't go away.
also i modified code so second bone weight = 1.0 - FirstBoneWeight (because outside shader my program actually calculates weights for all affecting bones, but in examples i use to test it's always <= 2 bones affecting vertex);

and to avoid wasting your time with snippets:
full code of stuff that actually affects that problem,
Spoiler

### #13SingularOne  Members

Posted 19 August 2011 - 11:08 AM

The problem was i'm using 3-component vectors for all my lighting calculation, so vertex w-component was modified by rotation matrix correctly, but wasn't affecting lighting. so i should do it manually.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.