C ++:OpenGL,glm和struct padding [英] C++: OpenGL, glm and struct padding

查看:200
本文介绍了C ++:OpenGL,glm和struct padding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以安全地使用glm :: *类型(例如vec4,mat4)来填充顶点缓冲对象吗?

can I safely use the glm::* types (e.g. vec4, mat4) to fill a vertex buffer object ?

std::vector<glm::vec3> vertices;    
glBufferData(GL_ARRAY_BUFFER,  sizeof(glm::vec3) * vertices.size(), &vertices[0], GL_STATIC_DRAW);

我不太确定,因为struct padding(成员对齐)可能会在我的

I'm not quite sure about that since struct padding (member alignment) could cause some trouble in my opinion, though all compilers I've tested returns the expected sizes.

我正在开发C ++ 11编译器(也许这会产生影响)。

I'm developing for C++11 Compilers (maybe this make a difference).

推荐答案

定义安全。

因为他们认为合适。就ISO C ++而言,这个工作是否是依赖于实现的行为。

C++ gives implementations wide latitude to pad structures as they see fit. So as far as ISO C++ is concerned, whether this "works" is implementation-dependent behavior.

它将适用于桌面平台的许多编译器。我不能说ARM CPU,但一般来说, glm :: vec3 将有3个浮点数。但是,如果你想确认,你总是可以执行一个简单的 static_assert

It will work in general across a number of compilers for desktop platforms. I can't speak for ARM CPUs, but generally, glm::vec3 will be 3 floats in size. However, if you want to make sure, you can always perform a simple static_assert:

static_assert(sizeof(glm::vec3) == sizeof(GLfloat) * 3, "Platform doesn't support this directly.");

这篇关于C ++:OpenGL,glm和struct padding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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