我有一堆代码(从各种教程中复制)应该绘制一个随机的变色立方体,相机每秒左右移动一次(带有一个变量,尚未使用计时器)。在我将我的代码移动到独特的类中并将其全部推入我的主函数之前,它过去是有效的,但现在我在主窗口上除了空白背景之外什么都看不到。我无法在此处查明任何特定问题,因为我没有收到任何错误或异常,并且我自己定义的代码已检出;当我调试时,每个变量都有一个我期望的值,并且在我重新组织我的代码之前,我使用的着色器(以字符串形式)在过去工作。我可以在相同的范围内打印出立方体的顶点glDrawArrays()
功能也一样,它们也有正确的值。基本上,我不知道我的代码有什么问题导致无法绘制任何内容。
我最好的猜测是我调用了 - 或忘记调用 - 在我的Model
班级的三种方法之一中使用错误的数据不正确地调用了某些 opengl 函数。在我的程序中,我创建了一个Model
对象(在初始化 glfw 和glad 之后,然后调用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()
.