在 Android 和 iOS 上在 OpenGL 中围绕图像绘制恒定宽度的边框

Drawing border of constant width around image in OpenGL on Android and iOS

我正在尝试围绕使用 OpenGLAndroidiOS 应用程序。我现在的做法是如图所示在图像的每个角周围指定 3 个顶点,并通过绘制三角形来填充它们。

我计算这些顶点的方法是 adding/subtracting 一个称为 BORDER_WIDTH 的绝对值,它被设置为一个浮点数。例如,渲染图像左角周围的 3 个顶点(在上面链接的图像中标记为 0、1、2)计算如下。

[bottom_left_viewport_vertex[0] - BORDER_WIDTH, bottom_left_viewport_vertex[1], 0.0f,
bottom_left_viewport_vertex[0] - BORDER_WIDTH, bottom_left_viewport_vertex[1] - BORDER_WIDTH, 0.0f,
bottom_left_viewport_vertex[0], bottom_left_viewport_vertex[1] - BORDER_WIDTH, 0.0f] 

此方法导致 2 个问题:

  1. 与 Android 设备相比,iOS 设备的边框要粗得多,可能是因为 Retina 显示屏。
  2. 图片顶部底部的边框宽度与不一样图片的边

解决此类问题的正确方法是什么? 谢谢。

The border is way thicker on iOS devices compared to Android devices probably because of the Retina display.

使用 DisplayMetrics 确定 DPI,并适当调整 BORDER_WIDTH 值。

The width of the border on the top and the bottom of the image is different from that on the sides of the image.

无论纵横比如何,视口坐标都介于 0 和 1 之间。如果您不校正宽高比,那么一切都会按比例缩放。

或者,您在假设像素是正方形的情况下进行计算。他们通常不是。