ios - OpenGL ES : Do vertex structs need x, y 和 z?

标签 ios swift opengl-es vertex vertex-array

这可能是个愚蠢的问题,但我一直在想 - OpenGL 中的顶点结构需要有 x、y 和 z 吗?我在 2D 中绘图,但我看到的所有教程似乎在其结构中都有一个 z 变量,即使它始终为 0。这不会减慢渲染速度吗?

此外,它们似乎总是有 r、g、b 和 a 作为颜色变量,但我不需要 alpha,因为它在我的项目中始终为 1。我真的需要每次都说它是 1 吗?

我只是想知道,因为 glVertexAttribPointer 允许您将第二个参数设置为任何值,我尝试将其设置为 2,因为我只使用 x 和 y,而且它起作用了。

最佳答案

来自spec第 2.8 节:

If size is one then the x component of the attribute is specified by the array; the y, z, and w components are implicitly set to zero, zero, and one, respectively. If size is two then the x and y components of the attribute are specified by the array; the z , and w components are implicitly set to zero, and one, respectively. If size is three then x, y, and z are specified, and w is implicitly set to one. If size is four then all components are specified.

因此,如果您没有为二维顶点定义 z 或 w。顶点组装器自动用 z=0,w=1 填充它们。您的代码安全且正确。

RGBA 与 XYZW 相同,因此假设您希望 alpha 为 1,则不必为颜色分量指定 alpha。

在性能方面,您通常应该通过使用较小的顶点组件来获得胜利,因为需要移动和复制到 GPU 的数据较少。我预计任何性能提升都是微不足道的,而且可能无法衡量。

一个异常(exception)是,如果你从一个顶点上削减几个字节并将其从一个很好的缓存友好的每个顶点 16 字节减少到每个顶点 12 字节,你可能会稍微降低性能,因为 8、16、32 字节大小就内存访问而言,这是一个神奇的数字。同样,这里的任何性能损失都是微小的,所以我不会让它给您带来麻烦。

关于ios - OpenGL ES : Do vertex structs need x, y 和 z?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40880989/

相关文章:

ios - 尝试使用 setNeedsDisplay()

ios - Xcode Storyboard 上的并排约束

ios - 如何在执行某些操作之前管理多个异步任务?

ios - 为 iOS 增强现实应用程序开发自定义过滤器

java - 顶点限制,如何加载大模型?

ios - UISegmented 控件 ObjectAtIndex 替换 swift

javascript - 在 Android 平板电脑/手机上调试 javascript?

iphone - glReadPixels 仅在 iOS 7 设备上提供黑色图像

ios - OpenCV + OpenGL : proper camera pose using solvePnP

ios - EKCalendar 和错误域=NSMachErrorDomain 代码=268435459