Esempio n. 1
0
        private void InitializeParameters()
        {
            paletteParam      = Parameters["MatrixPalette"];
            texParam          = Parameters["BasicTexture"];
            texEnabledParam   = Parameters["TextureEnabled"];
            worldParam        = Parameters["World"];
            viewParam         = Parameters["View"];
            projectionParam   = Parameters["Projection"];
            ambientParam      = Parameters["AmbientLightColor"];
            eyeParam          = Parameters["EyePosition"];
            emissiveParam     = Parameters["EmissiveColor"];
            lightEnabledParam = Parameters["LightingEnable"];
            diffuseParam      = Parameters["DiffuseColor"];
            specColorParam    = Parameters["SpecularColor"];
            specPowerParam    = Parameters["SpecularPower"];
            alphaParam        = Parameters["Alpha"];
            fogColorParam     = Parameters["FogColor"];
            fogEnabledParam   = Parameters["FogEnable"];
            fogStartParam     = Parameters["FogStart"];
            fogEndParam       = Parameters["FogEnd"];
            light0            = new BasicDirectionalLight(this, 0);
            light1            = new BasicDirectionalLight(this, 1);
            light2            = new BasicDirectionalLight(this, 2);

            FogColor   = Vector3.Zero;
            FogStart   = 0;
            FogEnd     = 1;
            FogEnabled = false;
        }
Esempio n. 2
0
        protected override void OnDraw(RenderTracer renderTracer)
        {
            RenderState renderState = renderTracer.Device.RenderState;

            //  Transform culling sphere
            if (enableCulling)
            {
                cullingSphere.Center = Vector3.Transform(cullingSphereLocalCenter,
                                                         RootAxis * Collide.TransformMatrix);

                //  Check if the the model is contained inside or intersecting
                //  with the frustum.
                ContainmentType cullingResult =
                    renderTracer.Frustum.Contains(cullingSphere);

                //  Draw a the model If inside in the frustum
                if (cullingResult == ContainmentType.Disjoint)
                {
                    return;
                }
            }

            renderState.AlphaTestEnable  = alphaTestEnable;
            renderState.AlphaBlendEnable = alphaBlendEnable;
            renderState.AlphaFunction    = alphaFunction;
            renderState.SourceBlend      = sourceBlend;
            renderState.DestinationBlend = destinationBlend;
            renderState.BlendFunction    = blendFunction;

            renderState.ReferenceAlpha         = referenceAlpha;
            renderState.DepthBufferEnable      = depthBufferEnable;
            renderState.DepthBufferWriteEnable = depthBufferWriteEnable;
            renderState.DepthBufferFunction    = depthBufferFunction;
            renderState.CullMode = cullMode;

            // Draw the model.
            for (int i = 0; i < ModelData.model.Meshes.Count; i++)
            {
                ModelMesh mesh = ModelData.model.Meshes[i];

                for (int j = 0; j < mesh.Effects.Count; j++)
                {
                    //  call a entried custom effect processing
                    if (RenderingCustomEffect != null)
                    {
                        //  Shader custom processing
                        RenderingCustomEffect(this,
                                              new RenderingCustomEffectEventArgs(renderTracer, mesh,
                                                                                 mesh.Effects[j], BoneTransforms[mesh.ParentBone.Index]));
                    }
                    else if (mesh.Effects[j] is BasicEffect)
                    {
                        BasicEffect effect = (BasicEffect)mesh.Effects[j];

                        //  Apply fog
                        if (renderTracer.Fog != null && ActiveFog)
                        {
                            RenderFog fog = renderTracer.Fog;

                            effect.FogEnabled = fog.enabled;

                            if (effect.FogEnabled)
                            {
                                effect.FogStart = fog.start;
                                effect.FogEnd   = fog.end;
                                effect.FogColor = fog.color.ToVector3();
                            }
                        }
                        else
                        {
                            effect.FogEnabled = false;
                        }

                        if (ActiveLighting)
                        {
                            //  Apply lighting
                            if (renderTracer.Lighting != null)
                            {
                                RenderLighting lighting = renderTracer.Lighting;

                                effect.LightingEnabled = lighting.enabled;

                                if (effect.LightingEnabled)
                                {
                                    effect.AmbientLightColor =
                                        lighting.ambientColor.ToVector3();

                                    effect.DirectionalLight0.Enabled = true;

                                    effect.DirectionalLight0.Direction =
                                        lighting.direction;

                                    effect.DirectionalLight0.DiffuseColor =
                                        lighting.diffuseColor.ToVector3();

                                    effect.DirectionalLight0.SpecularColor =
                                        lighting.specularColor.ToVector3();
                                }
                            }

                            if (Lighting != null)
                            {
                                effect.LightingEnabled = true;

                                for (int cnt = 0; cnt < Lighting.Length; cnt++)
                                {
                                    RenderLighting        lighting   = Lighting[cnt];
                                    BasicDirectionalLight basicLight = null;

                                    if (cnt == 0)
                                    {
                                        basicLight = effect.DirectionalLight1;
                                    }
                                    else if (cnt == 1)
                                    {
                                        basicLight = effect.DirectionalLight2;
                                    }
                                    else
                                    {
                                        continue;
                                    }

                                    if (lighting.enabled)
                                    {
                                        basicLight.Enabled = true;

                                        basicLight.Direction =
                                            lighting.direction;

                                        basicLight.DiffuseColor =
                                            lighting.diffuseColor.ToVector3();

                                        basicLight.SpecularColor =
                                            lighting.specularColor.ToVector3();
                                    }
                                    else
                                    {
                                        basicLight.Enabled = false;
                                    }
                                }
                            }

                            if (renderTracer.Lighting == null && Lighting == null)
                            {
                                effect.LightingEnabled = false;
                            }

                            //  Apply material
                            if (Material != null)
                            {
                                effect.Alpha = Material.alpha;

                                effect.DiffuseColor =
                                    Material.diffuseColor.ToVector3();

                                effect.SpecularColor =
                                    Material.specularColor.ToVector3();

                                effect.SpecularPower =
                                    Material.specularPower;

                                effect.EmissiveColor =
                                    Material.emissiveColor.ToVector3();

                                effect.VertexColorEnabled =
                                    Material.vertexColorEnabled;

                                effect.PreferPerPixelLighting =
                                    Material.preferPerPixelLighting;
                            }
                        }
                        else
                        {
                            effect.LightingEnabled = false;
                        }

                        //  Apply transform
                        effect.World      = BoneTransforms[mesh.ParentBone.Index];
                        effect.View       = renderTracer.View;
                        effect.Projection = renderTracer.Projection;
                    }
                }

                mesh.Draw();
            }
        }