Author Topic: Object3D, matrices and transformations  (Read 5408 times)

Offline Enk

  • byte
  • *
  • Posts: 22
    • View Profile
Object3D, matrices and transformations
« on: April 02, 2009, 03:20:52 pm »
Hi, I have some questions that maybe are more related to game engine design than jPCt, but anyway :)

I am currently working on a little 3D-engine in Java using LWJGL just for learning purposes, and the last couple of days I have been reading a lot about 3D-mathematics. I have some questions on how jPCT has implemented translation/rotation-matrices.

I got a working Matrix-class to handle basic 4x4-matrix operations and a simple Vector-class. I've built a little polygon-system on top of that, so currently I can load OBJ-files.

My question is how to implement the translation- and rotation-matrices in my "Object3D"-class. I got functions in my Matrix class for transforming the Matrix (translate, rotate XYZ).

Should I store the location in a Vector and have a translationMatrix in the Object-class, and have a Object3D.translate-function something like this:

Code: [Select]
public void translate(Vector3D trans) {
    this.translationMatrix.translate(trans);
    this.location.mul(translationMatrix);
}

I am very green on this subject and I haven't found any good books/resouces for this (found A LOT of books on Matrices and 3D-math but none about how you actually implements them in your program). So I just wondered how you experienced programmers would do it  ;D


Offline Enk

  • byte
  • *
  • Posts: 22
    • View Profile
Re: Object3D, matrices and transformations
« Reply #1 on: April 02, 2009, 03:53:15 pm »
Now I kind of realized that the location of an object is in the translationMatrix, and translating the object is then the trivial task of translating the translationMatrix. So the position (relative to its origin) would be the m3, m7 and m11 in a row-ordered matrix.

Should every polygon have its own translationMatrix? E.g. if I want to scale the polygon vertices and not just use glScale().

And how should use the rotationMatrix of the object (e.g. with OpenGL glRotatef). 

I know that these questions may seem odd, but this subject it (currently) a few inches over my head, and I haven't found any example code :)

« Last Edit: April 02, 2009, 04:10:07 pm by Enk »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11968
    • View Profile
    • http://www.jpct.net
Re: Object3D, matrices and transformations
« Reply #2 on: April 02, 2009, 04:16:03 pm »
You are right about the translation matrix. It already stores the translation. That's what jPCT does too: It stores a translation and a rotation matrix per Object3D (not per polygon, that would be very memory intense and very very slow to render). Another way would be to store the translation simply as a vector and create/fill the translation matrix in case you need one. In fact, jPCT does it the other round, i.e. storing the data in a matrix but doing most calculation "by hand" using the actual translation values. It's cheaper to do 3 adds or subs than a complete matrix multiplication. On hardware however, it's always a matrix anyway.

Offline Enk

  • byte
  • *
  • Posts: 22
    • View Profile
Re: Object3D, matrices and transformations
« Reply #3 on: April 02, 2009, 04:22:05 pm »
OK, but how is the rotationMatrix actually used?

I guess jPCT uses it own type of rendering-pipeline, but I am trying to get it to work with OpenGL only.

My "rendering" class works like this:
render scene -> render object (translates to the objects translationMatrix) -> render polygon-groups -> render polygons.

I want to be able to rotate an object around it own axis and be able to translate in a direction relative to the objects rotation, much like:
Code: [Select]
Vector v = obj.getXAxis();
v.mul(2.0f);
obj.translate(v);
« Last Edit: April 02, 2009, 04:27:36 pm by Enk »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11968
    • View Profile
    • http://www.jpct.net
Re: Object3D, matrices and transformations
« Reply #4 on: April 02, 2009, 05:33:00 pm »
jPCT does both (own pipeline and hw pipeline)...even more in the 1.18 beta that has been released yesterday. What i do in OpenGL native mode, is to create a transformation matrix out of an object's rotation, transformation, its parents transformation and the camera transformation and feed that into OpenGL's modelview matrix (after a conversion, because the formats differ, but this may not apply to you).

Offline Enk

  • byte
  • *
  • Posts: 22
    • View Profile
Re: Object3D, matrices and transformations
« Reply #5 on: April 02, 2009, 06:03:39 pm »
Ok, thanks. I'll look into it. Just discovered the glLoadMatrix-function and added a transpose and toGLFloatBuffer-function to my Matrix class :D

So I guess the rendering will look something like this:
Code: [Select]
public void renderScene(Scene scene) {

glPushMatrix();
    // the Camera-transformation will be inverted
    glLoadMatrix(scene.getCamera().toBuffer());
        glPushMatrix();
               // Iterate through objects in the scene
               glLoadMatrix(obj.getTranslation().toBuffer));
               drawObject(obj);

               // If the object has children, iterate through them
               glPushMatrix();
                    glLoadMatrix(child.getTranslation().toBuffer));
                    drawObject(child)
               glPopMatrix();
        glPopMatrix();

glPopMatrix();


}
« Last Edit: April 02, 2009, 06:15:08 pm by Enk »

Offline Enk

  • byte
  • *
  • Posts: 22
    • View Profile
Re: Object3D, matrices and transformations
« Reply #6 on: April 02, 2009, 10:41:22 pm »
Update: No I think I really got it (the translation bit at least).

The rendering "pipe-line" should look something like this (the [C] etc. just shows what Matrices are on the stack).

Code: [Select]
glLoadTransposeMatrix(CameraMatrix);               [C]
glPushMatrix();                                               [C, C]

....

// Object:

glMultTransposeMatrix(ObjectMatrix);                 [CO, C ]
glPushMatrix();                                               [CO, CO, C]


// Child

glMultTransposeMatrix(ChildMatrix);            [COK, CO, C]
           


I also found the glLoadTransposeMatrix and glMultTransposeMatrix (in OpenGL 1.3) which takes row-ordered matrices as arguments (guess OpenGL's transpose-function is as good as mine :P ).

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11968
    • View Profile
    • http://www.jpct.net
Re: Object3D, matrices and transformations
« Reply #7 on: April 02, 2009, 11:46:32 pm »
Yes, something like that. Except that you aren't taking the rotation into account, are you? I wasn't aware of that TransposeMatrix-thing. I don't need it anyway, because jPCT's Matrix-dump-method "accidently" transposes the matrix in the way OpenGL needs it, but it's good to know that it exists.

Offline Enk

  • byte
  • *
  • Posts: 22
    • View Profile
Re: Object3D, matrices and transformations
« Reply #8 on: April 02, 2009, 11:53:40 pm »
I am working on the rotations now. Not quite sure how to implement it though. Tried multiplying the rotation matrix and translation matrix but got some strange results :D

Do jPCT use matrices for camera-movement also? I thought it was just to -translate and -rotate the camera values first and everything was OK, but doesn't seem like it. Haven't started reading about quaternions yet :/

Edit: fixed the rotation of objects now (I use just one Matrix in the Object3D-class which handles both translation and rotation, and objects are translated -x, -y, -z before rotation and translated back afterwards to rotate around its own axis). Just have to get the camera class to work now, and some getXAxis-like function so that translation from the objects rotation is trivial.
« Last Edit: April 03, 2009, 12:48:23 am by Enk »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11968
    • View Profile
    • http://www.jpct.net
Re: Object3D, matrices and transformations
« Reply #9 on: April 03, 2009, 07:24:54 am »
Yes, jPCT uses matrices for the camera too. Don't go into quaternions. You absolutely don't need them for what you want to do. In fact, you actually don't need them at all, because they are mathematically equivalent to matrices, just harder to handle. Them *might* be useful for some rotation interpolation stuff, but personally, i've never ever feeled the urge to prefer them over matrices.

Offline Enk

  • byte
  • *
  • Posts: 22
    • View Profile
Re: Object3D, matrices and transformations
« Reply #10 on: April 03, 2009, 12:02:05 pm »
Thanks again for all the help.

Now I have a working Camera class and I can rotate all objects around their own axis. Just one quick questing about the Camera-class.

Since the transformation of the Camera is applied inverted to the Scene, should add a Invert-matrix method and use it? Inverting matrices seems like a time (and CPU) stealer. Guess the best way is just to create functions that invert the values beforehand e.g.:
Code: [Select]
public class Camera {
private Matrix transformation;

...

public void rotateX(float theta) {
    transformation.rotateX(-theta);
}


public void translate(Vector3D v) {
    v.mul(-1.0f);
    transformation.translate(v);
}

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11968
    • View Profile
    • http://www.jpct.net
Re: Object3D, matrices and transformations
« Reply #11 on: April 03, 2009, 12:11:02 pm »
I don't do this. I'm inverting them when i have to. You are actually inverting the camera once per frame...it usually isn't a performance issue. Apart form that, inverting a rotation matrix (which is in fact a 3x3 matrix wrapped into a 4x4) is much easier...just transpose it. That what jPCT Matrix.invert3x3()-method does. It's a lot faster that way.

Offline Enk

  • byte
  • *
  • Posts: 22
    • View Profile
Re: Object3D, matrices and transformations
« Reply #12 on: April 03, 2009, 03:05:08 pm »
My camera-rotation works before I invert (just have to rotateX(-theta) to rotate the right way). But when I try to invert it (by transposing the upper left 3x3 of the matrix I get some strange results). I (trying at least) to have both the translation and the rotation in one matrix.

In the Matrix-class I got a function which returns a FloatBuffer with the inverted matrix:
Code: [Select]
public FloatBuffer getInvertedBuffer() {
    Matrix temp = new Matrix(this);        // Creates a temporary matrix with this matrix' values.

    temp.invertTranslation();                // This function just negate the values in m3, m7 and m11 (the translation)
    temp.transpose3x3();                    // This function transposes the upper left 3x3 of the matrix

    return temp.getBuffer();
}

I have tested the transpose3x3-functions and it works fine. But doesn't seem like the getInvertedBuffer-functions gives me the right values. Really want this to work since pre-negating all values in all functions of the isn't elegant :P

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11968
    • View Profile
    • http://www.jpct.net
Re: Object3D, matrices and transformations
« Reply #13 on: April 03, 2009, 03:09:49 pm »
I'm not sure if i got this correctly, but if i did: You can't invert a transformation matrix (i.e. rotation and translation combined) that way. It will work on rotation matrices only.
« Last Edit: April 03, 2009, 03:33:24 pm by EgonOlsen »

Offline Enk

  • byte
  • *
  • Posts: 22
    • View Profile
Re: Object3D, matrices and transformations
« Reply #14 on: April 03, 2009, 03:13:48 pm »
Ok, I will try to seperate the camera into two matrices then.

Again thanks for the replies. Allways great to get advice from experienced nerds :)


Edit: Worked like a charm now. Objects and the camera got 2 matrices now and they are multiplied once per frame. Time to clean my code now :D
« Last Edit: April 03, 2009, 03:38:37 pm by Enk »