Implement MapBlock mesh generation

master
Elias Fleckenstein 2021-03-27 20:35:06 +01:00
parent dd1ac1b4b4
commit 8e5a653efb
6 changed files with 115 additions and 54 deletions

View File

@ -1,4 +1,4 @@
COMMON = array.o list.o map.o signal.o util.o types.o
COMMON = array.o list.o map.o signal.o util.o types.o node.o
SERVER = $(COMMON) server.o servercommands.o mapgen.o
CLIENT = $(COMMON) client.o clientcommands.o mesh.o scene.o mapblock_meshgen.o shaders.o
LIBRARIES = -lpthread -lm

View File

@ -1,4 +1,5 @@
#include <stdlib.h>
#include "node.h"
#include "mapblock_meshgen.h"
static struct
@ -11,57 +12,109 @@ static struct
bool cancel;
} meshgen;
static v3f vpos[6][6] = {
{
{-0.5f, -0.5f, -0.5f},
{+0.5f, -0.5f, -0.5f},
{+0.5f, +0.5f, -0.5f},
{+0.5f, +0.5f, -0.5f},
{-0.5f, +0.5f, -0.5f},
{-0.5f, -0.5f, -0.5f},
},
{
{-0.5f, -0.5f, +0.5f},
{+0.5f, +0.5f, +0.5f},
{+0.5f, -0.5f, +0.5f},
{+0.5f, +0.5f, +0.5f},
{-0.5f, -0.5f, +0.5f},
{-0.5f, +0.5f, +0.5f},
},
{
{-0.5f, +0.5f, +0.5f},
{-0.5f, -0.5f, -0.5f},
{-0.5f, +0.5f, -0.5f},
{-0.5f, -0.5f, -0.5f},
{-0.5f, +0.5f, +0.5f},
{-0.5f, -0.5f, +0.5f},
},
{
{+0.5f, +0.5f, +0.5f},
{+0.5f, +0.5f, -0.5f},
{+0.5f, -0.5f, -0.5f},
{+0.5f, -0.5f, -0.5f},
{+0.5f, -0.5f, +0.5f},
{+0.5f, +0.5f, +0.5f},
},
{
{-0.5f, -0.5f, -0.5f},
{+0.5f, -0.5f, -0.5f},
{+0.5f, -0.5f, +0.5f},
{+0.5f, -0.5f, +0.5f},
{-0.5f, -0.5f, +0.5f},
{-0.5f, -0.5f, -0.5f},
},
{
{-0.5f, +0.5f, -0.5f},
{+0.5f, +0.5f, -0.5f},
{+0.5f, +0.5f, +0.5f},
{+0.5f, +0.5f, +0.5f},
{-0.5f, +0.5f, +0.5f},
{-0.5f, +0.5f, -0.5f},
},
};
static v3s8 fdir[6] = {
{+0, +0, -1},
{+0, +0, +1},
{-1, +0, +0},
{+1, +0, +0},
{+0, -1, +0},
{+0, +1, +0},
};
#define GNODDEF(block, x, y, z) node_definitions[block->data[x][y][z].type]
#define VALIDPOS(pos) (pos.x >= 0 && pos.x < 16 && pos.y >= 0 && pos.y < 16 && pos.z >= 0 && pos.z < 16)
static Array make_vertices(MapBlock *block)
{
Array vertices = array_create(sizeof(GLfloat));
Array vertices = array_create(sizeof(GLfloat) * 6);
(void) block;
/*
ITERATE_MAPBLOCK {
MapNode node = block->data[x][y][z];
BlockDef *def = block->getDef();
if (! def->drawable)
continue;
ivec3 bpos(x, y, z);
vec3 pos_from_mesh_origin = vec3(bpos) - vec3(SIZE / 2 + 0.5);
for (int facenr = 0; facenr < 6; facenr++) {
ivec3 npos = bpos + face_dir[facenr];
const Block *neighbor_own, *neighbor;
neighbor_own = neighbor = getBlockNoEx(npos);
if (! neighbor)
neighbor = map->getBlock(pos * SIZE + npos);
if (neighbor && ! neighbor->getDef()->drawable)
any_drawable_block = true;
if (! mesh_created_before)
neighbor = neighbor_own;
if (! mesh_created_before && ! neighbor || neighbor && ! neighbor->getDef()->drawable) {
textures.push_back(def->tile_def.get(facenr));
for (int vertex_index = 0; vertex_index < 6; vertex_index++) {
for (int attribute_index = 0; attribute_index < 5; attribute_index++) {
GLdouble value = box_vertices[facenr][vertex_index][attribute_index];
switch (attribute_index) {
case 0:
value += pos_from_mesh_origin.x;
break;
case 1:
value += pos_from_mesh_origin.y;
break;
case 2:
value += pos_from_mesh_origin.z;
break;
}
vertices.push_back(value);
NodeDefintion *def = &GNODDEF(block, x, y, z);
if (def->visible) {
v3u8 pos = {x, y, z};
v3f offset = {x + 8.5f, y + 8.5f, z + 8.5f};
v3f color = get_node_color(def);
for (int f = 0; f < 6; f++) {
v3s8 *noff = &fdir[f];
v3s8 npos = {
pos.x + noff->x,
pos.y + noff->y,
pos.z + noff->z,
};
if (VALIDPOS(npos) && ! GNODDEF(block, npos.x, npos.y, npos.z).visible) {
for (int v = 0; v < 6; v++) {
GLfloat vertex[6] = {
vpos[f][v].x + offset.x,
vpos[f][v].y + offset.y,
vpos[f][v].z + offset.z,
color.x,
color.y,
color.z,
};
array_append(&vertices, &vertex);
}
}
}
}
}
*/
return vertices;
}
#undef GNODDEF
#undef VALIDPOS
static void *meshgen_thread(void *unused)
{
(void) unused;

View File

@ -1,7 +1,7 @@
#include <stdlib.h>
#include "mesh.h"
Mesh *mesh_create(GLfloat *vertices, GLsizei size)
Mesh *mesh_create(GLfloat *vertices, GLsizei count)
{
Mesh *mesh = malloc(sizeof(Mesh));
mesh->pos = (v3f) {0.0f, 0.0f, 0.0f};
@ -13,8 +13,7 @@ Mesh *mesh_create(GLfloat *vertices, GLsizei size)
mesh->VBO = 0;
mesh->remove = false;
mesh->vertices = vertices;
mesh->size = size;
mesh->count = size / 6;
mesh->count = count;
return mesh;
}
@ -49,7 +48,7 @@ static void mesh_configure(Mesh *mesh)
glBindVertexArray(mesh->VAO);
glBindBuffer(GL_ARRAY_BUFFER, mesh->VBO);
glBufferData(GL_ARRAY_BUFFER, mesh->size, mesh->vertices, GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, mesh->count * 6, mesh->vertices, GL_STATIC_DRAW);
GLsizei stride = 6 * sizeof(GLfloat);

View File

@ -16,10 +16,10 @@ typedef struct
GLuint VAO, VBO;
bool remove;
GLfloat *vertices;
GLsizei size, count;
GLsizei count;
} Mesh;
Mesh *mesh_create(GLfloat *vertices, GLsizei size);
Mesh *mesh_create(GLfloat *vertices, GLsizei count);
void mesh_delete(Mesh *mesh);
void mesh_transform(Mesh *mesh);
void mesh_render(Mesh *mesh, ShaderProgram *prog);

View File

@ -1,10 +1,15 @@
#include "util.h"
#include "node.h"
#include "util.h"
NodeDefintion node_definitions[NODE_UNLOADED] = {
{true, html_to_v3f("#F44026")},
{false, {0}},
{true, html_to_v3f("#00CB1F")},
{true, html_to_v3f("#854025")},
{true, html_to_v3f("#7A7A7A")},
{true, false, "#F44026", {0.0f, 0.0f, 0.0f}},
{false, false, "", {0.0f, 0.0f, 0.0f}},
{true, false, "#00CB1F", {0.0f, 0.0f, 0.0f}},
{true, false, "#854025", {0.0f, 0.0f, 0.0f}},
{true, false, "#7A7A7A", {0.0f, 0.0f, 0.0f}},
};
v3f get_node_color(NodeDefintion *def)
{
return def->color_initialized ? def->color : (def->color = html_to_v3f(def->color_str));
}

View File

@ -1,11 +1,11 @@
#ifndef _NODE_H_
#define _NODE_H_
#include <linmath.h/linmath.h>
#include "types.h"
typedef enum
{
NODE_INVALID, // Used for invalid nodes received from server (caused by outdated clients)
NODE_INVALID, // Used for unknown nodes received from server (caused by outdated clients)
NODE_AIR,
NODE_GRASS,
NODE_DIRT,
@ -16,9 +16,13 @@ typedef enum
typedef struct
{
bool visible;
vec3 color;
bool color_initialized;
const char *color_str;
v3f color;
} NodeDefintion;
v3f get_node_color(NodeDefintion *def);
extern NodeDefintion node_definitions[];
#endif