lefti lefti - 24 days ago 10
C Question

OpenGL - Allocating data to a list in C and using it in glBufferData doesn't work correctly

I have a problem when pushing back data to a list and then using this list in OpenGL glBufferData.

I have a simple OpenGL scene, where I draw 3 sprites using 3 different spritebatches. It works when I allocate memory for the vertices beforehand and then use this vertex array for the sprite vertices, then pushing back to the list. But what I want is to push back the sprite vertices directly into the list, which doesn't work and I only get 3 triangles (half sprites).

sb->vertices is a list.

This works and draws 3 sprites:

void add_verts(sprite_batch* sb, sprite* s[])
{
int cv = 0; // Current vertex

vertex* vertices; // Allocate memory for vertices
vertices = malloc(sb->sprite_count * sizeof(vertex) * 6); // Each sprite has 6 vertices

for(unsigned int i = 0; i < sb->sprite_count; i++)
{
// Top right
vertices[cv++] = s[i]->tr;
list_push_back(sb->vertices, &vertices[cv-1]);

// Top left
vertices[cv++] = s[i]->tl;
list_push_back(sb->vertices, &vertices[cv-1]);

// Bottom left
vertices[cv++] = s[i]->bl;
list_push_back(sb->vertices, &vertices[cv-1]);

// Bottom left
vertices[cv++] = s[i]->bl;
list_push_back(sb->vertices, &vertices[cv-1]);

// Bottom right
vertices[cv++] = s[i]->br;
list_push_back(sb->vertices, &vertices[cv-1]);

// Top right
vertices[cv++] = s[i]->tr;
list_push_back(sb->vertices, &vertices[cv-1]);
}
}


Working version

This doesn't work and draws only 3 half-sprites:

void add_verts(sprite_batch* sb, sprite* s[])
{
for(unsigned int i = 0; i < sb->sprite_count; i++)
{
// Top right
list_push_back(sb->vertices, &s[i]->tr);

// Top left
list_push_back(sb->vertices, &s[i]->tl);

// Bottom left
list_push_back(sb->vertices, &s[i]->bl);

// Bottom left
list_push_back(sb->vertices, &s[i]->bl);

// Bottom right
list_push_back(sb->vertices, &s[i]->br);

// Top right
list_push_back(sb->vertices, &s[i]->tr);
}
}


Not working

I thought pushing back to the list like this would give contiguous block of memory which I could use in glBufferData.

Here is how I call glBufferData():

glBufferData(GL_ARRAY_BUFFER, sb->vertices->num_items * sizeof(vertex), list_get(sb->vertices, 0), GL_STATIC_DRAW);


This is the code for the list:

typedef struct
{
int num_items;
int num_slots;
void** items;
} list;

static void list_reserve_more(list* l)
{
if (l->num_items > l->num_slots)
{
l->num_slots = ceil((l->num_slots + 1) * 1.5);
l->items = realloc(l->items, sizeof(void*) * l->num_slots);
}
}

void list_push_back(list* l, void* item)
{
l->num_items++;
list_reserve_more(l);
l->items[l->num_items-1] = item;
}

void* list_get(list* l, int index)
{

if (index < 0) { printf("Index out of bounds"); return NULL; }
if (index >= l->num_items) { printf("Index out of bounds"); return NULL; }

return l->items[index];
}

BDL BDL
Answer

What you create in your list struct is a continuous memory of pointers to the data, not a continuous memory containing the data. OpenGL expects you to pass an array containing the data (e.g., the vertices array in your first code sample).