对齐在 iOS 中的 opengl 顶点数据中有多重要 [英] How important is alignment in opengl vertex data in iOS

查看:33
本文介绍了对齐在 iOS 中的 opengl 顶点数据中有多重要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenGL ES 编程指南讨论了您应该避免未对齐的顶点数据,并给出了将数据对齐到 4 字节块的示例.

The OpenGL ES Programming guide discusses that you should avoid misaligned vertex data and gives an example of aligning the data to 4 byte blocks.

OpenGL ES 编程指南

然而,在我在网上找到的大多数演示材料和教程中,我没有看到有人这样做.以下示例来自 71 Squared 的演示项目:

However in most demo material and tutorials I've found online I don't see anyone doing this. Example below is from a demo project on 71 Squared:

static const SSTexturedVertexData3D EnemyFighterVertexData[] = {
    {/*v:*/{1.987003, -0.692074, -1.720503}, /*n:*/{0.946379, -0.165685, -0.277261}, /*t:*/{0.972816, 0.024320}},

你会怎么做呢?将虚拟 0.0 添加到 v 和 n 并将其中的 2 添加到 t?

And how would you do it anyway? Add a dummy 0.0 to the v and n and 2 of them to the t?

我的猜测是,如果您只为每个应用加载一次顶点数据,那不会有太大影响,但如果您在每个渲染帧上绑定和重新绑定到不同的数组,那就很重要了.我的计划是无论如何都将我所有的数组合并为一个,但我很好奇.

My guess is that if you're only loading the vertex data once per app it wouldn't matter much but if your binding and rebinding to different arrays on each rendered frame it would matter. My plan is to combine all my arrays into one anyway, but I'm curious.

推荐答案

一般顶点由 floats 组成,它们将要对齐,所以你不需要太关心那.事实上,很多书使用普通的 struct 来保存顶点数据,由于填充问题不能保证它们是连续的,但是如果你只有浮点数,那很好,因为你不是可能有填充.当然,有些人(包括我自己)更喜欢使用 C++ 中的 STL vector 但如果你在 iOS 上,而你可以使用 C++,你可能使用的是 Objective-C,因此在非结构体中是连续的方式将涉及普通的 C 数组,这些数组使用起来不太有趣.

General a vertex is made up of floats which are going to be aligned, so you don't need to get too concerned with that. In fact a lot of books use ordinary structs to hold vertex data, which are not guaranteed to be contiguous due to the padding issue, but if all you have are floats, that's fine since you aren't likely to have padding. Of course, some people (myself included) prefer to use the STL vector from C++ but if you are on iOS, while you can use C++ you are probably using Objective-C thus contiguity in a non-struct way would involve normal C arrays which are not so fun to work with.

如果您创建自定义结构来保存可绘制对象的顶点数据和其他数据,并且您使用 short 或其他类型,您可能会发现填充成为一个问题.但是如果你只坚持使用 float ,你会没事的,而且你会得到很好的陪伴,因为大多数关于 OpenGL 的教育资源都是这样做的.

If you create custom structures to hold vertex data as well as other data for your drawable object, and you use shorts or other types, you might find padding to become an issue. But if you stick with only floats you will be fine, and you will be in good company since most educational sources on OpenGL do this.

这篇关于对齐在 iOS 中的 opengl 顶点数据中有多重要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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