My application has two choices, generate world or load world. Generate world generates a random terrain and stores this in a file. Load map loads the terrain from the file.
Weird thing is it only crashes when I generating the world. It generates and saves the world correctly (as far as I know) but crashes on first glDrawElements that uses the new partial index buffers.
part of my generate world function
for(i=0; i<16; i++){
for(j=0; j<16; j++){
glGenBuffersARB(1, &partIB[j]);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, partIB[j]);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(unsigned int)*partIBSizes[j], partInd[j], GL_STATIC_DRAW_ARB);
fprintf(fout, "%d %d %d %d %d\n", i, j, partIBSizes[j], partIB[j], partInd[j][0]);
}
}
glGenBuffersARB(1, &vb);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vb);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(Vertex)*1022*1022*5, buf, GL_STATIC_DRAW_ARB);
memcpy(bufBig, buf, sizeof(Vertex)*1022*1022*5);
if(SAVE){
fwrite(buf, sizeof(Vertex)*1022*1022*5, 1, mapData);
for(i=0; i<16; i++){
for(j=0; j<16; j++){
fwrite(&partIBSizes[j], sizeof(unsigned int), 1, mapData);
fwrite(partInd[j], sizeof(unsigned int)*partIBSizes[j], 1, mapData);
}
}
}
and part of my load world function
fread(buf, sizeof(Vertex)*1022*1022*5, 1, mapData);
unsigned int *partInd = (unsigned int *)malloc(sizeof(unsigned int)*1024*1024);
for(int i=0; i<16; i++){
for(int j=0; j<16; j++){
fread(&partIBSizes[j], sizeof(unsigned int), 1, mapData);
fread(partInd, sizeof(unsigned int)*partIBSizes[j], 1, mapData);
glGenBuffersARB(1, &partIB[j]);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, partIB[j]);
glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(unsigned int)*partIBSizes[j], partInd, GL_STATIC_DRAW_ARB);
fprintf(fout, "%d %d %d %d %d\n", i, j, partIBSizes[j], partIB[j], partInd[0]);
}
}
free(partInd);
glGenBuffersARB(1, &vb);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vb);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(Vertex)*1022*1022*5, buf, GL_STATIC_DRAW_ARB);
As you can see both functions are pretty much same.
fprintf(fout, "%d %d %d %d %d\n", i, j, partIBSizes[j], partIB[j], partInd[0]);
prints same thing for both functions, however on first run of program, it generates world and saves it but crashes after draw method. However on second run it loads the world but does not crash. What might be the problem?
Also my render function. It creashes on first iteration of loop, after the glDrawElements call.
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vb);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(0));
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET(12));
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), BUFFER_OFFSET(24));
for(int i=0; i<16; i++){
for(int j=0; j<16; j++){
//fprintf(fout, "%d %d %d %d\n", i, j, partIBSizes[j], partIB[j]);
glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, partIB[j]);
glDrawElements(GL_TRIANGLES, partIBSizes[j], GL_UNSIGNED_INT, BUFFER_OFFSET(0));
}
}