ProjectedTextureUtil Updates

Hey, I have started this thread to avoid future hijacks of nymons "Moving on with jME" thread.



Last post by me:

basixs said:

...

okies, I took my 'lunch break' a wiped up a quick 'camera' only implementation. however I must be doing something wrong with the lookat because the projected textures are not in the right spot.  Maybe someone could take a look and see if anything pops out at them (probably the lookat vector just needs to be adjusted, but I have already used up my 'free time')...


import com.jme.image.Texture;
import com.jme.math.Matrix4f;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;
import com.jme.renderer.Renderer;
import com.jme.renderer.jogl.JOGLCamera;
import com.jme.renderer.jogl.JOGLRenderer;
import com.jme.renderer.lwjgl.LWJGLCamera;
import com.jme.renderer.lwjgl.LWJGLRenderer;
import com.jme.system.DisplaySystem;



/**
 * <code>ProjectedTextureUtil</code>
 *
 * @author Rikard Herlitz (MrCoder)
 *
 * Dual implementation framework:
 * @author Joshua Ellen (basixs)
 */
public class ProjectedTextureUtil {

    private static final Vector3f[] savedCamera = new Vector3f[ 4 ];
    static {
        for( int i = 0; i < savedCamera.length; ++i ){
            savedCamera[i] = new Vector3f();
        }
    }
    //
    private static Camera camera = null;
    private static Matrix4f lightProjectionMatrix = new Matrix4f();
    private static Matrix4f lightViewMatrix = new Matrix4f();
    private static Matrix4f biasMatrix = new Matrix4f( 0.5f, 0.0f, 0.0f, 0.0f,
            0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.5f, 0.5f,
            1.0f ); // bias from [-1, 1] to [0, 1]

    /**
     * Updated texture matrix on the provided texture
     *
     * @param texture
     *            Texture to update texturematrix on
     * @param fov
     *            Projector field of view, in angles
     * @param aspect
     *            Projector frustum aspect ratio
     * @param near
     *            Projector frustum near plane
     * @param far
     *            Projector frustum far plane
     * @param pos
     *            Projector position
     * @param aim
     *            Projector look at position
     */
    public static void updateProjectedTexture( Texture texture, float fov,
            float aspect, float near, float far, Vector3f pos, Vector3f aim,
            Vector3f up ) {

        if( camera == null ){
            // We have to use the derived implementation constructor because there is no
           // renderer.createCamera( width, height, dataOnly ); method...
            final Renderer renderer = DisplaySystem.getDisplaySystem().getRenderer();
            if ( renderer instanceof LWJGLRenderer ) {
                camera = new LWJGLCamera( renderer.getWidth(), renderer.getHeight(), true );
            } else if ( renderer instanceof JOGLRenderer ) {
                camera = new JOGLCamera( renderer.getWidth(), renderer.getHeight(), true );
            }
        }


        matrixPerspective( fov, aspect, near, far, lightProjectionMatrix );
        matrixLookAt( pos, aim, up, lightViewMatrix );

        texture.getMatrix().set(
                lightViewMatrix.multLocal( lightProjectionMatrix ).multLocal(
                biasMatrix ) ).transposeLocal();
    }

    private static void matrixLookAt( Vector3f location, Vector3f at,
            Vector3f up, Matrix4f result ) {

//        saveCameraFrame();
        camera.setLocation( location );
        camera.lookAt( at, up );
//        camera.update();
//        camera.apply();
        setModelViewMatrix( result );
//        result.invertLocal();
//        restoreCameraFrame();
    }

    private static void matrixPerspective( float fovY, float aspect,
            float near, float far, Matrix4f result ) {
       
//        saveCameraFrame();
        camera.setFrustumPerspective( fovY, aspect, near, far );
//        camera.update();
//        camera.apply();
        setModelViewMatrix( result );
//        result.invertLocal();
//        restoreCameraFrame();
    }

    private static void setModelViewMatrix( final Matrix4f result ) {
        result.set( ( (AbstractCamera) camera ).getModelViewMatrix() );
    }

    private static void saveCameraFrame() {
        savedCamera[0].set( camera.getLocation() );
        savedCamera[1].set( camera.getDirection() );
        savedCamera[2].set( camera.getUp() );
        savedCamera[3].set( camera.getLeft() );
    }

    private static void restoreCameraFrame() {
        camera.setLocation( savedCamera[0] );
        camera.setDirection( savedCamera[1] );
        camera.setUp( savedCamera[2] );
        camera.setLeft( savedCamera[3] );
        camera.apply();
    }   
   
}


(The commented lines are where I was testing to see what was wrong...)

(Not sure its how Momoko_Fan would have done it, but it seems to work except for the above mentioned problem...)


Thread here: http://www.jmonkeyengine.com/jmeforum/index.php?topic=10165.0