1 #include <dragonstd/tree.h>
6 #include "client/cube.h"
7 #include "client/opengl.h"
8 #include "client/mesh.h"
20 static v3s32 face_dir[6] = {
33 } __attribute__((packed)) LoadedVertex;
34 static VertexLayout loaded_layout = {
35 .attributes = (VertexAttribute[]) {
36 {GL_FLOAT, 3, sizeof(v3f32)}, // position
37 {GL_FLOAT, 3, sizeof(v3f32)}, // normal
38 {GL_FLOAT, 3, sizeof(v3f32)}, // color
41 .size = sizeof(LoadedVertex),
44 static int cmp_loaded_voxel(const LoadedVoxel *voxel, const v3s32 *pos)
46 return v3s32_cmp(&voxel->pos, pos);
49 static void render_loaded_voxel(LoadedVoxel *voxel, LoadedRenderArgs *args)
51 v3f32 pos = v3s32_to_f32(voxel->pos);
52 for (int f = 0; f < 6; f++) {
53 v3s32 neigh_pos = v3s32_add(voxel->pos, face_dir[f]);
54 if (tree_get(&args->voxels, &neigh_pos, &cmp_loaded_voxel, NULL))
57 for (int v = 0; v < 6; v++)
58 array_apd(&args->vertices, &(LoadedVertex) {
59 v3f32_add(cube_vertices[f][v].position, pos),
60 cube_vertices[f][v].normal,
66 void mesh_load(Mesh *mesh, const char *path)
68 mesh->layout = &loaded_layout;
69 mesh->vao = mesh->vbo = 0;
72 mesh->free_data = true;
74 LoadedRenderArgs args;
75 tree_ini(&args.voxels);
76 array_ini(&args.vertices, sizeof(LoadedVertex), 500);
78 FILE *file = fopen(path, "r");
80 fprintf(stderr, "[warning] failed to open mesh %s\n", path);
89 while ((length = getline(&line, &siz, file)) > 0) {
95 LoadedVoxel *voxel = malloc(sizeof *voxel);
98 if (sscanf(line, "%d %d %d %2x%2x%2x",
99 &voxel->pos.x, &voxel->pos.z, &voxel->pos.y,
100 &color.x, &color.y, &color.z) != 6) {
101 fprintf(stderr, "[warning] syntax error in mesh %s in line %d: %s\n",
107 voxel->color = (v3f32) {
108 (f32) color.x / 0xFF,
109 (f32) color.y / 0xFF,
110 (f32) color.z / 0xFF,
113 if (!tree_add(&args.voxels, &voxel->pos, voxel, &cmp_loaded_voxel, NULL)) {
114 fprintf(stderr, "[warning] more than one voxel at position (%d, %d, %d) in mesh %s in line %d\n",
115 voxel->pos.x, voxel->pos.y, voxel->pos.z, path, count);
125 tree_trv(&args.voxels, &render_loaded_voxel, &args, NULL, 0);
126 tree_clr(&args.voxels, &free, NULL, NULL, 0);
128 mesh->data = args.vertices.ptr;
129 mesh->count = args.vertices.siz;
132 // upload data to GPU (only done once)
133 void mesh_upload(Mesh *mesh)
135 glGenVertexArrays(1, &mesh->vao); GL_DEBUG
136 glGenBuffers(1, &mesh->vbo); GL_DEBUG
138 glBindVertexArray(mesh->vao); GL_DEBUG
139 glBindBuffer(GL_ARRAY_BUFFER, mesh->vbo); GL_DEBUG
141 glBufferData(GL_ARRAY_BUFFER, mesh->count * mesh->layout->size,
142 mesh->data, GL_STATIC_DRAW); GL_DEBUG
145 for (GLuint i = 0; i < mesh->layout->count; i++) {
146 VertexAttribute *attrib = &mesh->layout->attributes[i];
148 glVertexAttribPointer(i, attrib->length, attrib->type, GL_FALSE,
149 mesh->layout->size, (GLvoid *) offset); GL_DEBUG
150 glEnableVertexAttribArray(i); GL_DEBUG
152 offset += attrib->size;
155 glBindBuffer(GL_ARRAY_BUFFER, 0); GL_DEBUG
156 glBindVertexArray(0); GL_DEBUG
164 void mesh_render(Mesh *mesh)
170 glBindVertexArray(mesh->vao); GL_DEBUG
171 glDrawArrays(GL_TRIANGLES, 0, mesh->count); GL_DEBUG
174 void mesh_destroy(Mesh *mesh)
176 if (mesh->data && mesh->free_data)
180 glDeleteVertexArrays(1, &mesh->vao); GL_DEBUG
184 glDeleteBuffers(1, &mesh->vbo); GL_DEBUG
187 mesh->vao = mesh->vbo = 0;