OpenGL中的左手计算 [英] Left-handed calculations in OpenGL

查看:60
本文介绍了OpenGL中的左手计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在2年的中断之后,我重新回到了世界游戏编程领域.不幸的是,我关于3D数学的大部分知识都还很生锈.所以,请忍受我.

I'm jumping back into the world game programming after a 2yr hiatus. Unfortunately, most of my knowledge pertaining to 3D math is rather rusty. So bear with me.

我的引擎和游戏最初是为DirectX设计的,DirectX是使用行主要Matrix结构的左手系统.我的数学代码都是自制的,并且在该系统范围内可以完美地工作.我正要给我的游戏提供OpenGL渲染器.由于我所有的数学都使用左手主要行矩阵系统(例如,创建投影矩阵),因此将我的数学移植到OpenGL的左手主要列系统有多困难?

My engine and game were originally designed for DirectX, which is a left-handed system that uses a row-major Matrix structure. My math code is all home-brew and works perfectly within the confines of that system. I'm at a point where I want to give my game an OpenGL renderer. Since all my math uses a left-handed, row-major Matrix system (for example, to create a projection matrix), how hard would it be to port my math to OpenGL's left-handed, column major system?

是转置矩阵并将值粘贴到以列为主的结构中吗?还是我简化太多了?

Is it a matter of transposing the matrix and sticking the values into a column-major struct? Or am I simplifying this too much.

推荐答案

这要视情况而定.我们是在谈论基于着色器的OpenGL还是固定功能(FF)?

It depends. Are we talking shader-based OpenGL or fixed-function (FF)?

在FF领域中,您需要使用gluPerspective(或glFrustum)来生成透视矩阵,并使用在D3D下为代码赋予的类似参数.然后,您需要对要为D3D计算的矩阵进行转置(省去计算的投影部分),使之成为主要列,这就是glLoad/MultMatrix想要的方式.

In FF land, what you need to do is use gluPerspective (or glFrustum) to generate your perspective matrix, using similar parameters that you would give to your code under D3D. Then, you need to transpose the matrices you would compute for D3D (leaving out the projection component of the computation) to make the column-major, the way that glLoad/MultMatrix wants.

然后,您需要生成一个矩阵来翻转场景,并将其放在GL_MODELVIEW堆栈的最底部.弄清楚该怎么做的最简单方法是仅渲染所有内容并查看世界是如何反转的.然后在此处粘贴沿轴取反的矩阵;如果可以解决,那么您就完成了.

And then, you need to generate a matrix to flip your scene, which you put at the very bottom of the GL_MODELVIEW stack. The easiest way to figure out what to do is to just render everything and see how the world is inverted. Then stick a matrix there which negates along an axis; if that fixes it, you're done.

在伪代码中,您要执行的操作是

In pseudo-code, what you do is this:

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(/*Projection parameters here*/);

glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(/*Your flip matrix here*/);
glPushMatrix();

//Render your stuff here.
//When rendering an object:
Matrix mat = ComputeD3DModelToCameraMatrixForObject();
mat = Transpose(mat);
glPushMatrix();
glMultMatrixf(GetMatrixAsFloatArray(mat));
//Draw the object.
glPopMatrix();

//When finished rendering stuff:
glPopMatrix();

在着色器中,事情变得更简单.假设您使用自己的制服将矩阵传递给GLSL.

In shaders, things are simpler. This assumes that you're using your own uniforms to pass matrices to GLSL.

真的,您要做的就是查看OpenGL使用的剪辑空间与D3D使用的剪辑空间之间的区别.裁剪空间是从顶点着色器输出的顶点位置的空间.您可以像往常一样将矩阵传递给GLSL,因为glUniformMatrix函数具有一个参数,可让您指定是否对矩阵进行转置(行主).计算完D3D的D3D剪辑空间位置后,只需根据OpenGL的期望修改结果即可.

Really, all you need to do is look at the differences between the clip-space that OpenGL uses and the clip-space that D3D uses. Clip-space is the space of the vertex positions output from the vertex shader. You can pass your matrices to GLSL as normal, since the glUniformMatrix functions have a parameter that allows you to specify if the matrix is transposed (row-major). Once you have computed the D3D clip-space positions as you would have for D3D, simply modify the results based on what OpenGL expects.

我不记得有什么区别,但是OpenGL规范2.13节(在版本3.3中,对于其他版本可能是不同的部分)非常明确地详细说明了预期的坐标系以及随后的对坐标系的转换.窗口空间.

I don't recall the differences off-hand, but the OpenGL specification section 2.13 (in version 3.3, it may be a different section for other versions) very explicitly details the coordinate system expected, as well as the subsequent transformations to window-space.

这篇关于OpenGL中的左手计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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