Author Topic: model flickering sometimes without errors  (Read 476 times)

Offline ruspa

  • byte
  • *
  • Posts: 6
    • View Profile
model flickering sometimes without errors
« on: March 02, 2016, 07:00:44 pm »
hello! I'm writing a livewallpaper based on a GLSurfaceRender example for live wallpapers and jpct I found around. It works pretty well except that sometimes the 3d model flickers and stretches randomly for an instant and then works well again for some time.

What I do is loading a sphere model and apply the rotation matrix coming from the Sensors.Type_vector (sorry I know it's incorrect but I'm at a bar at the moment writing this post on my cellphone). I mean the newer sensor reading type that unifies all sensors to avoid gymbal locks.

So in the main activity I register and listen for sensor changes and remap the axes as I see fit. In the render class, in the onDraw method I just apply the rotation matrix and voilą, works like charm.

Now the issue part, that I hope someone can help me fix.

If I do not apply the rotation in the ondraw method the model remains in its initial position and never ever flickers. If I apply (pseudocode) setrotationmatrix(MainActivity.rotationmatrixFromTheSensors) my model rotates very smoothly as you move the phone but suffers of random flickering/tearing/swap to initial position.
On my opinion its something related to desyncronyzation between MainActivity.onSensorChanged() and RenderEngine.onDraw(). Looks like randomly the ondraw method cannot access correctly the rotationmatrix calculated on the main activity, or may be that sensors sometimes actually fails reading data (my phone is a nexus5 in wonderful shape, so it's unlikely the case).

I've tried to be more thread friendly by let the renderer class implement sensorlistener, but then nothing really worked.

Have someone any clue the cause of this random flicker?

I can provide code, or a video of that behavior if it can help.

Thanks in advance guys :)




P.S. If I do not apply the rotation matrix calculated on the main activity and instead apply dummy rotations in the onDraw() method no fllickers occur. These flickers came out only when I applied the rotation matrix from onSensorChanged with the no-gymbal-lock reading. I'm also persuaded that when I've been using the older geomagnetic and accelerometer readings there were no flickers, but I'm not sure. Got to doublecheck as I manage to get home
« Last Edit: March 02, 2016, 07:35:10 pm by ruspa »

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11776
    • View Profile
    • http://www.jpct.net
Re: model flickering sometimes without errors
« Reply #1 on: March 02, 2016, 07:49:18 pm »
jPCT-AE isn't thread safe (http://www.jpct.net/wiki/index.php?title=Thread_safety). You have to set the rotation matrix in the rendering thread, not in any other thread. I suggest to  copy the matrix that you get from the sensors into some buffer and apply it in the render thread. You also have to synchronize these blocks. Something like this (in a kind of pseudo-code):

Code: [Select]
public void someSenserMethod() {
   ...
   synchronized(bufferMatrix) {
      magicFill(sensorMatrix, buffferMatrix);
   }
}

...
public void onDrawFrame() {
    ....
    synchronized(bufferMatrix) {
        setRotationMatrix(bufferMatrix);
    }
    ....
    render();
}


That should fix the issue.

Offline ruspa

  • byte
  • *
  • Posts: 6
    • View Profile
Re: model flickering sometimes without errors
« Reply #2 on: March 02, 2016, 08:39:52 pm »
YOU ARE MY HERO  ;D ;D ;D

I'm a j2ee developer, to deal with JBoss Seam, Jbpm and eclipse plugin creation one can happily settle with a poor knowledge of some really basic stuff.
Like this sinchronyzed(something) that with a bit of shame I admit I never had the need to use before.

Just adding

synchronized(rotationMatrix){
                   SensorManager.getRotationMatrixFromVector(rotationMatrix, event.values);
                    SensorManager.remapCoordinateSystem(rotationMatrix, SensorManager.AXIS_MINUS_X, SensorManager.AXIS_Y, rotationMatrix);
}

in the main activity and

synchronized(MainActivity.rotationMatrix){
                   ball.setRotationMatrix(MainActivity.rotationMatrix);
}

in the render class totally solved the issue.
half bronze star to me for having correct suspects
100 gold stars to you that immediately known the perfect solution

thanks, really!

« Last Edit: March 02, 2016, 08:42:30 pm by ruspa »