diff --git a/game/shader.py b/game/shader.py index 33af448..76068ac 100644 --- a/game/shader.py +++ b/game/shader.py @@ -19,29 +19,40 @@ from engine import load_shader #TODO: preprocessing (at least includes) +def _cleanup(line): + return line.partition('//')[0].strip() + +def _filter(line): + return line + +def _subst(line): + if line.startswith('#include '): + path = Path('.') / 'game' / 'shaders' + lines = [] + for name in line.split()[1:]: + lines.extend(_load_source(path / name.strip('"'))) + return lines + return [line] + +def _load_source(_path): + assert _path.exists() + lines = filter(_filter, map(_cleanup, open(_path, 'rt'))) + source = [] + for line in lines: + source.extend(_subst(line)) + return source + +def _convert(line): + return bytes(line, 'utf-8') + b'\n' + def load(vert_name, frag_name = ''): path = Path('.') / 'game' / 'shaders' if not frag_name: frag_name = vert_name - def _cleanup(_line): - return _line.partition('//')[0].strip() - def _filter(_line): - return _line - def _convert(_line): - return bytes(_line, 'utf-8') + b'\n' - def load_source(_path): - if not _path.exists(): - print("Cannot find", _path) - return None - return list(map(_convert, filter(_filter, map(_cleanup, open(_path, 'rt'))))) print("Loading vertex shader", vert_name) - vert_lines = load_source(path / (vert_name + '_opengles.vert')) - if not vert_lines: - print("Error: Vertex shader is empty.") - return None + vert_lines = list(map(_convert, _load_source(path / (vert_name + '_opengles.vert')))) + assert vert_lines print("Loading fragment shader", frag_name) - frag_lines = load_source(path / (frag_name + '_opengles.frag')) - if not frag_lines: - print("Error: Fragment shader is empty.") - return None + frag_lines = list(map(_convert, _load_source(path / (frag_name + '_opengles.frag')))) + assert frag_lines return load_shader(vert_lines, frag_lines) diff --git a/game/shaders/common_opengles.frag b/game/shaders/common_opengles.frag index 6036b1d..03d22e7 100644 --- a/game/shaders/common_opengles.frag +++ b/game/shaders/common_opengles.frag @@ -16,6 +16,8 @@ #version 320 es precision highp float; +#include "include/environment.glsl" + in vec4 v_position; // view space in vec4 v_normal; // view space in vec4 v_terrain_normal; // view space (x, y, z, weight) @@ -23,9 +25,6 @@ in vec4 v_texcoord; // texture space (s, t, pixel_level, material_level) #define v_weight v_terrain_normal.w -uniform vec3 u_light_direction; // view space (-direction_x, -direction_y, -direction_z) -uniform vec3 u_light_color; // (color.r * power, color.g * power, color.b * power) - layout(binding=0) uniform highp sampler2DArray u_texture_sampler; layout(location = 0) out vec4 o_color; diff --git a/game/shaders/include/camera.glsl b/game/shaders/include/camera.glsl new file mode 100644 index 0000000..e43b267 --- /dev/null +++ b/game/shaders/include/camera.glsl @@ -0,0 +1,7 @@ +#ifndef CAMERA_INCLUDED +#define CAMERA_INCLUDED + +uniform mat4 u_view; // world space -> view space +uniform mat4 u_projection; // view space -> screen space + +#endif // CAMERA_INCLUDED diff --git a/game/shaders/include/common.glsl b/game/shaders/include/common.glsl new file mode 100644 index 0000000..9db998c --- /dev/null +++ b/game/shaders/include/common.glsl @@ -0,0 +1,20 @@ +#ifndef COMMON_INCLUDED +#define COMMON_INCLUDED + +layout(binding=1) uniform highp sampler2D u_height_sampler; +layout(binding=2) uniform highp sampler2D u_normal_sampler; + +const vec3 c_normal_scale = vec3(2.0, 2.0, 1.0); +const vec3 c_normal_shift = vec3(-1.0, -1.0, 0.0); +const vec2 c_st_scale = vec2(1.0 / 1023.0, 1.0 / 1023.0); +const vec2 c_terrain_scale = vec2(1.0 / 2048.0, -1.0 / 2048.0); +const vec2 c_terrain_shift = vec2(0.5, 0.5); +const float c_weight_scale = 1.0 / 64.f; +const vec3 c_world_forward = vec3(0.0, 1.0, 0.0); + +out vec4 v_position; // view space +out vec4 v_normal; // view space +out vec4 v_terrain_normal; // view space (x, y, z, weigth) +out vec4 v_texcoord; // texture space (s, t, pixel_level, material_level) + +#endif // COMMON_INCLUDED diff --git a/game/shaders/include/environment.glsl b/game/shaders/include/environment.glsl new file mode 100644 index 0000000..73bd117 --- /dev/null +++ b/game/shaders/include/environment.glsl @@ -0,0 +1,10 @@ +#ifndef ENVIRONMENT_INCLUDED +#define ENVIRONMENT_INCLUDED + +uniform vec3 u_light_direction; // view space +uniform vec3 u_light_color; +uniform vec3 u_horizon_color; +uniform vec3 u_sky_color; +uniform vec3 u_sun_color; + +#endif // ENVIRONMENT_INCLUDED diff --git a/game/shaders/include/vertex_format.glsl b/game/shaders/include/vertex_format.glsl new file mode 100644 index 0000000..1531941 --- /dev/null +++ b/game/shaders/include/vertex_format.glsl @@ -0,0 +1,8 @@ +#ifndef VERTEX_FORMAT_INCLUDED +#define VERTEX_FORMAT_INCLUDED + +layout(location = 0) in vec3 a_position; // model space +layout(location = 1) in vec3 a_normal; // model space +layout(location = 2) in vec3 a_texcoord; // texture space + +#endif // VERTEX_FORMAT_INCLUDED diff --git a/game/shaders/sky_opengles.frag b/game/shaders/sky_opengles.frag index 8648e21..1bd343e 100644 --- a/game/shaders/sky_opengles.frag +++ b/game/shaders/sky_opengles.frag @@ -16,14 +16,11 @@ #version 320 es precision highp float; +#include "include/camera.glsl" +#include "include/environment.glsl" + in vec3 v_position; -uniform mat4 u_view; // world space -> view space, unit = km -uniform vec3 u_light_direction; // view space (-direction_x, -direction_y, -direction_z) -uniform vec3 u_light_color; -uniform vec3 u_horizon_color; -uniform vec3 u_sky_color; -uniform vec3 u_sun_color; uniform float u_sea_phase; #define u_right u_view[0].xyz diff --git a/game/shaders/sky_opengles.vert b/game/shaders/sky_opengles.vert index f92d185..f996f9a 100644 --- a/game/shaders/sky_opengles.vert +++ b/game/shaders/sky_opengles.vert @@ -16,9 +16,9 @@ #version 320 es precision highp float; -layout(location = 0) in vec3 a_position; // view space +#include "include/camera.glsl" -uniform mat4 u_projection; // view space -> screen space +layout(location = 0) in vec3 a_position; // view space out vec3 v_position; // view space diff --git a/game/shaders/terrain_opengles.vert b/game/shaders/terrain_opengles.vert index 6d0b17b..d6825f2 100644 --- a/game/shaders/terrain_opengles.vert +++ b/game/shaders/terrain_opengles.vert @@ -16,31 +16,12 @@ #version 320 es precision highp float; -layout(location = 0) in vec3 a_position; // model space -layout(location = 1) in vec3 a_normal; // model space -layout(location = 2) in vec3 a_texcoord; // texture space +#include "include/vertex_format.glsl" +#include "include/camera.glsl" +#include "include/common.glsl" layout(location = 3) in vec3 i_translation; // per mesh, model space -> world space -uniform mat4 u_view; // world space -> view space -uniform mat4 u_projection; // view space -> screen space - -layout(binding=1) uniform highp sampler2D u_height_sampler; -layout(binding=2) uniform highp sampler2D u_normal_sampler; - -const vec3 c_normal_scale = vec3(2.0, 2.0, 1.0); -const vec3 c_normal_shift = vec3(-1.0, -1.0, 0.0); -const vec2 c_st_scale = vec2(1.0 / 1023.0, 1.0 / 1023.0); -const vec2 c_terrain_scale = vec2(1.0 / 2048.0, -1.0 / 2048.0); -const vec2 c_terrain_shift = vec2(0.5, 0.5); -const float c_weight_scale = 1.0 / 64.f; -const vec3 c_world_forward = vec3(0.0, 1.0, 0.0); - -out vec4 v_position; // view space -out vec4 v_normal; // view space -out vec4 v_terrain_normal; // view space (x, y, z, weigth) -out vec4 v_texcoord; // texture space (s, t, pixel_level, material_level) - void main(void) { vec4 world_position = vec4(i_translation + a_position, 1.0); vec2 terrain_coords = c_terrain_shift + world_position.xy * c_terrain_scale; diff --git a/game/shaders/tests_opengles.vert b/game/shaders/tests_opengles.vert index 998e2a5..ba3f938 100644 --- a/game/shaders/tests_opengles.vert +++ b/game/shaders/tests_opengles.vert @@ -16,32 +16,13 @@ #version 320 es precision highp float; -layout(location = 0) in vec3 a_position; // model space -layout(location = 1) in vec3 a_normal; // model space -layout(location = 2) in vec3 a_texcoord; // texture space +#include "include/vertex_format.glsl" +#include "include/camera.glsl" +#include "include/common.glsl" layout(location = 3) in vec3 i_translation; // per mesh, model space -> world space layout(location = 4) in mat3 i_orientation; // per mesh, model space -> world space -uniform mat4 u_view; // world space -> view space -uniform mat4 u_projection; // view space -> screen space - -layout(binding=1) uniform highp sampler2D u_height_sampler; -layout(binding=2) uniform highp sampler2D u_normal_sampler; - -const vec3 c_normal_scale = vec3(2.0, 2.0, 1.0); -const vec3 c_normal_shift = vec3(-1.0, -1.0, 0.0); -const vec2 c_st_scale = vec2(1.0 / 1023.0, 1.0 / 1023.0); -const vec2 c_terrain_scale = vec2(1.0 / 2048.0, -1.0 / 2048.0); -const vec2 c_terrain_shift = vec2(0.5, 0.5); -const float c_weight_scale = 1.0 / 64.f; -const vec3 c_world_forward = vec3(0.0, 1.0, 0.0); - -out vec4 v_position; // view space -out vec4 v_normal; // view space -out vec4 v_terrain_normal; // view space (x, y, z, weigth) -out vec4 v_texcoord; // texture space (s, t, pixel_level, material_level) - void main(void) { vec4 world_position = vec4(i_translation + i_orientation * a_position, 1.0); float weight = max(0.0, 1.0 - world_position.z * c_weight_scale);