Example #1
0
        // generate rays
        public H1Ray[] GenerateRays()
        {
            List <H1Ray> Rays = new List <H1Ray>();

            H1Vector3 CameraLocation = Position;

            Int32 ResolutionX = Convert.ToInt32(ViewFrustum.Viewport.ResolutionX);
            Int32 ResolutionY = Convert.ToInt32(ViewFrustum.Viewport.ResolutionY);

            Int32 IndexLT = Convert.ToInt32(H1ViewFrustum.EFrustumVerticeIndex.NEAR_0);

            H1Vector3 WorldScreenLT = H1Vector3.Transform(ViewFrustum.LocalFrustumVertices[IndexLT + 0], ViewMatrix);
            H1Vector3 WorldScreenLB = H1Vector3.Transform(ViewFrustum.LocalFrustumVertices[IndexLT + 1], ViewMatrix);
            H1Vector3 WorldScreenRB = H1Vector3.Transform(ViewFrustum.LocalFrustumVertices[IndexLT + 2], ViewMatrix);
            H1Vector3 WorldScreenRT = H1Vector3.Transform(ViewFrustum.LocalFrustumVertices[IndexLT + 3], ViewMatrix);

            H1Vector3 OffsetX = (WorldScreenRT - WorldScreenLT) / ResolutionX;
            H1Vector3 OffsetY = (WorldScreenLB - WorldScreenLT) / ResolutionY;

            for (Int32 PixelY = 0; PixelY < ResolutionY; ++PixelY)
            {
                for (Int32 PixelX = 0; PixelX < ResolutionX; ++PixelX)
                {
                    H1Vector3 Offset = WorldScreenLT + OffsetX * PixelX + OffsetY * PixelY;

                    H1Ray NewRay = new H1Ray();
                    NewRay.Origin    = CameraLocation;
                    NewRay.Direction = H1Vector3.Normalize(Offset - NewRay.Origin);

                    Rays.Add(NewRay);
                }
            }

            return(Rays.ToArray());
        }
        public bool Render()
        {
            H1Entity CameraObject = new H1Entity();
            H1RayTracerCameraComponent RayTracerCameraComponent = new H1RayTracerCameraComponent(CameraObject);

            Int32 ResolutionX = BackBuffer.Width;
            Int32 ResolutionY = BackBuffer.Height;

            H1Vector3 Direction = H1Vector3.Normalize(new H1Vector3(0, 1, -1));

            RayTracerCameraComponent.UpdateCamera(new H1Vector3(0, 0, 500), Direction);
            RayTracerCameraComponent.SetViewFrustum(100, 1000, (float)(30.0 / Math.PI), ResolutionX, ResolutionY);

            // test ray tracer component entity
            H1RayTracerComponent OneComponent = new H1RayTracerComponent(CameraObject);

            OneComponent.Activate();

            float Extent = 25.0f;

            OneComponent.CreateBoxBound(new H1Vector3(0, 500, 0), new H1Vector3(Extent, Extent, Extent));

            H1Ray Ray = new H1Ray(new H1Vector3(0, 0, 500), Direction);
            float T0, T1;

            bool bCollide = OneComponent.IsCollide(Ray, out T0, out T1);

            H1Ray[] Rays = RayTracerCameraComponent.GenerateRays();
            H1RayTracerSystem.Singleton.RayCast(Rays, ref BackBuffer);

            Bitmap NewBitmap = new Bitmap(ResolutionX, ResolutionY);

            for (Int32 PixelY = 0; PixelY < ResolutionY; ++PixelY)
            {
                for (Int32 PixelX = 0; PixelX < ResolutionX; ++PixelX)
                {
                    H1Color PixelColor = BackBuffer[PixelX, PixelY];
                    Color   OutColor   = Color.FromArgb(PixelColor.A, PixelColor.R, PixelColor.G, PixelColor.B);

                    NewBitmap.SetPixel(PixelX, PixelY, OutColor);
                }
            }

            NewBitmap.Save("RayTracerOutput.png", ImageFormat.Png);

            return(true);
        }
Example #3
0
        public void UpdateCamera(H1Vector3 InPosition, H1Vector3 InLookAt)
        {
            Position = InPosition;
            LookAt   = InLookAt;

            // create camera matrix (view matrix)
            H1Vector3 UpVector    = new H1Vector3(0, 0, 1);
            H1Vector3 RightVector = H1Vector3.Normalize(H1Vector3.Cross(LookAt, UpVector));

            UpVector = H1Vector3.Normalize(H1Vector3.Cross(RightVector, LookAt));

            ViewMatrix       = new H1Matrix();
            ViewMatrix.Look  = LookAt;
            ViewMatrix.Right = RightVector;
            ViewMatrix.Up    = UpVector;

            ViewMatrix.TranslationVector = InPosition;
        }