]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Tweak MapBlock meshgen queue and add Vertex structure
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 28 Mar 2021 08:50:42 +0000 (10:50 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 28 Mar 2021 08:50:42 +0000 (10:50 +0200)
src/mapblock_meshgen.c
src/mesh.c
src/mesh.h

index 5e5131220abc455e7df5e41fecac86dec77db851..ff67244324ab75d6e6feb6d0ee97850ae9c183bc 100644 (file)
@@ -77,7 +77,7 @@ static v3s8 fdir[6] = {
 
 static Array make_vertices(MapBlock *block)
 {
-       Array vertices = array_create(sizeof(GLfloat) * 6);
+       Array vertices = array_create(sizeof(Vertex));
 
        ITERATE_MAPBLOCK {
                NodeDefintion *def = &GNODDEF(block, x, y, z);
@@ -94,7 +94,7 @@ static Array make_vertices(MapBlock *block)
                                };
                                if (VALIDPOS(npos) && ! GNODDEF(block, npos.x, npos.y, npos.z).visible) {
                                        for (int v = 0; v < 6; v++) {
-                                               GLfloat vertex[6] = {
+                                               Vertex vertex = {
                                                        vpos[f][v].x + offset.x,
                                                        vpos[f][v].y + offset.y,
                                                        vpos[f][v].z + offset.z,
@@ -122,11 +122,12 @@ static void *meshgen_thread(void *unused)
        while (! meshgen.cancel) {
                ListPair **lptr = &meshgen.queue.first;
                if (*lptr) {
-                       MapBlock *block = map_get_block(meshgen.map, *(v3s32 *)(*lptr)->key, false);
+                       MapBlock *block = (*lptr)->key;
 
                        pthread_mutex_lock(&meshgen.mtx);
-                       free((*lptr)->key);
-                       *lptr = (*lptr)->next;
+                       ListPair *next = (*lptr)->next;
+                       free(*lptr);
+                       *lptr = next;
                        pthread_mutex_unlock(&meshgen.mtx);
 
                        Array vertices = make_vertices(block);
@@ -153,26 +154,16 @@ static void *meshgen_thread(void *unused)
 
 static void enqueue_block(MapBlock *block)
 {
-       v3s32 *posptr = malloc(sizeof(v3s32));
-       *posptr = block->pos;
        pthread_mutex_lock(&meshgen.mtx);
-       if (! list_put(&meshgen.queue, posptr, NULL))
-               free(posptr);
+       list_put(&meshgen.queue, block, NULL);
        pthread_mutex_unlock(&meshgen.mtx);
 }
 
-static bool compare_positions(void *p1, void *p2)
-{
-       v3s32 *pos1 = p1;
-       v3s32 *pos2 = p2;
-       return pos1->x == pos2->x && pos1->y == pos2->y && pos1->z == pos2->z;
-}
-
 void mapblock_meshgen_init(Map *map, Scene *scene)
 {
        meshgen.map = map;
        meshgen.scene = scene;
-       meshgen.queue = list_create(&compare_positions);
+       meshgen.queue = list_create(NULL);
        pthread_mutex_init(&meshgen.mtx, NULL);
        map->on_block_add = &enqueue_block;
        map->on_block_change = &enqueue_block;
index f3d36dc8aec441409eb51431167dc8faec9a3d5a..19102ab09ec741744efc7fb46708266eb92c111f 100644 (file)
@@ -1,7 +1,7 @@
 #include <stdlib.h>
 #include "mesh.h"
 
-Mesh *mesh_create(GLfloat *vertices, GLsizei count)
+Mesh *mesh_create(Vertex *vertices, GLsizei count)
 {
        Mesh *mesh = malloc(sizeof(Mesh));
        mesh->pos = (v3f) {0.0f, 0.0f, 0.0f};
@@ -12,7 +12,7 @@ Mesh *mesh_create(GLfloat *vertices, GLsizei count)
        mesh->VAO = 0;
        mesh->VBO = 0;
        mesh->remove = false;
-       mesh->vertices = vertices;
+       mesh->vertices = (GLfloat *) vertices;
        mesh->count = count;
        return mesh;
 }
index 39b2c8462ba82372bb0d98924d9164581c7ed1a7..255070165de85b075aac849ce09580b6eb754dbe 100644 (file)
@@ -19,7 +19,13 @@ typedef struct
        GLsizei count;
 } Mesh;
 
-Mesh *mesh_create(GLfloat *vertices, GLsizei count);
+typedef struct
+{
+       GLfloat x, y, z;
+       GLfloat r, g, b;
+} __attribute__((packed, aligned(4))) Vertex;
+
+Mesh *mesh_create(Vertex *vertices, GLsizei count);
 void mesh_delete(Mesh *mesh);
 void mesh_transform(Mesh *mesh);
 void mesh_render(Mesh *mesh, ShaderProgram *prog);