Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better borders. #1874

Merged
merged 4 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified assets/imp_border.dds
Binary file not shown.
Binary file modified assets/prov_border.dds
Binary file not shown.
17 changes: 13 additions & 4 deletions assets/shaders/glsl/map_f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ uniform sampler2D colormap_political;
uniform sampler2D province_highlight;
uniform sampler2D stripes_texture;
uniform sampler2D province_fow;
uniform sampler2D provinces_sea_mask;
uniform usampler2D diag_border_identifier;
uniform uint subroutines_index_2;
// location 0 : offset
// location 1 : zoom
// location 2 : screen_size
uniform vec2 map_size;
uniform vec2 screen_size;
uniform float time;
uniform float gamma;
vec4 gamma_correct(vec4 colour) {
Expand All @@ -40,7 +42,7 @@ vec2 get_corrected_coords(vec2 coords) {
// The water effect
vec4 get_water_terrain()
{
vec2 prov_id = texture(provinces_texture_sampler, tex_coord).xy;
vec2 prov_id = texture(provinces_texture_sampler, gl_FragCoord.xy / screen_size).xy;

// Water effect taken from Vic2 fx/water/PixelShader_HoiWater_2_0
const float WRAP = 0.8;
Expand Down Expand Up @@ -120,9 +122,16 @@ vec4 get_water_political() {

// The terrain color from the current texture coordinate offset with one pixel in the "corner" direction
vec4 get_terrain(vec2 corner, vec2 offset) {
vec2 prov_id = texture(provinces_texture_sampler, gl_FragCoord.xy / screen_size).xy;
float index = texture(terrain_texture_sampler, floor(tex_coord * map_size + vec2(0.5, 0.5)) / map_size + 0.5 * pix * corner).r;
index = floor(index * 256);
float is_water = step(64, index);

float is_water = 0.f; //step(64, index);

if (texture(provinces_sea_mask, prov_id).x > 0.0f || (prov_id.x == 0.f && prov_id.y == 0.f)) {
is_water = 1.f;
}

vec4 colour = texture(terrainsheet_texture_sampler, vec3(offset, index));
return mix(colour, vec4(0.), is_water);
}
Expand Down Expand Up @@ -171,7 +180,7 @@ vec4 get_land_political_close() {

rounded_tex_coords.y += ((int(test >> shift) & 1) != 0) && (abs(rel_coord.x) + abs(rel_coord.y) > 0.5) ? sign(rel_coord.y) / map_size.y : 0;

vec2 prov_id = texture(provinces_texture_sampler, rounded_tex_coords).xy;
vec2 prov_id = texture(provinces_texture_sampler, gl_FragCoord.xy / screen_size).xy;

// The primary and secondary map mode province colors
vec4 prov_color = texture(province_color, vec3(prov_id, 0.));
Expand All @@ -197,7 +206,7 @@ vec4 get_land_political_close() {
vec4 get_land_political_far() {
vec4 terrain = get_terrain(vec2(0, 0), vec2(0));
float is_land = terrain.a;
vec2 prov_id = texture(provinces_texture_sampler, tex_coord).xy;
vec2 prov_id = texture(provinces_texture_sampler, gl_FragCoord.xy / screen_size).xy;

// The primary and secondary map mode province colors
vec4 prov_color = texture(province_color, vec3(prov_id, 0.));
Expand Down
10 changes: 10 additions & 0 deletions assets/shaders/glsl/map_provinces_f.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
in vec2 tex_coord;
layout(location = 0) out vec4 frag_color;

uniform sampler2D real_provinces_texture_sampler;

// Only province id is used here
void main() {
vec2 prov_id = texture(real_provinces_texture_sampler, tex_coord).xy;
frag_color = vec4(prov_id, 0.f, 1.f);
}
3 changes: 2 additions & 1 deletion assets/shaders/glsl/textured_line_b_f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ in vec2 map_coord;
out vec4 frag_color;

uniform float gamma;
uniform vec2 screen_size;

uniform sampler2D provinces_texture_sampler;
uniform sampler2D province_fow;
Expand All @@ -19,7 +20,7 @@ void main() {


vec4 out_color = texture(line_texture, vec2(o_dist, tex_coord));
vec2 prov_id = texture(provinces_texture_sampler, map_coord).xy;
vec2 prov_id = texture(provinces_texture_sampler, gl_FragCoord.xy / screen_size).xy;
out_color.rgb *= texture(province_fow, prov_id).rgb;
frag_color = gamma_correct(out_color);
}
9 changes: 9 additions & 0 deletions assets/shaders/glsl/textured_line_b_provinces_f.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
in float tex_coord;
in float o_dist;
in vec2 map_coord;
flat in vec2 frag_province_index;
layout(location = 0) out vec4 frag_color;

void main() {
frag_color = vec4(frag_province_index, 0.f, 1.f);
}
51 changes: 32 additions & 19 deletions assets/shaders/glsl/textured_line_b_v.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
layout (location = 0) in vec2 vertex_position;
layout (location = 1) in vec2 prev_point;
layout (location = 2) in vec2 next_point;
layout (location = 3) in float texture_coord;
layout (location = 4) in float distance;
layout (location = 3) in vec2 province_index;
layout (location = 4) in float texture_coord;
layout (location = 5) in float distance;

out float tex_coord;
out float o_dist;
out vec2 map_coord;
flat out vec2 frag_province_index;

uniform vec2 offset;
uniform float aspect_ratio;
Expand Down Expand Up @@ -99,33 +101,44 @@ default: break;

void main() {
vec4 central_pos = calc_gl_position(vertex_position);
vec2 bpt = central_pos.xy;
vec2 apt = calc_gl_position(prev_point).xy;
vec2 cpt = calc_gl_position(next_point).xy;
vec2 corner_shift = vec2(0.f, 0.f);
float true_width = width;

// we want to thicken the line in "perceived" coordinates, so
// transform to perceived coordinates + depth
bpt.x *= aspect_ratio;
apt.x *= aspect_ratio;
cpt.x *= aspect_ratio;
if (texture_coord < 0.01f) {
true_width = 0.00001f;
}

//if((texture_coord > 0.5f) || (texture_coord < -0.5f) ) {
vec2 bpt = central_pos.xy;
vec2 apt = calc_gl_position(prev_point).xy;
vec2 cpt = calc_gl_position(next_point).xy;

// we want to thicken the line in "perceived" coordinates, so
// transform to perceived coordinates + depth
bpt.x *= aspect_ratio;
apt.x *= aspect_ratio;
cpt.x *= aspect_ratio;

// calculate normals in perceived coordinates + depth
vec2 adir = normalize(bpt - apt);
vec2 bdir = normalize(cpt - bpt);
// calculate normals in perceived coordinates + depth
vec2 adir = normalize(bpt - apt);
vec2 bdir = normalize(cpt - bpt);

vec2 anorm = vec2(-adir.y, adir.x);
vec2 bnorm = vec2(-bdir.y, bdir.x);
vec2 corner_normal = normalize(anorm + bnorm);
vec2 anorm = vec2(-adir.y, adir.x);
vec2 bnorm = vec2(-bdir.y, bdir.x);
vec2 corner_normal = normalize(anorm + bnorm);

vec2 corner_shift = corner_normal * zoom * width / (1.0f + max(-0.5f, dot(anorm, bnorm)));
corner_shift = corner_normal * zoom * true_width / (1.0f + max(-0.5f, dot(anorm, bnorm)));

// transform result back to screen + depth coordinates
corner_shift.x /= aspect_ratio;
// transform result back to screen + depth coordinates
corner_shift.x /= aspect_ratio;
//}

gl_Position = central_pos + vec4(corner_shift.x, corner_shift.y, 0.0f, 0.0f);

// pass data to frag shader
tex_coord = texture_coord;
o_dist = distance / (2.0f * width);
map_coord = vertex_position;

frag_province_index = province_index;
}
6 changes: 4 additions & 2 deletions assets/shaders/glsl/textured_line_river_f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ out vec4 frag_color;

uniform float gamma;

uniform vec2 screen_size;

uniform sampler2D line_texture;
uniform sampler2D colormap_water;
uniform sampler2D province_fow;
Expand All @@ -17,8 +19,8 @@ vec4 gamma_correct(vec4 colour) {

void main() {
vec4 out_color = texture(colormap_water, map_coord) * texture(line_texture, vec2(tex_coord, o_dist));
vec2 prov_id = texture(provinces_texture_sampler, map_coord).xy;
if (texture(provinces_sea_mask, prov_id).x > 0) {
vec2 prov_id = texture(provinces_texture_sampler, gl_FragCoord.xy / screen_size).xy;
if (texture(provinces_sea_mask, prov_id).x > 0 || (prov_id.x == 0.f && prov_id.y == 0.f)) {
discard;
}
float is_sea = texture(provinces_sea_mask, prov_id).x * 1000.f;
Expand Down
2 changes: 1 addition & 1 deletion src/economy/economy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,7 @@ void presimulate(sys::state& state) {
#ifdef NDEBUG
uint32_t steps = 365;
#else
uint32_t steps = 20;
uint32_t steps = 2;
#endif
for(uint32_t i = 0; i < steps; i++) {
update_factory_employment(state);
Expand Down
3 changes: 3 additions & 0 deletions src/gamestate/system_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ void state::on_resize(int32_t x, int32_t y, window::window_state win_state) {
ogl::deinitialize_msaa(*this);
ogl::initialize_msaa(*this, x, y);

ogl::deinitialize_framebuffer_for_province_indices(*this);
ogl::initialize_framebuffer_for_province_indices(*this, x, y);

if(win_state != window::window_state::minimized) {
ui_state.root->base_data.size.x = int16_t(x / user_settings.ui_scale);
ui_state.root->base_data.size.y = int16_t(y / user_settings.ui_scale);
Expand Down
38 changes: 38 additions & 0 deletions src/graphics/opengl_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,42 @@ std::string_view framebuffer_error(GLenum e) {
return "???";
}

void initialize_framebuffer_for_province_indices(sys::state& state, int32_t size_x, int32_t size_y) {
// prepare textures for rendering
glGenTextures(1, &state.open_gl.province_map_rendertexture);
glBindTexture(GL_TEXTURE_2D, state.open_gl.province_map_rendertexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, size_x, size_y, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

// framebuffer
glGenFramebuffers(1, &state.open_gl.province_map_framebuffer);
state.console_log(ogl::opengl_get_error_name(glGetError()));
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, state.open_gl.province_map_framebuffer);
state.console_log(ogl::opengl_get_error_name(glGetError()));
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, state.open_gl.province_map_rendertexture, 0);
state.console_log(ogl::opengl_get_error_name(glGetError()));

// drawbuffers
GLenum DrawBuffers[1] = { GL_COLOR_ATTACHMENT0 };
glDrawBuffers(1, DrawBuffers);

auto check = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);

if(check != GL_FRAMEBUFFER_COMPLETE) {
state.console_log(ogl::framebuffer_error(check));
} else {
state.console_log("provincial framebuffer is ready");
}
}

void deinitialize_framebuffer_for_province_indices(sys::state& state) {
if(state.open_gl.province_map_rendertexture)
glDeleteTextures(1, &state.open_gl.province_map_rendertexture);
if(state.open_gl.province_map_framebuffer)
glDeleteFramebuffers(1, &state.open_gl.province_map_framebuffer);
}

void initialize_msaa(sys::state& state, int32_t size_x, int32_t size_y) {
if(state.user_settings.antialias_level == 0)
return;
Expand Down Expand Up @@ -323,6 +359,8 @@ void initialize_opengl(sys::state& state) {
load_special_icons(state);

initialize_msaa(state, window::creation_parameters().size_x, window::creation_parameters().size_y);

initialize_framebuffer_for_province_indices(state, window::creation_parameters().size_x, window::creation_parameters().size_y);
}

static const GLfloat global_square_data[] = {
Expand Down
10 changes: 10 additions & 0 deletions src/graphics/opengl_wrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ struct data {
bool legacy_mode = false;
GLuint ui_shader_program = 0;

GLuint province_map_framebuffer;
GLuint province_map_rendertexture;

GLuint ui_shader_d_rect_uniform = 0;
GLuint ui_shader_subroutines_index_uniform = 0;
GLuint ui_shader_inner_color_uniform = 0;
Expand Down Expand Up @@ -254,6 +257,7 @@ struct data {
};

void notify_user_of_fatal_opengl_error(std::string message);
std::string_view opengl_get_error_name(GLenum t);

void create_opengl_context(sys::state& state); // you shouldn't call this directly; only initialize_opengl should call it
void initialize_opengl(sys::state& state);
Expand Down Expand Up @@ -299,6 +303,9 @@ class lines {
void set_y(float* v);
void bind_buffer();
};

std::string_view framebuffer_error(GLenum e);

void render_colored_rect(sys::state const& state, float x, float y, float width, float height, float red, float green, float blue, ui::rotation r, bool flipped, bool rtl);
void render_alpha_colored_rect(sys::state const& state, float x, float y, float width, float height, float red, float green, float blue, float alpha);
void render_simple_rect(sys::state const& state, float x, float y, float width, float height, ui::rotation r, bool flipped, bool rtl);
Expand Down Expand Up @@ -343,6 +350,9 @@ void render_text_commodity_icon(
float font_size, text::font& f
);

void deinitialize_framebuffer_for_province_indices(sys::state& state);
void initialize_framebuffer_for_province_indices(sys::state& state, int32_t size_x, int32_t size_y);

bool msaa_enabled(sys::state const& state);
void initialize_msaa(sys::state& state, int32_t x, int32_t y);
void deinitialize_msaa(sys::state& state);
Expand Down
Loading
Loading