Skip to content

Instantly share code, notes, and snippets.

@datenwolf
Created January 24, 2015 10:01
Show Gist options
  • Save datenwolf/8754cd99869b94877e5c to your computer and use it in GitHub Desktop.
Save datenwolf/8754cd99869b94877e5c to your computer and use it in GitHub Desktop.
small explanatory code for OpenGL vertex array draw modes
#include <stdio.h>
typedef long MYenum;
enum {
SINGLES,
PAIRS,
TRIPLES,
};
int *array = NULL;
/* mimic the call to glVertexAttribPointer */
void array_pointer(void *p)
{
array = p;
}
/* mimicks the behavior of glDrawArrays */
void print_arrays(MYenum mode, int first, size_t count)
{
size_t N, i, j;
if( !array ) return;
switch(mode) {
default: return;
case SINGLES: N = 1; break;
case PAIRS: N = 2; break;
case TRIPLES: N = 3; break;
}
for(i = 0; i <= count-N;) {
printf("(");
for(j = 0; j < N; ++j, ++i) {
printf("%d", array[ first + i ] );
if( j + 1 < N ) printf(", ");
}
printf(")");
if( i + 1 <= count-N+1 ) printf(", ");
}
printf("\n");
}
/* mimics the behavior of glDrawElements */
void print_elements(MYenum mode, size_t count, int const *indices)
{
size_t N, i, j;
if( !array
|| !indices ) return;
switch(mode) {
default: return;
case SINGLES: N = 1; break;
case PAIRS: N = 2; break;
case TRIPLES: N = 3; break;
}
for(i = 0; i <= count-N;) {
printf("(");
for(j = 0; j < N; ++j, ++i) {
printf("%d", array[ indices[i] ] );
if( j + 1 < N ) printf(", ");
}
printf(")");
if( i + 1 <= count-N+1 ) printf(", ");
}
printf("\n");
}
int main(int argc, char *argv[])
{
int singles[4] = {
11,
12,
13,
14
};
int single_elements[10] = {
0, 1, 0, 2, 3,
3, 0, 0, 2, 1
};
int pairs[6] = {
21, 22,
31, 32,
41, 42
};
int pairs_elements[12] = {
0, 1,
0, 2,
2, 3,
2, 4,
4, 5,
4, 0
};
int triples[9] = {
51, 52, 53,
61, 62, 63,
71, 72, 73
};
int triple_elements[15] = {
0, 1, 2,
3, 4, 5,
6, 7, 8,
7, 4, 1,
0, 3, 6
};
printf("== a few chains of singles ==\n");
array_pointer(singles);
printf("print array starting with 0, 3 consecutive elements:\n");
print_arrays(SINGLES, 0, 3);
printf("print array starting with 1, 3 consecutive elements:\n");
print_arrays(SINGLES, 1, 3);
printf("print 10 elements by index:\n");
print_elements(SINGLES, 10, single_elements);
printf("\n== a few chains of pairs ==\n");
array_pointer(pairs);
printf("print array starting with 0, 6 consecutive elements:\n");
print_arrays(PAIRS, 0, 6);
printf("print array starting with 1, 3 consecutive elements (should drop one element at the end since 3%%2 = 1):\n");
print_arrays(PAIRS, 1, 3);
printf("print 12 elements by index:\n");
print_elements(PAIRS, 12, pairs_elements);
printf("\n== a few chains of triples ==\n");
array_pointer(triples);
printf("print array starting with 0, 9 consecutive elements:\n");
print_arrays(TRIPLES, 0, 9);
printf("print array starting with 2, 6 consecutive elements:\n");
print_arrays(TRIPLES, 2, 6);
printf("print 15 elements by index:\n");
print_elements(TRIPLES, 15, triple_elements);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment