我正在使用 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/