我最近发现有一个名为“Assimp”的库,用于导入可以在 c++ opengl 中渲染动画的模型,现在我试图弄清楚我必须做什么,以便加载纹理模型并绘制它...
1) 推荐从 Blender 导出动画的格式是什么?
2)我已经有一个基本的网格类,正在绘制一个模型。我需要修改什么来支持动画?
我的数据结构:
struct ObjectData {
vector <glm::vec3> vertices, normals, colors;
vector <glm::vec2> texCoords;
vector <GLuint> vIndices, uIndices, nIndices;
vector <Material*> materials;
glm::vec3 size, center;
};
我的网格 VBO 并绘制:
void Mesh::draw() {
if (initialized) {
shader->enable(true);
if (texture != NULL) {
texture->enable(true);
}
//Tell OpenGL which array to use
glBindVertexArray(arrayObject);
glDrawElements(renderMode, object->vIndices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(NULL);
shader->enable(false);
if (texture != NULL) {
texture->enable(false);
}
}
}
void Mesh::updateVBO() {
if (!initialized) {
glGenVertexArrays(1, &arrayObject);
}
//Tell OpenGL which vertex array to use from now
glBindVertexArray(arrayObject);
if (!initialized) {
glGenBuffers(VBO_COUNT, buffers);
initialized = true;
}
//Set buffer data
glBindBuffer(GL_ARRAY_BUFFER, buffers[VBO_VERTEX]);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * object->vertices.size(), &object->vertices.front(), GL_STATIC_DRAW);
//Set shader attribute data
glEnableVertexAttribArray(VBO_VERTEX);
glVertexAttribPointer(VBO_VERTEX, 3, GL_FLOAT, GL_FALSE, NULL, NULL);
if (object->colors.size()) {
//Set buffer data
glBindBuffer(GL_ARRAY_BUFFER, buffers[VBO_COLOR]);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * object->colors.size(), &object->colors.front(), GL_STATIC_DRAW);
//Set shader attribute data
glEnableVertexAttribArray(VBO_COLOR);
glVertexAttribPointer(VBO_COLOR, 3, GL_FLOAT, GL_FALSE, NULL, NULL);
}
if (object->texCoords.size()) {
//Set buffer data
glBindBuffer(GL_ARRAY_BUFFER, buffers[VBO_TEXCORD]);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec2) * object->texCoords.size(), &object->texCoords.front(), GL_STATIC_DRAW);
//Set shader attribute data
glEnableVertexAttribArray(VBO_TEXCORD);
glVertexAttribPointer(VBO_TEXCORD, 2, GL_FLOAT, GL_FALSE, NULL, NULL);
}
if (object->normals.size()) {
//Set buffer data
glBindBuffer(GL_ARRAY_BUFFER, buffers[VBO_NORMAL]);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * object->normals.size(), &object->normals.front(), GL_STATIC_DRAW);
//Set shader attribute data
glEnableVertexAttribArray(VBO_NORMAL);
glVertexAttribPointer(VBO_NORMAL, 3, GL_FLOAT, GL_FALSE, NULL, NULL);
}
//Set buffer data
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[VBO_INDEX]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * object->vIndices.size(), &object->vIndices.front(), GL_STATIC_DRAW);
//Unbind vertex array
glBindVertexArray(NULL);
}