简体   繁体   English

OpenGL / GLM-切换坐标系方向

[英]OpenGL/GLM - Switching coordinate system direction

I am converting some old software to support OpenGL. 我正在转换一些旧软件以支持OpenGL。 DirectX and OpenGL have different coordinate systems (OpenGL is right, DirectX is left). DirectX和OpenGL具有不同的坐标系(OpenGL在右侧,DirectX在左侧)。 I know that in the old fixed pipeline functionality, I would use: 我知道在旧的固定管道功能中,我将使用:

glScalef(1.0f, 1.0f, -1.0f);

This time around, I am working with GLM and shaders and need a compatible solution. 这次,我正在使用GLM和着色器,并且需要兼容的解决方案。 I have tried multiplying my camera matrix by a scaling vector with no luck. 我尝试将相机矩阵乘以缩放矢量,但没有运气。

Here is my camera set up: 这是我的相机设置:

// Calculate the direction, right and up vectors
 direction = glm::vec3(cos(anglePitch) * sin(angleYaw), sin(anglePitch), cos(anglePitch) * cos(angleYaw));
 right = glm::vec3(sin(angleYaw - 3.14f/2.0f), 0, cos(angleYaw - 3.14f/2.0f));
 up = glm::cross(right, direction);

 // Update our camera matrix, projection matrix and combine them into my view matrix
 cameraMatrix = glm::lookAt(position, position+direction, up);
 projectionMatrix = glm::perspective(50.0f, 4.0f / 3.0f, 0.1f, 1000.f);
 viewMatrix = projectionMatrix * cameraMatrix;

I have tried a number of things including reversing the vectors and reversing the z coordinate in the shader. 我已经尝试了许多方法,包括反转着色器中的向量和反转z坐标。 I have also tried multiplying by the inverse of the various matrices and vectors and multiplying the camera matrix by a scaling vector. 我还尝试过乘以各种矩阵和向量的逆,然后将相机矩阵乘以缩放向量。

Don't think about the handedness that much. 不要过多考虑惯用性。 It's true, they use different conventions, but you can just choose not to use them and it boils down to almost the same thing in both APIs. 的确,它们使用不同的约定,但是您可以选择不使用它们,并且归结为两个API中几乎相同的东西。 My advice is to use the exact same matrices and setups in both APIs except for these two things: 我的建议是在这两个API中使用完全相同的矩阵和设置,但以下两项除外:

All you should need to do to port from DX to GL is: 从DX移植到GL所需要做的就是:

  1. Reverse the cull-face winding - DX culls counter-clockwise by default, while that's what GL keeps. 反转剔除面绕组-默认情况下,DX逆时针剔除,这就是GL保留的内容。
  2. Adjust for the different depth range: DX uses a depth range of 0(near) to 1(far), while GL uses a signed range from -1 for near and 1 for far. 调整为不同的深度范围:DX使用的深度范围是0(近)到1(远),而GL使用的有符号范围从-1(对于近距离)和1(对于远距离)。 You can just do this as "a last step" in the projection matrix. 您可以将其作为投影矩阵中的“最后一步”。

DX9 also has issues with pixel-coordinate offsets, but that's something else entirely and it's no longer an issue with DX10 onward. DX9也存在像素坐标偏移的问题,但这完全是另外一回事,而从DX10开始则不再是问题。

From what you describe, the winding is probably your problem, since you are using the GLM functions to generate matrices that should be alright for OpenGL. 从您的描述来看,缠绕可能是您的问题,因为您正在使用GLM函数生成应该适合OpenGL的矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM