public override void Render(int frame, int matFrame, Material[] materialList, SceneRenderState srs)
        {
            Vector3 cameraCenter = srs.CameraPosition;

            DrawPrimitive.Set(_vb.Instance, _ib.Instance, _verts.Length, 0, GFXVertexFormat.VertexSize, GFXVertexFormat.GetVertexDeclaration(srs.Gfx.Device));

            int nextCluster = _startCluster;
            do
            {
                // Render the cluster...
                for (int i = _clusters[nextCluster].StartPrimitive; i < _clusters[nextCluster].EndPrimitive; i++)
                {
                    DrawPrimitive.SetMaterial(_primitives[i].MaterialIndex & DrawPrimitive.MaterialMask, materialList, ref _bounds, srs);
                    DrawPrimitive.Render(_primitives[i].MaterialIndex & DrawPrimitive.TypeMask, _primitives[i].Start, _primitives[i].NumElements);
                }

                // determine Next cluster...
                if (_clusters[nextCluster].FrontCluster != _clusters[nextCluster].BackCluster)
                {
                    float dot = _clusters[nextCluster].Normal.X * cameraCenter.X +
                            _clusters[nextCluster].Normal.Y * cameraCenter.Y +
                            _clusters[nextCluster].Normal.Z * cameraCenter.Z;

                    // this is opposite of TGE (i.e., -dot rather than dot).
                    nextCluster = (-dot > _clusters[nextCluster].K) ? _clusters[nextCluster].FrontCluster : _clusters[nextCluster].BackCluster;
                }
                else
                {
                    nextCluster = _clusters[nextCluster].FrontCluster;
                }
            } while (nextCluster >= 0);

            DrawPrimitive.Clear();
        }
 void _SetMaterialList(Material[] mlist)
 {
     _materialList = mlist;
 }
        void _RenderMesh(ref ObjectInstance obj, int objectDetail, Material[] materialList, SceneRenderState srs)
        {
            if (obj.Visibility > 0.01f)
            {
                mesh = _GetMesh(ref obj.Object, objectDetail);

                if (mesh != null)
                {
                    srs.World.Push();

                    if (obj.Object.NodeIndex >= 0)
                        srs.World.MultiplyMatrixLocal(_nodeTransforms[obj.Object.NodeIndex]);

                    mesh.Render(obj.Frame, obj.MaterialFrame, materialList, srs);

                    srs.World.Pop();
                }
            }
        }
        /// <summary>
        /// Sets the material information for the draw primitive.
        /// </summary>
        /// <param name="material">The material to use.</param>
        /// <param name="bounds">The bounding box of the mesh being rendered.</param>
        /// <param name="srs">The scene render state.</param>
        public static void SetMaterial(ref Material material, ref Box3F bounds, SceneRenderState srs)
        {
            // perform name mapping to replace materials with custom materials
            if (material.Name != null && material._renderMaterial == null)
            {
                String baseName = Shape.CurrentFilePath + @"\" + material.Name;
                material._renderMaterial = MaterialManager.Lookup(baseName);
                if (material._renderMaterial == null)
                {
                    // if no material mapped, then use a default material
                    if (baseName != null)
                    {
                        LightingMaterial effect = new LightingMaterial();
                        effect.LightingMode = LightingMode.PerPixel;
                        effect.TextureFilename = baseName;
                        effect.IsTranslucent = (material.Flags & MaterialFlags.Translucent) != 0;

                        material._renderMaterial = MaterialManager.Add(baseName, effect);
                    }

                    // if we didn't find it this time, don't look it up again
                    if (material._renderMaterial == null)
                        material.Name = null;
                }
            }

            // material info
            _curRenderInst.Material = material._renderMaterial;

            bool translucent = (material.Flags & MaterialFlags.Translucent) != 0;
            _curRenderInst.Type = translucent ? RenderInstance.RenderInstanceType.Translucent2D : RenderInstance.RenderInstanceType.Mesh3D;

            bool sWrap = (material.Flags & MaterialFlags.S_Wrap) != 0;
            _curRenderInst.UTextureAddressMode = sWrap ? TextureAddressMode.Wrap : TextureAddressMode.Clamp;

            bool tWrap = (material.Flags & MaterialFlags.T_Wrap) != 0;
            _curRenderInst.VTextureAddressMode = tWrap ? TextureAddressMode.Wrap : TextureAddressMode.Clamp;

            // transform info
            Matrix transform = srs.World.Top;
            _curRenderInst.WorldBox = Box3F.Transform(ref bounds, ref transform);
            _curRenderInst.ObjectTransform = transform;
        }
        /// <summary>
        /// Sets the material information for the draw primitive. This will do nothing if the same material
        /// index was used with the previous call.
        /// </summary>
        /// <param name="matIndex">The index of the material to use.</param>
        /// <param name="matList">The material list to look up the material in.</param>
        /// <param name="bounds">The bounding box of the mesh being rendered.</param>
        /// <param name="srs">The scene render state.</param>
        public static void SetMaterial(uint matIndex, Material[] matList, ref Box3F bounds, SceneRenderState srs)
        {
            if (matIndex == _matIndex)
                return;

            _matIndex = matIndex;

            SetMaterial(ref matList[matIndex], ref bounds, srs);
        }
        /// <summary>
        /// Renders the mesh at the specified frame with the specified material.
        /// </summary>
        /// <param name="frame">The frame to render at.</param>
        /// <param name="matFrame">The material frame to render at.</param>
        /// <param name="materialList">The list of materials to use.</param>
        /// <param name="srs">The scene render state.</param>
        public virtual void Render(int frame, int matFrame, Material[] materialList, SceneRenderState srs)
        {
            if (_vertsPerFrame <= 0)
                return;

            // do need to recreate the vb and ib?
            if (_vb.IsNull)
                CreateVBIB();

            // compute offset into vertex buffer.  Assume we either Animate _verts or _tverts but not both.
            int startOffset = Math.Max(frame, matFrame) * _vertsPerFrame + _vertOffset;

            bool _useLighting = true;

            if (_useLighting)
                DrawPrimitive.Set(_vb.Instance, _ib.Instance, _vertsPerFrame, startOffset, GFXVertexFormat.VertexSize, GFXVertexFormat.GetVertexDeclaration(srs.Gfx.Device));
            else
                DrawPrimitive.Set(_vb.Instance, _ib.Instance, _vertsPerFrame, startOffset, VertexPositionColorTexture.SizeInBytes, srs.Gfx.GetVertexDeclarationVPCT());

            for (uint i = 0; i < _primitives.Length; i++)
            {
                DrawPrimitive.SetMaterial(_primitives[i].MaterialIndex & DrawPrimitive.MaterialMask, materialList, ref _bounds, srs);
                DrawPrimitive.Render(_primitives[i].MaterialIndex & DrawPrimitive.TypeMask, _primitives[i].Start + _indexOffset, _primitives[i].NumElements);
            }

            DrawPrimitive.Clear();
        }
        public override void Render(int frame, int matFrame, Material[] materialList, SceneRenderState srs)
        {
            // update _verts and normals...
            UpdateSkin();

            if (_vb.IsNull)
                base.CreateVBIB();

            if (!_vb.IsNull)
            {
                int sizeInBytes = _initialVerts.Length * GFXVertexFormat.VertexSize;
                GFXVertexFormat.PCTTBN[] v = TorqueUtil.GetScratchArray<GFXVertexFormat.PCTTBN>(_initialVerts.Length);

                for (int i = 0; i < _initialVerts.Length; ++i)
                {
                    Vector3 tang = new Vector3(_tangents[i].X, _tangents[i].Y, _tangents[i].Z);
                    Vector3 binormal = Vector3.Cross(_norms[i], tang) * _tangents[i].W;
                    v[i] = new GFXVertexFormat.PCTTBN(
                        new Vector3(_verts[i].X, _verts[i].Y, _verts[i].Z),
                        Color.White,
                        _tverts[i],
                        _tverts[i],
                        _tangents[i],
                        new Vector4(_norms[i].X, _norms[i].Y, _norms[i].Z, 0.0f));
                }

                GFXDevice.Instance.Device.Textures[0] = null;

                _vb.Instance.SetData<GFXVertexFormat.PCTTBN>(v, 0, _initialVerts.Length, SetDataOptions.NoOverwrite);
            }

            base.Render(frame, matFrame, materialList, srs);

            RestoreSkin();
        }