Exemplo n.º 1
0
        bool IRenderable.Render(Camera camera, Light light)
        {
            this.device.RawDevice.SetRenderState(RenderState.ZEnable, false);
            this.device.RawDevice.SetRenderState(RenderState.ZWriteEnable, false);
            this.device.RawDevice.SetRenderState(RenderState.AlphaBlendEnable, false);

            this.device.RawDevice.VertexDeclaration = this.device.ScreenVertexDecl.RawDecl;

            if (this.device.RawDevice.SetStreamSource(0, this.device.ScreenVertexBuffer.RawBuffer, 0, this.device.ScreenVertexBuffer.ElementSize).IsFailure)
                return false;

            this.shader.Params["m_ViewInv"].SetValue(camera.ViewMatrix.Invert());
            this.shader.Params["diffuseTexture"].SetValue(SkyTexture);
            this.shader.Params["ambientColor"].SetValue(AmbientColor);
            this.shader.Params["scale"].SetValue(1.0f);
            this.shader.Params["aspectRatio"].SetValue(camera.AspectRatio);

            bool ret = this.shader.RenderTechnique((pass) =>
                {
                    return this.device.RawDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2).IsSuccess;
                }, "Skybox");

            this.device.RawDevice.SetRenderState(RenderState.ZEnable, true);
            this.device.RawDevice.SetRenderState(RenderState.ZWriteEnable, true);

            return ret;
        }
Exemplo n.º 2
0
        public void Camera_EyePos_Test()
        {
            Camera camera = new Camera();
            Assert.AreEqual(Vector3.Zero, camera.EyePos);

            var newPos = new Vector3(1.0f, 2.0f, 3.0f);
            camera.EyePos = newPos;
            Assert.AreEqual(newPos, camera.EyePos);
        }
Exemplo n.º 3
0
        public SceneManager(Device device, AssetManager assetManager)
        {
            this.device = device;
            this.assetManager = assetManager;

            // Create default camera and light.
            Camera = new Camera();
            Light = new Light();
        }
Exemplo n.º 4
0
        public void Camera_AspectRatio_Test()
        {
            var camera = new Camera();
            Assert.AreEqual(1.0f / 3, camera.AspectRatio);

            var expected = 800.0f / 600.0f;
            camera.AspectRatio = expected;
            Assert.AreEqual(expected, camera.AspectRatio);
        }
Exemplo n.º 5
0
        private bool RenderToRenderTarget(IRenderTarget target, IList<IRenderable> renderables, IList<ISprite> sprites, bool doPresent, Camera camera, Light light)
        {
            this.device.SetRenderTarget(target);

            if (target.ClearBackGround)
                this.device.RawDevice.Clear(target.ClearOptions, target.ClearColor.ToArgb(), 1.0f, 0);

            Result result = this.device.RawDevice.BeginScene();
            if (result.IsFailure)
            {
                Log.Msg(TraceLevel.Error, this, "BeginScene failed, " + result.ToString());
                return false;
            }

            try
            {
                if (renderables != null && renderables.Count > 0)
                {
                    foreach (var renderable in renderables)
                    {
                        renderable.Render(camera, light);
                    }
                }

                if (sprites != null && sprites.Count > 0)
                {
                    this.spriteRenderer.RawSprite.Begin(SpriteFlags.AlphaBlend);
                    foreach (var sprite in sprites)
                    {
                        sprite.Render(this.spriteRenderer);
                    }
                    this.spriteRenderer.RawSprite.End();
                }
            }
            finally
            {
                this.device.RawDevice.EndScene();
            }

            if (doPresent)
                this.device.RawDevice.Present();

            target.OnRender();

            return true;
        }
Exemplo n.º 6
0
        public bool Render(Material material, Light light, Camera camera, Matrix4 worldMatrix, Func<int, bool> renderDelegate)
        {
            Params["m_World"].SetValue(worldMatrix);
            Params["m_WorldIT"].SetValue(worldMatrix.Invert().Transpose());
            Params["m_WVP"].SetValue(worldMatrix * camera.ViewMatrix * camera.ProjectionMatrix);
            Params["g_vLightPos"].SetValue(light.Position);
            Params["m_ViewInv"].SetValue(camera.ViewMatrix.Invert());
            Params["DiffuseTexture"].SetValue(material.DiffuseTexture);
            Params["useNormalMap"].SetValue(material.UseNormalMap);
            Params["NormalTexture"].SetValue(material.NormalTexture);
            Params["specularPower"].SetValue(material.SpecularPower);
            Params["specularColor"].SetValue(material.SpecularColor);
            Params["useOffsetMapping"].SetValue(material.UseOffsetMapping);
            Params["HeightMapTexture"].SetValue(material.HeightMapTexture);
            Params["g_fOffsetBias"].SetValue(material.OffsetHeight);

            return RenderTechnique(renderDelegate, material.Technique);
        }
Exemplo n.º 7
0
        bool IRenderable.Render(Camera camera, Light light)
        {
            this.device.RawDevice.VertexDeclaration = this.device.MeshVertexDecl.RawDecl;

            if (this.device.RawDevice.SetStreamSource(0, this.mesh.VertexBuffer.RawBuffer, 0, this.mesh.VertexBuffer.ElementSize).IsFailure)
                return false;

            this.device.RawDevice.Indices = this.mesh.IndexBuffer.RawBuffer;

            return this.mesh.Material.Shader.Render(this.mesh.Material, light, camera, WorldMatrix,
                (pass) =>
                {
                    var drawResult = this.device.RawDevice.DrawIndexedPrimitives(D3D.PrimitiveType.TriangleList,
                                                                0, this.mesh.Attribute.VertexStart, this.mesh.Attribute.VertexCount,
                                                                this.mesh.Attribute.FaceStart * 3, this.mesh.Attribute.FaceCount);
                    return drawResult.IsSuccess;
                });
        }
Exemplo n.º 8
0
        public void Camera_Direction_Test()
        {
            var camera = new Camera();
            Assert.AreEqual(Vector3.UnitZ, camera.Direction);

            var dir = new Vector3(1.0f, 2.0f, 3.0f);
            camera.Direction = dir;
            TestHelpers.AreEqual(dir.Normalize(), camera.Direction);

            var eyePos = new Vector3(1.0f, 2.0f, 3.0f);
            camera.EyePos = eyePos;
            var lookAt = new Vector3(4.0f, 5.0f, 6.0f);
            camera.LookAt = lookAt;
            TestHelpers.AreEqual((lookAt - eyePos).Normalize(), camera.Direction);

            // Set zero vector has no effect
            var oldDir = camera.Direction;
            camera.Direction = Vector3.Zero;
            Assert.AreEqual(oldDir, camera.Direction);
        }
Exemplo n.º 9
0
 bool IRenderable.Render(Camera camera, Light light)
 {
     for (int i = 0; i < this.materials.Count; ++i)
     {
         var material = this.materials[i];
         bool ret = material.Shader.Render(material, light, camera, Matrix4.Identity,
             (pass) =>
             {
                 foreach (var patch in this.patches)
                 {
                     if (patch.Render(i) == false)
                         return false;
                 }
                 return true;
             });
         if (ret == false)
             return false;
     }
     return true;
 }
Exemplo n.º 10
0
        bool IRenderable.Render(Camera camera, Light light)
        {
            if (IsEnabled == false)
                return true;

            if (this.textureRenderTarget == null ||
                this.textureRenderTarget.Size.Width != this.device.RenderTarget.Size.Width ||
                this.textureRenderTarget.Size.Height != this.device.RenderTarget.Size.Height)
            {
                this.textureRenderTarget = new TextureRenderTarget(this.device, this.device.RenderTarget.Size);
            }

            // Copy rendered buffer to our texture render target
            this.device.RawDevice.StretchRectangle(this.device.RenderTarget.TargetSurface, this.textureRenderTarget.TargetSurface, TextureFilter.None);

            this.device.RawDevice.SetRenderState(RenderState.ZEnable, false);
            this.device.RawDevice.SetRenderState(RenderState.ZWriteEnable, false);
            this.device.RawDevice.SetRenderState(RenderState.AlphaBlendEnable, false);

            this.device.RawDevice.VertexDeclaration = this.device.ScreenVertexDecl.RawDecl;

            if (this.device.RawDevice.SetStreamSource(0, this.device.ScreenVertexBuffer.RawBuffer, 0, this.device.ScreenVertexBuffer.ElementSize).IsFailure)
                return false;

            var windowSize = new Vector2(this.textureRenderTarget.Size.Width, this.textureRenderTarget.Size.Height);
            this.shader.Params["windowSize"].SetValue(windowSize);
            this.shader.Params["sceneMapTexture"].SetValue(this.textureRenderTarget.Texture);
            this.shader.Params["screenBorderFadeoutMapTexture"].SetValue(BorderTexture);

            bool ret = this.shader.RenderTechnique((pass) =>
            {
                return this.device.RawDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2).IsSuccess;
            }, "ScreenDarkenBorder");

            this.device.RawDevice.SetRenderState(RenderState.ZEnable, true);
            this.device.RawDevice.SetRenderState(RenderState.ZWriteEnable, true);

            return ret;
        }
Exemplo n.º 11
0
        bool IRenderable.Render(Camera camera, Light light)
        {
            if (IsEnabled == false)
                return true;

            var finalRenderTarget = this.device.RenderTarget;

            CreateTextureRenderTargets(finalRenderTarget.Size);
            CopyRenderTarget(finalRenderTarget, this.sceneMapRenderTarget);

            this.device.RawDevice.SetRenderState(RenderState.ZEnable, false);
            this.device.RawDevice.SetRenderState(RenderState.ZWriteEnable, false);
            this.device.RawDevice.SetRenderState(RenderState.AlphaBlendEnable, false);

            this.device.RawDevice.VertexDeclaration = this.device.ScreenVertexDecl.RawDecl;

            if (this.device.RawDevice.SetStreamSource(0, this.device.ScreenVertexBuffer.RawBuffer, 0, this.device.ScreenVertexBuffer.ElementSize).IsFailure)
                return false;

            var windowSize = new Vector2(finalRenderTarget.Size.Width, finalRenderTarget.Size.Height);
            this.shader.Params["windowSize"].SetValue(windowSize);
            this.shader.Params["radialBlurScaleFactor"].SetValue(RadialBlurScaleFactor);
            this.shader.Params["sceneMapTexture"].SetValue(this.sceneMapRenderTarget.Texture);
            this.shader.Params["radialSceneMapTexture"].SetValue(this.radialSceneMapRenderTarget.Texture);
            this.shader.Params["downsampleMapTexture"].SetValue(this.downsampleMapRenderTarget.Texture);
            this.shader.Params["blurMap1Texture"].SetValue(this.blurMap1RenderTarget.Texture);
            this.shader.Params["blurMap2Texture"].SetValue(this.blurMap2RenderTarget.Texture);
            this.shader.Params["blurWidth"].SetValue(BlurWidth);
            this.shader.Params["glowIntensity"].SetValue(GlowIntensity);
            this.shader.Params["highlightIntensity"].SetValue(HighlightIntensity);

            bool ret = this.shader.RenderTechnique(RenderPass, "ScreenGlow");

            this.device.RawDevice.SetRenderState(RenderState.ZEnable, true);
            this.device.RawDevice.SetRenderState(RenderState.ZWriteEnable, true);

            return ret;
        }
Exemplo n.º 12
0
        public bool Render(IRenderTarget renderTarget, IList<IRenderable> renderables, IList<ISprite> sprites, Camera camera, Light light, bool present)
        {
            if (camera == null || light == null)
                throw new ArgumentNullException("Argument camera or light is null");

            bool renderSuccess = false;
            try
            {
                // Save primary
                PrimaryRenderTarget.TargetSurface = this.device.RawDevice.GetRenderTarget(0);
                PrimaryRenderTarget.DepthStencilSurface = this.device.RawDevice.DepthStencilSurface;

                // Render this render target
                renderSuccess = RenderToRenderTarget(renderTarget, renderables, sprites, present, camera, light);
            }
            finally
            {
                // Restore primary
                this.device.SetRenderTarget(PrimaryRenderTarget);
            }

            return renderSuccess;
        }
Exemplo n.º 13
0
        public void Camera_Orientation_Test()
        {
            var camera = new Camera();
            Assert.AreEqual(Quaternion.Identity, camera.Orientation);

            var newOrientation = Quaternion.CreateFromAxisAngle(Vector3.UnitY, new Radian(MathUtils.PI / 2.0f));
            camera.Orientation = newOrientation;
            Assert.AreEqual(newOrientation, camera.Orientation);
        }
Exemplo n.º 14
0
 public void Camera_AspectRatio_Invalid_NegativeValue_Test()
 {
     var camera = new Camera();
     camera.AspectRatio = -1.0f;
 }
Exemplo n.º 15
0
 public void Camera_PlaneDistance_FarShouldBeLargerThanNear_Test()
 {
     var camera = new Camera();
     camera.SetPlaneDistance(100.0f, 100.0f);
 }
Exemplo n.º 16
0
        public void Camera_ViewMatrix_Test()
        {
            Camera camera = new Camera();
            camera.EyePos = new Vector3(1.0f, 2.0f, 3.0f);
            camera.LookAt = new Vector3(0.0f, 0.0f, 0.0f);
            camera.UpVec = new Vector3(2.0f, 3.0f, 4.0f);

            var zaxis = camera.Direction;
            var xaxis = Vector3.Cross(camera.UpVec, zaxis).Normalize();
            var yaxis = Vector3.Cross(zaxis, xaxis);
            var col0 = new Vector4(xaxis, -Vector3.Dot(xaxis, camera.EyePos));
            var col1 = new Vector4(yaxis, -Vector3.Dot(yaxis, camera.EyePos));
            var col2 = new Vector4(zaxis, -Vector3.Dot(zaxis, camera.EyePos));
            var expected = new Matrix4(col0, col1, col2, new Vector4(0, 0, 0, 1));

            Assert.AreEqual(expected, camera.ViewMatrix);
        }
Exemplo n.º 17
0
 public void Camera_FovY_Invalid_OverPI_Test()
 {
     var camera = new Camera();
     camera.FovY = new Radian(MathUtils.PI * 1.1f);
 }
Exemplo n.º 18
0
        public void Camera_PlaneDistance_Test()
        {
            var camera = new Camera();
            Assert.AreEqual(1.0f, camera.NearPlaneDistance);
            Assert.AreEqual(100000.0f, camera.FarPlaneDistance);

            var expectedNear = 200.0f;
            var expectedFar = 50000.0f;
            camera.SetPlaneDistance(expectedNear, expectedFar);
            Assert.AreEqual(expectedNear, camera.NearPlaneDistance);
            Assert.AreEqual(expectedFar, camera.FarPlaneDistance);
        }
Exemplo n.º 19
0
        public void Camera_MoveRelative_Test()
        {
            var camera = new Camera()
            {
                EyePos = Vector3.UnitX,
                Direction = Vector3.UnitZ
            };

            var moveVec = new Vector3(1.0f, 1.0f, 0.0f);

            camera.MoveRelative(moveVec);
            TestHelpers.AreEqual(Vector3.UnitX + moveVec, camera.EyePos);
        }
Exemplo n.º 20
0
        public void Camera_ProjectionMatrix_Test()
        {
            var camera = new Camera();
            var projMat = camera.ProjectionMatrix;

            // FovY changes
            var oldFovY = camera.FovY;
            camera.FovY = oldFovY / 2;
            Assert.AreNotEqual(projMat, camera.ProjectionMatrix);
            camera.FovY = oldFovY;  // restore
            Assert.AreEqual(projMat, camera.ProjectionMatrix);

            // Aspect ratio changes
            var oldAspect = camera.AspectRatio;
            camera.AspectRatio = oldAspect / 2;
            Assert.AreNotEqual(projMat, camera.ProjectionMatrix);
            camera.AspectRatio = oldAspect;
            Assert.AreEqual(projMat, camera.ProjectionMatrix);

            // Plane distance changes
            var oldNear = camera.NearPlaneDistance;
            var oldFar = camera.FarPlaneDistance;
            camera.SetPlaneDistance(oldNear / 2, oldFar / 2);
            Assert.AreNotEqual(projMat, camera.ProjectionMatrix);
            camera.SetPlaneDistance(oldNear, oldFar);
            Assert.AreEqual(projMat, camera.ProjectionMatrix);
        }
Exemplo n.º 21
0
        public void Camera_Yaw_Test()
        {
            var camera = new Camera();
            var angle = new Radian(MathUtils.PI / 2);

            camera.Yaw(angle);
            var expected = Quaternion.CreateFromAxisAngle(Vector3.UnitY, angle);
            TestHelpers.QuaternionAreNearEqual(expected, camera.Orientation);
        }
Exemplo n.º 22
0
 public void Camera_AspectRatio_Invalid_ZeroValue_Test()
 {
     var camera = new Camera();
     camera.AspectRatio = 0.0f;
 }
Exemplo n.º 23
0
        public void Camera_Move_Test()
        {
            var camera = new Camera();
            camera.Direction = Vector3.UnitY;

            var moveVec = new Vector3(1.0f, 1.0f, 0.0f);
            camera.Move(moveVec);

            Assert.AreEqual(moveVec, camera.EyePos);
        }
Exemplo n.º 24
0
 public void Camera_FovY_Invalid_Under0_Test()
 {
     var camera = new Camera();
     camera.FovY = new Radian(-0.1f);
 }
Exemplo n.º 25
0
        public void Camera_FovY_Test()
        {
            var camera = new Camera();
            Assert.AreEqual(new Radian(MathUtils.PI / 4.0f), camera.FovY);

            var expectedFovY = new Radian(2.0f);
            camera.FovY = expectedFovY;
            Assert.AreEqual(expectedFovY, camera.FovY);

            // FieldOfView is between 0 and pi (180 degrees)
            camera.FovY = new Radian(0.0f);
            camera.FovY = new Radian(MathUtils.PI);
        }
Exemplo n.º 26
0
 bool IRenderable.Render(Camera camera, Light light)
 {
     RenderCalled(this, EventArgs.Empty);
     return true;
 }
Exemplo n.º 27
0
        public void Camera_UpVec_Test()
        {
            Camera camera = new Camera();
            Assert.AreEqual(Vector3.UnitY, camera.UpVec);

            var upVec = new Vector3(1.0f, 2.0f, 3.0f);
            camera.UpVec = upVec;
            Assert.AreEqual(upVec, camera.UpVec);
        }