Vector4 DiffuseIBL(Vector2 Random, Vector3 N, BitmapData &img, bool hdr){ Vector4 DiffuseLighting; unsigned NumSamples = 4096 * 2; //for i in range(NumSamples) : for (unsigned int i = 0; i < NumSamples; i++){ Vector2 E = Hammersley(i, NumSamples, Random); Vector3 C = CosineSampleHemisphere(E); Vector3 L = TangentToWorld(C, N); float NoL = saturate(N.dot(L));; //#print("NoL", NoL); if (NoL > 0){ Vector2 uv2 = pos2uv(L); Vector4 result = img.getPixel32Int(uv2.x * img.width, uv2.y * img.height); //#lambert = DiffuseColor * NoL / PI //#pdf = NoL / PI float e = 1.0; if (hdr) { e = result.w - 128.0; e = pow(2, e); } DiffuseLighting.x += result.x * e; DiffuseLighting.y += result.y * e; DiffuseLighting.z += result.z * e; DiffuseLighting.w += 255; } } float Weight = 1.0 / NumSamples; DiffuseLighting = DiffuseLighting * (Weight); return DiffuseLighting; }