Using Yaw and Pitch to create a rotation matrix and simulate an FPS camera

First of all, I have to say that what I want to do here is all about Points, not objects or anything else. And my use of OpenGL is more for calculation here.

So here is my problem. I want to show some 3d points in my 2d context (winform for example or image); For this, I wrote this function: (It is in C # and uses OpenTK (OpenGL shell), but I am sure that if you know OpenGL, you can understand this regardless of your knowledge of C #)

    private Vector3 GetProjectedLocation(
        Vector3 pointPosition,
        Vector3 cameraPosition,
        Matrix4 cameraRotationMatrix)
    {
        float horizentalFov = MathHelper.DegreesToRadians(60.0f);
        float viewRatio = (float)this.RenderSize.Width / this.RenderSize.Height;
        Vector3 cameraReference = new Vector3(0, 0, -1);
        Vector3 transformedReference = Vector3.Transform(cameraReference, Matrix4.Invert(cameraRotationMatrix));
        Vector3 cameraLookatTarget = cameraPosition + transformedReference;
        Matrix4 modelMatrix = Matrix4.Identity;
        Matrix4 viewMatrix = Matrix4.LookAt(cameraPosition, cameraLookatTarget, new Vector3(0, 1, 0));
        Matrix4 projectionMatrix = Matrix4.CreatePerspectiveFieldOfView(horizentalFov, viewRatio, 0.1f, 100.0f);
        Matrix4 viewModelMatrix = viewMatrix * modelMatrix;
        return Helper.Project(
            pointPosition,
            viewModelMatrix,
            projectionMatrix,
            new Rectangle(new Point(), this.RenderSize));
    }

I also wrote another function called Project cuz. I did not find it in OpenTK:

    public static Vector3 Project(Vector3 point, Matrix4 viewmodel, Matrix4 projection, Rectangle viewport)
    {
        Vector4 point4 = new Vector4(point, 1.0f);
        Vector4 pointModel = Vector4.Transform(point4, viewmodel);
        Vector4 pointProjection = Vector4.Transform(pointModel, projection);
        pointProjection.W = (float)(1.0 / pointProjection.W);
        pointProjection.X *= pointProjection.W;
        pointProjection.Y *= pointProjection.W;
        pointProjection.Z *= pointProjection.W;
        return new Vector3
                   {
                       X = (float)((pointProjection.X * 0.5 + 0.5) * viewport.Width + viewport.X),
                       Y = (float)((pointProjection.Y * 0.5 + 0.5) * viewport.Height + viewport.Y),
                       Z = (float)((1.0 + pointProjection.Z) * 0.5)
                   };
    }

(, ) , , . , , . Roll , .

, 90deg. , , , , . , (-20deg +20deg), . , Roll , 90deg, , . -90deg , Yaw roll .

, , , , . , .

, , . , ( , ), . . , , , :

    private Matrix4 CreateRotationMatrix(char axis, float radians)
    {
        float c = (float)Math.Cos(radians);
        float s = (float)Math.Sin(radians);

        if (axis == 'X')
        {
            return new Matrix4(1.0f, 0.0f, 0.0f, 0.0f, 0.0f, c, -s, 0.0f, 0.0f, s, c, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
        }
        if (axis == 'Y')
        {
            return new Matrix4(c, 0.0f, s, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -s, 0.0f, c, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
        }
        return new Matrix4(c, -s, 0.0f, 0.0f, s, c, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f);
    }

    private Matrix4 MatrixFromEulerAngles(Vector3 euler, string order)
    {
        return CreateRotationMatrix(order[2], GetEulerAngle(order[2], euler))
               * CreateRotationMatrix(order[1], GetEulerAngle(order[1], euler))
               * CreateRotationMatrix(order[0], GetEulerAngle(order[0], euler));
    }

    private float GetEulerAngle(char angle, Vector3 euler)
    {
        if (angle == 'X') return euler.X;
        if (angle == 'Y') return euler.Y;
        return angle == 'Z' ? euler.Z : 0f;
    }

?

, : "XYZ", "XZY", "YXZ", "YZX", "ZXY", "ZYX"

, .

+4
1

, LookAt . , "", "" , , . , , "" ((0,1,0) ) , , undefined , , .

"" LookAt :

Vector3 localUpVec = Vector3.Transform(new Vector3(0, 1, 0), Matrix4.Invert(cameraRotationMatrix));
Matrix4 viewMatrix = Matrix4.LookAt(cameraPosition, cameraLookatTarget, localUpVec);

, , LookAt :

Matrix4 viewMatrix = Matrix4.Subtract( Matrix4.Transpose(cameraRotationMatrix), Matrix4.Translation(cameraPosition) );
+1

Source: https://habr.com/ru/post/1547239/


All Articles