diff options
Diffstat (limited to 'vbo.c')
-rw-r--r-- | vbo.c | 44 |
1 files changed, 24 insertions, 20 deletions
@@ -20,13 +20,14 @@ #define BUFFER_OFFSET(i) ((char *)NULL + (i)) /* for simplicity we pack */ +/* this structure represents a point with attributes */ #pragma pack(1) -typedef struct vbo_slice +typedef struct gltpoint { GLfloat normal[3]; GLfloat texcoord[2]; GLubyte color[3]; -} vbo_slice_t; +} gltpoint_t; #pragma pack() /* globals */ @@ -63,23 +64,24 @@ static int gltGenVBOInterleaved(GLuint *bufferobjects, float *vert_arr, float *texcoord_arr, float *norm_arr, unsigned char *color_arr, unsigned short *indices_arr, size_t points_n, size_t indices_n) { - /* skip error check for now */ + /* skip error checks for now */ /* create an interleaved data structure, is it worth the cpu time? */ size_t attrib_buff_sz = ((points_n * (3+2) * sizeof(GLfloat)) + (points_n * 3 * sizeof(GLubyte))); - if ((attrib_buff_sz % sizeof(vbo_slice_t)) != 0) + if ((attrib_buff_sz % sizeof(gltpoint_t)) != 0) { - fprintf(stderr, "gen_vbo: fatal, vbo_slice_t appears to be padded!\n"); + fprintf(stderr, "gltGenVBOInterleaved: fatal, gltpoint_t appears to be padded!\n"); exit(-1); } - size_t slices_n = attrib_buff_sz / sizeof(vbo_slice_t); + size_t slices_n = attrib_buff_sz / sizeof(gltpoint_t); - vbo_slice_t *vbo_slices_arr = (vbo_slice_t *) malloc(attrib_buff_sz); - if (!vbo_slices_arr) + gltpoint_t *gltpoints_arr = (gltpoint_t *) malloc(attrib_buff_sz); + if (!gltpoints_arr) { + fprintf(stderr, "%s failed, %s:%d\n", __FUNCTION__, __FILE__, __LINE__); perror("malloc"); exit(-1); } @@ -88,14 +90,14 @@ static int gltGenVBOInterleaved(GLuint *bufferobjects, float *vert_arr, int i; for (i = 0; i < slices_n; i++) { - vbo_slices_arr[i].normal[0] = norm_arr[i * 3 + 0]; - vbo_slices_arr[i].normal[1] = norm_arr[i * 3 + 1]; - vbo_slices_arr[i].normal[2] = norm_arr[i * 3 + 2]; - vbo_slices_arr[i].texcoord[0] = texcoord_arr[i * 2 + 0]; - vbo_slices_arr[i].texcoord[1] = texcoord_arr[i * 2 + 1]; - vbo_slices_arr[i].color[0] = color_arr[i * 3 + 0]; - vbo_slices_arr[i].color[1] = color_arr[i * 3 + 1]; - vbo_slices_arr[i].color[2] = color_arr[i * 3 + 2]; + gltpoints_arr[i].normal[0] = norm_arr[i * 3 + 0]; + gltpoints_arr[i].normal[1] = norm_arr[i * 3 + 1]; + gltpoints_arr[i].normal[2] = norm_arr[i * 3 + 2]; + gltpoints_arr[i].texcoord[0] = texcoord_arr[i * 2 + 0]; + gltpoints_arr[i].texcoord[1] = texcoord_arr[i * 2 + 1]; + gltpoints_arr[i].color[0] = color_arr[i * 3 + 0]; + gltpoints_arr[i].color[1] = color_arr[i * 3 + 1]; + gltpoints_arr[i].color[2] = color_arr[i * 3 + 2]; } glGenBuffers(3, bufferobjects); @@ -106,12 +108,14 @@ static int gltGenVBOInterleaved(GLuint *bufferobjects, float *vert_arr, /* attribute data, normals, texcoords, and color */ glBindBuffer(GL_ARRAY_BUFFER, bufferobjects[1]); - glBufferData(GL_ARRAY_BUFFER, attrib_buff_sz, (const GLvoid *) vbo_slices_arr, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, attrib_buff_sz, (const GLvoid *) gltpoints_arr, GL_STATIC_DRAW); /* indices data */ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferobjects[2]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short) * indices_n, (const GLvoid *) indices_arr, GL_STATIC_DRAW); + free(gltpoints_arr); + return 0; } @@ -128,10 +132,10 @@ static int gltDrawVBOInterleaved(GLuint *bufferobjects, size_t indices_n) /* attribute data, normals, texcoords, and color */ glBindBuffer(GL_ARRAY_BUFFER, bufferobjects[1]); - glNormalPointer(GL_FLOAT, sizeof(vbo_slice_t), BUFFER_OFFSET(0)); + glNormalPointer(GL_FLOAT, sizeof(gltpoint_t), BUFFER_OFFSET(0)); glClientActiveTexture(GL_TEXTURE0); - glTexCoordPointer(2, GL_FLOAT, sizeof(vbo_slice_t), BUFFER_OFFSET(sizeof(GLfloat) * 3)); - glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(vbo_slice_t), BUFFER_OFFSET(sizeof(GLfloat) * 5)); + glTexCoordPointer(2, GL_FLOAT, sizeof(gltpoint_t), BUFFER_OFFSET(sizeof(GLfloat) * 3)); + glColorPointer(3, GL_UNSIGNED_BYTE, sizeof(gltpoint_t), BUFFER_OFFSET(sizeof(GLfloat) * 5)); /* indices data */ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, bufferobjects[2]); |