Use logarithmic Z

This commit is contained in:
Nikita Lisitsa 2020-11-08 23:39:43 +03:00
parent 63da1987be
commit 34cdba80a1

View file

@ -40,7 +40,6 @@
// TODO: fix frustum culling // TODO: fix frustum culling
// TODO: fix seams at tile borders // TODO: fix seams at tile borders
// TODO: try a different coordinate system for closer tiles // TODO: try a different coordinate system for closer tiles
// TODO: use closer near plane, maybe try reversed-Z
// TODO: add space, stars, the sun // TODO: add space, stars, the sun
// TODO: add atmospheric glow // TODO: add atmospheric glow
@ -439,6 +438,8 @@ uniform vec3 u_p2;
uniform sampler1D u_colormap; uniform sampler1D u_colormap;
uniform sampler1D u_colormap_neg; uniform sampler1D u_colormap_neg;
uniform float u_far;
layout (location = 0) in float in_height; layout (location = 0) in float in_height;
out vec3 color; out vec3 color;
@ -456,6 +457,8 @@ void main()
vec3 p = normalize(u_p0 * t0 + u_p1 * t1 + u_p2 * t2) * (1.0 + in_height / 6400000.0); vec3 p = normalize(u_p0 * t0 + u_p1 * t1 + u_p2 * t2) * (1.0 + in_height / 6400000.0);
pos = p; pos = p;
gl_Position = u_transform * vec4(p, 1.0); gl_Position = u_transform * vec4(p, 1.0);
float C = 1.0;
gl_Position.z = (2.0 * log(C * gl_Position.w + 1.0) / log(C * u_far + 1.0) - 1.0) * gl_Position.w;
color = (in_height > 0.0) ? texture(u_colormap, in_height / 8000.0).rgb : texture(u_colormap_neg, -in_height / 10000.0).rgb; color = (in_height > 0.0) ? texture(u_colormap, in_height / 8000.0).rgb : texture(u_colormap_neg, -in_height / 10000.0).rgb;
})"; })";
@ -724,12 +727,14 @@ void srtm_app::present()
tile_close_program["u_light"] = geom::vector<float, 3>{0.f, 0.f, 1.f}; tile_close_program["u_light"] = geom::vector<float, 3>{0.f, 0.f, 1.f};
tile_close_program["u_colormap"] = 0; tile_close_program["u_colormap"] = 0;
tile_close_program["u_colormap_neg"] = 1; tile_close_program["u_colormap_neg"] = 1;
tile_close_program["u_far"] = camera.far_clip;
tile_far_program.bind(); tile_far_program.bind();
tile_far_program["u_transform"] = camera_transform; tile_far_program["u_transform"] = camera_transform;
tile_far_program["u_N"] = static_cast<int>(node_size); tile_far_program["u_N"] = static_cast<int>(node_size);
tile_far_program["u_light"] = geom::vector<float, 3>{0.f, 0.f, 1.f}; tile_far_program["u_light"] = geom::vector<float, 3>{0.f, 0.f, 1.f};
tile_far_program["u_colormap"] = 0; tile_far_program["u_colormap"] = 0;
tile_far_program["u_colormap_neg"] = 1; tile_far_program["u_colormap_neg"] = 1;
tile_far_program["u_far"] = camera.far_clip;
gl::ActiveTexture(gl::TEXTURE0); gl::ActiveTexture(gl::TEXTURE0);
color_map.bind(); color_map.bind();
gl::ActiveTexture(gl::TEXTURE1); gl::ActiveTexture(gl::TEXTURE1);