Obtaining world transformations in matrix form

Any issues, problems or troubleshooting topics related to the additional features present in the Prepar3D Professional Plus client application.
Post Reply
iggy-nacho
Posts: 1
Joined: Mon Oct 30, 2023 10:31 pm

Obtaining world transformations in matrix form

Post by iggy-nacho »

Hello,

This is my first time posting a question on this forum. please let me know if there are any conventions or information i should know or submit in order for the question to be answered. I've read the online PDK docs quite extensively. I'm currently on v5 but willing to try v6 if these things change there at all.

I am trying to render objects (meshes) within the scene. I've based off of the PDK example Texture and Effect rendering, combined with the D3DMesh.h/.cpp available in the SdkRenderUtils project within the SDK Samples solution. The trouble comes when trying to compose a WorldViewPerspective Matrix for meshes (prior to submitting for rendering)

Here is what I've seen. ObjectWorldTransform is not a homogeneous transform matrix (like you would need / what I'm looking for) that you can combine with the CameraViewMatrix and PerspectiveView Matrix (both obtainable from the CameraSystem).

The CameraViewMatrix seems to be a rotation-only matrix. If that's the case...is the current camera the "origin" of the scene? is this described in the documentation somewhere?

In essence, I'm tring to figure out how I can generate the appropriate tranformation matrix for custom rendered meshes in the P3D PDK.

Thanks,
User avatar
Beau Hollis
Lockheed Martin
Posts: 2452
Joined: Wed Oct 06, 2010 3:25 pm

Re: Obtaining world transformations in matrix form

Post by Beau Hollis »

The view matrix typically does not include any offsets, so indeed the camera is origin in world space. For our internal rendering we use a custom transform that includes a 64-bit xyz from the center of the earth and has a local level matrix pre-multiplied. We construct a standard world matrix on the GPU using the camera's transform as the origin.

If you request the camera transform at the eye reference, and zero out the pbh, you'll have an ObjectWorldTransform that matches the P3D world coordinate origin. You could then use the CalculateBodyRelativeOffset to get localt transforms (XYZ and PBH) which you can convert into a matrix.

The RenderHelper class has some static helpers for d3d plugins. The source for this should be included in the SdkRenderUtil project in the SDK.

It includes these conversions between ObjectLocalTransform and float4x4:

Code: Select all

void RenderHelper::MatrixToTransform(P3DMath::float4x4& matIn, ObjectLocalTransform& transformOut)
{
    transformOut.PBH.Heading = RAD_2_DEG * atan2(matIn.z.x, matIn.z.z);
    transformOut.PBH.Pitch = RAD_2_DEG * atan2(-matIn.z.y, sqrt(matIn.z.x * matIn.z.x + matIn.z.z * matIn.z.z));
    transformOut.PBH.Bank = RAD_2_DEG * atan2(matIn.x.y, matIn.y.y);
    transformOut.XYZ.X = matIn.w.x;
    transformOut.XYZ.Y = matIn.w.y;
    transformOut.XYZ.Z = matIn.w.z;
}

void RenderHelper::TransformToMatrix(ObjectLocalTransform& transformIn, P3DMath::float4x4& matOut)
{
    float sin_p, cos_p;
    float sin_b, cos_b;
    float sin_h, cos_h;

    cos_p = (float)cos(DEG_2_RAD * transformIn.PBH.Pitch);
    sin_p = (float)sin(DEG_2_RAD * transformIn.PBH.Pitch);
    cos_b = (float)cos(DEG_2_RAD * transformIn.PBH.Bank);
    sin_b = (float)sin(DEG_2_RAD * transformIn.PBH.Bank);
    cos_h = (float)cos(DEG_2_RAD * transformIn.PBH.Heading);
    sin_h = (float)sin(DEG_2_RAD * transformIn.PBH.Heading);

    matOut.m[0][0] = cos_h * cos_b + sin_h * sin_p * sin_b;
    matOut.m[1][0] = sin_h * sin_p * cos_b - cos_h * sin_b;
    matOut.m[2][0] = sin_h * cos_p;
    matOut.m[3][0] = transformIn.XYZ.X;

    matOut.m[0][1] = cos_p * sin_b;
    matOut.m[1][1] = cos_p * cos_b;
    matOut.m[2][1] = -sin_p;
    matOut.m[3][1] = transformIn.XYZ.Y;

    matOut.m[0][2] = cos_h * sin_p * sin_b - sin_h * cos_b;
    matOut.m[1][2] = cos_h * sin_p * cos_b + sin_h * sin_b;
    matOut.m[2][2] = cos_h * cos_p;
    matOut.m[3][2] = transformIn.XYZ.Z;
} 
Beau Hollis
Prepar3D Software Architect
Post Reply