| 1 | /******************************************************************************************* |
| 2 | * |
| 3 | * raylib [shaders] example - fog |
| 4 | * |
| 5 | * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, |
| 6 | * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. |
| 7 | * |
| 8 | * NOTE: Shaders used in this example are #version 330 (OpenGL 3.3). |
| 9 | * |
| 10 | * This example has been created using raylib 2.5 (www.raylib.com) |
| 11 | * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
| 12 | * |
| 13 | * Example contributed by Chris Camacho (@codifies) and reviewed by Ramon Santamaria (@raysan5) |
| 14 | * |
| 15 | * Chris Camacho (@codifies - http://bedroomcoders.co.uk/) notes: |
| 16 | * |
| 17 | * This is based on the PBR lighting example, but greatly simplified to aid learning... |
| 18 | * actually there is very little of the PBR example left! |
| 19 | * When I first looked at the bewildering complexity of the PBR example I feared |
| 20 | * I would never understand how I could do simple lighting with raylib however its |
| 21 | * a testement to the authors of raylib (including rlights.h) that the example |
| 22 | * came together fairly quickly. |
| 23 | * |
| 24 | * Copyright (c) 2019 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5) |
| 25 | * |
| 26 | ********************************************************************************************/ |
| 27 | |
| 28 | #include "raylib.h" |
| 29 | |
| 30 | #include "raymath.h" |
| 31 | |
| 32 | #define RLIGHTS_IMPLEMENTATION |
| 33 | #include "rlights.h" |
| 34 | |
| 35 | #if defined(PLATFORM_DESKTOP) |
| 36 | #define GLSL_VERSION 330 |
| 37 | #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB |
| 38 | #define GLSL_VERSION 100 |
| 39 | #endif |
| 40 | |
| 41 | int main(void) |
| 42 | { |
| 43 | // Initialization |
| 44 | //-------------------------------------------------------------------------------------- |
| 45 | const int screenWidth = 800; |
| 46 | const int screenHeight = 450; |
| 47 | |
| 48 | SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available) |
| 49 | InitWindow(screenWidth, screenHeight, "raylib [shaders] example - fog" ); |
| 50 | |
| 51 | // Define the camera to look into our 3d world |
| 52 | Camera camera = { |
| 53 | (Vector3){ 2.0f, 2.0f, 6.0f }, // position |
| 54 | (Vector3){ 0.0f, 0.5f, 0.0f }, // target |
| 55 | (Vector3){ 0.0f, 1.0f, 0.0f }, // up |
| 56 | 45.0f, CAMERA_PERSPECTIVE }; // fov, type |
| 57 | |
| 58 | // Load models and texture |
| 59 | Model modelA = LoadModelFromMesh(GenMeshTorus(0.4f, 1.0f, 16, 32)); |
| 60 | Model modelB = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f)); |
| 61 | Model modelC = LoadModelFromMesh(GenMeshSphere(0.5f, 32, 32)); |
| 62 | Texture texture = LoadTexture("resources/texel_checker.png" ); |
| 63 | |
| 64 | // Assign texture to default model material |
| 65 | modelA.materials[0].maps[MAP_DIFFUSE].texture = texture; |
| 66 | modelB.materials[0].maps[MAP_DIFFUSE].texture = texture; |
| 67 | modelC.materials[0].maps[MAP_DIFFUSE].texture = texture; |
| 68 | |
| 69 | // Load shader and set up some uniforms |
| 70 | Shader shader = LoadShader(FormatText("resources/shaders/glsl%i/base_lighting.vs" , GLSL_VERSION), |
| 71 | FormatText("resources/shaders/glsl%i/fog.fs" , GLSL_VERSION)); |
| 72 | shader.locs[LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel" ); |
| 73 | shader.locs[LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos" ); |
| 74 | |
| 75 | // Ambient light level |
| 76 | int ambientLoc = GetShaderLocation(shader, "ambient" ); |
| 77 | SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, UNIFORM_VEC4); |
| 78 | |
| 79 | float fogDensity = 0.15f; |
| 80 | int fogDensityLoc = GetShaderLocation(shader, "fogDensity" ); |
| 81 | SetShaderValue(shader, fogDensityLoc, &fogDensity, UNIFORM_FLOAT); |
| 82 | |
| 83 | // NOTE: All models share the same shader |
| 84 | modelA.materials[0].shader = shader; |
| 85 | modelB.materials[0].shader = shader; |
| 86 | modelC.materials[0].shader = shader; |
| 87 | |
| 88 | // Using just 1 point lights |
| 89 | CreateLight(LIGHT_POINT, (Vector3){ 0, 2, 6 }, Vector3Zero(), WHITE, shader); |
| 90 | |
| 91 | SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode |
| 92 | |
| 93 | SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
| 94 | //-------------------------------------------------------------------------------------- |
| 95 | |
| 96 | // Main game loop |
| 97 | while (!WindowShouldClose()) // Detect window close button or ESC key |
| 98 | { |
| 99 | // Update |
| 100 | //---------------------------------------------------------------------------------- |
| 101 | UpdateCamera(&camera); // Update camera |
| 102 | |
| 103 | if (IsKeyDown(KEY_UP)) |
| 104 | { |
| 105 | fogDensity += 0.001; |
| 106 | if (fogDensity > 1.0) fogDensity = 1.0; |
| 107 | } |
| 108 | |
| 109 | if (IsKeyDown(KEY_DOWN)) |
| 110 | { |
| 111 | fogDensity -= 0.001; |
| 112 | if (fogDensity < 0.0) fogDensity = 0.0; |
| 113 | } |
| 114 | |
| 115 | SetShaderValue(shader, fogDensityLoc, &fogDensity, UNIFORM_FLOAT); |
| 116 | |
| 117 | // Rotate the torus |
| 118 | modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025)); |
| 119 | modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012)); |
| 120 | |
| 121 | // Update the light shader with the camera view position |
| 122 | SetShaderValue(shader, shader.locs[LOC_VECTOR_VIEW], &camera.position.x, UNIFORM_VEC3); |
| 123 | //---------------------------------------------------------------------------------- |
| 124 | |
| 125 | // Draw |
| 126 | //---------------------------------------------------------------------------------- |
| 127 | BeginDrawing(); |
| 128 | |
| 129 | ClearBackground(GRAY); |
| 130 | |
| 131 | BeginMode3D(camera); |
| 132 | |
| 133 | // Draw the three models |
| 134 | DrawModel(modelA, Vector3Zero(), 1.0f, WHITE); |
| 135 | DrawModel(modelB, (Vector3){ -2.6, 0, 0 }, 1.0f, WHITE); |
| 136 | DrawModel(modelC, (Vector3){ 2.6, 0, 0 }, 1.0f, WHITE); |
| 137 | |
| 138 | for (int i = -20; i < 20; i += 2) DrawModel(modelA,(Vector3){ i, 0, 2 }, 1.0f, WHITE); |
| 139 | |
| 140 | EndMode3D(); |
| 141 | |
| 142 | DrawText(TextFormat("Use KEY_UP/KEY_DOWN to change fog density [%.2f]" , fogDensity), 10, 10, 20, RAYWHITE); |
| 143 | |
| 144 | EndDrawing(); |
| 145 | //---------------------------------------------------------------------------------- |
| 146 | } |
| 147 | |
| 148 | // De-Initialization |
| 149 | //-------------------------------------------------------------------------------------- |
| 150 | UnloadModel(modelA); // Unload the model A |
| 151 | UnloadModel(modelB); // Unload the model B |
| 152 | UnloadModel(modelC); // Unload the model C |
| 153 | UnloadTexture(texture); // Unload the texture |
| 154 | UnloadShader(shader); // Unload shader |
| 155 | |
| 156 | CloseWindow(); // Close window and OpenGL context |
| 157 | //-------------------------------------------------------------------------------------- |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |