// 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 SetViewFrustum(float InNearDist, float InFarDist, float InFov, float InScreenResolutionX, float InScreenResolutionY) { float ScreenRatio = InScreenResolutionX / InScreenResolutionY; if (ViewFrustum == null) { ViewFrustum = new H1ViewFrustum(); } ViewFrustum.Fov = InFov; ViewFrustum.ScreenRatio = ScreenRatio; ViewFrustum.NearDistance = InNearDist; ViewFrustum.FarDistance = InFarDist; ViewFrustum.Viewport.ResolutionX = InScreenResolutionX; ViewFrustum.Viewport.ResolutionY = InScreenResolutionY; // generate view frustum vertices H1Vector3 LookAt = new H1Vector3(0, 1, 0); H1Vector3 LocalX = new H1Vector3(1, 0, 0); H1Vector3 LocalZ = new H1Vector3(0, 0, 1); H1Vector3 NearPlanePosition = new H1Vector3(0, 0, 0) + ViewFrustum.NearDistance * LookAt; float HalfHeightForNearPlane = ViewFrustum.NearDistance * (float)Math.Tan(ViewFrustum.Fov); float HalfWidthForNearPlane = ViewFrustum.ScreenRatio * HalfHeightForNearPlane; Int32 NearPlaneStartIndex = Convert.ToInt32(H1ViewFrustum.EFrustumVerticeIndex.NEAR_0); ViewFrustum.LocalFrustumVertices[NearPlaneStartIndex + 0] = NearPlanePosition + HalfWidthForNearPlane * (-LocalX) + HalfHeightForNearPlane * (LocalZ); ViewFrustum.LocalFrustumVertices[NearPlaneStartIndex + 1] = NearPlanePosition + HalfWidthForNearPlane * (-LocalX) + HalfHeightForNearPlane * (-LocalZ); ViewFrustum.LocalFrustumVertices[NearPlaneStartIndex + 2] = NearPlanePosition + HalfWidthForNearPlane * (LocalX) + HalfHeightForNearPlane * (LocalZ); ViewFrustum.LocalFrustumVertices[NearPlaneStartIndex + 3] = NearPlanePosition + HalfWidthForNearPlane * (LocalX) + HalfHeightForNearPlane * (LocalZ); H1Vector3 FarPlanePosition = new H1Vector3(0, 0, 0) + ViewFrustum.FarDistance * LookAt; float HalfHeightForFarPlane = ViewFrustum.FarDistance * (float)Math.Tan(ViewFrustum.Fov); float HalfWidthForFarPlane = ViewFrustum.ScreenRatio * HalfHeightForFarPlane; Int32 FarPlaneStartIndex = Convert.ToInt32(H1ViewFrustum.EFrustumVerticeIndex.FAR_0); ViewFrustum.LocalFrustumVertices[FarPlaneStartIndex + 0] = FarPlanePosition + HalfWidthForFarPlane * (-LocalX) + HalfHeightForFarPlane * (LocalZ); ViewFrustum.LocalFrustumVertices[FarPlaneStartIndex + 1] = FarPlanePosition + HalfWidthForFarPlane * (-LocalX) + HalfHeightForFarPlane * (-LocalZ); ViewFrustum.LocalFrustumVertices[FarPlaneStartIndex + 2] = FarPlanePosition + HalfWidthForFarPlane * (LocalX) + HalfHeightForFarPlane * (LocalZ); ViewFrustum.LocalFrustumVertices[FarPlaneStartIndex + 3] = FarPlanePosition + HalfWidthForFarPlane * (LocalX) + HalfHeightForFarPlane * (LocalZ); return(true); }
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); }
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; }
// @TODO - it just temporary, need to think where to move public void CreateBoxBound(H1Vector3 InPosition, H1Vector3 InExtent) { Bound = new H1Bound(H1Bound.EBoundType.Box, InPosition, InExtent); }