c++ - 我们需要序列化 ​​VAO 和 VBO

标签 c++ opengl serialization boost

我正在使用 boost 序列化我的数据。

这就是我的类(class)的样子。

我正在序列化除 VAO 、 VBO 和着色器之外的大部分数据。

我的对象被正确反序列化。

#pragma once
#include <vector>
#include "Geometry.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.h"
#include <boost/serialization/serialization.hpp>



class Sum_Circle : public Geometry
{
public:
    Sum_Circle();
    Sum_Circle(const Shader& shader);
    Sum_Circle(const Sum_Circle& circle);
    Sum_Circle& operator=(const Sum_Circle& circle);
    ~Sum_Circle();
    Geometry* Clone() const;
    void init();
    void CleanUp();
    void draw();
    std::vector<float> GetMesh();
    std::vector<float> data;
    std::vector< Sum_Vertices > GetVertices();
    void CreateUI(QFormLayout* layout);

private:
    bool isInited;
    int iSegments;
    unsigned int m_VAO, m_VBO, m_EBO;
    int iNumsToDraw;
    bool isChanged;
    Shader shader;
    int iEntries;

public slots:
    void ParamChange();


private:
    typedef Geometry _Super;
    friend class boost::serialization::access;

    template<typename Archive>
    void save(Archive& ar, const unsigned int version ) const {

        ar & boost::serialization::base_object<_Super>(*this);
        ar & isInited & iSegments & m_VAO & m_VBO & m_EBO & iNumsToDraw & isChanged  & iEntries;
    }

    template<typename Archive>
    void load(Archive& ar, const unsigned int version)  {

        ar & boost::serialization::base_object<_Super>(*this);
        ar & isInited & iSegments & m_VAO & m_VBO & m_EBO & iNumsToDraw & isChanged  & iEntries;
    }

    BOOST_SERIALIZATION_SPLIT_MEMBER()

};

这就是着色器类的样子。

class Shader
{
public:
    // State
    GLuint ID;
    // Constructor
    Shader() { }
    // Sets the current shader as active
    Shader  &Use();
    // Compiles the shader from given source code
    void    Compile(const GLchar *vertexSource, const GLchar *fragmentSource, const GLchar *geometrySource = nullptr); // Note: geometry source code is optional 
    // Utility functions
    void    SetFloat(const GLchar *name, GLfloat value, GLboolean useShader = false);
    void    SetInteger(const GLchar *name, GLint value, GLboolean useShader = false);
    void    SetVector2f(const GLchar *name, GLfloat x, GLfloat y, GLboolean useShader = false);
    void    SetVector2f(const GLchar *name, const glm::vec2 &value, GLboolean useShader = false);
    void    SetVector3f(const GLchar *name, GLfloat x, GLfloat y, GLfloat z, GLboolean useShader = false);
    void    SetVector3f(const GLchar *name, const glm::vec3 &value, GLboolean useShader = false);
    void    SetVector4f(const GLchar *name, GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLboolean useShader = false);
    void    SetVector4f(const GLchar *name, const glm::vec4 &value, GLboolean useShader = false);
    void    SetMatrix4(const GLchar *name, const glm::mat4 &matrix, GLboolean useShader = false);
private:
    // Checks if compilation or linking failed and if so, print the error logs
    void    checkCompileErrors(GLuint object, std::string type);
};

我们是否需要序列化 ​​VAO 和 VBO,因为目前我没有这样做,但对象仍然正确反序列化。

最佳答案

如果您所说的 VAO 是指从 glGenVertexArrays 获得的那一个,那么“否”。
您需要在每次运行时生成它们。这些是 OpenGL Id。在每次应用程序运行时,它们可能不同。

关于c++ - 我们需要序列化 ​​VAO 和 VBO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57972153/

相关文章:

opengl - 权限被拒绝在Linux上运行我自己的程序?

mysql - 从字符串或序列化数组中查询数据

c# - 无法创建组件 .. 类型未标记为可序列化

c++ - 是否有一种工具可以帮助了解谁将特定参数传递给函数,以及该变量最初是在哪里创建的(在 C/C++ 中)

c++ - 调用 VirtualProtect 设置了错误的保护

c++ - std::list<std::reference_wrapper<T>> 的基于概念限制范围的 for 循环

OpenGL 渲染到多个纹理,结果为白色

c - OpenGL - glVertex2f(x, y) 中的 x 和 y 之间的映射到屏幕整数坐标

c# - XNA XML 序列化问题

c++ - 指向派生类的指针是否首先创建基类?