简体   繁体   English

从视口坐标转换为[-1,1] ^ 2顶点[OpenGL]

[英]Converting from Viewport Coordinates to [-1,1]^2 Vertex [OpenGL]

G'day! G'day!

Say I have a mouse click represented by its integer values in the window (with the window being resizable), so its domain would be (0,0) - > (current_width, current_height). 假设我在窗口中以整数值表示鼠标单击(窗口可调整大小),因此其域为(0,0)->(current_width,current_height)。 Is there an easy way to 'normalise' or convert this mouse click position to a vertex in OpenGL land? 有没有一种简单的方法可以“标准化”或将鼠标单击位置转换为OpenGL平台中的顶点? ie a point in R^2 with domain [-1,1]^2? 即R ^ 2中具有域[-1,1] ^ 2的点?

Eg: If the current width and height of the window was 400 and 600 respectively, and I clicked at 400,600, I'd like to easily convert that to [1,-1]. 例如:如果窗口的当前宽度和高度分别为400和600,而我单击了400,600,我想轻松地将其转换为[1,-1]。

Does this functionality exist within OpenGL or will I have to write the function myself? 该功能是否在OpenGL中存在,还是我必须自己编写该功能?

Cheers. 干杯。

You'll have to write the function yourself, but it's trivial: 您必须自己编写函数,但这很简单:

x_ogl = 2.0 * (x_mouse - width / 2) / width
y_ogl = -2.0 * (y_mouse - height / 2) / height

The negation in the second formula is to convert from "+ve Y down" in mouse / screen coordinates to more normal cartesian of "+ve Y up". 第二个公式中的否定是将鼠标/屏幕坐标中的“ + ve Y向下”转换为“ + ve Y向上”的更正常的笛卡尔坐标。

Also, it would be more conventional to maintain a 1:1 aspect ratio between X and Y coordinates with the larger axis maintaining the range [-1, 1] and the smaller axis having a smaller range. 同样,将传统的做法是在X和Y坐标之间保持1:1的纵横比,其中较大的轴保持范围[-1,1],较小的轴保持较小的范围。

If that's what you'd really prefer, then replace the right hand side of the outer division by max(width, height) in both of the two expressions above instead of as shown. 如果那是您真正想要的,则在上面两个表达式中的max(width, height)中替换外部分隔的右侧,而不是如图所示。

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

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