Пример #1
0
        private void DrawModel(Camera camera, PositionedModel model, RenderMode renderMode)
        {
#if PROFILE
            ModelsDrawnThisFrame++;
#endif

            //TimeManager.SumTimeSection("Draw Model Start");

            bool flipped = model.FlipX ^ model.FlipY ^ model.FlipZ;
#if XNA4

            SetCullStateForModel(model, flipped);

#else

            if (model.mDrawWireframe)
                FlatRedBallServices.GraphicsDevice.RenderState.FillMode = FillMode.WireFrame;

            switch (model.FaceCullMode)
            {
                case ModelFaceCullMode.CullClockwiseFace:
                    if (!flipped)
                        Renderer.GraphicsDevice.RenderState.CullMode = CullMode.CullClockwiseFace;
                    else
                        Renderer.GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;

                    break;
                case ModelFaceCullMode.CullCounterClockwiseFace:
                    if (!flipped)
                        Renderer.GraphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
                    else
                        Renderer.GraphicsDevice.RenderState.CullMode = CullMode.CullClockwiseFace;

                    break;
                case ModelFaceCullMode.None:
                    Renderer.GraphicsDevice.RenderState.CullMode = CullMode.None;

                    break;
            }



            #region Reset device settings if they've changed - They may change in a shader

            FlatRedBallServices.GraphicsDevice.RenderState.DepthBufferEnable = true;
            FlatRedBallServices.GraphicsDevice.RenderState.DepthBufferWriteEnable = true;

            #endregion
#endif
            //TimeManager.SumTimeSection("Set depth states");
#if XNA4

            // I don't think we need to worry about vertex declarations 
#else
            #region Set Vertex Declaration

            if (model.XnaModel != null)
            {
                if (model.XnaModel.Meshes.Count != 0 &&
                    model.XnaModel.Meshes[0].MeshParts.Count != 0)
                {
                    // The assumption is that each mesh part is using the same vertex declaration for the
                    // whole model.  Instead of setting the vertex declaration in the DrawMeshPart method we'll
                    // set it up here to save on the number of method calls on the 360.



                    FlatRedBallServices.GraphicsDevice.VertexDeclaration =
                        model.XnaModel.Meshes[0].MeshParts[0].VertexDeclaration;

                }
            }
            #endregion
#endif
            //TimeManager.SumTimeSection("Set vertex declaration");

            #region Set Point Light



            // Find the closest light
            //int lightIndex = 0;
            //float distance;

            Vector3 meshCenter = (model == null) ? Vector3.Zero : model.Position;

            #endregion

            #region Draw Model
#if WINDOWS_PHONE
            if (model.XnaModel != null)
            {

                model.XnaModel.CopyAbsoluteBoneTransformsTo(transforms);

                Matrix transformationMatrixFlippedAsNeeded = model.TransformationMatrix;

                foreach (ModelMeshXna mesh in model.XnaModel.Meshes)
                {
                    for( int iCurEffect = 0; iCurEffect < mesh.Effects.Count; ++iCurEffect )
                    {
                        GenericEffect effect = new GenericEffect( mesh.Effects[ iCurEffect ] );
                        ApplyColorOperation(model, effect);


                        effect.EnableDefaultLighting();

                        // Set this to false and all is fixed magically!
                        effect.VertexColorEnabled = false;

                        SpriteManager.Camera.SetDeviceViewAndProjection(effect, false);

                        // World can be used to set the mesh's transform
                        effect.World = transforms[mesh.ParentBone.Index] * transformationMatrixFlippedAsNeeded;
                    }


                    mesh.Draw();


                }
            }
            else if (model.CustomModel != null)
            {
                RenderCustomModel(model);
            }
#else
            #region If using Custom Effect

            if (model.CustomEffect != null)
            {

                // Set technique here if using custom effect
                model.CustomEffect.SetParameterValues();
                EffectTechnique technique = model.EffectCache.GetTechnique(
                    model.CustomEffect.Effect, Renderer.EffectTechniqueNames[(int)renderMode]);
                if (technique == null && renderMode == RenderMode.Default)
                {
                    technique = model.CustomEffect.Effect.Techniques[0];
                }

                if (technique != null)
                {
                    // Draw meshes only if custom effect has the required technique
                    model.CustomEffect.Effect.CurrentTechnique = technique;
                    DrawModelMeshes(camera, model, renderMode);
                }

            }



            #endregion
            else if (model.CustomModel != null)
            {
                RenderCustomModel(model);

            }
            else
            {
                // Just draw the meshes
                DrawModelMeshes(camera, model, renderMode);
            }
#endif
            #endregion

            //TimeManager.SumTimeSection("DrawModelMeshes");

            if (model.mDrawWireframe)
            {
#if XNA4
                throw new NotImplementedException();
#else
                FlatRedBallServices.GraphicsDevice.RenderState.FillMode = FillMode.Solid;
#endif
            }
        }
Пример #2
0
        private static void ApplyLighting(PositionedModel model, GenericEffect effect)
        {
            if (!LightManager.EnableLighting)
            {
                effect.LightingEnabled = false;

            }
            else if (AvailableLights != null && AvailableLights.Count > 0)
            {
                //effect.EnableDefaultLighting();
                //Start by turning off all the lights.
                effect.LightingEnabled = true;
                effect.DirectionalLight0.Enabled = effect.DirectionalLight1.Enabled = effect.DirectionalLight2.Enabled = false;

                //This tracks which directional light in the shader we are working with.
                int effectLightIndex = 0;
                //Loop through all lights
                for (int i = 0; i < AvailableLights.Count; i++)
                {

                    LightBase baseLight = AvailableLights[i];

                    if (baseLight.Enabled)
                    {

                        if (baseLight is AmbientLight)
                        {
                            SetAmbientLight(effect, baseLight);
                        }
                        else
                        {
                            Microsoft.Xna.Framework.Graphics.DirectionalLight directionalLight;
                            if (effectLightIndex == 0)
                                directionalLight = effect.DirectionalLight0;
                            else if (effectLightIndex == 1)
                                directionalLight = effect.DirectionalLight1;
                            else
                                directionalLight = effect.DirectionalLight2;

                            SetDirectionalLight(directionalLight, baseLight, model.Position);
                            effectLightIndex++;
                        }
                    }
                }

            }
            else
            {
                effect.EnableDefaultLighting();
                effect.DirectionalLight0.Enabled = effect.DirectionalLight1.Enabled = effect.DirectionalLight2.Enabled = false;
                effect.AmbientLightColor = Vector3.Zero;
            }
        }