我只是想用最基本的OpenGL代码渲染一个简单的三角形,这里是:(所有这些代码都在main()函数中)
GLfloat data[] = {
0, 0.5,
-0.5, -0.5,
0.5, -0.5
};
GLuint vertexBufferID;
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
while (!glfwWindowShouldClose(window))
{
glDrawArrays(GL_TRIANGLES, 0, 3);
}
如果我这样做,屏幕上会出现一个三角形,eveything工作正常,但问题是,只要我将缓冲区代码(glGenBuffers,BindBuffer,BufferData等)移动到另一个类,三角形消失了,这是代码:(这是main()函数)
GLfloat data[] = {
0, 0.5,
-0.5, -0.5,
0.5, -0.5
};
//the buffer code is now called by this VertexBuffer constructor which
//requires vertex data to be passed into
VertexBuffer vertexBuffer(data);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
while (!glfwWindowShouldClose(window))
{
glDrawArrays(GL_TRIANGLES, 0, 3);
}
这是VertexBuffer类构造函数代码:
VertexBuffer::VertexBuffer(const void* data)
{
//the vertexBufferID here is defined in the header
glGenBuffers(1, &vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
}
正如我所说的,VertexBuffer构造函数代码与main()函数中的缓冲区代码完全相同,除非它已被移动到另一个类中。我正在努力弄清问题是什么。任何帮助都会很棒,谢谢。