Author Topic: Set LookAt Matrix  (Read 3282 times)

Offline AugTech

  • int
  • **
  • Posts: 64
    • View Profile
    • Augmented Technologies
Set LookAt Matrix
« on: November 27, 2011, 01:49:32 pm »
Hi,
Last week I implemented jPCT-AE within my AR application instead of using my own OpenGL renderer; and first-off let me say that it is excellent, providing many more features than I could possibly code myself!

Like many others, I have some issues with rotations, but I did previously have this working (along with GPS location updates) very well. Due to the the various coordinate systems in play, I am generating vectors to describe the direction the camera should be facing in ECEF coordinates. These vectors were fed to the renderer as below;

Code: [Select]
private float[] mLookMatrix = new float[16];

    public void setCameraPosition(CamPosition newCamera) {
    Matrix.setLookAtM(mLookMatrix, 0,
    newCamera.eyeX,
    newCamera.eyeY,
    newCamera.eyeZ,
    newCamera.frontX + newCamera.eyeX,
    newCamera.frontY + newCamera.eyeY,
    newCamera.frontZ + newCamera.eyeZ,
    newCamera.upX,
    newCamera.upY,
    newCamera.upZ);
   }

with the following function being called on each DrawFrame();

Code: [Select]
private void setViewAndProjection() {
        float[] modelMatrix = new float[16]; // model matrix (identity)
        Matrix.setIdentityM(modelMatrix, 0);
        Matrix.translateM(modelMatrix, 0, 0, 0, 0);

        // calculating the current model-view-projection matrix
        Matrix.frustumM(mProjMatrix, 0,
        -fSize, fSize,
        -fSize / ratio, fSize / ratio,
        zNear, viewRange);

        // multiply matrices
        Matrix.multiplyMM(mWorldMatrix, 0, mProjMatrix, 0, mLookMatrix, 0);
        Matrix.multiplyMM(modelViewProjectionMatrix, 0, mWorldMatrix, 0, modelMatrix, 0);
       
        GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, modelViewProjectionMatrix, 0);
}

Obviously a lot of this is no longer needed, but my issue is finding a suitable replacement for the Matrix.setLookAt() function within jPCT-AE.

The issue I have is that using
Code: [Select]
    public void setCameraPosition(CamPosition newCamera) {
    SimpleVector dir = new SimpleVector(
    newCamera.frontX + newCamera.eyeX,
    newCamera.frontY + newCamera.eyeY,
    newCamera.frontZ + newCamera.eyeZ
    );
    SimpleVector up = new SimpleVector(
    newCamera.upX,
    newCamera.upY,
    newCamera.upZ);

    worldCamera.setOrientation(dir, up);
    worldCamera.setPosition(newCamera.eyeX, newCamera.eyeY, newCamera.eyeZ);
    }

...results in some very strange results! Initially 'the world' is viewed as before, but after a randomly short period of time, during movement of the device the axis of rotation get screwed up (x,y or z are inverted or switched) or just point in  another direction to the one I am facing.

I have tried getting and setting the camera matrix directly, but with no benefit. Additionally I have searched the forum, but the various code snippets didn't help either, due to angles not being used.

Could anyone suggest either what is wrong, or how I can set the lookAt vectors in jPCT-AE as is done in OpenGL ??

Many thanks in advance.


Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: Set LookAt Matrix
« Reply #1 on: November 27, 2011, 08:33:59 pm »
If i'm reading the (sparse) documentation of setLookAtM correctly, eye? is the position of the camera and front?+eye? is the point to look at? If that's true, your direction vector should rather be just front? not the addition of the two vectors.

Offline AugTech

  • int
  • **
  • Posts: 64
    • View Profile
    • Augmented Technologies
Re: Set LookAt Matrix
« Reply #2 on: December 01, 2011, 12:58:39 pm »
Many thanks for this - it has solved the issue!!

I previously had the eye position added to the direction vertex as the LookAt() function in OpenGL was taking the vector as an absolute point to look at. The effect was as I moved forward, the camera always pointed at the same place, therefore 'twisting' the rotation (as if continually turning one's head).

It appears jPCT is not doing the same, therefore you can keep looking in a direction, but walk parallel to it just by updating the camera position.

I have an additional question, but will start a seperate thread as its unrelated.
Cheers.

Offline AceGIS

  • byte
  • *
  • Posts: 32
    • View Profile
Re: Set LookAt Matrix
« Reply #3 on: September 04, 2012, 06:06:56 am »
Hi AugTech,

Any chance you could explain how you used the GPS with jPCT-AE?

Pseudo code is great.