floooh / oryol

A small, portable and extensible C++ 3D coding framework
MIT License
2k stars 200 forks source link

Question: How z buffer range considered with different graphic API? #339

Closed julee closed 4 years ago

julee commented 4 years ago

Metal depth goes from 0.0 at the near plane to +1.0 at the far plane, but opengl goes from -1.0 to 1.0.

I found the glm library of the currrent version ref by oryol , method glm::frustum matrix will make z value goes from -1.0 to 1.0.

So If we use oryol metal backend, and use glm library of this version, pass projection matrix from glm::frustum to shader, will cause z value goes from -1.0 to 1.0?

I can't find any code that oryol will consider this issue, or just I missed something?

floooh commented 4 years ago

The shadow rendering setup in the Bullet-Physics samples has some example code of how to deal with that problem:

https://github.com/floooh/oryol-samples/blob/424f4bce7929bb6002753be3537cdb2033d8cc19/src/BulletPhysicsCloth/BulletPhysicsCloth.cc#L114-L125

However, this shouldn't be needed in normal rendering situations, because SPIRVCross (used in the oryol-shdc tool) adds a fix to vertex shaders to "normalize" the clip space between the different 3D API conventions so that rendering can happen with the same projection matrices, no matter what 3D backend is used.

https://github.com/KhronosGroup/SPIRV-Cross/blob/master/README.md#clip-space-conventions