private static void UpdateProjViewMatrix(Size clientSize) { var verticalFov = (float)Math.PI / 4; var aspect = clientSize.Width / (float)clientSize.Height; var proj = MatrixCM.PerspectiveFovRH(verticalFov, aspect, 1.0f, 5000.0f); projView = proj * view; if (zfirst) { zfirst = false; var lookat = new Vector3(0, 0.5f, 0); //var pos = new Vector4(lookat + 0.1f * (cameraEye - lookat), 1);//new Vector4(1, 2, 3, 1); var pos = new Vector4(0.5f, 1.0f, 0.5f, 1); var projViewRm = projView; projViewRm.Transpose(); var homogeneous = Vector4.Transform(pos, projViewRm); Console.WriteLine("pos: " + pos); Console.WriteLine("posh: " + homogeneous); homogeneous /= homogeneous.W; Console.WriteLine("poshn: " + homogeneous); var projViewRmInv = projViewRm; projViewRmInv.Invert(); Console.WriteLine(); Console.WriteLine(projViewRmInv * projViewRm); Console.WriteLine(); Console.WriteLine(projViewRm * projViewRmInv); Console.WriteLine(); var x = Vector4.Transform(homogeneous, projViewRmInv); Console.WriteLine(x); x /= x.W; Console.WriteLine(x); } }
public void AddSpotlight(Vector3 position, Vector3 lookat, Vector3 up, float theta, Color color, float near, float far, float daRatioConstant, float daRatioLinear, float daRatioQuadratic, float edgeSpotlightAttenuationPercent = 1.0f / 256.0f) { var projCm = MatrixCM.PerspectiveFovRH(theta, 1.0f, near, far); var viewCm = MatrixCM.ViewLookAtRH(position, lookat, up); // solve distance attenuation constants: 1/256 = atten = 1 / (x * darc + far * x * darl + far * far * x * darq) // 256 = x * darc + far * x * darl + far * far * x * darq // 256 = x * (darc + far * darl + far * far * darq) var x = 256 / (daRatioConstant + far * daRatioLinear + far * far * daRatioQuadratic); //Console.WriteLine(x + " " + daRatioConstant + " " + daRatioLinear + " " + daRatioQuadratic); var direction = lookat - position; direction.Normalize(); //Console.WriteLine("@8: " + (daRatioConstant * x + daRatioLinear * x * 8 + daRatioQuadratic * x * 8 * 8)); //Console.WriteLine("@far: " + (daRatioConstant * x + daRatioLinear * x * far + daRatioQuadratic * x * far * far)); // solve spotlight attenuation constant. // edge% = atten_spotlight = dot(spotlightDirectionUnit, objectDirectionUnit) ^ power // edge% = cos(theta) ^ power // Math.Log(edge%, cos(theta)) = power var power = Math.Log(edgeSpotlightAttenuationPercent, Math.Cos(theta)); //Console.WriteLine("Power: " + power); //Console.WriteLine("@far: " + Math.Pow(Math.Cos(theta), power)); var projViewCm = projCm * viewCm; AddSpotlight(new SpotlightInfo { Origin = position, Direction = direction, Color = color, DistanceAttenuationConstant = x * daRatioConstant, DistanceAttenuationLinear = x * daRatioLinear, DistanceAttenuationQuadratic = x * daRatioQuadratic, SpotlightAttenuationPower = (float)power, ProjViewCM = projViewCm, }); }