为什么在GLFW窗口中没有用这个代码在我的屏幕上绘制立方体

How come no cube is drawn on my screen with this code in a GLFW window?

本文关键字:我的 代码 屏幕 绘制 立方体 窗口 GLFW 有用 为什么      更新时间:2023-10-16

我有一堆代码(从各种教程中复制而来(,应该绘制一个随机变色的立方体,相机每隔一秒左右就会移动一次(有一个变量,还没有使用计时器(。在我把代码移到不同的类中并把它们全部推到主函数中之前,它在过去是有效的,但现在我在主窗口上除了空白背景之外什么都看不到。我不能在这里指出任何特定的问题,因为我没有得到任何错误或异常,而且我自己定义的代码已经检查出来了;当我调试时,每个变量都有一个我期望的值,在我重新组织代码之前,我使用的着色器(以字符串形式(在过去是有效的。我也可以在与glDrawArrays()函数相同的范围内打印出立方体的顶点,它们也有正确的值。基本上,我不知道我的代码出了什么问题,导致什么都没画出来。

我的最佳猜测是,我在Model类的三个方法中的一个方法中,用错误的数据错误地调用了或忘记调用了某些opengl函数。在我的程序中,我创建了一个Model对象(在glfw和gree初始化后,它调用Model构造函数(,通过update()函数每隔一段时间更新一次(时间无关紧要(,然后在每次通过draw()函数运行主循环时将其绘制到屏幕上。

代码故障的可能位置:

Model::Model(std::vector<GLfloat> vertexBufferData, std::vector<GLfloat> colorBufferData) {
mVertexBufferData = vertexBufferData;
mColorBufferData = colorBufferData;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &VBO);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, VBO);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(mVertexBufferData), &mVertexBufferData.front(), GL_STATIC_DRAW);
glGenBuffers(1, &CBO);
glBindBuffer(GL_ARRAY_BUFFER, CBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(mColorBufferData), &mColorBufferData.front(), GL_STATIC_DRAW);
// Create and compile our GLSL program from the shaders
programID = loadShaders(zachos::DATA_DEF);
glUseProgram(programID);
}
void Model::update() {
for (int v = 0; v < 12 * 3; v++) {
mColorBufferData[3 * v + 0] = (float)std::rand() / RAND_MAX;
mColorBufferData[3 * v + 1] = (float)std::rand() / RAND_MAX;
mColorBufferData[3 * v + 2] = (float)std::rand() / RAND_MAX;
}
glBufferData(GL_ARRAY_BUFFER, sizeof(mColorBufferData), &mColorBufferData.front(), GL_STATIC_DRAW);
}
void Model::draw() {
// Setup some 3D stuff
glm::mat4 mvp = Mainframe::projection * Mainframe::view * model;
GLuint MatrixID = glGetUniformLocation(programID, "MVP");
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &mvp[0][0]);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(
0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
3,                  // size
GL_FLOAT,           // type
GL_FALSE,           // normalized?
0,                  // stride
(void*)0            // array buffer offset
);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, CBO);
glVertexAttribPointer(
1,                                // attribute. No particular reason for 1, but must match the layout in the shader.
3,                                // size
GL_FLOAT,                         // type
GL_FALSE,                         // normalized?
0,                                // stride
(void*)0                          // array buffer offset
);
// Draw the array
glDrawArrays(GL_TRIANGLES, 0, mVertexBufferData.size() / 3);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
};

我的问题很简单,为什么我的程序不会在屏幕上画一个立方体?这个问题是在这三个职能范围内还是在其他方面?如果需要,我可以提供有关绘图过程的更一般的信息,尽管我相信我提供的代码已经足够了,因为我实际上只是调用model.draw()

sizeof(std::vector(通常只有24字节(因为结构通常包含3个指针(。所以基本上,两个缓冲区都加载了6个浮点,这对于一个三角形来说是不够的,更不用说一个立方体了!

在将数据加载到顶点缓冲区时,应该对向量调用size((。

glBufferData(GL_ARRAY_BUFFER,
mVertexBufferData.size() * sizeof(float), ///< this!
mVertexBufferData.data(), ///< prefer calling data() here!
GL_STATIC_DRAW);
相关文章: