numpy で変換行列周りの確認

import numpy as np

def transform(pos):
    world = np.array(
        [
            [1.0, 0.0, 0.0, 0.0],
            [0.0, 1.0, 0.0, 0.0],
            [0.0, 0.0, 1.0, 0.0],
            [1.0, 2.0, 3.0, 1.0]
        ]
    )

    view = np.array(
        [
            [1.0, 0.0, 0.0, 0.0],
            [0.0, 1.0, 0.0, 0.0],
            [0.0, 0.0, 1.0, 0.0],
            [0.0, -1.0, 5.0, 1.0]
        ]
    )

    proj = np.array(
        [
            [0.75, 0.0, 0.0, 0.0],
            [0.0, 1.0, 0.0, 0.0],
            [0.0, 0.0, 1.00010002, 1.0],
            [0.0, 0.0, -0.0100010000, 0.0]
        ]
    )

    viewport = np.array(
        [
            [640.0/2, 0.0, 0.0, 0.0],
            [0.0, -480.0/2, 0.0, 0.0],
            [0.0, 0.0, 1.0, 0.0],
            [640.0/2, 480.0/2, 0.0, 1.0]
        ]
    )    
    
    pos = pos.dot(world)
    pos = pos.dot(view)
    pos = pos.dot(proj)    
    pos = pos.dot(viewport)
    print(pos / pos[3])

    
transform(np.array([-1.0, 1.0, -1.0, 1.0]))
transform(np.array([1.0, -1.0, -1.0, 1.0]))
transform(np.array([-1.0, -1.0, -1.0, 1.0]))

参考

https://docs.microsoft.com/en-us/windows/win32/direct3d9/transforms
https://docs.microsoft.com/en-us/windows/win32/dxtecharts/the-direct3d-transformation-pipeline

https://enginetrouble.net/2016/10/reconstructing-world-position-from-depth-2016.html
https://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/geometry/row-major-vs-column-major-vector?url=mathematics-physics-for-computer-graphics/geometry/row-major-vs-column-major-vector

コメントを残す

メールアドレスが公開されることはありません。