Example #1
0
        public void Draw(IShaderConstantsSetter shaderConstantsSetter)
        {
            game.Renderer.Draw(this, shaderConstantsSetter);

            if (showAfterBurnerEffects)
            {
                foreach (AfterBurner afterBurner in afterBurnerEffects)
                {
                    afterBurner.Draw(null);
                }
            }
        }
Example #2
0
        public void Draw(IModel model, IShaderConstantsSetter shaderConstantsSetter)
        {
            renderToScreen(model, shaderConstantsSetter);

            if (drawCollisionShapes && !renderDepth)
            {
                if (sphereMesh == null)
                {
                    InitCollisionShapeVisualization();
                }

                foreach (ShapeData shape in model.CollisionShapes)
                {
                    DrawCollisionShape(shape, model.GlobalTransformation);
                }
            }
        }
Example #3
0
        private void renderToScreen(IModel model, IShaderConstantsSetter shaderConstantsSetter)
        {
            if (model.Meshes.Count != model.Materials.Count)
            {
                throw new Exception("Model " + model.Name + " has " + model.Meshes.Count + " meshes and " + model.Materials.Count + " materials. These numbers must be the same.");
            }

            //Camera = game.World.Sky.Sunlight.ShadowMapLow.Scene.Camera;
            for (int i = 0; i < model.Meshes.Count; i++)
            {
                // current mesh
                IMesh mesh = model.Meshes[i];

                // current material
                IMaterial material = model.Materials[i];

                // calculate the matrices
                Matrix world          = mesh.GlobalTransformation;
                Matrix view           = camera.ViewMatrix;
                Matrix projection     = camera.ProjectionMatrix;
                Matrix wvp            = world * view * projection;
                Matrix viewProjection = view * projection;

                SetDefaultRenderStates();

                material.SetupForRendering();

                Effect effect = renderDepth ? game.Graphics.ShaderSelector.GetSuitableDepthShader(material.Effect.CurrentTechnique) : material.Effect;

                // set effect parameters
                if (effect.GetType().Name == "BasicEffect")
                {
                    if (renderDepth)
                    {
                        throw new Exception("There is no depth technique in BasicEffect.");
                    }

                    BasicEffect beffect = (material.Effect as BasicEffect);
                    beffect.World      = world;
                    beffect.View       = view;
                    beffect.Projection = projection;
                    beffect.EnableDefaultLighting();
                }
                else
                {
                    foreach (EffectParameter parameter in effect.Parameters)
                    {
                        if (parameter.Semantic != null)
                        {
                            if (parameter.Semantic == "WORLDVIEWPROJECTION")
                            {
                                parameter.SetValue(wvp);
                            }
                            else if (parameter.Semantic == "WORLD")
                            {
                                parameter.SetValue(world);
                            }
                            else if (parameter.Semantic == "VIEW")
                            {
                                parameter.SetValue(view);
                            }
                            else if (parameter.Semantic == "PROJECTION")
                            {
                                parameter.SetValue(projection);
                            }
                            else if (parameter.Semantic == "VIEWPROJECTION")
                            {
                                parameter.SetValue(viewProjection);
                            }

                            else if (parameter.Semantic == "CAMERA_POSITION")
                            {
                                parameter.SetValue(camera.GlobalPosition);
                            }

                            else if (parameter.Semantic == "MATERIAL_EMISSIVE")
                            {
                                parameter.SetValue(
                                    new Vector3(material.EmissiveColor.R / 255.0f, material.EmissiveColor.G / 255.0f,
                                                material.EmissiveColor.B / 255.0f));
                            }
                            else if (parameter.Semantic == "MATERIAL_DIFFUSE")
                            {
                                parameter.SetValue(
                                    new Vector4(material.DiffuseColor.R / 255.0f, material.DiffuseColor.G / 255.0f,
                                                material.DiffuseColor.B / 255.0f, material.DiffuseColor.A / 255.0f));
                            }
                            else if (parameter.Semantic == "MATERIAL_SPECULAR_AND_SHININESS")
                            {
                                parameter.SetValue(
                                    new Vector4(material.SpecularColor.R / 255.0f, material.SpecularColor.G / 255.0f,
                                                material.SpecularColor.B / 255.0f, material.Shininess));
                            }
                            else if (parameter.Semantic == "AMBIENT_LIGHT")
                            {
                                parameter.SetValue(
                                    new Vector3(game.World.AmbientLight.R / 255.0f, game.World.AmbientLight.G / 255.0f,
                                                game.World.AmbientLight.B / 255.0f));
                            }

                            else if (parameter.Semantic == "SUNLIGHT_DIRECTION")
                            {
                                parameter.SetValue(game.World.Sky.Sunlight.Direction);
                            }
                            else if (parameter.Semantic == "SUNLIGHT_COLOR")
                            {
                                parameter.SetValue(
                                    new Vector3(game.World.Sky.Sunlight.Color.R / 255.0f,
                                                game.World.Sky.Sunlight.Color.G / 255.0f,
                                                game.World.Sky.Sunlight.Color.B / 255.0f));
                            }
                            else if (parameter.Semantic == "SUNLIGHT_SHADOWMAP_LOW")
                            {
                                parameter.SetValue(game.World.Sky.Sunlight.ShadowMapLow.Texture);
                            }
                            else if (parameter.Semantic == "SUNLIGHT_SHADOWMAP_LOW_TEXELSIZE")
                            {
                                Texture2D shadowMap = game.World.Sky.Sunlight.ShadowMapLow.Texture;
                                if (shadowMap != null)
                                {
                                    parameter.SetValue(
                                        new Vector2(1.0f / shadowMap.Width,
                                                    1.0f / shadowMap.Height));
                                }
                            }
                            else if (parameter.Semantic == "SUNLIGHT_SHADOWMAP_LOW_MATRIX")
                            {
                                parameter.SetValue(game.World.Sky.Sunlight.ShadowMapLow.ShadowMatrix);
                            }
                            else if (parameter.Semantic == "SUNLIGHT_SHADOWMAP_HIGH")
                            {
                                parameter.SetValue(game.World.Sky.Sunlight.ShadowMapHigh.Texture);
                            }
                            else if (parameter.Semantic == "SUNLIGHT_SHADOWMAP_HIGH_TEXELSIZE")
                            {
                                Texture2D shadowMap = game.World.Sky.Sunlight.ShadowMapHigh.Texture;
                                if (shadowMap != null)
                                {
                                    parameter.SetValue(
                                        new Vector2(1.0f / shadowMap.Width,
                                                    1.0f / shadowMap.Height));
                                }
                            }
                            else if (parameter.Semantic == "SUNLIGHT_SHADOWMAP_HIGH_MATRIX")
                            {
                                parameter.SetValue(game.World.Sky.Sunlight.ShadowMapHigh.ShadowMatrix);
                            }

                            else if (parameter.Semantic == "POINTLIGHT_COUNT")
                            {
                                parameter.SetValue(Math.Min(game.World.PointLights.Count, game.Graphics.MaxPointLights));
                            }
                            else if (parameter.Semantic == "POINTLIGHT_POSITIONS")
                            {
                                Vector3[] positions         = new Vector3[game.Graphics.MaxPointLights];
                                int       currentPositionId = 0;

                                ReadOnlyCollection <IPointLight> lights = game.World.PointLights;

                                for (int k = 0; k < Math.Min(lights.Count, positions.Length); k++)
                                {
                                    positions[currentPositionId++] = lights[k].GlobalPosition;
                                }

                                for (int n = currentPositionId; n < positions.Length; n++)
                                {
                                    positions[n] = new Vector3(0, 0, 0);
                                }

                                parameter.SetValue(positions);
                            }
                            else if (parameter.Semantic == "POINTLIGHT_COLORS")
                            {
                                Vector3[] colors         = new Vector3[game.Graphics.MaxPointLights];
                                int       currentColorId = 0;

                                ReadOnlyCollection <IPointLight> lights = game.World.PointLights;

                                for (int k = 0; k < Math.Min(lights.Count, colors.Length); k++)
                                {
                                    colors[currentColorId++] = lights[k].Color.ToVector3();
                                }

                                for (int n = currentColorId; n < colors.Length; n++)
                                {
                                    colors[n] = new Vector3(0, 0, 0);
                                }

                                parameter.SetValue(colors);
                            }
                            else if (parameter.Semantic == "POINTLIGHT_ATTENUATIONS")
                            {
                                Vector2[] attenuations         = new Vector2[game.Graphics.MaxPointLights];
                                int       currentAttenuationId = 0;

                                ReadOnlyCollection <IPointLight> lights = game.World.PointLights;

                                for (int k = 0; k < Math.Min(lights.Count, attenuations.Length); k++)
                                {
                                    attenuations[currentAttenuationId++] =
                                        new Vector2(lights[k].SquaredAttenuation, lights[k].LinearAttenuation);
                                }

                                for (int n = currentAttenuationId; n < attenuations.Length; n++)
                                {
                                    attenuations[n] = new Vector2(0, 0);
                                }

                                parameter.SetValue(attenuations);
                            }

                            else if (parameter.Semantic.Length > 7 && parameter.Semantic.Substring(0, 7) == "TEXTURE")
                            {
                                int id = Convert.ToInt32(parameter.Semantic.Substring(7, 1));
                                parameter.SetValue(material.Textures[id]);
                            }

                            else if (parameter.Semantic == "FOG")
                            {
                                parameter.SetValue(game.Graphics.AllowFogShaders);
                            }

                            else if (shaderConstantsSetter != null)
                            {
                                shaderConstantsSetter.SetShaderConstant(parameter);
                            }
                        }
                    }
                }

                mesh.Effect = effect;

                mesh.Draw(game.Graphics.Device);
            }
        }