public static RealColor ComputeLightingEstimate(CompressedPhoton[] photons, RadiosityMaterial material, Vector3 surfaceNormal) { RealColor estimate = RealColor.Black.Copy(); for (int p = 0; p < photons.Length; p++) { float dotP = (surfaceNormal != Vector3.Empty) ? Vector3.Dot(surfaceNormal, photons[p].Direction) : 0.5f; if (dotP < 0) { continue; } estimate.Add(photons[p].Power.Copy().Multiply(dotP)); } estimate.Multiply((float)RadiosityHelper.RadiosityDiffuseConstant); estimate.Multiply(material.AmbientLight); return(estimate); }
private void CreateLightmap(float dist2, Vector3 badPoint, string path, int texIndex, float hemisphereSizeDivisor, TextureMap texMap, ref float maxIntensity) { int gatherCount = 100; int width = (int)(texMap.Width); // * RadiosityHelper.LightmapScale); int height = (int)(texMap.Height); // * RadiosityHelper.LightmapScale); float heightF = (float)texMap.Height; // *RadiosityHelper.LightmapScale; float widthF = (float)texMap.Width; // *RadiosityHelper.LightmapScale; float currentMaxIntensity = float.MinValue; float diffuse = (float)RadiosityHelper.RadiosityDiffuseConstant; widthF -= 1f; heightF -= 1f; SetOperation("Creating texture #" + (texIndex + 1)); //List<CompressedPhoton> photons; Bitmap bitm = new Bitmap(width, height, PixelFormat.Format32bppRgb); RadiosityMaterial mat; Vector3 norm; for (int x = 0; x < width; x++) { for (int y = 0; y < heightF; y++) { bitm.SetPixel(x, y, m_bsp[0].ConvertUVTo3D(x, y, texIndex, 1, out mat, out norm) != Vector3.Empty ? new RealColor(1, 1, 1).ToARGB() : RealColor.Black.ToARGB()); } } bitm.Save("k:\\convmap_" + texIndex + "_" + 1); //CreateLightmapWork(dist2, ref badPoint, texIndex, hemisphereSizeDivisor, texMap, width, height, heightF, widthF, ref currentMaxIntensity); // We are going to cycle through the materials of the bsp. EnhancedMesh[] meshes = m_bsp[0].GetMeshes(texIndex); for (int m = 0; m < meshes.Length; m++) { EnhancedMesh material = meshes[m]; int faceCount = material.Indices.Length; RectangleBounds <float> bounds = new RectangleBounds <float>(material.Vertices[0].u2, material.Vertices[0].v2); // First we're going to figure out a rectangle for this material. for (int v = 0; v < material.Vertices.Length; v++) { bounds.Update(material.Vertices[v].u2, material.Vertices[v].v2); } int x = (int)Math.Floor((widthF * bounds.left)); int startX = x; int endX = (int)Math.Ceiling(widthF * bounds.right); int y = (int)Math.Floor(heightF * bounds.top); int endY = (int)Math.Ceiling(heightF * bounds.bottom); //System.Diagnostics.Debugger.Break(); for (; y <= endY; y++) { x = startX; for (; x <= endX; x++) { RadiosityMaterial illMaterial; Vector3 faceNormal; //if (y == 81) // // if (x == 2) //System.Diagnostics.Debugger.Break(); Vector3 worldPoint = m_bsp[0].ConvertUVTo3D(x, y, texIndex, m, out illMaterial, out faceNormal); //AddDebugRay(worldPoint, faceNormal); if (worldPoint != Vector3.Empty) { AddDebugPoint(worldPoint, RadiosityDebugPointListID.EstimateLocations); var photons = photonMap.RetrieveNearestNeighbors((PhotonVector3)worldPoint, dist2, gatherCount, faceNormal); RealColor estimate = RealColor.Black; if (photons.Count > 0) { for (int p = 0; p < photons.Count; p++) { if (photons.list[p].photonPtr == null) { continue; } float dotP = Vector3.Dot(photons.list[p].photonPtr.Direction, faceNormal); if (dotP < 0) { dotP *= -1; //AddDebugPoint(photons.list[p].photonPtr //AddDebugRay(photons.list[p].photonPtr.position.ToVector3(), photons.list[p].photonPtr.Direction); } estimate.Add(photons.list[p].photonPtr.Power.Copy().Multiply(dotP)); } estimate.Multiply(diffuse); estimate.Multiply(hemisphereSizeDivisor); estimate.Multiply(illMaterial.AmbientLight); //estimate.Multiply(1f / photons.Count); if (RadiosityHelper.DoDistantLights) { if (illMaterial.DistantLightCount > 1) { estimate.LambertianShade(faceNormal, illMaterial.DistantLight2Color, illMaterial.DistantLight2Direction, illMaterial.AmbientLight); estimate.LambertianShade(faceNormal, illMaterial.DistantLight1Color, illMaterial.DistantLight1Direction, illMaterial.AmbientLight); } else if (illMaterial.DistantLightCount > 0) { estimate.LambertianShade(faceNormal, illMaterial.DistantLight1Color, illMaterial.DistantLight1Direction, illMaterial.AmbientLight); } } texMap.SetPixel(x, y, estimate); } else { texMap.SetPixel(x, y, new RealColor(Color.Pink)); } } //else continue; // if there is no normal, the estimate is useless. } } } if (currentMaxIntensity > maxIntensity) { maxIntensity = currentMaxIntensity; } texMap.SaveHDR(path + "lightmap_" + texIndex + ".hdr"); bitm = texMap.Filter(new LinearToneMapper(), false); bitm.Save(path + "lightmap_" + texIndex + ".bmp"); }
private void CreateRender() { int screenWidth = MdxRender.Device.Viewport.Width; int screenHeight = MdxRender.Device.Viewport.Height; float dist2 = RadiosityHelper.GatherDistance * RadiosityHelper.GatherDistance; Bitmap renderBitm;// = new Bitmap(screenWidth, screenHeight); float[,,] hdr = new float[screenWidth, screenHeight, 3]; ChangeStage(RadiosityStage.TextureGeneration, screenWidth * screenHeight); SetOperation("Rendering..."); int tenPercent = screenWidth / 10; int left = screenWidth / 2 - tenPercent; int right = screenWidth / 2 + tenPercent; for (int y = 0; y < screenHeight; y++) { for (int x = 0; x < screenWidth; x++) { Vector3 direction, origin; MdxRender.CalculatePickRayWorld(x, y, out direction, out origin); //AddDebugRay(origin, direction); RadiosityIntersection intersect = m_bsp[0].RadiosityIntersect(origin, direction); //AddDebugPoint(intersect.Position); if (!intersect.NoIntersection) { Vector3 normal = m_bsp[0].GetFaceNormal(intersect.LightmapIndex, intersect.MaterialIndex, intersect.FaceIndex); List <CompressedPhoton> photons = photonMap.RetrieveNearestNeighbors((PhotonVector3)intersect.Position, dist2, 10000); RealColor sum = RealColor.Black.Copy();//new RealColor(photons.Count, photons.Count, photons.Count); if (photons.Count == 0) { System.Diagnostics.Debugger.Break(); } foreach (CompressedPhoton phot in photons) { float dotP = Vector3.Dot(-phot.Direction, normal); if (dotP < 0) { dotP = 0; } sum.Add(phot.Power.Copy().Multiply(dotP)); } if ((sum.G > sum.B) && (sum.G > sum.R)) { if (x >= left) { if (x <= right) { if (photons.Count > 100) { AddDebugRay(origin, direction); AddDebugPoint(origin); foreach (var photon in photons) { AddDebugPoint(photon.position.ToVector3()); } } } } //System.Diagnostics.Debugger.Break(); } //sum.Multiply(1 / (dist2 * (float)Math.PI)); //sum.Multiply(intersect.Material.AmbientLight); for (int c = 0; c < 3; c++) { hdr[x, y, c] = sum[c]; } } else { hdr[x, y, 0] = 0; hdr[x, y, 1] = 0; hdr[x, y, 2] = 0; } IncrementProgress(); } } //renderBitm = LinearTonemap(hdr); Bitmap tempBitmap = new Bitmap(screenWidth, screenHeight); //Graphics g = Graphics.FromImage(tempBitmap); //g.DrawImage(renderBitm, 0, 0); //g.Dispose(); //renderBitm.Dispose(); tempBitmap.Save(@"C:\Users\David\render.bmp"); return; }