Skip to content
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
65 changes: 65 additions & 0 deletions examples/models/resources/shaders/glsl100/voxel_lighting.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#version 100

precision mediump float;

// Input from vertex shader
varying vec3 fragPosition;
varying vec4 fragColor;
varying vec3 fragNormal;

// Uniforms
uniform vec4 colDiffuse;
uniform vec4 ambient;
uniform vec3 viewPos;

#define MAX_LIGHTS 4
#define LIGHT_DIRECTIONAL 0
#define LIGHT_POINT 1

struct Light {
int enabled;
int type;
vec3 position;
vec3 target;
vec4 color;
};

uniform Light lights[MAX_LIGHTS];

void main()
{
vec3 lightDot = vec3(0.0);
vec3 normal = normalize(fragNormal);
vec3 viewD = normalize(viewPos - fragPosition);
vec3 specular = vec3(0.0);

for (int i = 0; i < MAX_LIGHTS; i++)
{
if (lights[i].enabled == 1)
{
vec3 light = vec3(0.0);

if (lights[i].type == LIGHT_DIRECTIONAL)
light = -normalize(lights[i].target - lights[i].position);

if (lights[i].type == LIGHT_POINT)
light = normalize(lights[i].position - fragPosition);

float NdotL = max(dot(normal, light), 0.0);
lightDot += lights[i].color.rgb * NdotL;

if (NdotL > 0.0)
{
float specCo = pow(max(0.0, dot(viewD, reflect(-light, normal))), 16.0);
specular += specCo;
}
}
}

vec4 finalColor = (fragColor * ((colDiffuse + vec4(specular, 1.0)) * vec4(lightDot, 1.0)));
finalColor += fragColor * (ambient / 10.0) * colDiffuse;

finalColor = pow(finalColor, vec4(1.0/2.2)); // gamma correction

gl_FragColor = finalColor;
}
28 changes: 28 additions & 0 deletions examples/models/resources/shaders/glsl100/voxel_lighting.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#version 100

precision mediump float;

// Input vertex attributes
attribute vec3 vertexPosition;
attribute vec3 vertexNormal;
attribute vec4 vertexColor;
// attribute vec2 vertexTexCoord;

// Input uniform values
uniform mat4 mvp;
uniform mat4 matModel;
uniform mat4 matNormal;

// Output to fragment shader
varying vec3 fragPosition;
varying vec4 fragColor;
varying vec3 fragNormal;

void main()
{
fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));
fragColor = vertexColor;
fragNormal = normalize(vec3(matNormal * vec4(vertexNormal, 1.0)));

gl_Position = mvp * vec4(vertexPosition, 1.0);
}
62 changes: 62 additions & 0 deletions examples/models/resources/shaders/glsl120/voxel_lighting.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#version 120
// Input from vertex shader
varying vec3 fragPosition;
varying vec4 fragColor;
varying vec3 fragNormal;

// Uniforms
uniform vec4 colDiffuse;
uniform vec4 ambient;
uniform vec3 viewPos;

#define MAX_LIGHTS 4
#define LIGHT_DIRECTIONAL 0
#define LIGHT_POINT 1

struct Light {
int enabled;
int type;
vec3 position;
vec3 target;
vec4 color;
};

uniform Light lights[MAX_LIGHTS];

void main()
{
vec3 lightDot = vec3(0.0);
vec3 normal = normalize(fragNormal);
vec3 viewD = normalize(viewPos - fragPosition);
vec3 specular = vec3(0.0);

for (int i = 0; i < MAX_LIGHTS; i++)
{
if (lights[i].enabled == 1)
{
vec3 light = vec3(0.0);

if (lights[i].type == LIGHT_DIRECTIONAL)
light = -normalize(lights[i].target - lights[i].position);

if (lights[i].type == LIGHT_POINT)
light = normalize(lights[i].position - fragPosition);

float NdotL = max(dot(normal, light), 0.0);
lightDot += lights[i].color.rgb * NdotL;

if (NdotL > 0.0)
{
float specCo = pow(max(0.0, dot(viewD, reflect(-light, normal))), 16.0);
specular += specCo;
}
}
}

vec4 finalColor = (fragColor * ((colDiffuse + vec4(specular, 1.0)) * vec4(lightDot, 1.0)));
finalColor += fragColor * (ambient / 10.0) * colDiffuse;

finalColor = pow(finalColor, vec4(1.0/2.2)); // gamma correction

gl_FragColor = finalColor;
}
24 changes: 24 additions & 0 deletions examples/models/resources/shaders/glsl120/voxel_lighting.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#version 120
// Input vertex attributes
attribute vec3 vertexPosition;
attribute vec3 vertexNormal;
attribute vec4 vertexColor;

// Uniforms
uniform mat4 mvp;
uniform mat4 matModel;
uniform mat4 matNormal;

// Output to fragment shader
varying vec3 fragPosition;
varying vec4 fragColor;
varying vec3 fragNormal;

void main()
{
fragPosition = vec3(matModel * vec4(vertexPosition, 1.0));
fragColor = vertexColor;
fragNormal = normalize(vec3(matNormal * vec4(vertexNormal, 1.0)));

gl_Position = mvp * vec4(vertexPosition, 1.0);
}
Loading