devkitPro / citro3d

Homebrew PICA200 GPU wrapper library for Nintendo 3DS
zlib License
248 stars 35 forks source link

Questions regarding Mtx_Translate() #55

Open BallM4788 opened 3 years ago

BallM4788 commented 3 years ago

Please be patient with me as I am extremely new to graphics libraries and the math involved in 3D rendering, and frankly have very little idea of what I'm talking about in general.

Background: ScummVM uses a modified version of TinyGL (a stripped-down version of OpenGL) to run several 3D games such as Grim Fandango and Myst 3: Exile on lower-end systems (including the 3DS), provided their ScummVM engine has alternate, TinyGL-specific graphics code. The problem is that attempting to run said 3D games crashes the system. Interestingly, the one 3D game that will run on the 3DS (Westwood's Blade Runner), does not use OpenGL nor TinyGL.

Anyway, there was speculation that the issue might be solved by having the 3DS hardware take over some functions. Since the 3DS backend for ScummVM already uses citro3d for screen rendering, I thought it might be worthwhile to try and write a version of ScummVM's TinyGL implementation that utilizes citro3d. Needless to say, due to tinyGL being column-major and citro3d being row-major, I've been in a slog from the beginning.

What I think I know: Correct me if I'm wrong on any of this. To my understanding, converting between column-major and row-major layout simply requires transposing the matrix (and in citro3d's case, additionally flipping the matrix horizontally to account for PICA200's WZYX ordering). This seemed to work out fine for matrix rotation, as doing this then performing a left-handed MTX_Rotate() produced the same values as TinyGL's glopRotate() (with value positions changed appropriately).

In TinyGL, the translation matrix in its Matrix4::translate() looks like this:

Therefore, I expected that citro3d's translation matrix would be situated like this (but flipped horizontally, of course):

However, according to citro3d's code, a left-sided MTX_Translate() changes every cell's value EXCEPT those in the bottom-row. Additionally, right-handed Mtx_Translate() uses a translation matrix that is a horizontal mirror of TinyGL's translation matrix, changing only the W cells.

Questions: 1) Is this intentional? 2) If so, am I not understanding something correctly? 3) If not, how can I work around this? 4) Am I going about this all wrong? 5) Is there anything you think I should know before I continue on this magical journey of mathematics, personal growth, and generally banging my head against a wall?

mtheall commented 3 years ago

C3D_Mtx bRightSide=true is an OpenGL-style right-handed coordinate system where matrix multiplication is right-associative and has the "effects" from right-to-left.

C3D_Mtx bRightSide=false is an DirectX-style left-handed coordinate system where matrix multiplication is left-associative and has the "effects" from left-to-right.

BallM4788 commented 3 years ago

Thanks, that helps. In light of that, I think I may have found something wrong in Mtx_Rotate(). I made two hackjob test files (one for TinyGL, one for Citro3D) by copypasting code bits.

TinyGL test file results: waterfox_2021-06-17_07-29-33

Citro3D test file results (matrix printouts flipped horizontally for ease of comparison): waterfox_2021-06-17_07-24-59

While the other functions produce appropriate results in the Citro3D test, Mtx_Rotate() does not. This might be because the function does not transpose om when bRightSide = true; doing so results in the expected output: waterfox_2021-06-17_07-49-35

Am I correct in concluding that Mtx_Rotate's current behavior for bRightSide = true is not intended?

mtheall commented 3 years ago

Have you had a look at https://github.com/devkitPro/citro3d/tree/master/test?

BallM4788 commented 3 years ago

test: main.cpp:214: void check_matrix(generator_t&, distribution_t&): Assertion `m == glm::mat4()' failed.

I probably did something wrong like install the wrong version of GLM, so I included the whole shebang in the pastebin (GLM install, build process, and test).

mtheall commented 3 years ago

Sorry looks like glm changed the way default constructors work. They are now uninitialized (or zero-initialized; didn't check) instead of identity-constructed. I created a branch https://github.com/devkitPro/citro3d/tree/update-glm that restores that behavior for the test, and also prints out the actual/expected data on failures.

BallM4788 commented 3 years ago

On the off chance I didn't screw up on my end, I went through and figured out which of the asserts were failing. I did this by going through and commenting out each line the test program failed at.

EDIT: Missed your reply, but this might be useful anyway.

check_matrix

check identity

check inverse (which you're aware of)

check perspective tilt

check perspective stereo tilt

check ortho tilt

check lookAt

check translate (reversed)

check rotate (reversed)

check rotate X (reversed)

check rotate Y (reversed)

check rotate Z (reversed)

check_quarternion

check identity

check rotation

check rotate X

check rotate Y

check rotate Z

BallM4788 commented 3 years ago

Just tried the updated test file and yeah, everything passes on that one. Guess I just need to look at all this more to familiarize myself with it.

mtheall commented 3 years ago

I would suggest inserting TinyGL into these tests and going from there

BallM4788 commented 3 years ago

That's a good idea, I'll try that.

BallM4788 commented 3 years ago

@mtheall I hope you don't mind me reusing this to avoid cluttering up the issues page. I noticed that the code in mtx_persp.c is different than that of the WolframAlpha equation,0,0,0%7D,%7B0,1%2Ftan(v),0,0%7D,%7B0,0,(n%2Bf)%2F(n-f),(2fn)%2F(n-f)%7D,%7B0,0,-1,0%7D%7D) it links to. Shouldn't line 16 read: mtx->r[2].w = 0.5f*(near + far) / (near - far) - 0.5f*mtx->r[3].z; instead of: mtx->r[2].z = -mtx->r[3].z * near / (near - far);?

mtheall commented 3 years ago

3DS GPU clip space has z in [-1, 0] where e.g. OpenGL has it in [-1, 1]