Skip to content

Commit

Permalink
Rework chunk handling
Browse files Browse the repository at this point in the history
- Use 3d chunks as they come from the server
- Render the chunk from 0,0,0 and translate in shader
- Use the chunk data as it is rather than unpacked to a map
- Remove limitations that prevents blocks y < 0 to be rendered properly
  • Loading branch information
petterarvidsson committed Jul 30, 2015
1 parent 42c4b39 commit b111dad
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 417 deletions.
11 changes: 7 additions & 4 deletions shaders/block_vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ uniform mat4 matrix;
uniform vec3 camera;
uniform float fog_distance;
uniform int ortho;
uniform vec4 chunk;
uniform mat4 rotation;

attribute vec4 position;
attribute vec3 normal;
Expand All @@ -20,7 +22,8 @@ const float pi = 3.14159265;
const vec3 light_direction = normalize(vec3(-1.0, 1.0, -1.0));

void main() {
gl_Position = matrix * position;
vec4 local_position = rotation * (position + chunk);
gl_Position = matrix * local_position;
fragment_uv = uv.xy;
fragment_ao = 0.3 + (1.0 - uv.z) * 0.7;
fragment_light = uv.w;
Expand All @@ -30,10 +33,10 @@ void main() {
fog_height = 0.0;
}
else {
float camera_distance = distance(camera, vec3(position));
float camera_distance = distance(camera, vec3(local_position));
fog_factor = pow(clamp(camera_distance / fog_distance, 0.0, 1.0), 4.0);
float dy = position.y - camera.y;
float dx = distance(position.xz, camera.xz);
float dy = local_position.y - camera.y;
float dx = distance(local_position.xz, camera.xz);
fog_height = (atan(dy, dx) + pi / 2) / pi;
}
}
26 changes: 26 additions & 0 deletions src/chunk.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "chunk.h"

char chunk_get(Chunk *chunk, int x, int y, int z) {
int lx = x - chunk->p * CHUNK_SIZE;
int ly = y - chunk->k * CHUNK_SIZE;
int lz = z - chunk->q * CHUNK_SIZE;

// TODO: Looking for a block in the wrong chunk is a bit weird, but hit code does it
if(lx < CHUNK_SIZE && ly < CHUNK_SIZE && lz < CHUNK_SIZE &&
lx >= 0 && ly >= 0 && lz >= 0) {
return chunk->blocks[lx+ly*CHUNK_SIZE+lz*CHUNK_SIZE*CHUNK_SIZE];
} else {
return 0;
}
}

void chunk_set(Chunk *chunk, int x, int y, int z, int w) {
int lx = x - chunk->p * CHUNK_SIZE;
int ly = y - chunk->k * CHUNK_SIZE;
int lz = z - chunk->q * CHUNK_SIZE;

if(lx < CHUNK_SIZE && ly < CHUNK_SIZE && lz < CHUNK_SIZE &&
lx >= 0 && ly >= 0 && lz >= 0) {
chunk->blocks[lx+ly*CHUNK_SIZE+lz*CHUNK_SIZE*CHUNK_SIZE] = w;
}
}
33 changes: 33 additions & 0 deletions src/chunk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef _chunk_h_
#define _chunk_h_

#include <GLFW/glfw3.h>

#define CHUNK_SIZE 32

typedef struct {
char blocks[CHUNK_SIZE*CHUNK_SIZE*CHUNK_SIZE];
int p;
int q;
int k;
int faces;
int dirty;
GLuint buffer;
} Chunk;

char chunk_get(Chunk *chunk, int x, int y, int z);
void chunk_set(Chunk *chunk, int x, int y, int z, int w);

#define CHUNK_FOR_EACH(blocks, ex, ey, ez, ew) \
for(int ex = 0; ex < CHUNK_SIZE; ex++) { \
for(int ey = 0; ey < CHUNK_SIZE; ey++) { \
for(int ez = 0; ez < CHUNK_SIZE; ez++) { \
int ew = blocks[ex+ey*CHUNK_SIZE+ez*CHUNK_SIZE*CHUNK_SIZE];

#define END_CHUNK_FOR_EACH \
} \
} \
}


#endif
1 change: 0 additions & 1 deletion src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
#define RENDER_CHUNK_RADIUS 10
#define RENDER_SIGN_RADIUS 4
#define DELETE_CHUNK_RADIUS 14
#define CHUNK_SIZE 32
#define COMMIT_INTERVAL 5
#define MAX_BLOCK_HEIGHT 4096

Expand Down
4 changes: 4 additions & 0 deletions src/inventory.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ void render_inventory_item(Attrib *attrib, Item item, float xpos, float ypos, fl
glUniform3f(attrib->camera, 0, 0, 5);
glUniform1i(attrib->sampler, 0); // GL_TEXTURE0
glUniform1f(attrib->timer, PI*2);
glUniform4f(attrib->extra5,0.0, 0.0, 0.0, 0.0);
float matrix[16];
GLuint buffer;
set_matrix_item_offs(matrix, width, height, 0.65 * scale, xpos, ypos, sel);
Expand All @@ -68,6 +69,9 @@ void render_inventory_item(Attrib *attrib, Item item, float xpos, float ypos, fl
buffer = gen_cube_buffer(0, 0, 0, 0.5, item.id);
}
glUniformMatrix4fv(attrib->matrix, 1, GL_FALSE, matrix);
float identity[16];
mat_identity(identity);
glUniformMatrix4fv(attrib->extra6, 1, GL_FALSE, identity);
if (is_plant(item.id)) {
draw_plant(attrib, buffer);
} else {
Expand Down
22 changes: 6 additions & 16 deletions src/konstructs.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _konstructs_h_
#define _konstructs_h_

#define MAX_CHUNKS 8192
#define MAX_CHUNKS 8192 * 4
#define MAX_PENDING_CHUNKS 50
#define MAX_PLAYERS 128
#define WORKERS 4
Expand All @@ -18,29 +18,17 @@
#define WORKER_BUSY 1
#define WORKER_DONE 2

#include "map.h"
#include "tinycthread.h"
#include "util.h"
#include "matrix.h"
#include "item.h"
#include "chunk.h"

typedef struct {
Map map;
int p;
int q;
int faces;
int dirty;
int miny;
int maxy;
GLuint buffer;
} Chunk;

typedef struct {
int p;
int q;
Map *block_maps[3][3];
int miny;
int maxy;
int k;
char neighbour_blocks[3][3][3][(CHUNK_SIZE + 2)*(CHUNK_SIZE + 2)*(CHUNK_SIZE + 2)];
int faces;
GLfloat *data;
} WorkerItem;
Expand Down Expand Up @@ -92,6 +80,8 @@ typedef struct {
GLuint extra2;
GLuint extra3;
GLuint extra4;
GLuint extra5;
GLuint extra6;
} Attrib;

typedef struct {
Expand Down
Loading

0 comments on commit b111dad

Please sign in to comment.