Reputation: 29
I write a code use glDrawElement to draw a rectangle:
Vertex verticesData[] =
{
Vertex( Vector3 (-0.5f, -0.5f, 0.0f) , Vector3 (1.0f, 0.0f, 0.0f) ), //0
Vertex( Vector3 (-0.5f, 0.5f, 0.0f ) , Vector3 (0.0f, 1.0f, 0.0f) ), //1
Vertex( Vector3 (0.5f, 0.5f, 0.0f ) , Vector3 (0.0f, 0.0f, 1.0f) ), //2
Vertex( Vector3 (-0.5f, -0.5f, 0.0f) , Vector3 (0.5f, 0.5f, 0.5f) ) //3
};
GLubyte indices[] =
{
0, 1, 2, //1
0, 2, 3 //2
};
glGenBuffers(1, &vboId);
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, sizeof(verticesData), verticesData, GL_STATIC_DRAW);
glGenBuffers(1, &indexBufferId);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferId);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
This is in Init() function. And the program make runetime error (access violate) when run to code line:
glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_BYTE, NULL);
This statement is in Draw() function. My code is very long so that i cant post all my code here, but if i replace 2 above code blocks by using glDrawArray, there is no error. So I think the problem is in above code. Anyone help me solve this problem,
Upvotes: 0
Views: 104
Reputation: 52084
GLubyte indices[] =
{
0, 1, 2, //1
0, 2, 3 //2
};
...
glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_BYTE, NULL);
^^
You have 6 indices. If you tell OpenGL you have 18 it'll read right off the end of your indices
array.
Pass in 6 instead.
Upvotes: 1