Question on camera projection_matrix

Hello,

I have a problem with a small osc callback which changes the camera data.
Setting camera lens and manipulating the projection matrix seems not to work at the same time. I can either change the lens or manipulate projection matrix per frame.
I’m not much into the internals, maybe someone can help me out or give a pointer how the projection_matrix is applied.

OSC Callback:

    
def update_camera(self, path, args):

        scene = bge.logic.getCurrentScene()
        camera =  scene.cameras[args[0]]
        
        camera.lens = args[1]
        camera.ortho_scale = args[2]
        camera.near = args[3]
        camera.far = args[4]
        camera.perspective = args[5]

        # if lens is changed the lensshift code will be ignored
        projection_matrix = camera.projection_matrix
        
        shift_x = args[6]
        shift_y = args[7]
        
        projection_matrix[0][2] = 2*shift_x
        projection_matrix[1][2] = 2*shift_y
        
        camera.projection_matrix = projection_matrix

Thanks in advance. offtools.

The lens parameter is one parameter that is used to calculate the projection matrix.

I do not know the implementation details. It might be that changing the lens does not immediately change the projection matrix and vice versa.

I suggest to use one method only whether camera arguments or matrix manipulation. One is like clicking “idiot-proof-buttons”. The other one is like an open-heart surgery ;). It allows much more freedom but has no security net if it fails.

Additional I recommend to read the wiki about projection matrix. I think that should help :).

code for setting fov and lensshift in camera.projection_matrix

 

        camera = bge.logic.getCurrentScene().active_camera
        projection_matrix = camera.projection_matrix

        e = 1.0/math.tan(angle/2.0)

        shift_x = args[7]
        shift_y = args[8]

        projection_matrix[0][0] = e
        projection_matrix[1][1] = e/aspect
        
        projection_matrix[0][2] = 2*shift_x
        projection_matrix[1][2] = 2*shift_y
        
        camera.projection_matrix = projection_matrix

Awesome, thanks for this! Been looking for this solution for ages. This should be documented somewhere!

By the way, the equation for e is wrong. It should be updated to:


e = 1.0/math.tan((angle*math.pi)/360)

Or use math.e? It has the exponent function and e I believe

Sent from my Nexus 7 using Tapatalk HD

Thanks for your comments, thats very helpfull. Will try it.

In reflection, I don’t think that math.e is relevant. I had glanced over and thought you were deriving it by hand but it appears to be completely irrelevant.

It’s just a variable name in this case. As far as I understand, it’s the scaling of the projection matrix that’s equivalent to the camera lens or viewing angle.