问题描述
我有我在OpenGL-ES 2.0已经创建了一个2D地图。现在我要画一个比例尺。
I have a 2D map that I have created in OpenGL-ES 2.0. Now I want to draw a scalebar.
当用户平移地图,这将不会被感动,我需要它在屏幕上绘制坐标弄好了。
As this will not be moved when the user pans the map, I need it to be drawn in screen coordinates somehow.
我可以使用上如何完成这样的一些建议。
I could use some advice on how to accomplish something like this.
推荐答案
要做到这一点,你需要知道的是你的视口的尺寸。要知道,视口变换是一个非常简单的规模和偏见的操作,它有效地是这样的:
To do this, all you need to know is the dimensions of your viewport. Know that the viewport transformation is a very simple scale and bias operation, it effectively works like this:
NDC Screen
--- ------
-1.0 -> x \_ X (Passed using glViewport (...))
1.0 -> x + width /
-1.0 -> y \_ Y (Passed using glViewport (...))
1.0 -> y + height /
-1.0 -> near \_ Z (Passed using glDepthRange (...))
1.0 -> far /
在换句话说,X和Y,标准化设备坐标(NDC)的缩放到合适的宽度和高度众多单位中,并在的底层最左侧的点是由(x,y)的定义。对于Z,这你可能不关心,NDC是从附近的价值定义的值为止您定义的缩放。
In other words, for X and Y, Normalized Device Coordinates (NDC) are scaled to fit within width and height many units, and the bottom-left-most point is defined by (x,y). For Z, which you probably do not care about, NDC is scaled from your defined near value to the defined far value.
你想要做什么的逆转视映射操作;开始屏幕空间坐标和工作回到NDC。
What you want to do is reverse the viewport mapping operation; start with screenspace coordinates and work back into NDC.
-
NDC =(2.0 *(屏幕 - 视)/视 ) - 1.0;
NDC = (2.0 * (Screen - Viewport) / Viewport) - 1.0;
NDC 是 =(2.0 *(屏幕 - 视)/视 ) - 1.0;
NDC = (2.0 * (Screen - Viewport) / Viewport) - 1.0;
这应该为您提供所有必要的步骤来产生NDC (X,Y)从屏幕(X,Y)坐标。一旦你有了NDC坐标,你可以直接通过他们使用的模型,视图和投影矩阵身份着色器。
This should provide you with all the necessary steps to produce NDC coordinates from Screen. Once you have NDC coordinates, you can pass them directly to your shaders using identity matrices for Model, View and Projection.
的选择是跳过了这一切,并使用一个定义1正交投影矩阵:投影和窗口(另一个名称屏幕空间)坐标1之间的映射,但是我觉得这是更好地教NDC以及如何视口映射工作,从长远来看:)
这篇关于画线按在OpenGL屏幕COORDS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!