/// <summary>
        /// Adds a render instance to have its material updated.
        /// </summary>
        /// <param name="instance">The render instance.</param>
        public void AddElement(RenderInstance instance)
        {
            IReflectionMaterial reflectionMaterial = instance.Material as IReflectionMaterial;
            Assert.Fatal(reflectionMaterial != null, "ReflectionManager.AddElement - Attempting to add non IReflectionMaterial.");

            ReflectionData reflection;
            reflection._transform = instance.ObjectTransform;
            reflection._bounds = instance.WorldBox;
            reflection._material = reflectionMaterial;
            _reflectionList.Add(reflection);
        }
        protected override void _SetupObjectParameters(RenderInstance renderInstance, SceneRenderState srs)
        {
            base._SetupObjectParameters(renderInstance, srs);

            EffectManager.SetParameter(_worldViewProjectionParameter, renderInstance.ObjectTransform * srs.View * srs.Projection);
            EffectManager.SetParameter(_opacityParameter, renderInstance.Opacity);

            ITextureMaterial material = renderInstance.Material as ITextureMaterial;
            if (material != null && !material.Texture.IsNull)
                EffectManager.SetParameter(_baseTextureParameter, material.Texture.Instance);
        }
        protected override void _SetupObjectParameters(RenderInstance renderInstance, SceneRenderState srs)
        {
            base._SetupObjectParameters(renderInstance, srs);

            BasicEffect basicEffect = _effect.Instance as BasicEffect;
            if (basicEffect != null)
                basicEffect.World = renderInstance.ObjectTransform;

            else
            {
                EffectParameter worldParam = _effect.Instance.Parameters["World"];
                worldParam.SetValue(srs.World.Top);
            }
        }
        protected override void _SetupObjectParameters(RenderInstance renderInstance, SceneRenderState srs)
        {
            base._SetupObjectParameters(renderInstance, srs);

            EffectManager.SetParameter(_worldMatrixParameter, renderInstance.ObjectTransform);
            EffectManager.SetParameter(_worldViewProjectionParameter, renderInstance.ObjectTransform * srs.View * srs.Projection);
        }
        // Performs per-object parameter setup.
        protected override void _SetupObjectParameters(RenderInstance renderInstance, SceneRenderState srs)
        {
            base._SetupObjectParameters(renderInstance, srs);

            // We'll access some instance data here soon.
        }
Пример #6
0
        /// <summary>
        /// Sets up the draw primitive for a specified scene render state and vertex information.
        /// </summary>
        /// <param name="srs">The scene render state.</param>
        /// <param name="_vb">The vertex buffer to draw with.</param>
        /// <param name="_ib">The index buffer to draw with.</param>
        /// <param name="numVerts">The number of vertices in the vertex buffer.</param>
        /// <param name="startOffset">The vertex index to start drawing at.</param>
        /// <param name="vertSize">The size</param>
        /// <param name="decl">The vertex declaration.</param>
        public static void Set(VertexBuffer vb, IndexBuffer ib, int numVerts, int startOffset, int vertSize, VertexDeclaration decl)
        {
            Assert.Fatal(vb != null, "DrawPrimitive.Set - Invalid vertex buffer!");
            Assert.Fatal(ib != null, "DrawPrimitive.Set - Invalid index buffer!");

            Assert.Fatal(_curRenderInst == null, "DrawPrimitive.Set - Current render instance is not null, a cleanup call was missed.");

            _curRenderInst = SceneRenderer.RenderManager.AllocateInstance();

            _curRenderInst.VertexSize = vertSize;
            _curRenderInst.VertexDeclaration = decl;

            _curRenderInst.VertexBuffer = vb;
            _curRenderInst.IndexBuffer = ib;

            _curRenderInst.VertexCount = numVerts;
            _curRenderInst.BaseVertex = startOffset;
        }
Пример #7
0
        /// <summary>
        /// Finalizes the render instance and submits it to the render manager.
        /// </summary>
        /// <param name="drawType">The primitive type to render as.</param>
        /// <param name="startIndex">The index in the index buffer to start at.</param>
        /// <param name="numElements">The number of elements to draw.</param>
        public static void Render(uint drawType, int startIndex, int numElements)
        {
            Assert.Fatal(drawType == DrawPrimitive.Strip || drawType == DrawPrimitive.Triangles, "DrawPrimitive.Render - DrawType must be Strip or Triangles.");

            PrimitiveType prim;

            if (drawType == DrawPrimitive.Strip)
            {
                prim = PrimitiveType.TriangleStrip;
                numElements -= 2; // convert strip length to triangle count
            }
            else
            {
                prim = PrimitiveType.TriangleList;
                numElements /= 3; // convert from vertex count to triangle count
            }

            _curRenderInst.PrimitiveType = prim;
            _curRenderInst.PrimitiveCount = numElements;
            _curRenderInst.StartIndex = startIndex;

            SceneRenderer.RenderManager.AddInstance(_curRenderInst);

            // Now, copy this render instance to support multiple renderings off the same shape
            RenderInstance ri = SceneRenderer.RenderManager.AllocateInstance();
            ri.Copy(_curRenderInst);
            _curRenderInst = ri;
        }
Пример #8
0
 /// <summary>
 /// Clears the draw primitive so it can be used with a new set of vertex data.
 /// </summary>
 public static void Clear()
 {
     _matIndex = 0xFFFFFFFF;
     SceneRenderer.RenderManager.FreeInstance(_curRenderInst);
     _curRenderInst = null;
 }
Пример #9
0
        /// <summary>
        /// Sets up the draw util.
        /// </summary>
        public static void Setup()
        {
            // set up the default gui material here
            _defaultMaterial = new Materials.SimpleMaterial();
            _defaultMaterial.TextureFilename = null;
            _defaultMaterial.IsTranslucent = true;

            // set up the default render instance to be used when rendering rects
            _rectRenderInstance = new RenderInstance();
            _rectRenderInstance.ObjectTransform = Matrix.Identity;

            // set up the vertex buffer
            _CreateVertexBuffer();
        }
        protected override void _SetupObjectParameters(RenderInstance renderInstance, SceneRenderState srs)
        {
            base._SetupObjectParameters(renderInstance, srs);

            EffectManager.SetParameter(_worldViewProjectionParameter, renderInstance.ObjectTransform * srs.View * srs.Projection);

            if (IsLightingEnabled && srs.Gfx.ShaderProfile >= ShaderProfile.PS_2_0)
            {
                EffectManager.SetParameter(_worldMatrixParameter, renderInstance.ObjectTransform);

                List<Light> lights = srs.SceneGraph.GetLights(renderInstance.WorldBox, MaxSupportedLights);

                int count = lights.Count;
                EffectManager.SetParameter(_lightCountParameter, count);

                // get light data into the arrays to be passed to the shader:
                // here we have to fill the unused light slots with empty data
                // to allow the shader to work on certain video cards
                int i = 0;

                for (; i < count; i++)
                {
                    _positions[i] = lights[i].Position;
                    _colors[i] = lights[i].DiffuseColor;
                    _ambients[i] = lights[i].AmbientColor;
                    _attenuations[i].X = lights[i].ConstantAttenuation;
                    _attenuations[i].Y = lights[i].LinearAttenuation;
                }
                for (; i < MaxSupportedLights; i++)
                {
                    _positions[i] = Vector3.Zero;
                    _colors[i] = Vector3.One;
                    _ambients[i] = Vector3.One;
                    _attenuations[i].X = 1.0f;
                    _attenuations[i].Y = 1.0f;
                }

                if (count > 0)
                {
                    EffectManager.SetParameter(_lightPositionParameter, _positions);
                    EffectManager.SetParameter(_lightDiffuseParameter, _colors);
                    EffectManager.SetParameter(_lightAmbientParameter, _ambients);
                    EffectManager.SetParameter(_lightAttenuationParameter, _attenuations);
                }
            }
        }
Пример #11
0
        public void FillRenderInstance(RenderInstance ri, float opacity, SceneRenderState srs)
        {
            GraphicsDevice d3d = srs.Gfx.Device;
            Assert.Fatal(d3d != null, "doh");

            ri.Type = RenderInstance.RenderInstanceType.Mesh2D;

            ri.ObjectTransform = srs.World.Top;

            ri.PrimitiveType = PrimitiveType.TriangleFan;
            ri.VertexDeclaration = GFXVertexFormat.GetVertexDeclaration(srs.Gfx.Device);

            ri.VertexBuffer = _vb.Buffer as VertexBuffer;
            ri.BaseVertex = _vb.StartIndex;
            ri.VertexCount = _vb.Count;
            ri.VertexSize = _vb.ElementSize;

            ri.PrimitiveCount = 2;

            ri.Opacity = opacity;
            ri.UTextureAddressMode = _texU;
            ri.VTextureAddressMode = _texV;

            ri.Material = _material;
        }
 /// <summary>
 /// This is called once on the material for each render instance each pass. It should be used to set
 /// the values for any effect parameters that are unique to each object using the material. The base
 /// method does nothing.
 /// </summary>
 /// <param name="renderInstance">The next render instance to be rendered with this material.</param>
 /// <param name="srs">The current scene render state.</param>
 protected virtual void _SetupObjectParameters(RenderInstance renderInstance, SceneRenderState srs)
 {
 }
        /// <summary>
        /// Sets up any object specific data on the material using the specified render instance.
        /// </summary>
        /// <param name="ri">The render instance that is about to be rendered.</param>
        /// <param name="srs">The current render state of the scene.</param>
        public virtual void SetupObject(RenderInstance ri, SceneRenderState srs)
        {
            if (_previousU != ri.UTextureAddressMode)
            {
                srs.Gfx.Device.SamplerStates[0].AddressU = ri.UTextureAddressMode;
                _previousU = ri.UTextureAddressMode;
            }

            if (_previousV != ri.VTextureAddressMode)
            {
                srs.Gfx.Device.SamplerStates[0].AddressV = ri.VTextureAddressMode;
                _previousV = ri.VTextureAddressMode;
            }

            _SetupObjectParameters(ri, srs);
            CommitChanges();
        }
 /// <summary>
 /// Copies a render instance's fields into this render instance.
 /// </summary>
 /// <param name="src">The render instance to copy.</param>
 public void Copy(RenderInstance src)
 {
     VertexBuffer = src.VertexBuffer;
     IndexBuffer = src.IndexBuffer;
     VertexDeclaration = src.VertexDeclaration;
     Material = src.Material;
     ObjectTransform = src.ObjectTransform;
     WorldBox = src.WorldBox;
     UTextureAddressMode = src.UTextureAddressMode;
     VTextureAddressMode = src.VTextureAddressMode;
     VertexSize = src.VertexSize;
     PrimitiveType = src.PrimitiveType;
     BaseVertex = src.BaseVertex;
     VertexCount = src.VertexCount;
     StartIndex = src.StartIndex;
     PrimitiveCount = src.PrimitiveCount;
     Type = src.Type;
     SortPoint = src.SortPoint;
     IsSortPointSet = src.IsSortPointSet;
     Opacity = src.Opacity;
 }
        protected override void _SetupObjectParameters(RenderInstance renderInstance, SceneRenderState srs)
        {
            base._SetupObjectParameters(renderInstance, srs);

            EffectManager.SetParameter(_sasWorldMatrixCount, 1);
            EffectManager.SetParameter(_sasWorldMatrix, renderInstance.ObjectTransform);
            EffectManager.SetParameter(_sasWorldInverseMatrix, Matrix.Invert(renderInstance.ObjectTransform));
            EffectManager.SetParameter(_sasWorldViewMatrix, renderInstance.ObjectTransform * srs.View);
            EffectManager.SetParameter(_sasWorldViewProjectionMatrix, renderInstance.ObjectTransform * srs.View * srs.Projection);

            List<Light> lights = srs.SceneGraph.GetLights(renderInstance.WorldBox, 8);
            int count = lights.Count;

            EffectManager.SetParameter(_sasLightCount, count);

            for (int i = 0, pi = 0, di = 0; i < count; i++)
            {
                Light light = lights[i];

                _SetParameter(_sasLights.SasLightColor, i, new Vector4(light.AmbientColor, 1.0f));

                if (light is PointLight)
                {
                    _SetParameter(_sasPointLights.SasLightColor, pi, new Vector4(light.DiffuseColor, 1.0f));
                    _SetParameter(_sasPointLights.SasLightRange, pi, 100.0f / light.ConstantAttenuation);
                    _SetParameter(_sasPointLights.SasLightPosition, pi, light.Position);
                    pi++;
                }
                else if (light is DirectionalLight)
                {
                    _SetParameter(_sasDirectionalLights.SasLightColor, di, new Vector4(light.DiffuseColor, 1.0f));
                    _SetParameter(_sasDirectionalLights.SasLightRange, di, 10000000.0f);
                    _SetParameter(_sasDirectionalLights.SasLightPosition, di, light.Position);
                    _SetParameter(_sasDirectionalLights.SasLightDirection, di, (light as DirectionalLight).Direction);
                    di++;
                }
            }
        }
        protected override void _SetupObjectParameters(RenderInstance renderInstance, SceneRenderState srs)
        {
            base._SetupObjectParameters(renderInstance, srs);

            EffectManager.SetParameter(_worldViewProjectionParameter, renderInstance.ObjectTransform * srs.View * srs.Projection);

            if (IsTranslucent)
                EffectManager.SetParameter(_opacityParameter, renderInstance.Opacity * _opacity);
        }
Пример #17
0
        /// <summary>
        /// Draws a stretched sub-region of a texture.
        /// </summary>
        /// <param name="material">The material used when rendering. The material must implement ITextureMaterial.</param>
        /// <param name="dstRect">Rectangle where the texture object will be drawn.</param>
        /// <param name="srcRect">Sub-region of the texture that will be applied over the <paramref name="dstRect"/>.</param>
        /// <param name="flipMode">Any flipping to be done of the source texture.</param>
        public static void BitmapStretchSR(RenderMaterial material, RectangleF dstRect, RectangleF srcRect, BitmapFlip flipMode)
        {
            // setup render state
            GUICanvas.Instance.RenderState.World.LoadIdentity();
            GUICanvas.Instance.RenderState.View = Matrix.Identity;
            GUICanvas.Instance.RenderState.Projection = _clipMatrix;

            // setup the material
            material.SetupEffect(GUICanvas.Instance.RenderState, null);

            _texLeft = srcRect.X / ((Texture2D)((ITextureMaterial)material).Texture.Instance).Width;
            _texRight = (srcRect.X + srcRect.Width) / ((Texture2D)((ITextureMaterial)material).Texture.Instance).Width;
            _texTop = srcRect.Y / ((Texture2D)((ITextureMaterial)material).Texture.Instance).Height;
            _texBottom = (srcRect.Y + srcRect.Height) / ((Texture2D)((ITextureMaterial)material).Texture.Instance).Height;

            _screenLeft = dstRect.X;
            _screenRight = dstRect.X + dstRect.Width;
            _screenTop = dstRect.Y;
            _screenBottom = dstRect.Y + dstRect.Height;

            // flip x
            if ((flipMode & BitmapFlip.FlipX) != 0)
            {
                float temp = _texLeft;
                _texLeft = _texRight;
                _texRight = temp;
            }

            // flip y
            if ((flipMode & BitmapFlip.FlipY) != 0)
            {
                float temp = _texTop;
                _texTop = _texBottom;
                _texBottom = temp;
            }

            color = _bitmapModulation;

            _vertexSet4[0].Position = new Vector3(_screenLeft - 0.5f, _screenTop - 0.5f, 0.0f);
            _vertexSet4[0].TextureCoordinate = new Vector2(_texLeft, _texTop);
            _vertexSet4[0].Color = color;

            _vertexSet4[1].Position = new Vector3(_screenRight - 0.5f, _screenTop - 0.5f, 0.0f);
            _vertexSet4[1].TextureCoordinate = new Vector2(_texRight, _texTop);
            _vertexSet4[1].Color = color;

            _vertexSet4[2].Position = new Vector3(_screenLeft - 0.5f, _screenBottom - 0.5f, 0.0f);
            _vertexSet4[2].TextureCoordinate = new Vector2(_texLeft, _texBottom);
            _vertexSet4[2].Color = color;

            _vertexSet4[3].Position = new Vector3(_screenRight - 0.5f, _screenBottom - 0.5f, 0.0f);
            _vertexSet4[3].TextureCoordinate = new Vector2(_texRight, _texBottom);
            _vertexSet4[3].Color = color;

            // adltodo: hacks
            _workingRenderInstance = SceneRenderer.RenderManager.AllocateInstance();
            _workingRenderInstance.ObjectTransform = Matrix.Identity;

            GUICanvas.Instance.RenderState.Gfx.Device.VertexDeclaration = GFXVertexFormat.GetVertexDeclaration(GUICanvas.Instance.RenderState.Gfx.Device);

            // draw the vertices
            while (material.SetupPass())
            {
                material.SetupObject(_workingRenderInstance, GUICanvas.Instance.RenderState);

                GUICanvas.Instance.RenderState.Gfx.Device.DrawUserPrimitives<GFXVertexFormat.PCTTBN>(PrimitiveType.TriangleStrip, _vertexSet4, 0, 2);
            }

            // cleanup the material
            material.CleanupEffect();
            SceneRenderer.RenderManager.FreeInstance(_workingRenderInstance);
        }