You are using an out of date browser. It may not display this or other websites correctly.

You should upgrade or use an alternative browser.

You should upgrade or use an alternative browser.

- Thread starter maha_x
- Start date

Code:

```
Matrix4f rotation = new Matrix4f();
Matrix4f rotateY = new Matrix4f();
Matrix4f rotateX = new Matrix4f();
Matrix4f rotateZ = new Matrix4f();
Matrix4f trans = new Matrix4f();
Matrix4f back = new Matrix4f();
public Matrix4f initMatrix(float xTheta, float yTheta, float zTheta, Vector3f loca)
{
rotateX.m00 = 1f; rotateX.m01 = 0f; rotateX.m02 = 0f; rotateX.m03 = 0f;
rotateX.m10 = 0f; rotateX.m11 = (float)Math.cos(xTheta); rotateX.m12 = (float)Math.sin(xTheta); rotateX.m13 = 0f;
rotateX.m20 = 0f; rotateX.m21 =-(float)Math.sin(xTheta); rotateX.m22 = (float)Math.cos(xTheta); rotateX.m23 = 0f;
rotateX.m30 = 0f; rotateX.m31 = 0f; rotateX.m32 = 0f; rotateX.m33 = 1f;
rotateY.m00 = (float)Math.cos(yTheta); rotateY.m01 = 0f; rotateY.m02 =-(float)Math.sin(yTheta); rotateY.m03 = 0f;
rotateY.m10 = 0f; rotateY.m11 = 1f; rotateY.m12 = 0f; rotateY.m13 = 0f;
rotateY.m20 = (float)Math.sin(yTheta); rotateY.m21 = 0f; rotateY.m22 = (float)Math.cos(yTheta); rotateY.m23 = 0f;
rotateY.m30 = 0f; rotateY.m31 = 0f; rotateY.m32 = 0f; rotateY.m33 = 1f;
rotateZ.m00 = (float)Math.cos(zTheta); rotateZ.m01 = (float)Math.sin(zTheta); rotateZ.m02 = 0f; rotateZ.m03 = 0f;
rotateZ.m10 =-(float)Math.sin(zTheta); rotateZ.m11 = (float)Math.cos(zTheta); rotateZ.m12 = 0f; rotateZ.m13 = 0f;
rotateZ.m20 = 0f; rotateZ.m21 = 0f; rotateZ.m22 = 1f; rotateZ.m23 = 0f;
rotateZ.m30 = 0f; rotateZ.m31 = 0f; rotateZ.m32 = 0f; rotateZ.m33 = 1f;
trans.m00 = 1f; trans.m01 = 0f; trans.m02 = 0f; trans.m03 = 0f;
trans.m10 = 0f; trans.m11 = 1f; trans.m12 = 0f; trans.m13 = 0f;
trans.m20 = 0f; trans.m21 = 0f; trans.m22 = 1f; trans.m23 = 0f;
trans.m30 = -loca.x; trans.m31 = -loca.y; trans.m32 = -loca.z; trans.m33 = 1f;
back.m00 = 1f; back.m01 = 0f; back.m02 = 0f; back.m03 = 0f;
back.m10 = 0f; back.m11 = 1f; back.m12 = 0f; back.m13 = 0f;
back.m20 = 0f; back.m21 = 0f; back.m22 = 1f; back.m23 = 0f;
back.m30 = loca.x; back.m31 = loca.y; back.m32 = loca.z; back.m33 = 1f;
Matrix4f temp = new Matrix4f();
temp.mul(rotateZ, rotateY);
rotation.mul(temp,rotateX);
temp = new Matrix4f();
temp.mul(trans, rotation);
rotation.mul(temp,back);
return rotation;
}
```

PS: This was written using Java3D, but I guess you can make the few steps to port it to c++. After all Java is a derivative of C/C++

maha_x said:

The glRotate() function does not only create a rotation matrix. It also multiplies it with the current matrix. So just calling glRotate() two times will include both rotations. Like this:

glLoadIdentity(); // I

glRotatef(rot1, ... ); // RotM1 * I

glRotatef(rot2, ... ); // RotM2 * RotM1 * I

...

Note that it performs left-multiplications, so the last rotate you pass in is the first rotation applied to the vertex.