投影矩阵适应OpenGL

投影矩阵适应OpenGL

本文介绍了如何使DirectX风格的世界/视图/投影矩阵适应OpenGL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用DirectX的应用程序,因此它是一个左手世界坐标系统,一个向下看正z的视图和一个投影到单位立方体中的投影,但z从0到1.

I have an application which uses DirectX, and hence a left-handed world-coordinate systen, a view which looks down positive z and a projection which projects into the unit cube but with z from 0..1.

我将这个应用程序移植到OpenGL,而OpenGL有一个不同的坐标系等。特别是,它是右手的,视图沿着负z看下去,而投影是到真正的单位立方体。

I'm porting this application to OpenGL, and OpenGL has a different coordinate system etc. In particular, it's right-handed, the view looks down along negative z, and the projection is into the real unit cube.

调整DirectX矩阵到OpenGL的最简单方法是什么?世界空间的变化似乎很容易,只是通过翻转X轴,但我不完全确定如何改变视图/投影。我可以单独修改所有矩阵,然后将它们相乘并将它们发送给我的着色器。如果可能的话,我想尽量使用基于DirectX的坐标系尽可能保持最低点(即,我不想改变放置物体的位置,计算视角的方法等。 - 理想情况下,我在将它们交给我的着色器之前,我只会对矩阵进行一些修改。)

What is the easiest way to adapt the DirectX matrices to OpenGL? The world-space change seems to be easy, just by flipping the x-axis, but I'm not entirely sure how to change the view/projection. I can modify all of the matrices individually before multiplying them together and sending them off to my shader. If possible, I would like to stick with the DirectX based coordinate system as far down the pipeline as possible (i.e. I don't want to change where I place my objects, how I compute my view frustum, etc. -- ideally, I'll only apply some modification to the matrices right before handing them over to my shaders.)

推荐答案

答案是:没有需要翻转任何东西,只是为了调整深度范围。 OpenGL有-1..1,DX有0..1。您可以像以前一样使用DX矩阵,并且只在投影矩阵顶部的末端乘以比例/偏置矩阵,以将深度值从0..1缩放到-1..1。

The answer is: There's no need to flip anything, only to tweak the depth range. OpenGL has -1..1, and DX has 0..1. You can use the DX matrices just as before, and only multiply a scale/bias matrix at the end on top of the projection matrix to scale the depth values from 0..1 to -1..1.

这篇关于如何使DirectX风格的世界/视图/投影矩阵适应OpenGL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:31