protected void InitializePrimitive()
        {
            if (materialShader == null)
                materialShader = MaterialShader.DefaultMaterial(Renderer);
            if (this.GetType() == typeof(SkinnedMeshPrimitive))
                GeometryData.VertexDefinition = MaterialShader.SkinnedMeshVertexDefinition;
            else
                GeometryData.VertexDefinition = MaterialShader.StaticMeshVertexDefinition;
            GeometryData.VertexStride = GeometryData.VertexDefinition.SizeInBytes;
            GeometryData.VertexCount = GeometryData.Vertices.Count;
            GeometryData.IndexCount = GeometryData.Indices.Count;

            DataStream VerticesStream = new DataStream(GeometryData.VertexCount * GeometryData.VertexStride, true, true);
            Vector3[] Tangent;
            Vector3[] Bitangent;
            GenerateTangentFrame(out Tangent, out Bitangent);
            for (int j = 0; j < GeometryData.Vertices.Count; j++)
            {
                Vertex v = GeometryData.Vertices[j];
                for (int i = 0; i < GeometryData.VertexDefinition.Parameters.Count; i++)
                {
                    VertexParameterType vp = GeometryData.VertexDefinition.Parameters[i];
                    if (vp == VertexParameterType.Position)
                        VerticesStream.Write(v.GetVertex<Vector3>(vp).Value);
                    else if (vp == VertexParameterType.Normal)
                        VerticesStream.Write(v.GetVertex<Vector3>(vp).Value);
                    else if (vp == VertexParameterType.TextureCoordinate)
                        VerticesStream.Write(v.GetVertex<Vector2>(vp).Value);
                    else if (vp == VertexParameterType.Tangent)
                    {
                        //VerticesStream.Write(Vector3.Normalize(Tangent[j]));
                        VerticesStream.Write(MathHelper.CalculateTangent(v.GetVertex<Vector3>(VertexParameterType.Normal).Value));
                        /*if ((j + (GeometryData.Vertices.Count % 3)) % 3 == 0 & GeometryData.Vertices.Count - (j + (GeometryData.Vertices.Count % 3)) > 2)
                        {
                            tan = MathHelper.CalculateTangent(
                            v.GetVertex<Vector3>(VertexParameterType.Position).Value,
                            GeometryData.Vertices[j + 1].GetVertex<Vector3>(VertexParameterType.Position).Value,
                            GeometryData.Vertices[j + 2].GetVertex<Vector3>(VertexParameterType.Position).Value,
                            v.GetVertex<Vector2>(VertexParameterType.TextureCoordinate).Value,
                            GeometryData.Vertices[j + 1].GetVertex<Vector2>(VertexParameterType.TextureCoordinate).Value,
                            GeometryData.Vertices[j + 2].GetVertex<Vector2>(VertexParameterType.TextureCoordinate).Value
                            );
                        }
                        Vector3 val = v.GetVertex<Vector3>(VertexParameterType.Normal).Value;
                        Vector3 vec = MathHelper.ToVector3(Vector3.Transform(val, Matrix.RotationX(MathHelper.PiOver2)));
                        if (v is VertexPosNorTexTanBin)
                        {
                            VerticesStream.Write(v.GetVertex<Vector3>(vp).Value);
                        }
                        else
                        {
                            VerticesStream.Write(MathHelper.CalculateTangent(v.GetVertex<Vector3>(VertexParameterType.Normal).Value));
                        }*/
                    }
                    else if (vp == VertexParameterType.Binormal)
                    {
                        //VerticesStream.Write(Vector3.Normalize(Bitangent[j]));
                        VerticesStream.Write(-MathHelper.CalculateTangent(v.GetVertex<Vector3>(VertexParameterType.Normal).Value));
                        /*if ((j + (GeometryData.Vertices.Count % 3)) % 3 == 0 & GeometryData.Vertices.Count - (j + (GeometryData.Vertices.Count % 3)) > 2)
                        {
                            bin = MathHelper.CalculateBitangent(
                                v.GetVertex<Vector3>(VertexParameterType.Position).Value,
                                GeometryData.Vertices[j + 1].GetVertex<Vector3>(VertexParameterType.Position).Value,
                                GeometryData.Vertices[j + 2].GetVertex<Vector3>(VertexParameterType.Position).Value,
                                v.GetVertex<Vector2>(VertexParameterType.TextureCoordinate).Value,
                                GeometryData.Vertices[j + 1].GetVertex<Vector2>(VertexParameterType.TextureCoordinate).Value,
                                GeometryData.Vertices[j + 2].GetVertex<Vector2>(VertexParameterType.TextureCoordinate).Value
                                );
                        }
                        Vector3 val = v.GetVertex<Vector3>(VertexParameterType.Normal).Value;
                        Vector3 vec = MathHelper.ToVector3(Vector3.Transform(val, Matrix.RotationZ(MathHelper.PiOver2)));
                        if (v is VertexPosNorTexTanBin)
                        {
                            VerticesStream.Write(v.GetVertex<Vector3>(vp).Value);
                        }
                        else
                        {
                            VerticesStream.Write(MathHelper.CalculateBitangent(v.GetVertex<Vector3>(VertexParameterType.Normal).Value));
                        }*/
                    }
                    else if (vp == VertexParameterType.Bones32Bit)
                    {
                        Vector4 vec = (v.GetVertex<Vector4>(vp).Value);
                        VerticesStream.Write((uint)vec.X);
                        VerticesStream.Write((uint)vec.Y);
                        VerticesStream.Write((uint)vec.Z);
                        VerticesStream.Write((uint)vec.W);
                    }
                    else if (vp == VertexParameterType.Weights)
                    {
                        Vector4 vec = (v.GetVertex<Vector4>(vp).Value);
                        VerticesStream.Write(v.GetVertex<Vector4>(vp).Value);
                    }
                    else if (vp == VertexParameterType.ColorRGB)
                        VerticesStream.Write(v.GetVertex<Vector3>(vp).Value);
                }
            }
            VerticesStream.Position = 0;
            GeometryData.VertexBuffer = new Buffer(Renderer.Device, VerticesStream, GeometryData.VertexStride * GeometryData.VertexCount,
                ResourceUsage.Default, BindFlags.VertexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, GeometryData.VertexStride);

            DataStream IndicesStream = new DataStream(GeometryData.Indices.ToArray(), true, true);
            IndicesStream.Position = 0;

            GeometryData.IndexBuffer = new Buffer(Renderer.Device, IndicesStream, sizeof(ushort) * GeometryData.IndexCount,
                ResourceUsage.Default, BindFlags.IndexBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, sizeof(ushort));

            IndicesStream.Close();
            VerticesStream.Close();
        }
 public void End()
 {
     LightingSystem.End(null);
     ActiveMaterial = null;
     isRendering = false;
 }
        static MaterialShader GenerateDefaultMaterial(Renderer Renderer)
        {
            string path = @"R:\Users\Rox Cox\Documents\Visual Studio 2013\Projects\LightingEngine_v2\LightingEngine_v2\LightingD3D11\HLSL\";
            ShaderFlags flags = ShaderFlags.Debug | ShaderFlags.EnableStrictness | ShaderFlags.PackMatrixRowMajor;
            MaterialShader m = new MaterialShader(Renderer);
            m.StaticMeshVertexDefinition = new VertexDefinition()
            {
                Parameters = new List<VertexParameterType>()
                {
                    VertexParameterType.Position,
                    VertexParameterType.Normal,
                    VertexParameterType.TextureCoordinate,
                    VertexParameterType.Tangent,
                    VertexParameterType.Binormal,
                }
            };
            using (ShaderBytecode bytecode = ShaderBytecode.CompileFromFile(path + "materialVS.hlsl", "VS", "vs_5_0", flags, EffectFlags.None))
            {
                m.VertexShader = new VertexShader(Renderer.Device, bytecode);
                m.StaticMeshInputLayout = m.StaticMeshVertexDefinition.GetInputLayout(bytecode);
            }
            /*using (ShaderBytecode bytecode = ShaderBytecode.CompileFromFile(path + "materialPS.hlsl", "ps_5_0"))
            {
                m.PixelShader = new PixelShader(Renderer.Device, bytecode);
            }*/
            m.Topology = PrimitiveTopology.TriangleList;
            m.Samplers.Add(SamplerState.FromDescription(Renderer.Device, new SamplerDescription()
            {
                AddressU = TextureAddressMode.Wrap,
                AddressV = TextureAddressMode.Wrap,
                AddressW = TextureAddressMode.Wrap,
                BorderColor = new Color4(1, 1, 1, 1),
                ComparisonFunction = Comparison.Never,
                Filter = Filter.Anisotropic,
                MaximumAnisotropy = 16,
                MaximumLod = float.MaxValue,
                MinimumLod = float.MinValue,
                MipLodBias = 0,
            }));
            ConstantBufferWrapper bf0 = new ConstantBufferWrapper(Renderer, sizeof(float) * 64, ShaderType.VertexShader, 0);
            bf0.Semantics.Add(Semantic.World);
            bf0.Semantics.Add(Semantic.ViewProj);
            bf0.Semantics.Add(Semantic.View);
            bf0.Semantics.Add(Semantic.WorldView);
            m.ConstantBuffers.Add(bf0);
            using (Texture2D tex = ContentHelper.TextureFromBitmap(Properties.Resources.notexture))
            {
                m.ShaderResources.Add(new ShaderResourceView(Renderer.Device, tex));
            }
            using (Texture2D tex = ContentHelper.TextureFromBitmap(Properties.Resources.notexture_NORM))
            {
                m.ShaderResources.Add(new ShaderResourceView(Renderer.Device, tex));
            }

            return m;
        }
 public void DrawPrimitive(GeometricPrimitive primitive)
 {
     if (ActiveMaterial != primitive.MaterialShader)
     {
         ActiveMaterial = primitive.MaterialShader;
         ActiveMaterial.Begin();
     }
     ActiveMaterial.SetBuffers(primitive);
     primitive.Draw(Context);
 }
 public static MaterialShader DefaultMaterial(Renderer Renderer)
 {
     if (defaultMaterial == null)
         defaultMaterial = GenerateDefaultMaterial(Renderer);
     return defaultMaterial;
 }