You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
6.4 KiB

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