public void rebuildLightmap() { Console.WriteLine(">> Rebuilding Light Map ... [" + lights + " light sources]"); Vector l; float fnx, fny, angle, phongfact, sheen, spread; uint diffuse, specular, cos, dr, dg, db, sr, sg, sb; for (var ny = -128; ny < 128; ny++) { fny = (float)ny * divBy128; for (var nx = -128; nx < 128; nx++) { pos = nx + 128 + ((ny + 128) << 8); fnx = nx * divBy128; sr = sg = sb = 0; dr = ColorUtility.getRed(ambient); dg = ColorUtility.getGreen(ambient); db = ColorUtility.getBlue(ambient); for (var i = 0; i < lights; i++) { l = light[i].v; diffuse = light[i].diffuse; specular = light[i].specular; sheen = light[i].highlightSheen * divBy255; spread = (float)light[i].highlightSpread * divBy4096; spread = (spread < 0.01f)?0.01f:spread; cos = (uint)(255 * Vector.Angle(light[i].v, new Vector(fnx, fny, sphere[pos]))); cos = (cos > 0)?cos:0; dr += (ColorUtility.getRed(diffuse) * cos) >> 8; dg += (ColorUtility.getGreen(diffuse) * cos) >> 8; db += (ColorUtility.getBlue(diffuse) * cos) >> 8; phongfact = sheen * (float)Math.Pow(cos * divBy255, 1 / spread); sr += (uint)(ColorUtility.getRed(specular) * phongfact); sg += (uint)(ColorUtility.getGreen(specular) * phongfact); sb += (uint)(ColorUtility.getBlue(specular) * phongfact); } this.diffuse[pos] = ColorUtility.getCropColor(dr, dg, db); this.specular[pos] = ColorUtility.getCropColor(sr, sg, sb); } } }