From 3e7760f4f84364365ef9044d2296ea744f1da825 Mon Sep 17 00:00:00 2001 From: darkrose Date: Tue, 19 Apr 2016 15:50:17 +1000 Subject: [PATCH] some meshgen and map rendering updates --- data/shaders/map_fragment.glsl | 32 ++++++++++-- data/shaders/map_vertex.glsl | 14 ++++++ inc/map.h | 1 + inc/wm.h | 2 + src/client/main.c | 6 +-- src/graphics/meshgen/meshgen_chunk.c | 89 +++++++++++++++++++++++++-------- src/graphics/meshgen/meshgen_cubelike.c | 48 +++++++++--------- src/graphics/render.c | 6 ++- src/graphics/shader.c | 5 +- src/graphics/wm.c | 2 + src/graphics/wm_w32.c | 6 +++ src/graphics/wm_x11.c | 6 +++ src/map/mapgen_terrain.c | 13 ++++- 13 files changed, 173 insertions(+), 57 deletions(-) diff --git a/data/shaders/map_fragment.glsl b/data/shaders/map_fragment.glsl index f8ea097..11f14fb 100644 --- a/data/shaders/map_fragment.glsl +++ b/data/shaders/map_fragment.glsl @@ -1,13 +1,39 @@ #version 330 core in vec2 uv; +in vec3 normal; +in vec3 to_cam; +in vec3 light0_to; -out vec4 out_Colour; +out vec4 final_colour; uniform sampler2D texture0; +vec4 light0_colour = vec4(1.0,1.0,1.0,1.0); +float light0_att = 0.0; + void main(void) { - out_Colour = texture(texture0,uv); - //out_Colour = vec4(0.0,1.0,0.0,1.0); + vec3 n = normalize(normal); + vec3 diffuse = vec3(0.0); + + float att = 1.0; + if (light0_att > 0.0) { + float dist = length(light0_to); + if (dist > light0_att) { + att = 0.0; + }else{ + att = 1.0-((1.0/light0_att)*dist); + } + } + if (att > 0.0) { + vec3 to = normalize(light0_to); + diffuse += max((max(dot(n,to),0.0)*light0_colour.rgb)*att,0.2); + }else{ + diffuse += vec3(0.2); + } + + vec4 texdata = texture(texture0,uv); + + final_colour = vec4(diffuse,1.0)*texdata; } diff --git a/data/shaders/map_vertex.glsl b/data/shaders/map_vertex.glsl index 15c9a46..fd19285 100644 --- a/data/shaders/map_vertex.glsl +++ b/data/shaders/map_vertex.glsl @@ -5,16 +5,30 @@ in vec3 normals; in vec2 texcoords; out vec2 uv; +out vec3 normal; +out vec3 to_cam; +out vec3 light0_to; uniform mat4 transformationMatrix; uniform mat4 projectionMatrix; uniform mat4 viewMatrix; +uniform vec4 plane; + +vec3 light0_pos = vec3(-5.0,500.0,500.0); + void main(void) { vec4 worldpos = transformationMatrix*vec4(position, 1.0); + gl_ClipDistance[0] = dot(worldpos,plane); + gl_Position = projectionMatrix*viewMatrix*worldpos; uv = texcoords; + normal = (transformationMatrix*vec4(normals,0.0)).xyz; + + light0_to = light0_pos - worldpos.xyz; + + to_cam = (inverse(viewMatrix)*vec4(0.0,0.0,0.0,1.0)).xyz - worldpos.xyz; } diff --git a/inc/map.h b/inc/map.h index 20598c3..48e97fc 100644 --- a/inc/map.h +++ b/inc/map.h @@ -19,6 +19,7 @@ #define CHUNK_STATE_BADMESH 0x10 /* old mesh_expired = true */ #define CHUNK_STATE_UNGENERATED 0x20 #define CHUNK_STATE_BADSPAWN 0x40 +#define CHUNK_STATE_EMPTY 0x80 /* contains only air/vacuum - a speed up for meshgen */ #define MAPOCT_MINUS_XYZ 0 #define MAPOCT_MINUS_XY_PLUS_Z 1 diff --git a/inc/wm.h b/inc/wm.h index c0e70c5..6e98eca 100644 --- a/inc/wm.h +++ b/inc/wm.h @@ -106,6 +106,8 @@ typedef struct wm_s { int fullscreen; int frame_cap; + int lfps[4]; + int lfps_i; int fps; char* title; GLfloat distance; diff --git a/src/client/main.c b/src/client/main.c index f102bbf..5f04fe1 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -104,8 +104,8 @@ int main(int argc, char** argv) c = camera_get(); c->x = 0.0; c->y = 20.6; - c->z = 60.0; - c->pitch = 40.0; + c->z = 0.0; + c->pitch = 20.0; @@ -117,7 +117,7 @@ int main(int argc, char** argv) if (state_client == VLSTATE_PLAY) { textbuffer_clear(&tb); c = camera_get(); - sprintf(buff,"FPS: %d (%f,%f,%f) %f %f",wm_data.fps,c->x,c->y,c->z,c->yaw,c->pitch); + sprintf(buff,"FPS: %d (%.1f, %.1f, %.1f) %.1f %.1f",wm_data.fps,c->x,c->y,c->z,c->yaw,c->pitch); textbuffer_addstr(&tb,buff); render2d_textbuffer(&tb); } diff --git a/src/graphics/meshgen/meshgen_chunk.c b/src/graphics/meshgen/meshgen_chunk.c index 1d5a8df..a782410 100644 --- a/src/graphics/meshgen/meshgen_chunk.c +++ b/src/graphics/meshgen/meshgen_chunk.c @@ -60,6 +60,20 @@ static void meshgen_chunk(mapobj_t *o) if (!b) return; + if (ch->mesh.obj) { + int i; + int k; + mesh_t *m; + + for (i=0; i<4; i++) { + for (k=0; kobjs[i].meshes->length; k++) { + m = ((mesh_t**)o->objs[i].meshes->data)[k]; + if (m->vao.state == 1) + m->vao.state = 2; + } + } + } + ch->mesh.obj = o; render_map_chunk(o); @@ -94,6 +108,7 @@ static void *meshgen_thread(void *data) return NULL; } +/* initialise meshgen */ int meshgen_init() { meshgen_data.state = VLSTATE_PLAY; @@ -103,6 +118,7 @@ int meshgen_init() return 0; } +/* deinit meshgen, and wait for the thread to exit */ void meshgen_exit() { meshgen_data.state = VLSTATE_EXIT; @@ -113,6 +129,7 @@ void meshgen_exit() meshgen_data.thread = NULL; } +/* add a chunk to the list for generating a mesh, called by UPDATE and LOAD map triggers */ void meshgen_add_chunk(chunk_t *ch, pos_t *p) { mapobj_t *o; @@ -121,42 +138,70 @@ void meshgen_add_chunk(chunk_t *ch, pos_t *p) if (meshgen_data.state != VLSTATE_PLAY) return; - /* TODO: see if it's already in the list */ - - if (ch->mesh.obj) { - /* TODO: decide what to do in this case */ + if ((ch->state&CHUNK_STATE_EMPTY) == CHUNK_STATE_EMPTY) return; + + mutex_lock(meshgen_data.mutex); + o = meshgen_data.objs; + while (o) { + if (o->pos.x == p->x && o->pos.y == p->y && o->pos.z == p->z) { + mutex_unlock(meshgen_data.mutex); + return; + } + o = o->next; } + mutex_unlock(meshgen_data.mutex); - o = malloc(sizeof(mapobj_t)); - if (!o) { - vlprintf(CN_WARN,"allocation failure in meshgen_add_chunk (%d,%d,%d)",p->x,p->y,p->z); + if (ch->mesh.obj) { + int k; + mesh_t *m; + + /* TODO: the below will (may) segfault, so don't */ return; - } - for (i=0; i<4; i++) { - o->objs[i].meshes = NULL; - } + o = ch->mesh.obj; - o->chunk = ch; - o->pos.x = p->x; - o->pos.y = p->y; - o->pos.z = p->z; + for (i=0; i<4; i++) { + for (k=0; kobjs[i].meshes->length; k++) { + m = ((mesh_t**)o->objs[i].meshes->data)[k]; + m->v->length = 0; + m->n->length = 0; + m->t->length = 0; + m->i->length = 0; + } + } + }else{ + o = malloc(sizeof(mapobj_t)); + if (!o) { + vlprintf(CN_WARN,"allocation failure in meshgen_add_chunk (%d,%d,%d)",p->x,p->y,p->z); + return; + } + + for (i=0; i<4; i++) { + o->objs[i].meshes = NULL; + } - array_init(&o->blockobjs,ARRAY_TYPE_PTR); + o->chunk = ch; + o->pos.x = p->x; + o->pos.y = p->y; + o->pos.z = p->z; - o->bounds.min.x = o->pos.x; - o->bounds.min.y = o->pos.y; - o->bounds.min.z = o->pos.z; - o->bounds.max.x = o->pos.x+16.0; - o->bounds.max.y = o->pos.y+16.0; - o->bounds.max.z = o->pos.z+16.0; + array_init(&o->blockobjs,ARRAY_TYPE_PTR); + + o->bounds.min.x = o->pos.x; + o->bounds.min.y = o->pos.y; + o->bounds.min.z = o->pos.z; + o->bounds.max.x = o->pos.x+16.0; + o->bounds.max.y = o->pos.y+16.0; + o->bounds.max.z = o->pos.z+16.0; + } mutex_lock(meshgen_data.mutex); meshgen_data.objs = list_push(&meshgen_data.objs,o); mutex_unlock(meshgen_data.mutex); } +/* free a mesh's chunk, called by UNLOAD map trigger */ void meshgen_free_chunk(chunk_t *ch, pos_t *p) { } diff --git a/src/graphics/meshgen/meshgen_cubelike.c b/src/graphics/meshgen/meshgen_cubelike.c index 93f4698..f122fae 100644 --- a/src/graphics/meshgen/meshgen_cubelike.c +++ b/src/graphics/meshgen/meshgen_cubelike.c @@ -42,7 +42,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.y = p->y+0.5; v.z = p->z+0.5; - n.x = 0.0; + n.x = -1.0; n.y = 0.0; n.z = 0.0; @@ -57,7 +57,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.y = p->y+0.5; v.z = p->z-0.5; - n.x = 0.0; + n.x = -1.0; n.y = 0.0; n.z = 0.0; @@ -72,7 +72,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.y = p->y-0.5; v.z = p->z-0.5; - n.x = 0.0; + n.x = -1.0; n.y = 0.0; n.z = 0.0; @@ -87,7 +87,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.y = p->y-0.5; v.z = p->z+0.5; - n.x = 0.0; + n.x = -1.0; n.y = 0.0; n.z = 0.0; @@ -115,7 +115,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.y = p->y-0.5; v.z = p->z+0.5; - n.x = 0.0; + n.x = 1.0; n.y = 0.0; n.z = 0.0; @@ -130,7 +130,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.y = p->y-0.5; v.z = p->z-0.5; - n.x = 0.0; + n.x = 1.0; n.y = 0.0; n.z = 0.0; @@ -145,7 +145,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.y = p->y+0.5; v.z = p->z-0.5; - n.x = 0.0; + n.x = 1.0; n.y = 0.0; n.z = 0.0; @@ -160,7 +160,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.y = p->y+0.5; v.z = p->z+0.5; - n.x = 0.0; + n.x = 1.0; n.y = 0.0; n.z = 0.0; @@ -189,7 +189,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.z = p->z+0.5; n.x = 0.0; - n.y = 0.0; + n.y = -1.0; n.z = 0.0; t.x = 0.0; @@ -204,7 +204,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.z = p->z+0.5; n.x = 0.0; - n.y = 0.0; + n.y = -1.0; n.z = 0.0; t.x = 1.0; @@ -219,7 +219,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.z = p->z-0.5; n.x = 0.0; - n.y = 0.0; + n.y = -1.0; n.z = 0.0; t.x = 1.0; @@ -234,7 +234,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.z = p->z-0.5; n.x = 0.0; - n.y = 0.0; + n.y = -1.0; n.z = 0.0; t.x = 0.0; @@ -262,7 +262,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.z = p->z-0.5; n.x = 0.0; - n.y = 0.0; + n.y = 1.0; n.z = 0.0; t.x = 1.0; @@ -277,7 +277,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.z = p->z-0.5; n.x = 0.0; - n.y = 0.0; + n.y = 1.0; n.z = 0.0; t.x = 0.0; @@ -292,7 +292,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.z = p->z+0.5; n.x = 0.0; - n.y = 0.0; + n.y = 1.0; n.z = 0.0; t.x = 0.0; @@ -307,7 +307,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u v.z = p->z+0.5; n.x = 0.0; - n.y = 0.0; + n.y = 1.0; n.z = 0.0; t.x = 1.0; @@ -336,7 +336,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u n.x = 0.0; n.y = 0.0; - n.z = 0.0; + n.z = -1.0; t.x = 0.0; t.y = 0.0; @@ -351,7 +351,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u n.x = 0.0; n.y = 0.0; - n.z = 0.0; + n.z = -1.0; t.x = 1.0; t.y = 0.0; @@ -366,7 +366,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u n.x = 0.0; n.y = 0.0; - n.z = 0.0; + n.z = -1.0; t.x = 1.0; t.y = 1.0; @@ -381,7 +381,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u n.x = 0.0; n.y = 0.0; - n.z = 0.0; + n.z = -1.0; t.x = 0.0; t.y = 1.0; @@ -409,7 +409,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u n.x = 0.0; n.y = 0.0; - n.z = 0.0; + n.z = 1.0; t.x = 0.0; t.y = 0.0; @@ -424,7 +424,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u n.x = 0.0; n.y = 0.0; - n.z = 0.0; + n.z = 1.0; t.x = 1.0; t.y = 0.0; @@ -439,7 +439,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u n.x = 0.0; n.y = 0.0; - n.z = 0.0; + n.z = 1.0; t.x = 1.0; t.y = 1.0; @@ -454,7 +454,7 @@ static void meshgen_cubelike_lod1(mapobj_t *o, block_t *b, v3_t *p, pos_t *bp, u n.x = 0.0; n.y = 0.0; - n.z = 0.0; + n.z = 1.0; t.x = 0.0; t.y = 1.0; diff --git a/src/graphics/render.c b/src/graphics/render.c index 2e76dee..0372625 100644 --- a/src/graphics/render.c +++ b/src/graphics/render.c @@ -131,7 +131,11 @@ void render_post() /* calculate a scale factor for smooth animations */ render_data.tticks = interval_delay(render_data.ticks,wm_data.frame_cap); - wm_data.fps = calc_fps(render_data.ticks,render_data.tticks); + wm_data.lfps_i++; + if (wm_data.lfps_i > 3) + wm_data.lfps_i = 0; + wm_data.lfps[wm_data.lfps_i] = calc_fps(render_data.ticks,render_data.tticks); + wm_data.fps = (wm_data.lfps[0]+wm_data.lfps[1]+wm_data.lfps[2]+wm_data.lfps[3])/4; render_data.dtime = time_dtime(render_data.ticks); render_data.ticks = render_data.tticks; } diff --git a/src/graphics/shader.c b/src/graphics/shader.c index 62b295a..b8b9708 100644 --- a/src/graphics/shader.c +++ b/src/graphics/shader.c @@ -287,10 +287,9 @@ int shader_uniform_matrix(shader_t *s, char* name, matrix_t *value) return 1; var = glGetUniformLocation(s->program,name); - if (var < 0) { - vlprintf(CN_INFO,"no uniform found for '%s' in '%s'",name,s->name); + if (var < 0) return 1; - } + glUniformMatrix4fv(var,1,GL_FALSE,value->data); return 0; diff --git a/src/graphics/wm.c b/src/graphics/wm.c index 413d9ad..cd8628f 100644 --- a/src/graphics/wm.c +++ b/src/graphics/wm.c @@ -33,6 +33,8 @@ wm_t wm_data = { }, 0, 30, + {30,30,30,30}, + 0, 30, NULL, 1000.0, diff --git a/src/graphics/wm_w32.c b/src/graphics/wm_w32.c index 6c91f32..bac43d3 100644 --- a/src/graphics/wm_w32.c +++ b/src/graphics/wm_w32.c @@ -28,6 +28,12 @@ int wm_init() { wm_data.isinit = 1; + wm_data.lfps_i = 0; + wm_data.lfps[0] = 0; + wm_data.lfps[1] = 0; + wm_data.lfps[2] = 0; + wm_data.lfps[3] = 0; + return wm_create(); } diff --git a/src/graphics/wm_x11.c b/src/graphics/wm_x11.c index 7cf82a1..b5940d8 100644 --- a/src/graphics/wm_x11.c +++ b/src/graphics/wm_x11.c @@ -101,6 +101,12 @@ int wm_init() wm_data.fb_cfg = NULL; wm_data.cursor.mat = NULL; + wm_data.lfps_i = 0; + wm_data.lfps[0] = 0; + wm_data.lfps[1] = 0; + wm_data.lfps[2] = 0; + wm_data.lfps[3] = 0; + if (XSetLocaleModifiers("") == NULL) return 1; diff --git a/src/map/mapgen_terrain.c b/src/map/mapgen_terrain.c index b759f56..cfb4cea 100644 --- a/src/map/mapgen_terrain.c +++ b/src/map/mapgen_terrain.c @@ -33,6 +33,8 @@ typedef struct mg_offsets_s { } mg_offsets_t; static block_t terrain_blocks[32][288][32]; +static int terrain_heights[32][32]; +static int terrain_genheights[32][32]; static mg_offsets_t terrain_offsets[72] = { {16,0,16, 0,-80,0}, {0,0,16, -16,-80,0}, @@ -115,6 +117,7 @@ void mapgen_terrain(pos_t *p) int y; int z; int i; + int u; block_t *b; block_t *cb; pos_t cp; @@ -124,8 +127,9 @@ void mapgen_terrain(pos_t *p) for (x=0; x<32; x++) { for (z=0; z<32; z++) { - /* TODO: get the real height here */ h = noise_height(p->x+x,p->z+z); + terrain_heights[x][z] = h; + terrain_genheights[x][z] = h; h += 80; for (y=0; y<288; y++) { b = &terrain_blocks[x][y][z]; @@ -150,6 +154,7 @@ void mapgen_terrain(pos_t *p) cp.y = p->y+terrain_offsets[i].cy; cp.z = p->z+terrain_offsets[i].cz; ch = mapgen_create_chunk(&cp); + u = 0; for (x=0; x<16; x++) { for (y=0; y<16; y++) { for (z=0; z<16; z++) { @@ -159,11 +164,17 @@ void mapgen_terrain(pos_t *p) cb->param1 = b->param1; cb->param2 = b->param2; cb->param3 = b->param3; + if (cb->content != CONTENT_AIR) + u++; } } } ch->state &= ~CHUNK_STATE_UNGENERATED; + if (!u) + ch->state |= CHUNK_STATE_EMPTY; + if (u == 4096) + ch->state |= CHUNK_STATE_UNDERGROUND; map_add_chunk(ch); } -- 2.11.4.GIT