diff --git a/graph_dev_opengl.c b/graph_dev_opengl.c index 78bbb940..b8c1210b 100644 --- a/graph_dev_opengl.c +++ b/graph_dev_opengl.c @@ -24,6 +24,9 @@ #include "snis_typeface.h" #include "opengl_cap.h" +#define AMBIENT_LIGHT_DEFINE "#define AMBIENT 0.05\n" +#define UNIVERSAL_SHADER_DEFINES AMBIENT_LIGHT_DEFINE + #define TEX_RELOAD_DELAY 1.0 #define CUBEMAP_TEX_RELOAD_DELAY 3.0 #define MAX_LOADED_TEXTURES 40 @@ -2538,7 +2541,8 @@ static void setup_single_color_lit_shader(struct graph_dev_gl_single_color_lit_s /* Create and compile our GLSL program from the shaders */ shader->program_id = load_shaders(shader_directory, "single-color-lit-per-vertex.vert", - "single-color-lit-per-vertex.frag", NULL); + "single-color-lit-per-vertex.frag", + UNIVERSAL_SHADER_DEFINES); /* Get a handle for our "MVP" uniform */ shader->mvp_matrix_id = glGetUniformLocation(shader->program_id, "u_MVPMatrix"); @@ -2597,10 +2601,12 @@ static void setup_textured_cubemap_shader(const char *basename, struct graph_dev const char *vert_header = "#version 120\n" - "#define INCLUDE_VS 1\n"; + "#define INCLUDE_VS 1\n" + UNIVERSAL_SHADER_DEFINES; const char *frag_header = "#version 120\n" - "#define INCLUDE_FS 1\n"; + "#define INCLUDE_FS 1\n" + UNIVERSAL_SHADER_DEFINES; char shader_filename[PATH_MAX]; snprintf(shader_filename, sizeof(shader_filename), "%s.shader", basename); @@ -2636,7 +2642,8 @@ static void setup_filled_wireframe_shader(struct graph_dev_gl_filled_wireframe_s { /* Create and compile our GLSL program from the shaders */ shader->program_id = load_shaders(shader_directory, - "wireframe_filled.vert", "wireframe_filled.frag", NULL); + "wireframe_filled.vert", "wireframe_filled.frag", + UNIVERSAL_SHADER_DEFINES); shader->viewport_id = glGetUniformLocation(shader->program_id, "Viewport"); shader->mvp_matrix_id = glGetUniformLocation(shader->program_id, "ModelViewProjectionMatrix"); @@ -2659,7 +2666,8 @@ static void setup_trans_wireframe_shader(const char *basename, struct graph_dev_ snprintf(frag_filename, sizeof(frag_filename), "%s.frag", basename); /* Create and compile our GLSL program from the shaders */ - shader->program_id = load_shaders(shader_directory, vert_filename, frag_filename, NULL); + shader->program_id = load_shaders(shader_directory, vert_filename, frag_filename, + UNIVERSAL_SHADER_DEFINES); shader->mvp_matrix_id = glGetUniformLocation(shader->program_id, "u_MVPMatrix"); shader->mv_matrix_id = glGetUniformLocation(shader->program_id, "u_MVMatrix"); @@ -2675,7 +2683,8 @@ static void setup_single_color_shader(struct graph_dev_gl_single_color_shader *s { /* Create and compile our GLSL program from the shaders */ shader->program_id = load_shaders(shader_directory, - "single_color.vert", "single_color.frag", NULL); + "single_color.vert", "single_color.frag", + UNIVERSAL_SHADER_DEFINES); /* Get a handle for our "MVP" uniform */ shader->mvp_matrix_id = glGetUniformLocation(shader->program_id, "u_MVPMatrix"); @@ -2688,7 +2697,8 @@ static void setup_single_color_shader(struct graph_dev_gl_single_color_shader *s static void setup_vertex_color_shader(struct graph_dev_gl_vertex_color_shader *shader) { shader->program_id = load_shaders(shader_directory, - "per_vertex_color.vert", "per_vertex_color.frag", NULL); + "per_vertex_color.vert", "per_vertex_color.frag", + UNIVERSAL_SHADER_DEFINES); shader->mvp_matrix_id = glGetUniformLocation(shader->program_id, "u_MVPMatrix"); @@ -2700,7 +2710,8 @@ static void setup_line_single_color_shader(struct graph_dev_gl_line_single_color { /* Create and compile our GLSL program from the shaders */ shader->program_id = load_shaders(shader_directory, - "line-single-color.vert", "line-single-color.frag", NULL); + "line-single-color.vert", "line-single-color.frag", + UNIVERSAL_SHADER_DEFINES); shader->mvp_matrix_id = glGetUniformLocation(shader->program_id, "u_MVPMatrix"); shader->viewport_id = glGetUniformLocation(shader->program_id, "u_Viewport"); @@ -2722,7 +2733,8 @@ static void setup_point_cloud_shader(const char *basename, struct graph_dev_gl_p snprintf(frag_filename, sizeof(frag_filename), "%s.frag", basename); /* Create and compile our GLSL program from the shaders */ - shader->program_id = load_shaders(shader_directory, vert_filename, frag_filename, NULL); + shader->program_id = load_shaders(shader_directory, vert_filename, frag_filename, + UNIVERSAL_SHADER_DEFINES); /* Get a handle for our "MVP" uniform */ shader->mvp_matrix_id = glGetUniformLocation(shader->program_id, "u_MVPMatrix"); @@ -2737,7 +2749,8 @@ static void setup_point_cloud_shader(const char *basename, struct graph_dev_gl_p static void setup_color_by_w_shader(struct graph_dev_gl_color_by_w_shader *shader) { /* Create and compile our GLSL program from the shaders */ - shader->program_id = load_shaders(shader_directory, "color_by_w.vert", "color_by_w.frag", NULL); + shader->program_id = load_shaders(shader_directory, "color_by_w.vert", "color_by_w.frag", + UNIVERSAL_SHADER_DEFINES); /* Get a handle for our "MVP" uniform */ shader->mvp_id = glGetUniformLocation(shader->program_id, "u_MVPMatrix"); @@ -2756,7 +2769,8 @@ static void setup_color_by_w_shader(struct graph_dev_gl_color_by_w_shader *shade static void setup_skybox_shader(struct graph_dev_gl_skybox_shader *shader) { /* Create and compile our GLSL program from the shaders */ - shader->program_id = load_shaders(shader_directory, "skybox.vert", "skybox.frag", NULL); + shader->program_id = load_shaders(shader_directory, "skybox.vert", "skybox.frag", + UNIVERSAL_SHADER_DEFINES); glUseProgram(shader->program_id); /* Get a handle for our "MVP" uniform */ @@ -2858,7 +2872,8 @@ static void setup_textured_particle_shader(struct graph_dev_gl_textured_particle { /* Create and compile our GLSL program from the shaders */ shader->program_id = load_shaders(shader_directory, - "textured-particle.vert", "textured-particle.frag", NULL); + "textured-particle.vert", "textured-particle.frag", + UNIVERSAL_SHADER_DEFINES); glUseProgram(shader->program_id); shader->mvp_matrix_id = glGetUniformLocation(shader->program_id, "u_MVPMatrix"); @@ -3177,10 +3192,12 @@ int graph_dev_setup(const char *shader_dir) setup_point_cloud_shader("point_cloud-intensity-noise", &point_cloud_intensity_noise_shader); setup_color_by_w_shader(&color_by_w_shader); setup_skybox_shader(&skybox_shader); - setup_textured_shader("textured", "", &textured_shader); - setup_textured_shader("textured-with-sphere-shadow-per-pixel", "", &textured_with_sphere_shadow_shader); - setup_textured_shader("textured-and-lit-per-pixel", "", &textured_lit_shader); - setup_textured_shader("textured-and-lit-per-pixel", "#define USE_EMIT_MAP", &textured_lit_emit_shader); + setup_textured_shader("textured", UNIVERSAL_SHADER_DEFINES, &textured_shader); + setup_textured_shader("textured-with-sphere-shadow-per-pixel", UNIVERSAL_SHADER_DEFINES, + &textured_with_sphere_shadow_shader); + setup_textured_shader("textured-and-lit-per-pixel", UNIVERSAL_SHADER_DEFINES, &textured_lit_shader); + setup_textured_shader("textured-and-lit-per-pixel", UNIVERSAL_SHADER_DEFINES "#define USE_EMIT_MAP", + &textured_lit_emit_shader); setup_textured_cubemap_shader("textured-cubemap-and-lit-per-pixel", &textured_cubemap_lit_shader); setup_textured_cubemap_shader("textured-cubemap-shield-per-pixel", &textured_cubemap_shield_shader); setup_textured_cubemap_shader("textured-cubemap-and-lit-with-annulus-shadow-per-pixel", diff --git a/share/snis/shader/single-color-lit-per-pixel.frag b/share/snis/shader/single-color-lit-per-pixel.frag index 2e56dba2..43362e6b 100644 --- a/share/snis/shader/single-color-lit-per-pixel.frag +++ b/share/snis/shader/single-color-lit-per-pixel.frag @@ -6,6 +6,10 @@ varying vec3 v_Position; // Interpolated position for this fragment. varying vec3 v_Color; // This is the color from the vertex shader interpolated across the triangle per fragment varying vec3 v_Normal; // Interpolated normal for this fragment. +#if !defined(AMBIENT) +#define AMBIENT 0.1 +#endif + void main() { // Get a lighting direction vector from the light to the vertex. @@ -18,8 +22,8 @@ void main() // mimic the original snis software render lighting dot = (dot + 1.0) / 2.0; - // give 10% ambient light - float diffuse = max(dot, 0.1); + // ambient light + float diffuse = max(dot, AMBIENT); // Multiply the color by the diffuse illumination level to get final output color. gl_FragColor = vec4(v_Color * diffuse, 1.0); diff --git a/share/snis/shader/single-color-lit-per-vertex.vert b/share/snis/shader/single-color-lit-per-vertex.vert index be4e5a2e..dc30cf1b 100644 --- a/share/snis/shader/single-color-lit-per-vertex.vert +++ b/share/snis/shader/single-color-lit-per-vertex.vert @@ -12,6 +12,10 @@ attribute vec3 a_Normal; // Per-vertex normal information we will pass in. varying vec3 v_Color; // This will be passed into the fragment shader. +#if !defined(AMBIENT) +#define AMBIENT 0.1 +#endif + void main() // The entry point for our vertex shader. { // Transform the vertex into eye space. @@ -30,8 +34,8 @@ void main() // The entry point for our vertex shader. // mimic the original snis software render lighting /* dot = (dot + 1.0) / 2.0; */ - // give 10% ambient - float diffuse = max(dot, 0.1); + // ambient + float diffuse = max(dot, AMBIENT); // Multiply the color by the illumination level. It will be interpolated across the triangle. v_Color = u_Color * diffuse; diff --git a/share/snis/shader/textured-and-lit-per-pixel.shader b/share/snis/shader/textured-and-lit-per-pixel.shader index 1a101c0a..8f5ed774 100644 --- a/share/snis/shader/textured-and-lit-per-pixel.shader +++ b/share/snis/shader/textured-and-lit-per-pixel.shader @@ -1,7 +1,9 @@ #define USE_SPECULAR 1 +#if !defined(AMBIENT) #define AMBIENT 0.1 +#endif #ifdef USE_CUBEMAP #define TEX_SAMPLER samplerCube diff --git a/share/snis/shader/textured-and-lit-per-vertex.shader b/share/snis/shader/textured-and-lit-per-vertex.shader index ab13eb2a..b394f95f 100644 --- a/share/snis/shader/textured-and-lit-per-vertex.shader +++ b/share/snis/shader/textured-and-lit-per-vertex.shader @@ -12,6 +12,10 @@ varying vec2 v_TexCoord; attribute vec3 a_Normal; // Per-vertex normal information we will pass in. attribute vec2 a_TexCoord; // Per-vertex texture coord we will pass in. +#if !defined(AMBIENT) +#define AMBIENT 0.1 +#endif + void main() { // Transform the vertex into eye space. @@ -25,7 +29,7 @@ varying vec2 v_TexCoord; // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are // pointing in the same direction then it will get max illumination. - float diffuse = max(0.1, dot(normal, light_dir)); + float diffuse = max(AMBIENT, dot(normal, light_dir)); // Multiply the color by the illumination level. It will be interpolated across the triangle. v_LightColor = vec3(diffuse); diff --git a/share/snis/shader/textured-cubemap-and-lit-per-pixel.shader b/share/snis/shader/textured-cubemap-and-lit-per-pixel.shader index 127d965d..b8c97ce8 100644 --- a/share/snis/shader/textured-cubemap-and-lit-per-pixel.shader +++ b/share/snis/shader/textured-cubemap-and-lit-per-pixel.shader @@ -21,7 +21,9 @@ Jeremy Van Grinsven */ +#if !defined(AMBIENT) #define AMBIENT 0.1 +#endif varying vec3 v_Position; varying vec3 v_Normal; diff --git a/share/snis/shader/textured-cubemap-and-lit-per-vertex.shader b/share/snis/shader/textured-cubemap-and-lit-per-vertex.shader index 3009a364..2ce481e1 100644 --- a/share/snis/shader/textured-cubemap-and-lit-per-vertex.shader +++ b/share/snis/shader/textured-cubemap-and-lit-per-vertex.shader @@ -2,6 +2,10 @@ varying vec3 v_TexCoord; varying vec3 v_LightColor; +#if !defined(AMBIENT) +#define AMBIENT 0.1 +#endif + #if defined(INCLUDE_VS) uniform mat4 u_MVPMatrix; // A constant representing the combined model/view/projection matrix. uniform mat4 u_MVMatrix; // A constant representing the combined model/view matrix. @@ -24,7 +28,7 @@ varying vec3 v_LightColor; // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are // pointing in the same direction then it will get max illumination. - float diffuse = max(0.1, dot(normal, light_dir)); + float diffuse = max(AMBIENT, dot(normal, light_dir)); // Multiply the color by the illumination level. It will be interpolated across the triangle. v_LightColor = vec3(diffuse); diff --git a/share/snis/shader/textured-cubemap-and-lit-with-annulus-shadow-per-pixel.shader b/share/snis/shader/textured-cubemap-and-lit-with-annulus-shadow-per-pixel.shader index 9bf7f9f7..b34ed449 100644 --- a/share/snis/shader/textured-cubemap-and-lit-with-annulus-shadow-per-pixel.shader +++ b/share/snis/shader/textured-cubemap-and-lit-with-annulus-shadow-per-pixel.shader @@ -84,7 +84,9 @@ varying vec3 v_TexCoord; return false; } +#if !defined(AMBIENT) #define AMBIENT 0.1 +#endif void main() { diff --git a/share/snis/shader/textured-cubemap-shield-per-pixel.shader b/share/snis/shader/textured-cubemap-shield-per-pixel.shader index 97766dba..61cfb094 100644 --- a/share/snis/shader/textured-cubemap-shield-per-pixel.shader +++ b/share/snis/shader/textured-cubemap-shield-per-pixel.shader @@ -21,7 +21,9 @@ Jeremy Van Grinsven */ +#if !defined(AMBIENT) #define AMBIENT 0.1 +#endif varying vec3 v_Position; varying vec3 v_Normal;