OpenGL/GLM-切换坐标系方向 [英] OpenGL/GLM - Switching coordinate system direction

查看:497
本文介绍了OpenGL/GLM-切换坐标系方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在转换一些旧软件以支持OpenGL. DirectX和OpenGL具有不同的坐标系(OpenGL在右侧,DirectX在左侧).我知道在旧的固定管道功能中,我将使用:

I am converting some old software to support OpenGL. DirectX and OpenGL have different coordinate systems (OpenGL is right, DirectX is left). I know that in the old fixed pipeline functionality, I would use:

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

这一次,我正在使用GLM和着色器,并且需要兼容的解决方案.我尝试将相机矩阵乘以缩放矢量,但没有运气.

This time around, I am working with GLM and shaders and need a compatible solution. I have tried multiplying my camera matrix by a scaling vector with no luck.

这是我的摄像头设置:

// 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;

我已经尝试了很多方法,包括反转着色器中的向量和反转z坐标.我还尝试过乘以各种矩阵和向量的逆,然后将相机矩阵乘以缩放向量.

I have tried a number of things including reversing the vectors and reversing the z coordinate in the shader. I have also tried multiplying by the inverse of the various matrices and vectors and multiplying the camera matrix by a scaling vector.

推荐答案

不要过多考虑使用习惯.的确,它们使用不同的约定,但是您可以选择不使用它们,并且归结为两个API中几乎相同的东西.我的建议是,除了这两个方面,在两个API中使用完全相同的矩阵和设置:

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. My advice is to use the exact same matrices and setups in both APIs except for these two things:

从DX移植到GL所需要做的就是:

All you should need to do to port from DX to GL is:

  1. 反转剔除面绕组-默认情况下,DX剔除逆时针剔除,这就是GL保留的内容.
  2. 调整不同的深度范围:DX使用的深度范围是0(近)到1(远),而GL使用的有符号范围从-1(近)到1(远).您可以将其作为投影矩阵中的最后一步".

DX9也存在像素坐标偏移的问题,但这完全是另外一回事,而从DX10开始则不再是问题.

DX9 also has issues with pixel-coordinate offsets, but that's something else entirely and it's no longer an issue with DX10 onward.

根据您的描述,绕组可能是您的问题,因为您正在使用GLM函数来生成应该适合OpenGL的矩阵.

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.

这篇关于OpenGL/GLM-切换坐标系方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆