Author Topic: glPushMatrix() & glPopMatrix()  (Read 2546 times)

Offline tinhtinhcd

  • byte
  • *
  • Posts: 16
    • View Profile
glPushMatrix() & glPopMatrix()
« on: May 26, 2017, 09:08:44 am »
I have to rotate object around the axis

Code: [Select]
public void onDrawFrame(GL10 gl) {
....
parentObj.rotateAxis(new SimpleVector(mPquart[1],mPquart[2],mPquart[3]),mPquart[0]);
...
}

The problem is mPquart is Quaternion and get from SensorManager.
So, I need something like glPushMatrix() & glPopMatrix() to make the object stable when I don't rotate the phone.
How can I do it.

I try gl.glPushMatrix(); and gl.glPopMatrix(); but nothing happen.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: glPushMatrix() & glPopMatrix()
« Reply #1 on: May 26, 2017, 11:04:50 am »
You are not supposed to do direct GL calls within jPCT-AE. Either convert your quat to angles or a Matrix and use the rotation methods or setRotationMatrix() on the Object3D. Also keep in mind that jPCT's coordinate system differs from that of OpenGL, so you have to convert the matrix. The Matrix class itself should have methods for this. Anyway, this problem has been solved in the past IIRC, so either the Wiki or this should actually provide some help as well.

Offline tinhtinhcd

  • byte
  • *
  • Posts: 16
    • View Profile
Re: glPushMatrix() & glPopMatrix()
« Reply #2 on: June 06, 2017, 09:48:34 am »
Hi,
I'm not good at Math. Base on what I understand. Is this what I need.

Code: [Select]
private Matrix convertMatrix(){
        Matrix matrix = new Matrix();

       float angle = (float)  Math.acos(mPquart[0]);
        float c = (float) Math.cos(angle);
        float s = (float) Math.sin(angle);
        float x = mPquart[1];
        float y = mPquart[2];
        float z = mPquart[3];

        float[] dump = new float[16];

        dump[0] = x*x*(1-c)+c;
        dump[1] = x*y*(1-c)-z*s;
        dump[2] = x*z*(1-c)+y*s;
        dump[3] = 0f;
        dump[4] = y*x*(1-c)+z*s;
        dump[5] = y*2*(1-c)+c;
        dump[6] = y*z*(1-c)-x*s;
        dump[7] = 0f;
        dump[8] = x*z*(1-c)-y*s;
        dump[9] = y*z*(1-c)+x*s;
        dump[10] = z*2*(1-c)+c;
        dump[11] = 0f;
        dump[12] = 0f;
        dump[13] = 0f;
        dump[14] = 0f;
        dump[15] = 1f;

        matrix.fillDump(dump);

        return matrix;
    }
and
Code: [Select]
obj.setRotationMatrix(convertMatrix());
« Last Edit: June 06, 2017, 10:35:42 am by tinhtinhcd »

Offline tinhtinhcd

  • byte
  • *
  • Posts: 16
    • View Profile
Re: glPushMatrix() & glPopMatrix()
« Reply #3 on: June 08, 2017, 05:56:45 am »
Hi, the above code work but it is not what i need for my project.

My app connect with a real device and receive quaternion data from that.
What I need is when my device rotate in real world, the object must rotate the same way.
Currently, it only work when i put the these XAxis in same direction.

thanks.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: glPushMatrix() & glPopMatrix()
« Reply #4 on: June 08, 2017, 07:56:47 am »
I'm not sure what kind of matrix the code above is supposed to create. One in jPCT's coordinate system or one in OpenGL's? If it's the latter, try to apply this method on the resulting matrix: http://www.jpct.net/jpct-ae/doc/com/threed/jpct/Matrix.html#transformToGL()

Offline tinhtinhcd

  • byte
  • *
  • Posts: 16
    • View Profile
Re: glPushMatrix() & glPopMatrix()
« Reply #5 on: June 09, 2017, 06:15:40 am »
Hi,
after check again,
I see my matrix is not correct, so I update it and use transformToGL();
Code: [Select]
Matrix matrix = new Matrix();

        float x = mPquart[1];
        float y = mPquart[2];
        float z = mPquart[3];
        float w = mPquart[0];

        float[] dump = new float[16];

        final float xx = x * x;
        final float xy = x * y;
        final float xz = x * z;
        final float xw = x * w;
        final float yy = y * y;
        final float yz = y * z;
        final float yw = y * w;
        final float zz = z * z;
        final float zw = z * w;

        dump[0] = 1 - 2 * (yy + zz);
        dump[4] = 2 * (xy - zw);
        dump[8] = 2 * (xz + yw);
        dump[12] = 0;
        dump[1] = 2 * (xy + zw);
        dump[5] = 1 - 2 * (xx + zz);
        dump[9] = 2 * (yz - xw);
        dump[13] = 0;
        dump[2] = 2 * (xz - yw);
        dump[6] = 2 * (yz + xw);
        dump[10] = 1 - 2 * (xx + yy);
        dump[14] = 0;
        dump[3] = 0;
        dump[7] = 0;
        dump[11] = 0;
        dump[15] = 1;

        matrix.setDump(dump);

       matrix.transformToGL();

I saw the object rotate different, but I still have a problem.
When I rotate my device on xAxis, it will rotate y or z, it only rotate properly when i put the device and my phone same direction.
Is there anything that I miss.

Thanks so much for support. 

Offline tinhtinhcd

  • byte
  • *
  • Posts: 16
    • View Profile
Re: glPushMatrix() & glPopMatrix()
« Reply #6 on: June 09, 2017, 08:09:03 am »
Finally it work as expectation.
But a strange thing that I must switch z<=>y and x=-x.
I have no idea why it work on that way.