Exemple #1
0
        private Render renderer; // Class to render the scene.

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Method to setup the rendering device.
        /// Also initalizes all other variables used.
        /// </summary>
        /// <param name="panel">Panel to be used for rendering.</param>
        public ResourceManager(Panel panel)
        {
            setupDevice(panel);

            camera = new Camera(ref device);
            camera.SetCamera((float)panel.Width, (float)panel.Height);

            mesh = new List<Model>();
            lights = new List<LightObj>();

            // Add two blank meshes. One for the ground plane and one for the first model loaded.
            groundPlane = new GroundPlane(device, effect);
            mesh.Add(new Model(device, effect));

            // Add the 3 lights.
            lights.Add(new LightObj(device, effect, true));
            lights.Add(new LightObj(device, effect, false));
            lights.Add(new LightObj(device, effect, false));

            renderer = new Render(ref device, ref effect, ref mesh, ref groundPlane, panel.Width, panel.Height);
        }
Exemple #2
0
        /// <summary>
        /// Sets the variables within the shader code.
        /// </summary>
        /// <param name="light">List of lights used.</param>
        /// <param name="camera">The camera.</param>
        private void SetVariables(List<LightObj> light, Camera camera)
        {
            // Create an array of the 3 lights intensities.
            float[] lightIntensity = new float[3];
            int[] enableShadows = new int[3];
            int[] enableLights = new int[3];

            for (int i = 0; i < 3; i++)
            {
                lightIntensity[i] = light[i].Intensity;
                enableShadows[i] = Convert.ToInt32(light[i].Shadows);
                enableLights[i] = Convert.ToInt32(light[i].Enabled);
            }

            // Create an array of the 3 lights positions.
            Vector4[] lightPositions = new Vector4[3];
            for (int i = 0; i < 3; i++)
                lightPositions[i] = new Vector4(light[i].Direction.X, light[i].Direction.Y, light[i].Direction.Z, 1.0f);

            Vector4[] lightAmbient = new Vector4[3];
            Vector4[] lightDiffuse = new Vector4[3];
            Vector4[] lightSpecular = new Vector4[3];

            // Copy over the light colors to different vector4's
            for (int i = 0; i < 3; i++)
            {
                lightAmbient[i] = new Vector4(light[i].Ambient.R / 255.0f, light[i].Ambient.G / 255.0f, light[i].Ambient.B / 255.0f, 1.0f);
                lightDiffuse[i] = new Vector4(light[i].Diffuse.R / 255.0f, light[i].Diffuse.G / 255.0f, light[i].Diffuse.B / 255.0f, 1.0f);
                lightSpecular[i] = new Vector4(light[i].Specular.R / 255.0f, light[i].Specular.G / 255.0f, light[i].Specular.B / 255.0f, 1.0f);
            }

            // Set the world, project and view matrixes in the shader
            effect.SetValue("projMat", camera.Projection);
            effect.SetValue("viewMat", camera.View);
            effect.SetValue("worldMat", Matrix.Identity);

            // Set the emissive texture
            effect.SetValue("glowMap", renderedGlow);

            // Sets the light and camera's position in the shader.
            effect.SetValue("lightPos", lightPositions);
            effect.SetValue("enableLight", enableLights);
            effect.SetValue("enableShadows", enableShadows);
            effect.SetValue("eyePos", new float[] { camera.Position.X, camera.Position.Y, camera.Position.Z });

            // Setup the light information
            effect.SetValue("lightIntensity", lightIntensity);
            effect.SetValue("lightAmbientColor", lightAmbient);
            effect.SetValue("lightDiffuseColor", lightDiffuse);
            effect.SetValue("lightSpecularColor", lightSpecular);
        }
Exemple #3
0
        /// <summary>
        /// Renders the scene.
        /// </summary>
        public void RenderScene(Color background, List<LightObj> lights, Camera camera)
        {
            bool useGlow = false;

            // Sets the variables for the shader.
            SetVariables(lights, camera);
            //effect.Technique = "Test";

            for (int i = 0; i < mesh.Count; i++)
            {
                if (mesh[i].Emissive.Enable && !mesh[i].Emissive.Path.Equals(""))
                {
                    useGlow = true;
                    break;
                }
            }

            // If/Else statement to control rendering with emissive glow or not.
            if (useGlow)
                RenderGlow();
            else
                BeginRender(background);

            // If emissive glow is used, the base scene is rendered to a texture.
            if (useGlow)
            {
                surfRender.BeginScene(renderedScene.GetSurfaceLevel(0), device.Viewport);
                device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, background, 1.0f, 0);
            }

            if (groundPlane.Enabled)
                RenderModel((Model)groundPlane, lights[0].Shadows, 0);

            // Loops through all the models and renders each.
            for (int i = 0; i < mesh.Count; i++)
            {
                if (mesh[i].Enabled)
                    RenderModel(mesh[i], lights[0].Shadows, 0);
            }

            if (showLights)
            {
                using (Sprite spriteobject = new Sprite(device))
                {
                    foreach (LightObj light in lights)
                    {
                        if (!light.Enabled)
                            continue;

                        spriteobject.SetWorldViewRH(device.Transform.World, device.Transform.View);

                        spriteobject.Begin(SpriteFlags.AlphaBlend | SpriteFlags.Billboard | SpriteFlags.SortTexture | SpriteFlags.ObjectSpace);
                        //spriteobject.Transform = Matrix.Scaling(0.25f, 0.25f, 0.25f);
                        spriteobject.Draw(lightTexture, Rectangle.Empty, Vector3.Empty, light.Direction, Color.White);
                        spriteobject.End();
                    }
                }
            }

            // If emissive glow is used, the textures are set to the shader and rendered to a sprite.
            if (useGlow)
            {
                surfRender.EndScene(Filter.None);

                effect.SetValue("renderedScene", renderedScene);

                BeginRender(background);

                using (Sprite spriteobject = new Sprite(device))
                {
                    prevTechnique = effect.Technique;
                    effect.Technique = "Bloom";

                    effect.Begin(FX.None);

                    spriteobject.Begin(SpriteFlags.None);

                    effect.BeginPass(0);

                    spriteobject.Draw(renderedGlow, Rectangle.Empty, new Vector3(0, 0, 0), new Vector3(0, 0, 0), Color.White);

                    effect.EndPass();

                    spriteobject.End();

                    effect.End();

                    effect.Technique = prevTechnique;
                }
            }

            EndRender();
        }