Reputation: 420
During every call to glDrawElements, my graphics driver crashes/freezes and recovers after a few seconds (Windows 7 Timeout Detection/Recovery). glGetError() always returns GL_NO_ERROR.
Edit: Just to be clear about what exactly happens: the first time glDrawElements is called, my computer freezes for 5-10 seconds, then the screen goes black for a few more seconds, then it recovers and Windows gives me a message: "Display driver stopped responding and has recovered". My program keeps running, but it's stuck in glDrawElements.
Update: Adding a call to glFlush() just before glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) makes it work. I don't understand why.
Here's my code, somewhat simplified and without error checking. Am I doing anything wrong?
struct Vertex
{
float pos[3];
float color[3];
};
struct Tri
{
unsigned int idxs[3];
};
// ...
GLuint m_vbo;
GLuint m_ibo;
std::vector<Vertex> m_verts;
std::vector<Tri> m_faces;
// ...
glGenBuffers(1, &m_vbo);
Vertex* vBuf = &(m_verts[0]);
unsigned int vboSize = sizeof(vBuf[0]) * m_verts.size();
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, vboSize, vBuf, GL_STATIC_DRAW);
glGenBuffers(1, &m_ibo);
unsigned int* iBuf = (unsigned int*) (&(m_faces[0]));
unsigned int iboSize = sizeof(iBuf[0]) * (m_faces.size() * 3);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, iboSize, iBuf, GL_STATIC_DRAW);
// ...
// this line fixes it
// glFlush();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(someShaderProgram);
// ...
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
// the attribute locations are queried using glGetAttribLocation in the real code
glEnableVertexAttribArray(1);
glVertexAttribPointer
(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(0));
glEnableVertexAttribArray(0);
glVertexAttribPointer
(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(3*sizeof(float)));
// doesn't get past this line
glDrawElements(GL_TRIANGLES, m_faces.size()*3, GL_UNSIGNED_INT, 0);
// ...
glfwSwapBuffers();
Upvotes: 1
Views: 2779
Reputation: 3510
The last argument for glVertexAttribPointer
is "a offset of the first component" - in byte(!). Are you sure you want the value 3
there?
Upvotes: 1