我们是不是需要序列化 VAO 和 VBO
Posted
技术标签:
【中文标题】我们是不是需要序列化 VAO 和 VBO【英文标题】:Do we need to serialize the VAO and VBO我们是否需要序列化 VAO 和 VBO 【发布时间】:2019-09-17 10:18:50 【问题描述】:我正在使用 boost 序列化我的数据。
这就是我的班级的样子。
我正在序列化除 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,因为目前我不这样做,但对象仍然可以正确反序列化。
【问题讨论】:
请附上minimal reproducible example,展示您如何序列化和反序列化。谢谢! @Max Vollmer 我已经更新了代码。 对象存储的 V*O 整数是 OpenGL 内存中的句柄。如果您打算在程序的不同调用中使用它们,则需要重新创建它们(glGenVertexArray
、glGenBuffers
和 glCreateShader
)。这可能发生在您的 Sum_Circle
构造函数中,但您没有显示代码,因此我们无法为您回答问题。
【参考方案1】:
如果 VAO 是指从 glGenVertexArrays 获得的那个,那么“否”。 您需要在每次运行时生成它们。这些是 OpenGL Id。在每个应用程序运行时,它们可能不同。
【讨论】:
以上是关于我们是不是需要序列化 VAO 和 VBO的主要内容,如果未能解决你的问题,请参考以下文章
VAO 是不是同时记住 EBO/IBO(元素或索引)和 VBO?