Пример #1
0
        /// <summary>
        /// Throws the Render event.
        /// </summary>
        /// <param name="e">The event data.</param>
        public override void OnRender(RenderEventArgs e)
        {
            base.OnRender(e);

            // if (_vertexBuffer != null)
            if ((_vertexBuffer != null) && (_sculptor.VertexPositionTextureNormals != null))
            {
                // For texture rendering
                e.GraphicsDevice.SamplerStates[0] = _samplerState;

                e.GraphicsDevice.SetVertexBuffer(null);   // to prevent InvalidOperationException during _vertexBuffer.SetData()
                _vertexBuffer.SetData(0, _sculptor.VertexPositionTextureNormals, 0, _sculptor.VertexPositionTextureNormals.Length, 0);
                // e.GraphicsDevice.SetVertexBuffer(_vertexBuffer);

                // Vertex pipeline
                e.GraphicsDevice.SetVertexBuffer(_vertexBuffer);
                ShaderHelper.SetVertexShader(e.GraphicsDevice, _scene._vertexShader);
                ShaderHelper.SetVertexShaderConstantMatrix(e.GraphicsDevice, ref e.ViewProjection);
                ShaderHelper.SetVertexShaderConstantMatrix(e.GraphicsDevice, ref _matrix);

                // Pixel pipeline
                ShaderHelper.SetPixelShader(e.GraphicsDevice, _scene._pixelShader);
                Vector4 lookDirection = new Vector4(_scene.Camera.LookTarget, 0.0f);
                ShaderHelper.SetPixelShaderConstantFloat4(e.GraphicsDevice, ref lookDirection);
                ShaderHelper.SetPixelShaderConstantFloat4(e.GraphicsDevice, ref _scene._ambientLightVector);
                ShaderHelper.SetPixelShaderConstantFloat4(e.GraphicsDevice, ref _scene._directionalLightVector1);
                ShaderHelper.SetPixelShaderConstantFloat4(e.GraphicsDevice, ref _scene._directionalLightVector2);
                ShaderHelper.SetPixelShaderConstantFloat4(e.GraphicsDevice, ref _scene._directionalLightVector3);
                ShaderHelper.SetPixelShaderConstantFloat4(e.GraphicsDevice, ref _scene._directionalLightVector4);
                ShaderHelper.SetPixelShaderConstantMaterial(e.GraphicsDevice, ref _material);

                if (_texture != null)
                {
                    e.GraphicsDevice.Textures[0] = _texture;
                }

                if (_sculptor.Triangles.Count > 0) // to prevent ArgumentOutOfRangeException on GraphicsDevice.DrawPrimitives()
                {
                    e.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, _sculptor.Triangles.Count);

                    if (_backTexture != null)
                    {
                        e.GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
                        e.GraphicsDevice.Textures[0]     = _backTexture;
                        ShaderHelper.SetPixelShaderConstantMaterial(e.GraphicsDevice, ref _backMaterial);
                        e.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, _sculptor.Triangles.Count);
                        e.GraphicsDevice.RasterizerState = Helper3D.DefaultRasterizerState;
                    }
                }
            }
        }