我可以避免在下面使用offsetof吗? [英] Can I avoid using offsetof in the following?

查看:135
本文介绍了我可以避免在下面使用offsetof吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接着我的另一个问题,这里是一个具体的例子,我想避免 offsetof

Following another question from me, here is a specific example where I want to avoid offsetof.

使用 glVertexAttribPointer ,我必须对最后一个参数使用 offsetof

For using with glVertexAttribPointer, I have to use offsetof for the last parameter.

 glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex),
            (const GLvoid *) offsetof(Vertex, _color));

顶点是一个类。有没有办法我可以避免使用这一个?

Vertex is a class. Is there a way I can avoid using this one? I tried with pointer to members, but no luck.

无法在以下

glVertexAttribPointer(GLKVertexAttribColor, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
        (const GLvoid *)&Vertex::_color);


推荐答案

您无法获取非静态成员的地址限定为 Vertex :: _ color 。您将需要一个 Vertex 的实例,但是即使返回的地址也是相对于程序的地址空间,这不是你想要的,当你使用VBO。

You cannot get the address of a non-static member qualified as Vertex::_color. You would need an instance of Vertex, but even then the address returned would be relative to the program's address space and this is not what you want when you use VBOs.

offsetof(...)用于查找数据结构中元素的地址。这个地址不指向实际的内存,它只是一个偏移从结构的开始(这就是为什么它使用 size_t 而不是

offsetof (...) is used is to find the address of an element in a data structure. This address does not point to actual memory, it is literally just an offset from the beginning of the structure (and this is why it uses size_t rather than intptr_t or void *).

历史上,当顶点数组在OpenGL中被引入时1.1,它在调用 glVertexPointer(...)时使用数据指针来引用客户端(程序)内存。那时,你传递的地址实际上指向你程序中的内存。从顶点缓冲区对象(GL 1.5)开始,OpenGL重新设计了 glVertexPointer(...)中的指针参数,作为指向服务器(GPU)内存的指针。如果你有一个非零的VBO绑定当你调用 glVertexPointer(...)那么指针实际上是一个偏移。

Historically, when vertex arrays were introduced in OpenGL 1.1, it used the data pointer in a call to glVertexPointer (...) to reference client (program) memory. Back then, the address you passed actually pointed to memory in your program. Beginning with Vertex Buffer Objects (GL 1.5), OpenGL re-purposed the pointer parameter in glVertexPointer (...) to serve as a pointer to server (GPU) memory. If you have a non-zero VBO bound when you call glVertexPointer (...) then the pointer is actually an offset.

更精确地说,使用VBO时的指针相对于绑定的VBO的数据存储,并且VBO中的第一个数据从地址 0 开始。这就是为什么 offsetof(...)在这种情况下是有意义的,没有理由避免使用它。

More precisely, the pointer when using VBOs is relative to the bound VBO's data store and the first datum in your VBO begins at address 0. This is why offsetof (...) makes sense in this context, and there is no reason to avoid using it.

这篇关于我可以避免在下面使用offsetof吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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