使用OpenGL将结构体加载到成员为Private的VBO中

Using C++ to Load Structs into a VBO whose Members are Private using OpenGL

本文关键字:Private VBO 成员 OpenGL 结构体 加载 使用      更新时间:2023-10-16

可以将结构体加载到成员变量为私有的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,则会丢失标准布局。