Skip to content

Commit 08d0183

Browse files
Merge pull request #6 from Pebaz/master
Add Fog Shader Example
2 parents 95b31c4 + aa21811 commit 08d0183

File tree

5 files changed

+416
-0
lines changed

5 files changed

+416
-0
lines changed

examples/shaders/fog.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
"""
2+
Example converted to Python from:
3+
http://bedroomcoders.co.uk/raylib-fog/
4+
"""
5+
6+
from raylib.dynamic import raylib as rl, ffi
7+
from raylib.colors import *
8+
9+
rl.SetConfigFlags(rl.FLAG_MSAA_4X_HINT | rl.FLAG_WINDOW_RESIZABLE)
10+
rl.InitWindow(1280, 768, b'Fog Test')
11+
12+
camera = ffi.new('struct Camera3D *', [
13+
[2, 2, 6],
14+
[0, 5, 0],
15+
[0, 1, 0],
16+
45,
17+
rl.CAMERA_PERSPECTIVE
18+
])
19+
20+
model = rl.LoadModelFromMesh(rl.GenMeshTorus(0.4, 1, 16, 32))
21+
model2 = rl.LoadModelFromMesh(rl.GenMeshCube(1, 1, 1))
22+
model3 = rl.LoadModelFromMesh(rl.GenMeshSphere(0.5, 32, 32))
23+
24+
texture = rl.LoadTexture(b'resources/test.png')
25+
26+
model.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
27+
model2.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
28+
model3.materials[0].maps[rl.MAP_DIFFUSE].texture = texture
29+
30+
shader = rl.LoadShader(b'resources/shaders/fogLight.vs', b'resources/shaders/fogLight.fs')
31+
shader.locs[rl.LOC_MATRIX_MODEL] = rl.GetShaderLocation(shader, b'matModel')
32+
shader.locs[rl.LOC_VECTOR_VIEW] = rl.GetShaderLocation(shader, b'viewPos')
33+
34+
amb = rl.GetShaderLocation(shader, b'ambient')
35+
rl.SetShaderValue(shader, amb, ffi.new('float[]', [0.2, 0.2, 0.2, 1.0]), rl.UNIFORM_VEC4)
36+
37+
fog_color = [0.2, 0.2, 1.0, 1.0]
38+
fogC = rl.GetShaderLocation(shader, b'fogColor')
39+
rl.SetShaderValue(shader, fogC, ffi.new('float[]', fog_color), rl.UNIFORM_VEC4)
40+
41+
fogD = rl.GetShaderLocation(shader, b'FogDensity')
42+
fogDensity = 0.12
43+
rl.SetShaderValue(shader, fogD, ffi.new('float[]', [fogDensity]), rl.UNIFORM_FLOAT)
44+
45+
46+
model.materials[0].shader = shader
47+
model2.materials[0].shader = shader
48+
model3.materials[0].shader = shader
49+
50+
rl.SetCameraMode(camera[0], rl.CAMERA_ORBITAL)
51+
rl.SetTargetFPS(60)
52+
53+
while not rl.WindowShouldClose():
54+
rl.UpdateCamera(camera)
55+
56+
if rl.IsKeyDown(rl.KEY_UP):
57+
fogDensity = min(fogDensity + 0.001, 1)
58+
59+
if rl.IsKeyDown(rl.KEY_DOWN):
60+
fogDensity = max(fogDensity - 0.001, 0)
61+
62+
rl.SetShaderValue(shader, fogD, ffi.new('float[]', [fogDensity]), rl.UNIFORM_FLOAT)
63+
64+
rl.SetShaderValue(shader, shader.locs[rl.LOC_VECTOR_VIEW], ffi.new('float[]', [camera.position.x]), rl.UNIFORM_VEC3)
65+
66+
rl.BeginDrawing()
67+
68+
rl.ClearBackground([int(255 * i) for i in fog_color])
69+
70+
rl.BeginMode3D(camera[0])
71+
rl.DrawModel(model, [0] * 3, 1, WHITE)
72+
rl.DrawModel(model2, [-2.6, 0, 0], 1, WHITE)
73+
rl.DrawModel(model3, [ 2.6, 0, 0], 1, WHITE)
74+
75+
for i in range(-20, 20, 2):
76+
rl.DrawModel(model, [i, 0, 2], 1, WHITE)
77+
78+
79+
rl.DrawGizmo([1000, 1000, 1000])
80+
81+
rl.EndMode3D()
82+
83+
rl.DrawFPS(10, 10)
84+
rl.DrawText(f'Up/Down to change fog density: {fogDensity}'.encode('utf-8'), 10, 30, 20, WHITE)
85+
86+
rl.EndDrawing()
87+
88+
89+
rl.CloseWindow()
90+
rl.UnloadModel(model)
91+
rl.UnloadModel(model2)
92+
rl.UnloadModel(model3)
93+
rl.UnloadTexture(texture)
94+
rl.UnloadShader(shader)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#version 330
2+
3+
// Input vertex attributes (from vertex shader)
4+
in vec2 fragTexCoord;
5+
in vec4 fragColor;
6+
in vec3 fragPosition;
7+
in vec3 fragNormal;
8+
9+
// Input uniform values
10+
uniform sampler2D texture0;
11+
uniform vec4 colDiffuse;
12+
13+
// Output fragment color
14+
out vec4 finalColor;
15+
16+
// NOTE: Add here your custom variables
17+
18+
#define MAX_LIGHTS 4
19+
#define LIGHT_DIRECTIONAL 0
20+
#define LIGHT_POINT 1
21+
22+
struct MaterialProperty {
23+
vec3 color;
24+
int useSampler;
25+
sampler2D sampler;
26+
};
27+
28+
struct Light {
29+
int enabled;
30+
int type;
31+
vec3 position;
32+
vec3 target;
33+
vec4 color;
34+
};
35+
36+
// Input lighting values
37+
uniform Light lights[MAX_LIGHTS];
38+
uniform vec4 ambient;
39+
uniform vec3 viewPos;
40+
uniform float FogDensity;
41+
uniform vec4 fogColor;
42+
43+
void main()
44+
{
45+
// Texel color fetching from texture sampler
46+
vec4 texelColor = texture(texture0, fragTexCoord);
47+
vec3 lightDot = vec3(0.0);
48+
vec3 normal = normalize(fragNormal);
49+
vec3 viewD = normalize(viewPos - fragPosition);
50+
vec3 specular = vec3(0.0);
51+
52+
// NOTE: Implement here your fragment shader code
53+
54+
for (int i = 0; i < MAX_LIGHTS; i++)
55+
{
56+
if (lights[i].enabled == 1)
57+
{
58+
vec3 light = vec3(0.0);
59+
if (lights[i].type == LIGHT_DIRECTIONAL) {
60+
light = -normalize(lights[i].target - lights[i].position);
61+
}
62+
if (lights[i].type == LIGHT_POINT) {
63+
light = normalize(lights[i].position - fragPosition);
64+
}
65+
float NdotL = max(dot(normal, light), 0.0);
66+
lightDot += lights[i].color.rgb * NdotL;
67+
68+
float specCo = 0.0;
69+
if(NdotL > 0.0)
70+
specCo = pow(max(0.0, dot(viewD, reflect(-(light), normal))), 16);//16 =shine
71+
specular += specCo;
72+
73+
}
74+
}
75+
76+
finalColor = (texelColor * ((colDiffuse+vec4(specular,1)) * vec4(lightDot, 1.0)));
77+
finalColor += texelColor * (ambient/10.0);
78+
// gamma
79+
finalColor = pow(finalColor, vec4(1.0/2.2));
80+
81+
float dist = length(viewPos - fragPosition) ;
82+
float fogFactor = 1.0 / exp( (dist * FogDensity) * (dist * FogDensity));
83+
84+
// linear less nice
85+
//const float fogStart = 2.0;
86+
//const float fogEnd = 10.0;
87+
//float fogFactor = (fogEnd - dist)/(fogEnd - fogStart);
88+
89+
fogFactor = clamp( fogFactor, 0.0, 1.0 );
90+
finalColor = mix(fogColor, finalColor, fogFactor);
91+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#version 330
2+
3+
// Input vertex attributes
4+
in vec3 vertexPosition;
5+
in vec2 vertexTexCoord;
6+
in vec3 vertexNormal;
7+
in vec4 vertexColor;
8+
9+
// Input uniform values
10+
uniform mat4 mvp;
11+
uniform mat4 matModel;
12+
13+
// Output vertex attributes (to fragment shader)
14+
out vec2 fragTexCoord;
15+
out vec4 fragColor;
16+
out vec3 fragPosition;
17+
out vec3 fragNormal;
18+
19+
// NOTE: Add here your custom variables
20+
21+
void main()
22+
{
23+
// Send vertex attributes to fragment shader
24+
fragTexCoord = vertexTexCoord;
25+
fragColor = vertexColor;
26+
fragPosition = vec3(matModel*vec4(vertexPosition, 1.0f));
27+
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
28+
fragNormal = normalize(normalMatrix*vertexNormal);
29+
30+
// Calculate final vertex position
31+
gl_Position = mvp*vec4(vertexPosition, 1.0);
32+
}

examples/shaders/resources/test.png

55.8 KB
Loading

0 commit comments

Comments
 (0)