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); } } }
/// <summary> /// Backface culling and frustrum clipping. /// </summary> /// <param name="w"></param> /// <param name="h"></param> public void ClipFrustrum(int w, int h) { if (getParent().material == null) { visible = false; return; } outOfFrustrum = (p1.clipcode & p2.clipcode & p3.clipcode) != 0; if (outOfFrustrum) { visible = false; return; } if (n2.Z > 0.5) { visible = true; return; } triangleCenter.X = (p1.pos2.X + p2.pos2.X + p3.pos2.X); triangleCenter.Y = (p1.pos2.Y + p2.pos2.Y + p3.pos2.Y); triangleCenter.Z = (p1.pos2.Z + p2.pos2.Z + p3.pos2.Z); visible = Vector.Angle(triangleCenter, n2) > 0; }