Skip to content

Instantly share code, notes, and snippets.

@jwbuurlage
Created February 26, 2012 10:01
Show Gist options
  • Save jwbuurlage/1915777 to your computer and use it in GitHub Desktop.
Save jwbuurlage/1915777 to your computer and use it in GitHub Desktop.
void Terrain::generateIndices() {
// we calculate the amount of levels and make the index buffers
int level_max = log2(patch_size - 1);
indexCount = new GLuint[level_max + 1];
indexBuffer = new GLuint[level_max + 1];
for(int l = 0; l < level_max; ++l)
{
// make a container for the indices, calculate the
// patch size for this level, and init the index counter
int patch_size_level = pow(2,level_max - l) + 1;
int current_index = 0;
indexCount[l] = (2*patch_size_level+1) * (patch_size_level - 1);
GLuint* indices = new GLuint[indexCount[l]];
for(int i = 0; i < patch_size - 1; i += pow(2,l)){
if((int)(i/pow(2,l)) % 2 == 0) {
// dir ---->
for(int j = 0; j < patch_size; j += pow(2,l)) {
indices[current_index] = (i * patch_size) + j;
++current_index;
indices[current_index] = ((i + pow(2,l)) * patch_size) + j;
++current_index;
}
indices[current_index] = (i + pow(2,l) + 1) * patch_size - 1; //make degenerate triangle
++current_index;
}
else {
// dir <----
for(int j = 0; j < patch_size; j += pow(2,l)) {
indices[current_index] = (i + 1) * patch_size - 1 - j;
++current_index;
indices[current_index] = (i + 1 + pow(2, l)) * patch_size - 1 - j;
++current_index;
}
indices[current_index] = (i + pow(2,l)) * patch_size; //make degenerate triangle
++current_index;
}
}
// save the indexbuffer
glGenBuffers(1, &indexBuffer[l]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer[l]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GL_UNSIGNED_INT) * indexCount[l], indices, GL_STATIC_DRAW);
delete[] indices;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment