I’m having a rather strange crash with glDrawElements. It seems that my application (which is a really simple OpenGL test App that only draws a textured quad on the screen) crashes with EXCEPTION_ACCESS_VIOLATION (0xc0000005) after it has run for some time.
I did a little experimentation and it crashes somewhere around frame 12900 with vsync enabled, frame 13700 without vsync and 2500 if I don’t set the vertex array pointer with glVertexArrayPointer for every frame.
The rendering is done in render method:
public void render()
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
GL11.glLoadIdentity();
createVertexArray(vQuads);
for(Quad q: quads) drawQuads(q);
update();
}
Setting the vertices:
public void createVertexArray(Vector<Vertex> vList)
{
if(vArraySet) return; // for testing, tests if there is anything to be updated to the GL's vertex list
if(v==null || v.length != vList.size() * 3) // check if the size of the Vector holding the vertices has changed (otherwise no need to make new arrays
{
v = new float[vList.size() * 3];
n = new float[vList.size() * 3];
t = new float[vList.size() * 2];
}
for(int i = 0, vCount = 0; vCount < vList.size(); vCount++)
{
// fill the array holding vertex coordinates
int j = i;
v[i++] = vList.get(vCount).getX();
v[i++] = vList.get(vCount).getY();
v[i++] = vList.get(vCount).getZ();
if(useTexture)
{
// fill the array to hold the texture coords
float[] texture = new float[2];
texture = vList.get(vCount).textureArray();
t[vCount*2] = texture[0];
t[vCount*2 +1] = texture[1];
}
}
FloatBuffer vb = filledFloatBuffer(v); // creates and fill the floatbuffer with the specified array
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glVertexPointer(3, 0, vb);
if(useTexture)
{
FloatBuffer tb = filledFloatBuffer(t); // as above
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
GL11.glTexCoordPointer(2, 0, tb);
}
vArraySet = true;
}
And the method that does the actual rendering:
public void drawQuads(Quad q)
{
if(useTexture)
{
if(q.hasTexture())
{
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, q.getTexture());
}
else GL11.glDisable(GL11.GL_TEXTURE_2D);
}
// for testing that the array actually hold the right information (to see if the crash would actually happen because the buffer wasn't allocated right
// the Quad class just holds the vertex indices that the quad is comprised of and the texture index
int[] a =q.asArray();
IntBuffer ib = filledIntBuffer(q.asArray());
if(ib.get(0) == a[0] && ib.get(a.length-1) == a[a.length-1])
{
GL11.glDrawElements(GL11.GL_QUADS, ib);
}
}