I'm beginning to lose my sanity over this. Could someone please take a
look at the attached minimal sample program and tell me what's going on
there?

What I am trying to archive is reproducing the perspective projection
the camera does in order to find the exact 2D bounds of a 3D object.

To test this I have generated a sphere of radius 100 at the origin and a
perspective lens camera at (0,0,-300). I then use Camera3D.screen and
Camera3D.lens.project to project a pair of test coordinates. I have
verified (by tracing the sphere's vertices) that these coordinates are
on the surface of the sphere. However, the screenVertices I get are not
on the 2D sphere silhouette as I would have expected. Am I using the
wrong matrix? The wrong approach?

Global stage scaling is off. I'm using the latest subversion revision of
Away3D (not lite).

best regards,

    Sören

package  
{
        import away3d.cameras.lenses.PerspectiveLens;
        import away3d.cameras.TargetCamera3D;
        import away3d.containers.View3D;
        import away3d.core.base.Vertex;
        import away3d.core.math.Number3D;
        import away3d.materials.WireframeMaterial;
        import away3d.primitives.Sphere;
        import flash.display.StageQuality;
        import flash.display.StageScaleMode;
        import flash.display.StageAlign;
        import flash.events.Event;

        public class TestProjection extends View3D
        {
                private var m_Globe:Sphere;
                private var m_Camera:TargetCamera3D;
                
                public function TestProjection() 
                {
                        super();
        
                        stage.quality = StageQuality.LOW;
                        stage.scaleMode = StageScaleMode.NO_SCALE;
                        stage.align = StageAlign.TOP_LEFT;
                        
                        // center the view on the stage
                        x = stage.stageWidth / 2;
                        y = stage.stageHeight / 2;
                        
                        scene.addChild( m_Globe = new Sphere( { 
                                name:"Earth", x:0, y:0, z:0, segmentsH:32, 
segmentsW:32, radius:100, material:new WireframeMaterial( 0x0000FF ) } ));

                        camera = m_Camera = new TargetCamera3D();
                        m_Camera.position = new Number3D( 0, 0, -300 );
                        m_Camera.lookAt( m_Globe.position );
                        m_Camera.lens = new PerspectiveLens();          
                        
                        stage.addEventListener( Event.ENTER_FRAME, onEnterFrame 
);                      
                }       
                
                private function onEnterFrame( event:Event ) : void  
                {
                        render();
                        
                        var vertices:Array = new Array( 2 );
                        vertices[0] = new Vertex( 0,  100, 0 );
                        vertices[1] = new Vertex( 0, -100, 0 );
                        var screenVertices:Array = new Array();
                        m_Camera.lens.project( m_Camera.viewMatrix, vertices, 
screenVertices );
                        
                        trace( screenVertices + "\n" + m_Camera.screen( 
m_Globe, vertices[0] ) );
                }               
        }
}

Reply via email to