将3d世界位置转换为2d位置

时间:2017-05-04 18:57:55

标签: opengl math matrix world-of-warcraft

我试图将XYZ从魔兽世界中的玩家转换为屏幕上的XY。 为此,我找到了播放器XYZ,相机矩阵,相机XYZ,Fov,NearClip,FarClip和CameraAspect。

我在游戏中找到的参数:

相机矩阵

[-0.79|-0.51|-0.34]
[ 0.55|-0.84| 0.00]
[-0.28|-0.18| 0.94]

玩家(目标)

X: -9468
Y: 46
Z: 56

相机

X: -9438
Y: 75
Z: 67

Fov: 1.57
FarClip: 28
NearClip: 28
CameraAspect: 3.7

如果计算了所有内容,则在使用1920 x 1080屏幕时显示X:774 Y:433。 一位朋友给了我这段代码,但我不明白:/

using System;
using SlimDX;

#pragma warning disable 649
#pragma warning disable 169

namespace VanillaMagic
{
    public static class Camera
    {
        internal static IntPtr BaseAddress
        {
            get
            {
                var ptr = WoW.hook.Memory.Read<IntPtr>(Offsets.Camera.CameraPtr, true);
                return WoW.hook.Memory.Read<IntPtr>(ptr + Offsets.Camera.CameraPtrOffset);
            }
        }

        private static Offsets.CGCamera cam => WoW.hook.Memory.Read<Offsets.CGCamera>(BaseAddress);

        public static float X => cam.Position.X;
        public static float Y => cam.Position.Y;
        public static float Z => cam.Position.Z;
        public static float FOV => cam.FieldOfView;
        public static float NearClip => cam.NearClip;
        public static float FarClip => cam.FarClip;
        public static float Aspect => cam.Aspect;

        private static Matrix Matrix
        {
            get
            {
                var bCamera = WoW.hook.Memory.ReadBytes(BaseAddress + Offsets.Camera.CameraMatrix, 36);
                var m = new Matrix();
                m[0, 0] = BitConverter.ToSingle(bCamera, 0);
                m[0, 1] = BitConverter.ToSingle(bCamera, 4);
                m[0, 2] = BitConverter.ToSingle(bCamera, 8);
                m[1, 0] = BitConverter.ToSingle(bCamera, 12);
                m[1, 1] = BitConverter.ToSingle(bCamera, 16);
                m[1, 2] = BitConverter.ToSingle(bCamera, 20);
                m[2, 0] = BitConverter.ToSingle(bCamera, 24);
                m[2, 1] = BitConverter.ToSingle(bCamera, 28);
                m[2, 2] = BitConverter.ToSingle(bCamera, 32);

                return m;
            }
        }

        public static Vector2 WorldToScreen(float x, float y, float z)
        {
            var Projection = Matrix.PerspectiveFovRH(FOV * 0.5f, Aspect, NearClip, FarClip);

            var eye = new Vector3(X, Y, Z);
            var lookAt = new Vector3(X + Matrix[0, 0], Y + Matrix[0, 1], Z + Matrix[0, 2]);
            var up = new Vector3(0f, 0f, 1f);

            var View = Matrix.LookAtRH(eye, lookAt, up);
            var World = Matrix.Identity;

            var WorldPosition = new Vector3(x, y, z);

            var ScreenPosition = Vector3.Project(WorldPosition, 0f, 0f, WindowHelper.WindowWidth, WindowHelper.WindowHeight, NearClip, FarClip, World*View*Projection);
            return new Vector2(ScreenPosition.X, ScreenPosition.Y-20f);
        }
    }
}

有人可以帮我解决数学问题,以及如何计算这些数据?

0 个答案:

没有答案