如何使用现代OpenGL在透视投影中绘制对象的正交轴? [英] How to draw ortho axes for object in perspective projection using modern OpenGL?

查看:100
本文介绍了如何使用现代OpenGL在透视投影中绘制对象的正交轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带有透视投影的3D场景.我也可以在场景中选择一个对象.我需要为所选对象绘制轴.问题在于轴没有在透视投影中保存其尺寸.如果物体离眼睛(相机)较远,则轴也将变小.

I have 3D scene with perspective projection. Also I can select an object on the scene. I need to draw axes for selected object. The problem is the axes don't save their size in perspective projection. If object is far from the eye (camera), axes is going be small too.

无论眼睛(相机)的位置如何,如何绘制相同大小的轴?

How to draw axes with the same size regardless of the position of the eye (camera)?

推荐答案

有两种方法可以实现此目的:

There are two ways to achieve this:

仅着色器方法

查看透视图投影时,根据深度的大小变化是由透视图划分(将所有分量除以w)引起的.如果要防止这种情况发生,可以将投影顶点的x和y坐标乘以w坐标,这将消除透视划分.这样做有点棘手,因为在所有其他转换之前进行校正,但是对于一般情况,应该遵循以下思路:

When looking at the perspective projection, the size change according to depth is caused by the perspective divide (dividing all components by w). If you want to prevent this from happening, you can multiply the x and y coordinate of the projected vertices with the w-coordinate which will cancel out the perspective divide. It's a bit tricky to do because the correction before all other transformations, but something along this line should work for the general case:

vec4 ndc = MVP * vec4(pos, 1);
float sz = ndc.w;

gl_Position= MVP * vec4(pos.xy * sz, pos.z, 1);

缺点:需要专门的着色​​器

Drawback: Needs a specialized shader

CPU方法

另一种选择是在计算必须放置在CPU上的位置时,以正投影方式渲染轴.

The other option is to render the axis with a orthographic projection, while calculating the location where it has to be placed on the CPU.

例如,这可以通过用透视投影投影目标位置,执行透视划分来完成.产生的x,y分量给出了屏幕空间中必须放置轴的位置.

This can, for example, be done by projection the target location with the perspective projection, perform the perspective divide. The resulting x,y components give the location in screen-space where the axis have to be placed.

现在使用此位置以正投影方式渲染轴,无论轴距多远,都将保持大小.

Now use this position to render the axis with orthographic projection which will maintain the sizes no matter how far away the axis are.

缺点:采用这种方法时,深度值可能与场景的透视投影部分不兼容.

Drawbacks: With this approach depth values might not be compatible with the perspective projected part of the scene.

这篇关于如何使用现代OpenGL在透视投影中绘制对象的正交轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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