shaders_fog_rendering.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - fog rendering
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
  8. * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version
  9. *
  10. * NOTE: Shaders used in this example are #version 330 (OpenGL 3.3)
  11. *
  12. * Example originally created with raylib 2.5, last time updated with raylib 3.7
  13. *
  14. * Example contributed by Chris Camacho (@chriscamacho) and reviewed by Ramon Santamaria (@raysan5)
  15. *
  16. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  17. * BSD-like license that allows static linking with closed source software
  18. *
  19. * Copyright (c) 2019-2025 Chris Camacho (@chriscamacho) and Ramon Santamaria (@raysan5)
  20. *
  21. ********************************************************************************************/
  22. #include "raylib.h"
  23. #include "raymath.h"
  24. #define RLIGHTS_IMPLEMENTATION
  25. #include "rlights.h"
  26. #if defined(PLATFORM_DESKTOP)
  27. #define GLSL_VERSION 330
  28. #else // PLATFORM_ANDROID, PLATFORM_WEB
  29. #define GLSL_VERSION 100
  30. #endif
  31. //------------------------------------------------------------------------------------
  32. // Program main entry point
  33. //------------------------------------------------------------------------------------
  34. int main(void)
  35. {
  36. // Initialization
  37. //--------------------------------------------------------------------------------------
  38. const int screenWidth = 800;
  39. const int screenHeight = 450;
  40. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  41. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - fog rendering");
  42. // Define the camera to look into our 3d world
  43. Camera camera = { 0 };
  44. camera.position = (Vector3){ 2.0f, 2.0f, 6.0f }; // Camera position
  45. camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point
  46. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  47. camera.fovy = 45.0f; // Camera field-of-view Y
  48. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  49. // Load models and texture
  50. Model modelA = LoadModelFromMesh(GenMeshTorus(0.4f, 1.0f, 16, 32));
  51. Model modelB = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  52. Model modelC = LoadModelFromMesh(GenMeshSphere(0.5f, 32, 32));
  53. Texture texture = LoadTexture("resources/texel_checker.png");
  54. // Assign texture to default model material
  55. modelA.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  56. modelB.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  57. modelC.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  58. // Load shader and set up some uniforms
  59. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/lighting.vs", GLSL_VERSION),
  60. TextFormat("resources/shaders/glsl%i/fog.fs", GLSL_VERSION));
  61. shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel");
  62. shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
  63. // Ambient light level
  64. int ambientLoc = GetShaderLocation(shader, "ambient");
  65. SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4);
  66. float fogDensity = 0.15f;
  67. int fogDensityLoc = GetShaderLocation(shader, "fogDensity");
  68. SetShaderValue(shader, fogDensityLoc, &fogDensity, SHADER_UNIFORM_FLOAT);
  69. // NOTE: All models share the same shader
  70. modelA.materials[0].shader = shader;
  71. modelB.materials[0].shader = shader;
  72. modelC.materials[0].shader = shader;
  73. // Using just 1 point lights
  74. CreateLight(LIGHT_POINT, (Vector3){ 0, 2, 6 }, Vector3Zero(), WHITE, shader);
  75. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  76. //--------------------------------------------------------------------------------------
  77. // Main game loop
  78. while (!WindowShouldClose()) // Detect window close button or ESC key
  79. {
  80. // Update
  81. //----------------------------------------------------------------------------------
  82. UpdateCamera(&camera, CAMERA_ORBITAL);
  83. if (IsKeyDown(KEY_UP))
  84. {
  85. fogDensity += 0.001f;
  86. if (fogDensity > 1.0f) fogDensity = 1.0f;
  87. }
  88. if (IsKeyDown(KEY_DOWN))
  89. {
  90. fogDensity -= 0.001f;
  91. if (fogDensity < 0.0f) fogDensity = 0.0f;
  92. }
  93. SetShaderValue(shader, fogDensityLoc, &fogDensity, SHADER_UNIFORM_FLOAT);
  94. // Rotate the torus
  95. modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025f));
  96. modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012f));
  97. // Update the light shader with the camera view position
  98. SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], &camera.position.x, SHADER_UNIFORM_VEC3);
  99. //----------------------------------------------------------------------------------
  100. // Draw
  101. //----------------------------------------------------------------------------------
  102. BeginDrawing();
  103. ClearBackground(GRAY);
  104. BeginMode3D(camera);
  105. // Draw the three models
  106. DrawModel(modelA, Vector3Zero(), 1.0f, WHITE);
  107. DrawModel(modelB, (Vector3){ -2.6f, 0, 0 }, 1.0f, WHITE);
  108. DrawModel(modelC, (Vector3){ 2.6f, 0, 0 }, 1.0f, WHITE);
  109. for (int i = -20; i < 20; i += 2) DrawModel(modelA,(Vector3){ (float)i, 0, 2 }, 1.0f, WHITE);
  110. EndMode3D();
  111. DrawText(TextFormat("Use KEY_UP/KEY_DOWN to change fog density [%.2f]", fogDensity), 10, 10, 20, RAYWHITE);
  112. EndDrawing();
  113. //----------------------------------------------------------------------------------
  114. }
  115. // De-Initialization
  116. //--------------------------------------------------------------------------------------
  117. UnloadModel(modelA); // Unload the model A
  118. UnloadModel(modelB); // Unload the model B
  119. UnloadModel(modelC); // Unload the model C
  120. UnloadTexture(texture); // Unload the texture
  121. UnloadShader(shader); // Unload shader
  122. CloseWindow(); // Close window and OpenGL context
  123. //--------------------------------------------------------------------------------------
  124. return 0;
  125. }