2010-09-08 06:09:03 +00:00
|
|
|
|
#region === Copyright (c) 2010 Pascal van der Heiden ===
|
|
|
|
|
|
|
|
|
|
using CodeImp.DoomBuilder.Geometry;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2012-11-27 21:12:20 +00:00
|
|
|
|
namespace CodeImp.DoomBuilder.BuilderModes
|
2010-09-08 06:09:03 +00:00
|
|
|
|
{
|
|
|
|
|
internal struct TexturePlane
|
|
|
|
|
{
|
|
|
|
|
// Geometry coordinates (left-top, right-top and right-bottom)
|
|
|
|
|
public Vector3D vlt;
|
|
|
|
|
public Vector3D vrt;
|
|
|
|
|
public Vector3D vrb;
|
|
|
|
|
|
|
|
|
|
// Texture coordinates on the points above
|
|
|
|
|
public Vector2D tlt;
|
|
|
|
|
public Vector2D trt;
|
|
|
|
|
public Vector2D trb;
|
|
|
|
|
|
|
|
|
|
// This returns interpolated texture coordinates for the point p on the plane defined by vlt, vrt and vrb
|
|
|
|
|
public Vector2D GetTextureCoordsAt(Vector3D p)
|
|
|
|
|
{
|
|
|
|
|
// Delta vectors
|
|
|
|
|
Vector3D v31 = vrb - vlt;
|
|
|
|
|
Vector3D v21 = vrt - vlt;
|
|
|
|
|
Vector3D vp1 = p - vlt;
|
2020-05-21 12:20:02 +00:00
|
|
|
|
|
2010-09-08 06:09:03 +00:00
|
|
|
|
// Compute dot products
|
2020-05-21 12:20:02 +00:00
|
|
|
|
double d00 = Vector3D.DotProduct(v31, v31);
|
|
|
|
|
double d01 = Vector3D.DotProduct(v31, v21);
|
|
|
|
|
double d02 = Vector3D.DotProduct(v31, vp1);
|
|
|
|
|
double d11 = Vector3D.DotProduct(v21, v21);
|
|
|
|
|
double d12 = Vector3D.DotProduct(v21, vp1);
|
2010-09-08 06:09:03 +00:00
|
|
|
|
|
|
|
|
|
// Compute barycentric coordinates
|
2020-05-21 12:20:02 +00:00
|
|
|
|
double invd = 1.0f / (d00 * d11 - d01 * d01);
|
|
|
|
|
double u = (d11 * d02 - d01 * d12) * invd;
|
|
|
|
|
double v = (d00 * d12 - d01 * d02) * invd;
|
2010-09-08 06:09:03 +00:00
|
|
|
|
|
|
|
|
|
// Delta texture coordinates
|
|
|
|
|
Vector2D t21 = trt - tlt;
|
|
|
|
|
Vector2D t31 = trb - tlt;
|
|
|
|
|
|
|
|
|
|
// Lerp
|
|
|
|
|
return tlt + t31 * u + t21 * v;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|