使用OpenGL将结构体加载到成员为Private的VBO中
Using C++ to Load Structs into a VBO whose Members are Private using OpenGL
可以将结构体加载到成员变量为私有的vbo中吗?
这个问题是一般性的,没有样本,因为我不知道如何问具体的问题。下面的答案正是我想要的。但是对于那些没有看到引发问题的代码的人来说:
#ifndef POINT2D_H_INCLUDED
#define POINT2D_H_INCLUDED
#include <glext.h>
namespace glext
{
/*! class Point 2D class geometry based
* brief This class defines a 2D point_2d
*
* Some details about the Test class
*/
template <typename T>
class point_2d
{
private:
/// The first element in the ordered pair
T _x;
/// The second element in the ordered pair
T _y;
public:
/*! brief default constructor sets both elements in ordered pair to 0
*/
point_2d();
/*! brief constructor sets both elements to the paramaters provided
*/
point_2d(const T &x, const T &y);
/*! brief copy constructor sets both elements in ordered pair to 0
*/
point_2d(const point_2d &rhs);
/*! brief destructor
*/
~point_2d();
/*! brief assignment uses copy-swap idiom
*/
point_2d &operator=(point_2d rhs);
/*! brief setter for x element
*/
void x(const T &x);
/*! brief setter for y element
*/
void y(const T &y);
/*! brief setter for both x and y element
*/
void x_and_y(const T &x, const T &y);
/*! brief swizzle for both x and y returns a copy of a point_2d
*/
point_2d xy() const;
/*! brief swizzle for x element returns a copy of a point_2d
*/
point_2d xx() const;
/*! brief swizzle for y element returns a copy of a point_2d
*/
point_2d yy() const;
/*! brief swizzle for reverse of y and x returns a copy of a point_2d
*/
point_2d yx() const;
/*! brief getter for x element returns a reference to x of type T
*/
T& x() const;
/*! brief getter for y element returns a reference to y of type T
*/
T& y() const;
};
template <typename T>
void swap(point_2d<T> &lhs, point_2d<T> &rhs);
template <typename T>
bool operator<(const point_2d<T> &lhs, const point_2d<T> &rhs);
template <typename T>
bool operator>(const point_2d<T> &lhs, const point_2d<T> &lhs);
template <typename T>
bool operator==(const point_2d<T> &lhs, const point_2d<T> &rhs);
template <typename T>
bool operator!=(const point_2d<T> &lhs, const point_2d<T> &rhs);
}
#include "type_point_2d_2d.inl"
#endif
这真的与OpenGL或缓冲区对象无关。你问的是复制带有私有成员的结构体的二进制数据的结果是什么。这就是你从memcpy
得到的。
c++ 98/03只允许这样的内存布局是合法的,如果对象是一个POD(普通旧数据)类型。具有私有成员的类不是pod。
c++ 11放宽了这些规则。对于memcpy
的工作(这是glBufferSubData
最终做的),您需要的类型是平凡的可复制的。所以没有虚函数或非平凡成员。为了确保数据的实际布局(例如,这样offsetof
将实际工作),类型必须是标准布局。
特别对您来说,这意味着如果您有私有成员,那么所有(非静态)成员变量的必须是私有的。如果某些成员是public而其他成员是private,则会丢失标准布局。
相关文章:
- std::make_shared和protected/private构造函数
- OpenGL VBO Indexing ( How to compute Index Array)
- QT QOpenGLWidget:如何在不使用数据块复制的情况下修改VBO中的单个顶点值?
- 将单个未唱的 int 发送到 VBO
- 如何摆脱C++中未解析的外部符号"private: static char"错误?
- VBO必须在VAO之后绑定
- C++ - "private"单身人士?
- 将复制构造函数设置为private和=delete有什么区别
- 如何在顶点着色器中使用VBO数据作为位置数据,在片段着色器中使用统一数据作为颜色数据
- OpenGL:第二个VBO破坏了第一个VBO
- 库设计混乱.. "public"/"private"(模板)标题,库文件..?
- 如何告诉 VBO 处理给定的数据?(布料模拟)
- 我是否必须在类中的所有变量C++设置为 private?
- QString& QString::operator=(const QByteArray&)' is private
- 错误 LNK2001:未解析的外部符号"private: static class Game Game::game_"
- 我们是否需要序列化 VAO 和 VBO
- OpenGL VAO VBO resize
- C++ API-MS-WIN-SERVICE-PRIVATE-L1-1-1.DLL依赖项问题
- glVertexAttribDivisor 存储在哪里 - VAO、VBO 或全局状态?
- 使用OpenGL将结构体加载到成员为Private的VBO中