Skip to content
This repository was archived by the owner on Dec 18, 2021. It is now read-only.

Commit d24f79a

Browse files
committed
Add rlights, Add fog example
1 parent 74cfae4 commit d24f79a

9 files changed

Lines changed: 723 additions & 5 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 vec3 fragPosition;
15+
out vec2 fragTexCoord;
16+
out vec4 fragColor;
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+
fragPosition = vec3(matModel*vec4(vertexPosition, 1.0));
25+
fragTexCoord = vertexTexCoord;
26+
fragColor = vertexColor;
27+
28+
mat3 normalMatrix = transpose(inverse(mat3(matModel)));
29+
fragNormal = normalize(normalMatrix*vertexNormal);
30+
31+
// Calculate final vertex position
32+
gl_Position = mvp*vec4(vertexPosition, 1.0);
33+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
42+
void main()
43+
{
44+
// Texel color fetching from texture sampler
45+
vec4 texelColor = texture(texture0, fragTexCoord);
46+
vec3 lightDot = vec3(0.0);
47+
vec3 normal = normalize(fragNormal);
48+
vec3 viewD = normalize(viewPos - fragPosition);
49+
vec3 specular = vec3(0.0);
50+
51+
// NOTE: Implement here your fragment shader code
52+
53+
for (int i = 0; i < MAX_LIGHTS; i++)
54+
{
55+
if (lights[i].enabled == 1)
56+
{
57+
vec3 light = vec3(0.0);
58+
59+
if (lights[i].type == LIGHT_DIRECTIONAL) light = -normalize(lights[i].target - lights[i].position);
60+
if (lights[i].type == LIGHT_POINT) light = normalize(lights[i].position - fragPosition);
61+
62+
float NdotL = max(dot(normal, light), 0.0);
63+
lightDot += lights[i].color.rgb*NdotL;
64+
65+
float specCo = 0.0;
66+
if (NdotL > 0.0) specCo = pow(max(0.0, dot(viewD, reflect(-(light), normal))), 16.0); // Shine: 16.0
67+
specular += specCo;
68+
}
69+
}
70+
71+
finalColor = (texelColor*((colDiffuse + vec4(specular,1))*vec4(lightDot, 1.0)));
72+
finalColor += texelColor*(ambient/10.0);
73+
74+
// Gamma correction
75+
finalColor = pow(finalColor, vec4(1.0/2.2));
76+
77+
// Fog calculation
78+
float dist = length(viewPos - fragPosition);
79+
80+
// these could be parameters...
81+
const vec4 fogColor = vec4(0.5, 0.5, 0.5, 1.0);
82+
//const float fogDensity = 0.16;
83+
84+
// Exponential fog
85+
float fogFactor = 1.0/exp((dist*fogDensity)*(dist*fogDensity));
86+
87+
// Linear fog (less nice)
88+
//const float fogStart = 2.0;
89+
//const float fogEnd = 10.0;
90+
//float fogFactor = (fogEnd - dist)/(fogEnd - fogStart);
91+
92+
fogFactor = clamp(fogFactor, 0.0, 1.0);
93+
94+
finalColor = mix(fogColor, finalColor, fogFactor);
95+
}
55.8 KB
Loading

examples/shaders/shaders_fog.php

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
include __DIR__ . '/../utils.php';
4+
5+
// Initialization
6+
//--------------------------------------------------------------------------------------
7+
$screenWidth = 800;
8+
$screenHeight = 450;
9+
10+
SetConfigFlags(RL_FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
11+
InitWindow($screenWidth, $screenHeight, "raylib [shaders] example - fog");
12+
13+
// Define the camera to look into our 3d world
14+
$camera = Camera3D(
15+
Vector3( 2.0, 2.0, 6.0 ), // position
16+
Vector3( 0.0, 0.5, 0.0 ), // target
17+
Vector3( 0.0, 1.0, 0.0 ), // up
18+
45.0, RL_CAMERA_PERSPECTIVE ); // fov, type
19+
20+
// Load models and texture
21+
$modelA = LoadModelFromMesh(GenMeshTorus(0.4, 1.0, 16, 32));
22+
$modelB = LoadModelFromMesh(GenMeshCube(1.0, 1.0, 1.0));
23+
$modelC = LoadModelFromMesh(GenMeshSphere(0.5, 32, 32));
24+
$texture = LoadTexture(__DIR__ . "/resources/texel_checker.png");
25+
26+
// Assign texture to default model material
27+
SetModelMaterialTexture($modelA, 0, RL_MAP_ALBEDO, $texture);
28+
SetModelMaterialTexture($modelB, 0, RL_MAP_ALBEDO, $texture);
29+
SetModelMaterialTexture($modelC, 0, RL_MAP_ALBEDO, $texture);
30+
31+
// Load shader and set up some uniforms
32+
$shader = LoadShader(__DIR__ . sprintf("/resources/shaders/glsl%d/base_lighting.vs", 330),
33+
__DIR__ . sprintf("/resources/shaders/glsl%d/fog.fs", 330));
34+
35+
SetShaderLoc($shader, RL_LOC_MATRIX_MODEL, GetShaderLocation($shader, "matModel"));
36+
SetShaderLoc($shader, RL_LOC_VECTOR_VIEW, GetShaderLocation($shader, "viewPos"));
37+
38+
// Ambient light level
39+
$ambientLoc = GetShaderLocation($shader, "ambient");
40+
SetShaderValue($shader, $ambientLoc, pack('f4', 0.2, 0.2, 0.2, 1.0), RL_UNIFORM_VEC4);
41+
42+
$fogDensity = 0.15;
43+
$fogDensityLoc = GetShaderLocation($shader, "fogDensity");
44+
SetShaderValue($shader, $fogDensityLoc, pack('f1', $fogDensity), RL_UNIFORM_FLOAT);
45+
46+
// NOTE: All models share the same shader
47+
SetModelMaterialShader($modelA, 0, $shader);
48+
SetModelMaterialShader($modelB, 0, $shader);
49+
SetModelMaterialShader($modelC, 0, $shader);
50+
51+
// Using just 1 point lights
52+
CreateLight(RL_LIGHT_POINT, Vector3(0, 2, 6), Vector3Zero(), WHITE(), $shader);
53+
54+
SetCameraMode($camera, RL_CAMERA_ORBITAL); // Set an orbital camera mode
55+
56+
//SetTargetFPS(60); // Set our game to run at 60 frames-per-second
57+
//--------------------------------------------------------------------------------------
58+
59+
// Main game loop
60+
while (!WindowShouldClose()) // Detect window close button or ESC key
61+
{
62+
// Update
63+
//----------------------------------------------------------------------------------
64+
UpdateCamera($camera); // Update camera
65+
66+
if (IsKeyDown(RL_KEY_UP))
67+
{
68+
$fogDensity += 0.001;
69+
if ($fogDensity > 1.0) $fogDensity = 1.0;
70+
}
71+
72+
if (IsKeyDown(RL_KEY_DOWN))
73+
{
74+
$fogDensity -= 0.001;
75+
if ($fogDensity < 0.0) $fogDensity = 0.0;
76+
}
77+
78+
SetShaderValue($shader, $fogDensityLoc, pack('f1', $fogDensity), RL_UNIFORM_FLOAT);
79+
80+
// Rotate the torus
81+
$modelA->transform = MatrixMultiply($modelA->transform, MatrixRotateX(-0.025));
82+
$modelA->transform = MatrixMultiply($modelA->transform, MatrixRotateZ(0.012));
83+
84+
// Update the light shader with the camera view position
85+
$v = pack('f3', $camera->position->x, $camera->position->y, $camera->position->z);
86+
SetShaderValue($shader, $shader->locs[RL_LOC_VECTOR_VIEW], $v, RL_UNIFORM_VEC3);
87+
//----------------------------------------------------------------------------------
88+
89+
// Draw
90+
//----------------------------------------------------------------------------------
91+
BeginDrawing();
92+
93+
ClearBackground(GRAY());
94+
95+
BeginMode3D($camera);
96+
97+
// Draw the three models
98+
DrawModel($modelA, Vector3Zero(), 1.0, WHITE());
99+
DrawModel($modelB, Vector3(-2.6, 0, 0), 1.0, WHITE());
100+
DrawModel($modelC, Vector3(2.6, 0, 0), 1.0, WHITE());
101+
102+
for ($i = -20; $i < 20; $i += 2) DrawModel($modelA, Vector3($i, 0, 2), 1.0, WHITE());
103+
104+
EndMode3D();
105+
106+
DrawText(sprintf("Use KEY_UP/KEY_DOWN to change fog density [%.2f]", $fogDensity), 10, 10, 20, RAYWHITE());
107+
108+
DrawFPS(10, 30);
109+
EndDrawing();
110+
//----------------------------------------------------------------------------------
111+
}
112+
113+
// De-Initialization
114+
//--------------------------------------------------------------------------------------
115+
UnloadModel($modelA); // Unload the model A
116+
UnloadModel($modelB); // Unload the model B
117+
UnloadModel($modelC); // Unload the model C
118+
UnloadTexture($texture); // Unload the texture
119+
UnloadShader($shader); // Unload shader
120+
121+
CloseWindow(); // Close window and OpenGL context
122+
//--------------------------------------------------------------------------------------

examples/utils.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ function SetModelMaterialShader($model, $materialIdx, $shader)
1717
$materials[0] = $material;
1818
$model->materials = $materials;
1919
}
20+
21+
function SetShaderLoc($shader, $idx, $loc)
22+
{
23+
$locs = $shader->locs;
24+
$locs[$idx] = $loc;
25+
$shader->locs = $locs;
26+
}

0 commit comments

Comments
 (0)