OpenGL—在C中将数据分配到列表中,并在glBufferData中使用它;我不能正常工作
我在将数据推回列表,然后在OpenGL glBufferData中使用此列表时遇到问题 我有一个简单的OpenGL场景,我用3个不同的SpriteBatch绘制了3个精灵。当我事先为顶点分配内存,然后将此顶点数组用于精灵顶点,然后将其推回列表时,它就会起作用。但是我想要的是将精灵顶点直接推回到列表中,这不起作用,我只得到3个三角形(半精灵) sb->顶点是一个列表 这将工作并绘制3个精灵:OpenGL—在C中将数据分配到列表中,并在glBufferData中使用它;我不能正常工作,c,list,opengl,sprite,contiguous,C,List,Opengl,Sprite,Contiguous,我在将数据推回列表,然后在OpenGL glBufferData中使用此列表时遇到问题 我有一个简单的OpenGL场景,我用3个不同的SpriteBatch绘制了3个精灵。当我事先为顶点分配内存,然后将此顶点数组用于精灵顶点,然后将其推回列表时,它就会起作用。但是我想要的是将精灵顶点直接推回到列表中,这不起作用,我只得到3个三角形(半精灵) sb->顶点是一个列表 这将工作并绘制3个精灵: void add_verts(sprite_batch* sb, sprite* s[]) { i
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]);
}
}
以下是列表的代码:
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];
}
typedef结构
{
int num_项目;
int num_插槽;
作废**项;
}名单;
静态无效列表\保留\更多(列表*l)
{
如果(l->num\u项目>l->num\u插槽)
{
l->num_slots=ceil((l->num_slots+1)*1.5);
l->items=realloc(l->items,sizeof(void*)*l->num\u插槽);
}
}
作废列表(列表*l,作废*项)
{
l->num_items++;
列出更多的储备(l);
l->items[l->num\u items-1]=项目;
}
void*list\u get(list*l,int索引)
{
如果(索引<0){printf(“索引超出边界”);返回NULL;}
如果(index>=l->num_items){printf(“索引超出边界”);返回NULL;}
返回l->项目[索引];
}
在列表结构中创建的是指向数据的指针的连续内存,而不是包含数据的连续内存。OpenGL希望您传递包含数据的数组(例如,第一个代码示例中的顶点数组)。在列表结构中创建的是指向数据的指针的连续内存,而不是包含数据的连续内存。OpenGL希望您传递一个包含数据的数组(例如,第一个代码示例中的顶点
数组)
glBufferData(GL_ARRAY_BUFFER, sb->vertices->num_items * sizeof(vertex), list_get(sb->vertices, 0), GL_STATIC_DRAW);
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];
}