|
| 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 | +//-------------------------------------------------------------------------------------- |
0 commit comments