Show
Ignore:
Timestamp:
07/03/08 18:39:10 (5 months ago)
Author:
phill
Message:

o Added camera-follows-tag feature.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/x42view.net/Gui/ModelWindow.Preview.cs

    r490 r594  
    7777                } 
    7878 
     79                #region Camera override stuff 
     80                private Libx42.Tag cameraTag; 
     81                public Libx42.Tag CameraTag 
     82                { 
     83                        get { return cameraTag; } 
     84                        set 
     85                        { 
     86                                if( value != null && value.Owner != model.Model ) 
     87                                        throw new ArgumentException( "Tag belongs to a different model." ); 
     88 
     89                                if( value == cameraTag ) 
     90                                        return; 
     91 
     92                                cameraTag = value; 
     93 
     94                                pn3DCamera.Enabled = cameraTag == null; 
     95                                UpdateCameraModeMenu(); 
     96 
     97                                pn3D.Invalidate(); 
     98                        } 
     99                } 
     100 
     101                private void GetCameraMatrices( out Matrix view, out Matrix projection ) 
     102                { 
     103                        if( cameraTag == null ) 
     104                        { 
     105                                view = pn3DCamera.ViewMatrix; 
     106                                projection = pn3DCamera.ProjectionMatrix; 
     107                        } 
     108                        else 
     109                        { 
     110                                Libx42.Math.Affine tagMat = modelDXData.Pose.TagMatrices[cameraTag.Index]; 
     111 
     112                                Vector3 o = Helpers.ConvertToDX( tagMat.TransformPoint( new Libx42.Math.Vector3( 0 ) ) ); 
     113                                Vector3 x = Helpers.ConvertToDX( tagMat.TransformPoint( new Libx42.Math.Vector3( 1, 0, 0 ) ) ); 
     114                                Vector3 y = Helpers.ConvertToDX( tagMat.TransformPoint( new Libx42.Math.Vector3( 0, 1, 0 ) ) ); 
     115                                Vector3 z = Helpers.ConvertToDX( tagMat.TransformPoint( new Libx42.Math.Vector3( 0, 0, 1 ) ) ); 
     116 
     117                                view = Matrix.LookAtRH( o, -x, y ); 
     118 
     119                                float zf = pn3DCamera.ZFar; 
     120                                float zn = pn3DCamera.ZNear; 
     121                                float aspect = pn3DCamera.AspectRatio; 
     122 
     123                                const float defFov = 45; 
     124                                const float DegToRad = (float)(Math.PI / 180.0); 
     125 
     126                                float fovX = defFov * Helpers.Clamp( Libx42.Math.Vector3.Length( tagMat.GetColumn( 2 ) ), 0.1F, 179.0F / defFov ); 
     127                                float fovY = defFov * Helpers.Clamp( Libx42.Math.Vector3.Length( tagMat.GetColumn( 1 ) ), 0.1F, 179.0F / defFov ); 
     128 
     129                                fovX *= DegToRad; 
     130                                fovY *= DegToRad; 
     131 
     132                                projection.M11 = 1.0F / ((float)Math.Tan( 0.5F * fovX ) * aspect); 
     133                                projection.M12 = 0; 
     134                                projection.M13 = 0; 
     135                                projection.M14 = 0; 
     136 
     137                                projection.M21 = 0; 
     138                                projection.M22 = 1.0F / (float)Math.Tan( 0.5F * fovY ); 
     139                                projection.M23 = 0; 
     140                                projection.M24 = 0; 
     141 
     142                                projection.M31 = 0; 
     143                                projection.M32 = 0; 
     144                                projection.M33 = zf / (zn - zf); 
     145                                projection.M34 = -1; 
     146 
     147                                projection.M41 = 0; 
     148                                projection.M42 = 0; 
     149                                projection.M43 = zn * zf / (zn - zf); 
     150                                projection.M44 = 0; 
     151 
     152                                //projection = Matrix.PerspectiveFovRH( 90, 1, pn3DCamera.ZNear, pn3DCamera.ZFar ); 
     153                        } 
     154                } 
     155                #endregion 
     156 
    79157                #region Drawing helpers 
    80158                private void SetupCameraLight() 
     
    82160                        Device dev = pn3D.Device; 
    83161 
     162                        Matrix view, proj; 
     163                        GetCameraMatrices( out view, out proj ); 
     164 
    84165                        dev.Lights[0].Type = LightType.Directional; 
    85                         dev.Lights[0].Direction = Vector3.Normalize( cam.LookAt - cam.Position ); 
     166                        dev.Lights[0].Direction = new Vector3( -view.M13, -view.M23, -view.M33 ); 
    86167                        dev.Lights[0].Specular = Color.White; 
    87168                        dev.Lights[0].Ambient = Color.White; 
     
    120201                                pnPreviewOpts.BackgroundColor, 1.0F, 0 ); 
    121202 
    122                         dev.Transform.Projection = cam.ProjectionMatrix; 
    123                         dev.Transform.View = cam.ViewMatrix; 
     203                        Matrix view, projection; 
     204                        GetCameraMatrices( out view, out projection ); 
     205 
     206                        dev.Transform.Projection = projection; 
     207                        dev.Transform.View = view; 
    124208                        dev.Transform.World = Matrix.Identity; 
    125209