model flickering sometimes without errors

Started by ruspa, March 02, 2016, 07:00:44 PM

Previous topic - Next topic

ruspa

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

EgonOlsen

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):


public void someSenserMethod() {
   ...
   synchronized(bufferMatrix) {
      magicFill(sensorMatrix, buffferMatrix);
   }
}

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



That should fix the issue.

ruspa

#2
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!