分享例程
界面截图:
备注说明:
关于世界坐标转屏幕坐标的D3DXVec3Project”转换纯源码
C#源码: 参考一:
```csharp
using System.Numerics;
public struct Vector3
{
public float X, Y, Z;
public Vector3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
public static Vector3 operator *(Vector3 vector, Matrix4x4 matrix)
{
return new Vector3(
(vector.X * matrix.M11 + vector.Y * matrix.M21 + vector.Z * matrix.M31 + 1 * matrix.M41),
(vector.X * matrix.M12 + vector.Y * matrix.M22 + vector.Z * matrix.M32 + 1 * matrix.M42),
(vector.X * matrix.M13 + vector.Y * matrix.M23 + vector.Z * matrix.M33 + 1 * matrix.M43)
);
}
}
public struct Matrix4x4
{
public float M11, M12, M13, M14;
public float M21, M22, M23, M24;
public float M31, M32, M33, M34;
public float M41, M42, M43, M44;
// Constructor and other methods would be defined here...
}
public class MatrixOperations
{
public static Matrix4x4 Multiply(Matrix4x4 left, Matrix4x4 right)
{
return new Matrix4x4()
{
M11 = left.M11 * right.M11 + left.M12 * right.M21 + left.M13 * right.M31 + left.M14 * right.M41,
M12 = left.M11 * right.M12 + left.M12 * right.M22 + left.M13 * right.M32 + left.M14 * right.M42,
M13 = left.M11 * right.M13 + left.M12 * right.M23 + left.M13 * right.M33 + left.M14 * right.M43,
M14 = left.M11 * right.M14 + left.M12 * right.M24 + left.M13 * right.M34 + left.M14 * right.M44,
M21 = left.M21 * right.M11 + left.M22 * right.M21 + left.M23 * right.M31 + left.M24 * right.M41,
M22 = left.M21 * right.M12 + left.M22 * right.M22 + left.M23 * right.M32 + left.M24 * right.M42,
M23 = left.M21 * right.M13 + left.M22 * right.M23 + left.M23 * right.M33 + left.M24 * right.M43,
M24 = left.M21 * right.M14 + left.M22 * right.M24 + left.M23 * right.M34 + left.M24 * right.M44,
M31 = left.M31 * right.M11 + left.M32 * right.M21 + left.M33 * right.M31 + left.M34 * right.M41,
M32 = left.M31 * right.M12 + left.M32 * right.M22 + left.M33 * right.M32 + left.M34 * right.M42,
M33 = left.M31 * right.M13 + left.M32 * right.M23 + left.M33 * right.M33 + left.M34 * right.M43,
M34 = left.M31 * right.M14 + left.M32 * right.M24 + left.M33 * right.M34 + left.M34 * right.M44,
M41 = left.M41 * right.M11 + left.M42 * right.M21 + left.M43 * right.M31 + left.M44 * right.M41,
M42 = left.M41 * right.M12 + left.M42 * right.M22 + left.M43 * right.M32 + left.M44 * right.M42,
M43 = left.M41 * right.M13 + left.M42 * right.M23 + left.M43 * right.M33 + left.M44 * right.M43,
M44 = left.M41 * right.M14 + left.M42 * right.M24 + left.M43 * right.M34 + left.M44 * right.M44,
};
}
}
public static Vector3 ProjectPoint(Vector3 point, Matrix world, Matrix view, Matrix projection, int viewportWidth, int viewportHeight)
{
Matrix combinedMatrix = world * view * projection;
Vector3 transformedPoint = point * combinedMatrix;
// 透视除法
float oneOverW = 1.0f / transformedPoint.Z;
Vector3 screenPoint = new Vector3(
transformedPoint.X * oneOverW,
transformedPoint.Y * oneOverW,
transformedPoint.Z * oneOverW
);
// 转换到视口坐标
screenPoint.X = screenPoint.X * (viewportWidth / 2) + viewportWidth / 2;
screenPoint.Y = -screenPoint.Y * (viewportHeight / 2) + viewportHeight / 2; // 注意Y轴的翻转
return screenPoint;
}
}
这里的`Vector3`和`Matrix4x4`结构体以及相关的运算符重载和方法都是简化的版本
E没有相对应的库只能构造一个`Vector3`和`Matrix4x4`类型两份C源码+一份E纯源码都在文件里哦
矩阵相乘 回帖查看密码送精币哦→qun398897142[/hide]
纯源码文件下载→[hide]源码密码:
易语言D3DXVec3Project运算纯源码.rar
(21.47 KB, 下载次数: 8)