PerspectiveCamera - setMatrices sets the incorrect matrix

Anything libgdx related goes here!

PerspectiveCamera - setMatrices sets the incorrect matrix

Postby TeaAndBiscuits » Sun Jan 02, 2011 3:49 pm

Hi,

While using PersepctiveCamera I enabled fog in my game and noticed some strange results. Moving the camera around fog was applied in world Z axis and NOT in the camera Z axis.

Looking at the code in the source branch I noticed something in the following code...
Code: Select all
public void setMatrices () {
                setViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
                update();
                GL10 gl = Gdx.gl10;
                gl.glMatrixMode(GL10.GL_PROJECTION);
                gl.glLoadMatrixf(getCombinedMatrix().val, 0);
                gl.glMatrixMode(GL10.GL_MODELVIEW);
                gl.glLoadIdentity();
        }

Notice that the camera is being applied to the Projection matrix and not to the ModelView matrix.

I fixed this in my game by not calling setMatrices and instead running the following code
Code: Select all
GL10 gl = Gdx.gl10;
      
       gl.glMatrixMode(GL10.GL_PROJECTION);
       gl.glLoadIdentity();
       gl.glLoadMatrixf(mCamera.getProjectionMatrix().val, 0);
      
       gl.glMatrixMode(GL10.GL_MODELVIEW);
       gl.glLoadIdentity();
       gl.glLoadMatrixf(mCamera.getModelviewMatrix().val, 0);


NOTE: I don't call update as it gets called elsewhere as an optimisation in my code :)


One last thing. Looking at OpenGL FAQ I found a list of reasons why the camera needs to be in the modelview matrix http://www.opengl.org/resources/faq/technical/projection_abuse.php



I hope this is helpful!!!

Cheers
TeaAndBiscuits
 
Posts: 5
Joined: Sun Dec 26, 2010 10:32 pm

Re: PerspectiveCamera - setMatrices sets the incorrect matri

Postby mzechner » Sun Jan 02, 2011 8:39 pm

Projection abuse is a pretty fitting title for what PerspectiveCamera does :)

Here's the reasoning behind that: you can eliminate push and pop operations when you place both the project and view matrix into the OpenGL ES projection matrix. We shouldn't do this by default though so i'll change that asap.

Thanks for pointing that out!
mzechner
Site Admin
 
Posts: 4879
Joined: Sat Jul 10, 2010 3:50 pm

Re: PerspectiveCamera - setMatrices sets the incorrect matri

Postby TeaAndBiscuits » Sun Jan 02, 2011 9:19 pm

I was wondering if that was the reason. It does make sense as an optimisation, though for my needs (fog!) it breaks :D

Glad to help!
TeaAndBiscuits
 
Posts: 5
Joined: Sun Dec 26, 2010 10:32 pm


Return to Libgdx

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 1 guest