Skip to content
50 changes: 34 additions & 16 deletions whippersnappy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def init_window(width, height, title="PyOpenGL", visible=True):
return window


def setup_shader(meshdata, triangles, width, height, specular=True):
def setup_shader(meshdata, triangles, width, height, specular=True, ambient=0.0):
"""
Create vertex and fragment shaders.

Expand All @@ -488,6 +488,8 @@ def setup_shader(meshdata, triangles, width, height, specular=True):
Window height (to set perspective projection).
specular : Boolean
By default specular is set as True.
ambient : float
Ambient light strength, by default 0: use only diffuse light sources.

Returns
-------
Expand Down Expand Up @@ -532,46 +534,50 @@ def setup_shader(meshdata, triangles, width, height, specular=True):

out vec4 FragColor;

uniform vec3 lightColor;
uniform bool doSpecular;
uniform vec3 lightColor = vec3(1.0, 1.0, 1.0);
uniform bool doSpecular = true;
uniform float ambientStrength = 0.0;

void main()
{
// ambient
float ambientStrength = 0.0;
vec3 ambient = ambientStrength * lightColor;

// diffuse
vec3 norm = normalize(Normal);
// values for overhead, front, below, back lights
//vec4 diffweights = vec4(0.4, 0.6, 0.4, 0.4); //more light below
vec4 diffweights = vec4(0.6, 0.4, 0.4, 0.3); //orig more shadow

// key light (overhead)
vec3 lightPos1 = vec3(0.0,5.0,5.0);
vec3 lightDir = normalize(lightPos1 - FragPos);
float diff = max(dot(norm, lightDir), 0.0);
float key = 0.6;
vec3 diffuse = key * diff * lightColor;
vec3 diffuse = diffweights[0] * diff * lightColor;

// headlight (at camera)
vec3 lightPos2 = vec3(0.0,0.0,5.0);
lightDir = normalize(lightPos2 - FragPos);
vec3 ohlightDir = lightDir;
vec3 ohlightDir = lightDir; // needed for specular
diff = max(dot(norm, lightDir), 0.0);
diffuse = diffuse + 0.68 * key * diff * lightColor;
diffuse = diffuse + diffweights[1] * diff * lightColor;

// fill light (from below)
vec3 lightPos3 = vec3(0.0,-5.0,5.0);
lightDir = normalize(lightPos3 - FragPos);
diff = max(dot(norm, lightDir), 0.0);
diffuse = diffuse + 0.6 * key * diff * lightColor;
diffuse = diffuse + diffweights[2] * diff * lightColor;

// left right back lights
// left right back lights (both are same brightness)
vec3 lightPos4 = vec3(5.0,0.0,-5.0);
lightDir = normalize(lightPos4 - FragPos);
diff = max(dot(norm, lightDir), 0.0);
diffuse = diffuse + 0.52 * key * diff * lightColor;
diffuse = diffuse + diffweights[3] * diff * lightColor;

vec3 lightPos5 = vec3(-5.0,0.0,-5.0);
lightDir = normalize(lightPos5 - FragPos);
diff = max(dot(norm, lightDir), 0.0);
diffuse = diffuse + 0.52 * key * diff * lightColor;
diffuse = diffuse + diffweights[3] * diff * lightColor;

// specular
vec3 result;
Expand Down Expand Up @@ -669,6 +675,10 @@ def setup_shader(meshdata, triangles, width, height, specular=True):
lightColor_loc = gl.glGetUniformLocation(shader, "lightColor")
gl.glUniform3f(lightColor_loc, 1.0, 1.0, 1.0)

# setup ambient light strength (default=0)
ambientLight_loc = gl.glGetUniformLocation(shader, "ambientStrength")
gl.glUniform1f(ambientLight_loc, ambient)

return shader


Expand Down Expand Up @@ -985,6 +995,7 @@ def snap1(
font_file=None,
specular=True,
brain_scale=1,
ambient=0.0,
):
"""
Snap one view (view and hemisphere is determined by the user).
Expand Down Expand Up @@ -1048,6 +1059,8 @@ def snap1(
Specular is by default set as True.
brain_scale : float
Brain scaling factor. Default: 1.
ambient : float
Ambient light, default 0, only use diffuse light sources.

Returns
-------
Expand All @@ -1073,11 +1086,11 @@ def snap1(
screen_height = mode.size.height
if WWIDTH > screen_width:
print(
"[INFO] Requested width exceeds screen width, expect black bars"
f"[INFO] Requested width {WWIDTH} exceeds screen width {screen_width}, expect black bars"
)
elif WHEIGHT > screen_height:
print(
"[INFO] Requested height exceeds screen height, expect black bars"
f"[INFO] Requested height {WHEIGHT} exceeds screen height {screen_height}, expect black bars"
)

# Create the base image
Expand Down Expand Up @@ -1133,7 +1146,8 @@ def snap1(
sys.exit(1)

# Upload to GPU and compile shaders
shader = setup_shader(meshdata, triangles, brain_display_width, brain_display_height, specular=specular)
shader = setup_shader(meshdata, triangles, brain_display_width, brain_display_height,
specular=specular, ambient=ambient)

# Draw
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
Expand Down Expand Up @@ -1276,6 +1290,7 @@ def snap4(
outpath=None,
font_file=None,
specular=True,
ambient=0.0,
):
"""
Snap four views (front and back for left and right hemispheres).
Expand Down Expand Up @@ -1316,6 +1331,8 @@ def snap4(
Path to the file describing the font to be used in captions.
specular : bool
Specular is by default set as True.
ambient : float
Ambient light, default 0, only use diffuse light sources.

Returns
-------
Expand Down Expand Up @@ -1392,7 +1409,8 @@ def snap4(
sys.exit(1)

# upload to GPU and compile shaders
shader = setup_shader(meshdata, triangles, wwidth, wheight, specular=specular)
shader = setup_shader(meshdata, triangles, wwidth, wheight,
specular=specular, ambient=ambient)

# draw
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
Expand Down
Loading