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.

160 lines
6.5 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. * 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 (@chriscamacho) and reviewed by Ramon Santamaria (@raysan5)
  14. *
  15. * Chris Camacho (@chriscamacho - 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 (@chriscamacho) and Ramon Santamaria (@raysan5)
  25. *
  26. ********************************************************************************************/
  27. #include "raylib.h"
  28. #include "raymath.h"
  29. #define RLIGHTS_IMPLEMENTATION
  30. #include "rlights.h"
  31. #if defined(PLATFORM_DESKTOP)
  32. #define GLSL_VERSION 330
  33. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  34. #define GLSL_VERSION 100
  35. #endif
  36. int main(void)
  37. {
  38. // Initialization
  39. //--------------------------------------------------------------------------------------
  40. const int screenWidth = 800;
  41. const int screenHeight = 450;
  42. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  43. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - fog");
  44. // Define the camera to look into our 3d world
  45. Camera camera = {
  46. (Vector3){ 2.0f, 2.0f, 6.0f }, // position
  47. (Vector3){ 0.0f, 0.5f, 0.0f }, // target
  48. (Vector3){ 0.0f, 1.0f, 0.0f }, // up
  49. 45.0f, CAMERA_PERSPECTIVE }; // fov, type
  50. // Load models and texture
  51. Model modelA = LoadModelFromMesh(GenMeshTorus(0.4f, 1.0f, 16, 32));
  52. Model modelB = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  53. Model modelC = LoadModelFromMesh(GenMeshSphere(0.5f, 32, 32));
  54. Texture texture = LoadTexture("resources/texel_checker.png");
  55. // Assign texture to default model material
  56. modelA.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  57. modelB.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  58. modelC.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  59. // Load shader and set up some uniforms
  60. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/base_lighting.vs", GLSL_VERSION),
  61. TextFormat("resources/shaders/glsl%i/fog.fs", GLSL_VERSION));
  62. shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel");
  63. shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
  64. // Ambient light level
  65. int ambientLoc = GetShaderLocation(shader, "ambient");
  66. SetShaderValue(shader, ambientLoc, (float[4]){ 0.2f, 0.2f, 0.2f, 1.0f }, SHADER_UNIFORM_VEC4);
  67. float fogDensity = 0.15f;
  68. int fogDensityLoc = GetShaderLocation(shader, "fogDensity");
  69. SetShaderValue(shader, fogDensityLoc, &fogDensity, SHADER_UNIFORM_FLOAT);
  70. // NOTE: All models share the same shader
  71. modelA.materials[0].shader = shader;
  72. modelB.materials[0].shader = shader;
  73. modelC.materials[0].shader = shader;
  74. // Using just 1 point lights
  75. CreateLight(LIGHT_POINT, (Vector3){ 0, 2, 6 }, Vector3Zero(), WHITE, shader);
  76. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  77. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  78. //--------------------------------------------------------------------------------------
  79. // Main game loop
  80. while (!WindowShouldClose()) // Detect window close button or ESC key
  81. {
  82. // Update
  83. //----------------------------------------------------------------------------------
  84. UpdateCamera(&camera); // Update camera
  85. if (IsKeyDown(KEY_UP))
  86. {
  87. fogDensity += 0.001;
  88. if (fogDensity > 1.0) fogDensity = 1.0;
  89. }
  90. if (IsKeyDown(KEY_DOWN))
  91. {
  92. fogDensity -= 0.001;
  93. if (fogDensity < 0.0) fogDensity = 0.0;
  94. }
  95. SetShaderValue(shader, fogDensityLoc, &fogDensity, SHADER_UNIFORM_FLOAT);
  96. // Rotate the torus
  97. modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateX(-0.025));
  98. modelA.transform = MatrixMultiply(modelA.transform, MatrixRotateZ(0.012));
  99. // Update the light shader with the camera view position
  100. SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], &camera.position.x, SHADER_UNIFORM_VEC3);
  101. //----------------------------------------------------------------------------------
  102. // Draw
  103. //----------------------------------------------------------------------------------
  104. BeginDrawing();
  105. ClearBackground(GRAY);
  106. BeginMode3D(camera);
  107. // Draw the three models
  108. DrawModel(modelA, Vector3Zero(), 1.0f, WHITE);
  109. DrawModel(modelB, (Vector3){ -2.6, 0, 0 }, 1.0f, WHITE);
  110. DrawModel(modelC, (Vector3){ 2.6, 0, 0 }, 1.0f, WHITE);
  111. for (int i = -20; i < 20; i += 2) DrawModel(modelA,(Vector3){ i, 0, 2 }, 1.0f, WHITE);
  112. EndMode3D();
  113. DrawText(TextFormat("Use KEY_UP/KEY_DOWN to change fog density [%.2f]", fogDensity), 10, 10, 20, RAYWHITE);
  114. EndDrawing();
  115. //----------------------------------------------------------------------------------
  116. }
  117. // De-Initialization
  118. //--------------------------------------------------------------------------------------
  119. UnloadModel(modelA); // Unload the model A
  120. UnloadModel(modelB); // Unload the model B
  121. UnloadModel(modelC); // Unload the model C
  122. UnloadTexture(texture); // Unload the texture
  123. UnloadShader(shader); // Unload shader
  124. CloseWindow(); // Close window and OpenGL context
  125. //--------------------------------------------------------------------------------------
  126. return 0;
  127. }