Skip to content

Commit

Permalink
do not hard code ambient light and allow to be overridden by shader i…
Browse files Browse the repository at this point in the history
…nclude
  • Loading branch information
smcameron committed Jul 7, 2014
1 parent b87ae07 commit 053ced4
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 22 deletions.
49 changes: 33 additions & 16 deletions graph_dev_opengl.c
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");

Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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 */
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 6 additions & 2 deletions share/snis/shader/single-color-lit-per-pixel.frag
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions share/snis/shader/single-color-lit-per-vertex.vert
Expand Up @@ -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.
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions 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
Expand Down
6 changes: 5 additions & 1 deletion share/snis/shader/textured-and-lit-per-vertex.shader
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions share/snis/shader/textured-cubemap-and-lit-per-pixel.shader
Expand Up @@ -21,7 +21,9 @@
Jeremy Van Grinsven
*/

#if !defined(AMBIENT)
#define AMBIENT 0.1
#endif

varying vec3 v_Position;
varying vec3 v_Normal;
Expand Down
6 changes: 5 additions & 1 deletion share/snis/shader/textured-cubemap-and-lit-per-vertex.shader
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
Expand Up @@ -84,7 +84,9 @@ varying vec3 v_TexCoord;
return false;
}

#if !defined(AMBIENT)
#define AMBIENT 0.1
#endif

void main()
{
Expand Down
2 changes: 2 additions & 0 deletions share/snis/shader/textured-cubemap-shield-per-pixel.shader
Expand Up @@ -21,7 +21,9 @@
Jeremy Van Grinsven
*/

#if !defined(AMBIENT)
#define AMBIENT 0.1
#endif

varying vec3 v_Position;
varying vec3 v_Normal;
Expand Down

0 comments on commit 053ced4

Please sign in to comment.