Skip to content

Commit

Permalink
added several SHOW_* options to enabled/disable
Browse files Browse the repository at this point in the history
rendering of various things
  • Loading branch information
fogleman committed Dec 25, 2013
1 parent a2cbbf6 commit bb25446
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 47 deletions.
12 changes: 12 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@
#define USE_CACHE 1
#define DAY_LENGTH 300

// rendering options
#define SHOW_SKY_DOME 1
#define SHOW_PLANTS 1
#define SHOW_CLOUDS 1
#define SHOW_TREES 1
#define SHOW_ITEM 1
#define SHOW_CROSSHAIRS 1
#define SHOW_WIREFRAME 1
#define SHOW_INFO_TEXT 1
#define SHOW_CHAT_TEXT 1
#define SHOW_PLAYER_NAMES 1

// key bindings
#define CRAFT_KEY_QUIT 'Q'
#define CRAFT_KEY_FORWARD 'W'
Expand Down
88 changes: 57 additions & 31 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ typedef struct {
GLuint timer;
GLuint extra1;
GLuint extra2;
GLuint extra3;
} Attrib;

static GLFWwindow *window;
Expand Down Expand Up @@ -790,6 +791,7 @@ int render_chunks(Attrib *attrib, Player *player) {
glUniform1i(attrib->sampler, 0);
glUniform1i(attrib->extra1, 2);
glUniform1f(attrib->extra2, get_daylight());
glUniform1i(attrib->extra3, SHOW_SKY_DOME);
glUniform1f(attrib->timer, time_of_day());
for (int i = 0; i < chunk_count; i++) {
Chunk *chunk = chunks + i;
Expand Down Expand Up @@ -1148,6 +1150,7 @@ int main(int argc, char **argv) {
block_attrib.sampler = glGetUniformLocation(program, "sampler");
block_attrib.extra1 = glGetUniformLocation(program, "sky_sampler");
block_attrib.extra2 = glGetUniformLocation(program, "daylight");
block_attrib.extra3 = glGetUniformLocation(program, "show_sky_dome");
block_attrib.camera = glGetUniformLocation(program, "camera");
block_attrib.timer = glGetUniformLocation(program, "timer");

Expand Down Expand Up @@ -1434,50 +1437,69 @@ int main(int argc, char **argv) {
// RENDER 3-D SCENE //
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
render_sky(&sky_attrib, player, sky_buffer);
glClear(GL_DEPTH_BUFFER_BIT);
if (SHOW_SKY_DOME) {
render_sky(&sky_attrib, player, sky_buffer);
glClear(GL_DEPTH_BUFFER_BIT);

This comment has been minimized.

Copy link
@mclark4386

mclark4386 Dec 30, 2013

out of curiosity, why only clear the depth buffer if you are rendering the sky? seems like you would want that either way.

This comment has been minimized.

Copy link
@fogleman

fogleman Dec 30, 2013

Author Owner

It was already cleared on line 1439. Only need to clear it again if we did something.

}
int face_count = render_chunks(&block_attrib, player);
render_players(&block_attrib, player);
render_wireframe(&line_attrib, player);
if (SHOW_WIREFRAME) {
render_wireframe(&line_attrib, player);
}

// RENDER HUD //
glClear(GL_DEPTH_BUFFER_BIT);
render_crosshairs(&line_attrib);
render_item(&block_attrib);
if (SHOW_CROSSHAIRS) {
render_crosshairs(&line_attrib);
}
if (SHOW_ITEM) {
render_item(&block_attrib);
}

// RENDER TEXT //
char text_buffer[1024];
float ts = 12 * scale;
float tx = ts / 2;
float ty = height - ts;
int hour = time_of_day() * 24;
char am_pm = hour < 12 ? 'a' : 'p';
hour = hour % 12;
hour = hour ? hour : 12;
snprintf(
text_buffer, 1024, "(%d, %d) (%.2f, %.2f, %.2f) [%d, %d, %d] %d%cm %dfps",
chunked(x), chunked(z), x, y, z,
player_count, chunk_count, face_count * 2, hour, am_pm, fps.fps);
render_text(&text_attrib, LEFT, tx, ty, ts, text_buffer);
for (int i = 0; i < MAX_MESSAGES; i++) {
int index = (message_index + i) % MAX_MESSAGES;
if (strlen(messages[index])) {
ty -= ts * 2;
render_text(&text_attrib, LEFT, tx, ty, ts, messages[index]);
if (SHOW_INFO_TEXT) {
int hour = time_of_day() * 24;
char am_pm = hour < 12 ? 'a' : 'p';
hour = hour % 12;
hour = hour ? hour : 12;
snprintf(
text_buffer, 1024,
"(%d, %d) (%.2f, %.2f, %.2f) [%d, %d, %d] %d%cm %dfps",
chunked(x), chunked(z), x, y, z,
player_count, chunk_count,
face_count * 2, hour, am_pm, fps.fps);
render_text(&text_attrib, LEFT, tx, ty, ts, text_buffer);
ty -= ts * 2;
}
if (SHOW_CHAT_TEXT) {
for (int i = 0; i < MAX_MESSAGES; i++) {
int index = (message_index + i) % MAX_MESSAGES;
if (strlen(messages[index])) {
render_text(&text_attrib, LEFT, tx, ty, ts,
messages[index]);
ty -= ts * 2;
}
}
}
if (typing) {
ty -= ts * 2;
snprintf(text_buffer, 1024, "> %s", typing_buffer);
render_text(&text_attrib, LEFT, tx, ty, ts, text_buffer);
ty -= ts * 2;
}
if (player != me) {
render_text(&text_attrib, CENTER, width / 2, ts, ts, player->name);
}
Player *other = player_crosshair(player);
if (other) {
render_text(&text_attrib, CENTER,
width / 2, height / 2 - ts - 24, ts, other->name);
if (SHOW_PLAYER_NAMES) {
if (player != me) {
render_text(&text_attrib, CENTER, width / 2, ts, ts,
player->name);
}
Player *other = player_crosshair(player);
if (other) {
render_text(&text_attrib, CENTER,
width / 2, height / 2 - ts - 24, ts, other->name);
}
}

// RENDER PICTURE IN PICTURE //
Expand Down Expand Up @@ -1507,13 +1529,17 @@ int main(int argc, char **argv) {
ortho = 0;
fov = 65;

render_sky(&sky_attrib, player, sky_buffer);
glClear(GL_DEPTH_BUFFER_BIT);
if (SHOW_SKY_DOME) {
render_sky(&sky_attrib, player, sky_buffer);
glClear(GL_DEPTH_BUFFER_BIT);
}
render_chunks(&block_attrib, player);
render_players(&block_attrib, player);

glClear(GL_DEPTH_BUFFER_BIT);
render_text(&text_attrib, CENTER, pw / 2, ts, ts, player->name);
if (SHOW_PLAYER_NAMES) {
render_text(&text_attrib, CENTER, pw / 2, ts, ts,
player->name);
}
}

// swap buffers
Expand Down
6 changes: 5 additions & 1 deletion shaders/block_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ uniform sampler2D sampler;
uniform sampler2D sky_sampler;
uniform float timer;
uniform float daylight;
uniform bool show_sky_dome;

varying vec2 fragment_uv;
varying float fog_factor;
Expand All @@ -18,11 +19,14 @@ void main() {
discard;
}
bool cloud = color == vec3(1.0, 1.0, 1.0);
vec3 fog_color = vec3(texture2D(sky_sampler, vec2(timer, fog_height)));
vec3 light_color = vec3(daylight * 0.6);
vec3 ambient = vec3(daylight * 0.2 + 0.2);
vec3 light = ambient + light_color * (cloud ? 1.0 - diffuse : diffuse);
color = min(color * light, vec3(1.0));
vec3 fog_color = vec3(0.53, 0.81, 0.92);
if (show_sky_dome) {
fog_color = vec3(texture2D(sky_sampler, vec2(timer, fog_height)));
}
color = mix(color, fog_color, fog_factor);
gl_FragColor = vec4(color, 1.0);
}
9 changes: 6 additions & 3 deletions shaders/block_vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

uniform mat4 matrix;
uniform vec3 camera;
uniform bool show_sky_dome;

attribute vec4 position;
attribute vec3 normal;
Expand All @@ -21,7 +22,9 @@ void main() {
diffuse = max(0.0, dot(normal, light_direction));
float camera_distance = distance(camera, vec3(position));
fog_factor = pow(clamp(camera_distance / 192.0, 0.0, 1.0), 4.0);
float dy = position.y - camera.y;
float dx = distance(position.xz, camera.xz);
fog_height = 1.0 - (atan(dy, dx) + pi / 2) / pi;
if (show_sky_dome) {
float dy = position.y - camera.y;
float dx = distance(position.xz, camera.xz);
fog_height = 1.0 - (atan(dy, dx) + pi / 2) / pi;
}
}
30 changes: 18 additions & 12 deletions world.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ void create_world(Map *map, int p, int q) {
}
// TODO: w = -1 if outside of chunk
if (w == 1) {
// grass
if (simplex2(-x * 0.1, z * 0.1, 4, 0.8, 2) > 0.6) {
map_set(map, x, h, z, 17);
}
// flowers
if (simplex2(x * 0.05, -z * 0.05, 4, 0.8, 2) > 0.7) {
int w = 18 + simplex2(x * 0.1, z * 0.1, 4, 0.8, 2) * 7;
map_set(map, x, h, z, w);
if (SHOW_PLANTS) {
// grass
if (simplex2(-x * 0.1, z * 0.1, 4, 0.8, 2) > 0.6) {
map_set(map, x, h, z, 17);
}
// flowers
if (simplex2(x * 0.05, -z * 0.05, 4, 0.8, 2) > 0.7) {
int w = 18 + simplex2(x * 0.1, z * 0.1, 4, 0.8, 2) * 7;
map_set(map, x, h, z, w);
}
}
// trees
int ok = 1;
int ok = SHOW_TREES;
if (dx - 4 < 0 || dz - 4 < 0 ||
dx + 4 >= CHUNK_SIZE || dz + 4 >= CHUNK_SIZE)
{
Expand All @@ -61,9 +63,13 @@ void create_world(Map *map, int p, int q) {
}
}
// clouds
for (int y = 64; y < 72; y++) {
if (simplex3(x * 0.01, y * 0.1, z * 0.01, 8, 0.5, 2) > 0.75) {
map_set(map, x, y, z, 16);
if (SHOW_CLOUDS) {
for (int y = 64; y < 72; y++) {
if (simplex3(
x * 0.01, y * 0.1, z * 0.01, 8, 0.5, 2) > 0.75)
{
map_set(map, x, y, z, 16);
}
}
}
}
Expand Down

0 comments on commit bb25446

Please sign in to comment.