/// <summary> /// </summary> /// <param name="u">value between 0 to 1</param> /// <param name="v">value between 0 to 1</param> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">the geometry</param> /// <returns></returns> public override Vector3 GetTexile(float u, float v, IntersectionRecord rec, RTGeometry g) { float useU = u * mRepeat; useU = useU - ((int)useU); return((useU * mColor2) + (1 - useU) * mColor1); }
public KdTree(int maxDepth, int maxLeavePrimitives, SceneDatabase sceneDatabase) { mMaxNumOfPrimitives = maxLeavePrimitives; mMaxDepth = maxDepth; // List <RTGeometry> allGeom = new List <RTGeometry>(); Vector3 worldMin = new Vector3(float.MaxValue); Vector3 worldMax = new Vector3(float.MinValue); // we need to compute the compute the world min/max for (int i = 0; i < sceneDatabase.GetNumGeom(); i++) { RTGeometry g = sceneDatabase.GetGeom(i); allGeom.Add(g); worldMin = Vector3.Min(worldMin, g.Min); worldMax = Vector3.Max(worldMax, g.Max); } int rootDepth = 0; mRoot = new KdTreeNode(worldMin, worldMax, new KdTreeAxis(rootDepth), allGeom); BuildKDTree(mRoot, rootDepth, allGeom); }
/// <summary> /// </summary> /// <param name="u">value between 0 to 1</param> /// <param name="v">value between 0 to 1</param> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">the geometry</param> /// <returns></returns> public override Vector3 GetTexile(float u, float v, IntersectionRecord rec, RTGeometry g) { float useU = u * mDirection.X + v * mDirection.Y; float theta = useU * mThetaRange; float sineV = 0.5f * (((float)Math.Sin(theta)) + 1); return((sineV * mColor1) + (1f - sineV) * mColor2); }
/// <summary> /// </summary> /// <param name="u">value between 0 to 1</param> /// <param name="v">value between 0 to 1</param> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">the geometry</param> /// <returns></returns> public override Vector3 GetTexile(float u, float v, IntersectionRecord rec, RTGeometry g) { float useU = u * mDirection.X + v * mDirection.Y; float theta = useU * mThetaRange; float sineV = 0.5f * (((float) Math.Sin(theta)) + 1); return (sineV * mColor1) + (1f - sineV) * mColor2; }
/// <summary> /// UV are values between 0 to 1, maps to texture width/height linearly and returns the /// corresponding textile. /// </summary> /// <param name="u">value between 0 to 1</param> /// <param name="v">value between 0 to 1</param> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">the geometry</param> /// <returns></returns> public override Vector3 GetTexile(float u, float v, IntersectionRecord rec, RTGeometry g) { Vector3 norm = Vector3.UnitY; if ((u >= 0) && (u <= 1f) && (v >= 0) && (v <= 1f)) { int u0Index = 0, u1Index = 0, v0Index = 0, v1Index = 0; float uIndex, vIndex; float bumpAtu0v0, bumpAtu0v1, bumpAtu1v0, bumpAtu1v1; lock (mTextureImage) { GetIndices(u, mTextureImage.Width, ref u0Index, ref u1Index); GetIndices(v, mTextureImage.Height, ref v0Index, ref v1Index); v0Index = mTextureImage.Height - 1 - v0Index; v1Index = mTextureImage.Height - 1 - v1Index; uIndex = u * mTextureImage.Width; vIndex = mTextureImage.Height - 1 - (v * mTextureImage.Height); bumpAtu0v0 = ColorToVec(mTextureImage.GetPixel(u0Index, v0Index)).Length(); bumpAtu1v0 = ColorToVec(mTextureImage.GetPixel(u1Index, v0Index)).Length(); bumpAtu0v1 = ColorToVec(mTextureImage.GetPixel(u0Index, v1Index)).Length(); bumpAtu1v1 = ColorToVec(mTextureImage.GetPixel(u1Index, v1Index)).Length(); } float du = uIndex - u0Index; float dv = vIndex - v1Index; float bumpAtV0 = du * bumpAtu1v0 + (1 - du) * bumpAtu0v0; float bumpAtV1 = du * bumpAtu1v1 + (1 - du) * bumpAtu0v1; float bumpAtU0 = dv * bumpAtu0v1 + (1 - dv) * bumpAtu0v0; float bumpAtU1 = dv * bumpAtu1v1 + (1 - dv) * bumpAtu1v0; float dDu = mGain * (bumpAtV1 - bumpAtV0); float dDv = mGain * (bumpAtU1 - bumpAtU0); float u0Ref, u1Ref, v0Ref, v1Ref; u0Ref = u0Index * mInvWidth; u1Ref = u1Index * mInvWidth; v0Ref = v0Index * mInvHeight; v1Ref = v1Index * mInvHeight; Vector3 Pu0 = g.GetPosition(u0Ref, v); Vector3 Pu1 = g.GetPosition(u1Ref, v); Vector3 Pv0 = g.GetPosition(u, v0Ref); Vector3 Pv1 = g.GetPosition(u, v1Ref); Vector3 Pu = Pu1 - Pu0; Vector3 Pv = Pv1 - Pv0; Vector3 aDir = Vector3.Normalize(Vector3.Cross(Pu, rec.NormalAtIntersect)); Vector3 bDir = Vector3.Normalize(Vector3.Cross(Pv, rec.NormalAtIntersect)); Vector3 D = (dDv * aDir) - (dDu * bDir); norm = rec.NormalAtIntersect + D; norm.Normalize(); } return(norm); }
/// <summary> /// UV are values between 0 to 1, maps to texture width/height linearly and returns the /// corresponding textile. /// </summary> /// <param name="u">value between 0 to 1</param> /// <param name="v">value between 0 to 1</param> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">the geometry</param> /// <returns></returns> public override Vector3 GetTexile(float u, float v, IntersectionRecord rec, RTGeometry g) { Vector3 norm = Vector3.UnitY; if ((u >= 0) && (u <= 1f) && (v >= 0) && (v <= 1f)) { int u0Index=0, u1Index=0, v0Index=0, v1Index=0; float uIndex, vIndex; float bumpAtu0v0, bumpAtu0v1, bumpAtu1v0, bumpAtu1v1; lock (mTextureImage) { GetIndices(u, mTextureImage.Width, ref u0Index, ref u1Index); GetIndices(v, mTextureImage.Height, ref v0Index, ref v1Index); v0Index = mTextureImage.Height - 1 - v0Index; v1Index = mTextureImage.Height - 1 - v1Index; uIndex = u * mTextureImage.Width; vIndex = mTextureImage.Height - 1 - (v * mTextureImage.Height); bumpAtu0v0 = ColorToVec(mTextureImage.GetPixel(u0Index, v0Index)).Length(); bumpAtu1v0 = ColorToVec(mTextureImage.GetPixel(u1Index, v0Index)).Length(); bumpAtu0v1 = ColorToVec(mTextureImage.GetPixel(u0Index, v1Index)).Length(); bumpAtu1v1 = ColorToVec(mTextureImage.GetPixel(u1Index, v1Index)).Length(); } float du = uIndex - u0Index; float dv = vIndex - v1Index; float bumpAtV0 = du * bumpAtu1v0 + (1 - du) * bumpAtu0v0; float bumpAtV1 = du * bumpAtu1v1 + (1 - du) * bumpAtu0v1; float bumpAtU0 = dv * bumpAtu0v1 + (1 - dv) * bumpAtu0v0; float bumpAtU1 = dv * bumpAtu1v1 + (1 - dv) * bumpAtu1v0; float dDu = mGain * (bumpAtV1 - bumpAtV0); float dDv = mGain * (bumpAtU1 - bumpAtU0); float u0Ref, u1Ref, v0Ref, v1Ref; u0Ref = u0Index * mInvWidth; u1Ref = u1Index * mInvWidth; v0Ref = v0Index * mInvHeight; v1Ref = v1Index * mInvHeight; Vector3 Pu0 = g.GetPosition(u0Ref, v); Vector3 Pu1 = g.GetPosition(u1Ref, v); Vector3 Pv0 = g.GetPosition(u, v0Ref); Vector3 Pv1 = g.GetPosition(u, v1Ref); Vector3 Pu = Pu1 - Pu0; Vector3 Pv = Pv1 - Pv0; Vector3 aDir = Vector3.Normalize(Vector3.Cross(Pu, rec.NormalAtIntersect)); Vector3 bDir = Vector3.Normalize(Vector3.Cross(Pv, rec.NormalAtIntersect)); Vector3 D = (dDv * aDir) - (dDu * bDir); norm = rec.NormalAtIntersect + D; norm.Normalize(); } return norm; }
public Vector3 TextureLookup(IntersectionRecord rec, RTGeometry g) { if (mTexture.NeedUV()) { float u = 0f, v = 0f; g.GetUV(rec.IntersectPosition, rec.HitPtBC, ref u, ref v); return mTexture.GetTexile(u, v, rec, g); } else { return mTexture.GetTexile(rec, g); } }
public Vector3 TextureLookup(IntersectionRecord rec, RTGeometry g) { if (mTexture.NeedUV()) { float u = 0f, v = 0f; g.GetUV(rec.IntersectPosition, rec.HitPtBC, ref u, ref v); return(mTexture.GetTexile(u, v, rec, g)); } else { return(mTexture.GetTexile(rec, g)); } }
/// <summary> /// UV are values between 0 to 1, maps to texture width/height linearly and returns the /// corresponding textile. /// </summary> /// <param name="u">value between 0 to 1</param> /// <param name="v">value between 0 to 1</param> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">the geometry</param> /// <returns></returns> public override Vector3 GetTexile(float u, float v, IntersectionRecord rec, RTGeometry g) { float uVal = u * mURepeat; float vVal = v * mVRepeat; int uBit = ((int)uVal) % 2; int vBit = ((int)vVal) % 2; Vector3 c = mColor1; if (uBit == vBit) c = mColor2; return c; }
private void ComputeVisibility(Ray r, IntersectionRecord rec, int exceptGeomIndex) { #if KDTREE mKdTree.ComputeVisibility(r, rec, exceptGeomIndex); #else for (int i = 0; i < mSceneDatabase.GetNumGeom(); i++) { if (i != exceptGeomIndex) { RTGeometry g = mSceneDatabase.GetGeom(i); g.Intersect(r, rec); } } #endif }
/// <summary> /// (x,y,z) is the visible pt, returns solid texture value at x,y,z /// </summary> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">The geometry</param> /// <returns></returns> public override Vector3 GetTexile(IntersectionRecord rec, RTGeometry g) { float x = rec.IntersectPosition.X; float y = rec.IntersectPosition.Y; float z = rec.IntersectPosition.Z; float n = ComputeTurbulanceNoise(x, y, z); Vector3 v = new Vector3(x, y, z); v.Normalize(); float useU = Vector3.Dot(mDirection, v) + n; float theta = useU * mThetaRange; float sineV = 0.5f * (((float) Math.Sin(theta)) + 1); return (sineV * mColor1) + (1f - sineV) * mColor2; }
/// <summary> /// (x,y,z) is the visible pt, returns solid texture value at x,y,z /// </summary> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">The geometry</param> /// <returns></returns> public override Vector3 GetTexile(IntersectionRecord rec, RTGeometry g) { float x = rec.IntersectPosition.X; float y = rec.IntersectPosition.Y; float z = rec.IntersectPosition.Z; float n = ComputeTurbulanceNoise(x, y, z); Vector3 v = new Vector3(x, y, z); v.Normalize(); float useU = Vector3.Dot(mDirection, v) + n; float theta = useU * mThetaRange; float sineV = 0.5f * (((float)Math.Sin(theta)) + 1); return((sineV * mColor1) + (1f - sineV) * mColor2); }
/// <summary> /// UV are values between 0 to 1, maps to texture width/height linearly and returns the /// corresponding textile. /// </summary> /// <param name="u">value between 0 to 1</param> /// <param name="v">value between 0 to 1</param> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">the geometry</param> /// <returns></returns> public override Vector3 GetTexile(float u, float v, IntersectionRecord rec, RTGeometry g) { float uVal = u * mURepeat; float vVal = v * mVRepeat; int uBit = ((int)uVal) % 2; int vBit = ((int)vVal) % 2; Vector3 c = mColor1; if (uBit == vBit) { c = mColor2; } return(c); }
/// <summary> /// UV are values between 0 to 1, maps to texture width/height linearly and returns the /// corresponding textile. /// </summary> /// <param name="u">value between 0 to 1</param> /// <param name="v">value between 0 to 1</param> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">the geometry</param> /// <returns></returns> /// /// only available to Vista and later /// [MethodImpl(MethodImplOptions.Synchronized)] public override Vector3 GetTexile(float u, float v, IntersectionRecord rec, RTGeometry g) { System.Drawing.Color c = System.Drawing.Color.Black; if (null != mTextureImage) { if ((u >= 0) && (u <= 1f) && (v >= 0) && (v <= 1f)) { lock (mTextureImage) { int x = (int)(u * (mTextureImage.Width - 1) + 0.5f); int y = (int)(v * (mTextureImage.Height - 1) + 0.5f); y = mTextureImage.Height - y - 1; c = mTextureImage.GetPixel(x, y); } } } return ColorToVec(c); }
/// <summary> /// UV are values between 0 to 1, maps to texture width/height linearly and returns the /// corresponding textile. /// </summary> /// <param name="u">value between 0 to 1</param> /// <param name="v">value between 0 to 1</param> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">the geometry</param> /// <returns></returns> /// /// only available to Vista and later /// [MethodImpl(MethodImplOptions.Synchronized)] public override Vector3 GetTexile(float u, float v, IntersectionRecord rec, RTGeometry g) { System.Drawing.Color c = System.Drawing.Color.Black; if (null != mTextureImage) { if ((u >= 0) && (u <= 1f) && (v >= 0) && (v <= 1f)) { lock (mTextureImage) { int x = (int)(u * (mTextureImage.Width - 1) + 0.5f); int y = (int)(v * (mTextureImage.Height - 1) + 0.5f); y = mTextureImage.Height - y - 1; c = mTextureImage.GetPixel(x, y); } } } return(ColorToVec(c)); }
/// <summary> /// </summary> /// <param name="u">value between 0 to 1</param> /// <param name="v">value between 0 to 1</param> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">the geometry</param> /// <returns></returns> public override Vector3 GetTexile(float u, float v, IntersectionRecord rec, RTGeometry g) { float useU = u * mURepeat; useU = useU - ((int)useU); float useV = v * mVRepeat; useV = useV - ((int)useV); Vector3 resultColor = mColor1; if ((useU > kLowBound) && (useU < kUpBound)) { resultColor = GetGridColor(useU); } else if ((useV > kLowBound) && (useV < kUpBound)) { resultColor = GetGridColor(useV); } return resultColor; }
/// <summary> /// </summary> /// <param name="u">value between 0 to 1</param> /// <param name="v">value between 0 to 1</param> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">the geometry</param> /// <returns></returns> public override Vector3 GetTexile(float u, float v, IntersectionRecord rec, RTGeometry g) { float useU = u * mURepeat; useU = useU - ((int)useU); float useV = v * mVRepeat; useV = useV - ((int)useV); Vector3 resultColor = mColor1; if ((useU > kLowBound) && (useU < kUpBound)) { resultColor = GetGridColor(useU); } else if ((useV > kLowBound) && (useV < kUpBound)) { resultColor = GetGridColor(useV); } return(resultColor); }
/// <summary> /// (x,y,z) is the visible pt, returns solid texture value at x,y,z /// </summary> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">The geometry</param> /// <returns></returns> public virtual Vector3 GetTexile(IntersectionRecord rec, RTGeometry g) { return Vector3.Zero; }
/// <summary> /// </summary> /// <param name="u">value between 0 to 1</param> /// <param name="v">value between 0 to 1</param> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">the geometry</param> /// <returns></returns> public override Vector3 GetTexile(float u, float v, IntersectionRecord rec, RTGeometry g) { float useU = u * mRepeat; useU = useU - ((int)useU); return (useU * mColor2) + (1 - useU) * mColor1; }
/// <summary> /// (x,y,z) is the visible pt, returns solid texture value at x,y,z /// </summary> /// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">The geometry</param> /// <returns></returns> public virtual Vector3 GetTexile(IntersectionRecord rec, RTGeometry g) { return(Vector3.Zero); }
/// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">The geometry</param> /// <returns></returns> public override Vector3 GetTexile(IntersectionRecord rec, RTGeometry g) { float noise = ComputeFractalNoise(rec.IntersectPosition.X, rec.IntersectPosition.Y, rec.IntersectPosition.Z); return((noise * mColor1) + ((1f - noise) * mColor2)); }
private const float kDepthImageDist = 1f; // depth map image plane distance from the light source private void InitDepthMap(SceneDatabase sceneDatabase) { if (null != mDepthMap) { return; // done } // remember!! mDireciton is L, or a vector from Visible point to the light. // Here we want direction from light source towards the scene Vector3 useDir = -mDirection; // 1. Find a Up direction // guess up direction to be (0, 1, 0), if view direction is also this, // use (1, 0, 0) as view direction Vector3 up = Vector3.UnitY; if (Math.Abs(Vector3.Dot(up, useDir)) > 0.99999) { up = Vector3.UnitX; } // 2. define Orthonormal base Vector3 sideV = Vector3.Cross(up, useDir); up = Vector3.Cross(useDir, sideV); sideV.Normalize(); up.Normalize(); // 3. compute the depth map image plane, // define the image plane to be located at ... a distance of kDepthImageDist away Vector3 ptOnImage = mPosition + kDepthImageDist * useDir; // 4. compute depth map image size float halfImageSize = kDepthImageDist * (float)Math.Tan(mOuterAngle / 2f); // 5. Compute the 4 vertices the defines the depth map Vector3[] v = new Vector3[4]; v[0] = ptOnImage - halfImageSize * up - halfImageSize * sideV; v[1] = ptOnImage - halfImageSize * up + halfImageSize * sideV; v[2] = ptOnImage + halfImageSize * up + halfImageSize * sideV; v[3] = ptOnImage + halfImageSize * up - halfImageSize * sideV; // 6. create a Geometry that represents the map // ** Be caureful **!! // RTRectangle uses v[0] as the origin for texture lookup // we _MUST_ follow the same in order to take advante of GetUV() function! mDepthMapGeom = new RTRectangle(v); // 7. Now allocate memory for the actual map mDepthMap = new float[mRes][]; mGeomID = new int[mRes][]; for (int i = 0; i < mRes; i++) { mDepthMap[i] = new float[mRes]; mGeomID[i] = new int[mRes]; } // now, trace rays through each of the pixels in the depth map and record the depth and geomID float pixelSize = halfImageSize / (0.5f * mRes); Vector3 upPixelVector = pixelSize * up; Vector3 sidePixelVector = pixelSize * sideV; for (int y = 0; y < mRes; y++) { Vector3 yDisp = v[0] + (y + 0.5f) * upPixelVector; for (int x = 0; x < mRes; x++) { Vector3 pixelPos = ((x + 0.5f) * sidePixelVector) + yDisp; Ray r = new Ray(mPosition, pixelPos); IntersectionRecord rec = new IntersectionRecord(); for (int i = 0; i < sceneDatabase.GetNumGeom(); i++) { RTGeometry g = sceneDatabase.GetGeom(i); g.Intersect(r, rec); } mDepthMap[x][y] = rec.HitDistance; // closes intersection distance, any object that is // further away from the light than this distance is in the shadow of this light mGeomID[x][y] = rec.GeomIndex; // this object can never be in shadow, becuase it is the closest to the light! } } }
/// <summary> /// Geometry: Can be access as an array, the collection is a simple array /// </summary> public void AddGeom(RTGeometry g) { g.SetResourceIndex(mAllGeoms.Count); mAllGeoms.AddResource(g); }
/// <param name="rec">IntersectionRecord to be texture mapped</param> /// <param name="g">The geometry</param> /// <returns></returns> public override Vector3 GetTexile(IntersectionRecord rec, RTGeometry g) { float noise = ComputeFractalNoise(rec.IntersectPosition.X, rec.IntersectPosition.Y, rec.IntersectPosition.Z); return (noise * mColor1) + ((1f - noise) * mColor2); }