c++ - 使用 C++ 将结构加载到使用 OpenGL 的成员为私有(private)的 VBO

标签 c++ opengl

能否将结构加载到成员变量为私有(private)的 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 或缓冲区对象无关。您要问的是复制具有私有(private)成员的结构的二进制数据的结果是什么。也就是说,您可以使用 memcpy 获得什么。

C++98/03 只有在 the object is a POD (plain old data) type 时才允许这样的内存布局合法。 .具有私有(private)成员的类不是 POD。

C++11 relaxes these rules.要使 memcpy 工作(这是 glBufferSubData 最终所做的),您需要将类型设置为 trivially copyable。 .所以没有虚函数或非平凡成员。为了确保数据的实际布局(例如,offsetof 会真正起作用),类型必须是standard layout。 .

特别适合您,这意味着如果您有私有(private)成员,则所有(非静态)成员变量都必须是私有(private)的。如果某些成员是公共(public)的而其他成员是私有(private)的,您将失去标准布局。

关于c++ - 使用 C++ 将结构加载到使用 OpenGL 的成员为私有(private)的 VBO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16753523/

相关文章:

c++ - 使用 OpenGL 渲染 QImage

c++ - 怎样让白色变透明

c++ - 无法从文件中读取值

c++ - 如何清除进程命令行?

c++ - 斐波那契堆的 STL?

C++ 类变量不更新

c++ - OpenGL 帧缓冲区 ColorAttachment 到计算着色器

java - 水波纹效果错误(Java Opengl)

c++ - 从文件读取数据到链表很慢

c++ - OpenGL 投影/矩阵混淆