c - OpenGL - 将数据分配给 C 中的列表并在 glBufferData 中使用它无法正常工作

标签 c list opengl sprite contiguous

我在将数据推回列表然后在 OpenGL glBufferData 中使用此列表时遇到问题。

我有一个简单的 OpenGL 场景,我在其中使用 3 个不同的 Sprite 批处理绘制了 3 个 Sprite 。当我预先为顶点分配内存,然后将这个顶点数组用于 Sprite 顶点,然后推回列表时,它就起作用了。但我想要的是将 Sprite 顶点直接推回到列表中,这是行不通的,我只得到 3 个三角形(半 Sprite )。

sb->顶点是一个列表。

这有效并绘制了 3 个 Sprite :

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

这不起作用,只绘制了 3 个半 Sprite :

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

我认为像这样推回列表会提供连续的内存块,我可以在 glBufferData 中使用它。

下面是我调用 glBufferData() 的方式:

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]; 
}

最佳答案

您在列表结构中创建的是指向数据的指针的连续内存,而不是包含数据的连续内存。 OpenGL 希望您传递一个包含数据的数组(例如,第一个代码示例中的 vertices 数组)。

关于c - OpenGL - 将数据分配给 C 中的列表并在 glBufferData 中使用它无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40633785/

相关文章:

C - 轻松将 void** 转换为 char* 吗?

c - 十六进制到字符的转换和转储输出

c - setvbuf() - 当 buf 为 NULL 时的大小参数

python - 将列表的一部分与Python中的完整列表进行比较

c++ - 如何为我的 3d 曲线添加可变性 - openGL/C++ 中的球面坐标

python - Pyglet 未绘制预定函数

c - 我是否必须使用 C99 在每个 .c 文件上声明 "extern inline"?

c# - 如何根据条件从列表中减去属性值

ios - 如何在列表中搜索具有特定属性的对象的索引?

macos - 将OpenCV集成到OpenGL应用程序中