Camera.getWorldCoordinates() throws exception

I tried to use the jmonkey cameras in dataOnly mode (I assumed dataOnly means running without OpenGL instructions). My intention is the use of AbstractCamera's built-in worldCoordinate to/from screenLocation transformations. I tried it out with LWGLCamera and JOGLCamera. But in both cases I got an exception when calling getWorldCoordinates() method to retrieve bottom left corner of camera screen (please look at souce+stacktrace).



I looked at AbstractCamera source code and it seems, the problem is, that something is wrong with the modelViewProjection member. But this private matrix4f should not be directly manipulated and has no OpenGL dependencies wich I could found. How to use the cameras correctly for view dependent coordinate transformation?



# source:

boolean dataOnly = true;

LWJGLCamera camera = new LWJGLCamera(640, 480, dataOnly);

camera.getLocation().set(0, 0, 0);

camera.getDirection().set(0, 0, 1);

camera.getUp().set(1,0,0);

camera.getLeft().set(-1,0,0);

camera.apply();

camera.update();

Vector3f worldBottomLeft = camera.getWorldCoordinates(new Vector2f(0,0), 0);





# stacktrace:

04.10.2008 18:49:07 com.jme.renderer.AbstractCamera <init>

INFO: Camera created.

Exception in thread "main" java.lang.ArithmeticException: This matrix cannot be inverted

at com.jme.math.Matrix4f.invert(Matrix4f.java:1041)

at com.jme.renderer.AbstractCamera.getWorldCoordinates(AbstractCamera.java:1017)

at com.jme.renderer.AbstractCamera.getWorldCoordinates(AbstractCamera.java:941)





thx for any advice.

problem solved, invalid left vector used (equal to up vector).

Camera works correctly with this fix:



boolean dataOnly = true;

LWJGLCamera camera = new LWJGLCamera(640, 480, dataOnly);

camera.getLocation().set(0, 0, 0);

camera.getDirection().set(0, 0, 1);

camera.getUp().set(1,0,0);

camera.getLeft().set(0,-1,0);

camera.apply();

camera.update();

Vector3f worldBottomLeft = camera.getWorldCoordinates(new Vector2f(0,0), 0);



But problem I found a real problem at JOGLCamera when using dataOnly == true :



    protected void doFrustumChange() {



        final GL gl = GLU.getCurrentGL();



        if (!isDataOnly()) {

            // set projection matrix

            RendererRecord matRecord = (RendererRecord) DisplaySystem.getDisplaySystem().getCurrentContext().getRendererRecord();

            matRecord.switchMode(GL.GL_PROJECTION);



            tmp_FloatBuffer.rewind();

            getProjectionMatrix().fillFloatBuffer(tmp_FloatBuffer);

            tmp_FloatBuffer.rewind();

            gl.glLoadMatrixf(tmp_FloatBuffer);

        }



    }

   

I think, better would be this to avoid exceptions:



    protected void doFrustumChange() {

        if (!isDataOnly()) {

       

            final GL gl = GLU.getCurrentGL();



            // set projection matrix

            RendererRecord matRecord = (RendererRecord) DisplaySystem.getDisplaySystem().getCurrentContext().getRendererRecord();

            matRecord.switchMode(GL.GL_PROJECTION);



            tmp_FloatBuffer.rewind();

            getProjectionMatrix().fillFloatBuffer(tmp_FloatBuffer);

            tmp_FloatBuffer.rewind();

            gl.glLoadMatrixf(tmp_FloatBuffer);

        }



    }



ciao!

Well you have to get to glThread somehow. I advise to search wiki for "from simple game to standard game" article. Or look at my sample code:



Callable<?> call = new Callable<Object>(){

            public Object call() throws Exception {

    Vector2f screenPos = new Vector2f(x,y);

            Vector3f worldCoords = camera.getWorldCoordinates(screenPos, 0);

            Vector3f worldCoords2 = camera.getWorldCoordinates(screenPos, 1);

            return null;

            }

};

GameTaskQueueManager.getManager().update(call);