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

查看:91
本文介绍了对齐在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.

但是,在网上找到的大多数演示材料和教程中,我看不到有人这样做.下面的示例来自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}},

您将如何做呢?向v添加虚拟0.0,向n添加虚拟2,向t添加虚拟2?

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天全站免登陆