opengl - 配置VAO和VBO时,顶点数据数组是否应该使用相同的方法?(OpenGL)
问题描述
我正在使用 VBO 和 VAO 使用 OpenGL 实现人体骨骼动画。不过,我发现了一些有趣的事情。首先,这是我的基本“骨骼”课程
class Bone {
private:
//Joint information compared to the parent Joint!
int BoneID;
glm::mat4 R;
glm::mat4 T;
//orientation of the bone
glm::mat4 Orientation;
Bone * childBone[MAX];
int nChildren = 0;
void recalculateOrientation() {
Orientation = Orientation * R;
}
public:
unsigned int VAO, VBO;
Bone(int BoneID, glm::mat4 R = glm::mat4(), glm::mat4 T = glm::mat4(), glm::mat4 Orientation = glm::mat4()) {
//setup the matrices and orientation
this->BoneID = BoneID;
this->R = R;
this->T = T;
this->Orientation = Orientation;
}
void setVAO(float * vertexData) {
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
}
void setChild(Bone * child) {
childBone[nChildren] = child;
nChildren++;
}
......
};
重点关注的方法是Bone::setVAO(float * vertexData)
。然后我使用以下方法将顶点位置数据设置为骨骼数组。
void setVertexData(Bone * Bones) {
//Bones
float pelvisVertices[] = {
// Back face
-0.5f, -0.2f, -0.2f, // Bottom-left
0.5f, 0.2f, -0.2f, // top-right
0.5f, -0.2f, -0.2f, // bottom-right
0.5f, 0.2f, -0.2f, // top-right
-0.5f, -0.2f, -0.2f, // bottom-left
-0.5f, 0.2f, -0.2f, // top-left
......
}
Bones[0].setVAO(pelvisVertices);
}
在我调用setVertexData()
from之后main()
,我在渲染循环中尝试了以下代码。
//For debugging
glm::mat4 model;
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
glm::mat4 view = camera.GetViewMatrix();
shader.setMat4("model", model);
shader.setMat4("projection", projection);
shader.setMat4("view", view);
glBindVertexArray(Bones[0].returnVAO());
glDrawArrays(GL_TRIANGLES, 0, 36);
然而,窗户上什么也没画。所以我稍微修改了代码,这一次,我决定setVertexData(Bone * bones)
像这样配置里面的 VAO 和 VBO。
void setVertexData(Bone * Bones) {
//Bones
float pelvisVertices[] = {
// Back face
-0.5f, -0.2f, -0.2f, // Bottom-left
0.5f, 0.2f, -0.2f, // top-right
0.5f, -0.2f, -0.2f, // bottom-right
0.5f, 0.2f, -0.2f, // top-right
-0.5f, -0.2f, -0.2f, // bottom-left
-0.5f, 0.2f, -0.2f, // top-left
......
};
// Bones[0].setVAO(pelvisVertices);
glGenVertexArrays(1, &Bones[0].VAO);
glGenBuffers(1, &Bones[0].VBO);
glBindVertexArray(Bones[0].VAO);
glBindBuffer(GL_ARRAY_BUFFER, Bones[0].VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(pelvisVertices), pelvisVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
}
可以看到,我只是简单的把顶点数据数组和VAO、VBO配置放在同一个方法上。不过,这一次是画在屏幕上的!我从未听说过顶点数据数组应该与 VAO、VBO 配置采用相同的方法。发生这种情况的原因是什么?
解决方案
这是因为 sizeof 指针不会返回元素的数量,而是指针的大小(4 或 8 字节)。因此,您告诉 OpenGL 您的顶点数组有 4 或 8 个字节
推荐阅读
- chisel - 如何使用 ChiselStage 将模块拆分为单独的文件?
- c++ - 将字符串或向量传递给函数时 & 是什么意思?
- reactjs - 登录到不同的屏幕后如何从jwt传递数据
- c++ - 回调指向 this 的智能指针,以便对象在回调中保持活动状态
- javascript - Next JS 在刷新之前无法正确加载 Material UI 样式
- r - 当 R 脚本中存在 if 条件时,TaskscheduleR 不起作用
- email - 配置外发邮件服务器返回错误
- css - React Native 中的 Flexbox
- pandas - 熊猫数据框如何对每行较大列的所有值求和
- c# - EF Core 和 Automapper - 过滤子记录