我有一堆代码(从各种教程中复制),它们应该绘制一个随机的变色立方体,使相机每秒钟左右移动一次(使用变量,尚未使用计时器)。在我将代码移入独特的类并将其全部塞入我的主函数之前,它曾经起作用过,但是现在除了空白背景之外,我在主窗口中什么也看不到。我无法在此处指出任何特定问题,因为我没有收到任何错误或异常,我自己定义的代码也可以签出;当我调试时,每个变量都具有我期望的值,并且在重新组织代码之前,我以前使用的着色器(以字符串形式)可以工作。我也可以在与glDrawArrays()
函数相同的作用域中打印出多维数据集的顶点,并且它们也具有正确的值。基本上,我不知道我的代码有什么问题,导致什么也没画。
我的最佳猜测是,我的Model
类的三种方法之一使用错误的数据调用了(或忘记调用了)某些opengl函数。在我的程序中,我创建了一个Model
对象(在glfw和glad初始化之后,然后调用Model
构造函数),每隔一段时间(通过时间进行更新)通过{ {1}}函数,然后在每次通过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,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,// stride
(void*)0 // array buffer offset
);
// Draw the array
glDrawArrays(GL_TRIANGLES,mVertexBufferData.size() / 3);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
};
。