shaders_basic_lighting.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - basic lighting
  4. *
  5. * Example complexity rating: [★★★★] 4/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 3.0, last time updated with raylib 4.2
  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 - basic lighting");
  42. // Define the camera to look into our 3d world
  43. Camera camera = { 0 };
  44. camera.position = (Vector3){ 2.0f, 4.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 basic lighting shader
  50. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/lighting.vs", GLSL_VERSION),
  51. TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
  52. // Get some required shader locations
  53. shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
  54. // NOTE: "matModel" location name is automatically assigned on shader loading,
  55. // no need to get the location again if using that uniform name
  56. //shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel");
  57. // Ambient light level (some basic lighting)
  58. int ambientLoc = GetShaderLocation(shader, "ambient");
  59. SetShaderValue(shader, ambientLoc, (float[4]){ 0.1f, 0.1f, 0.1f, 1.0f }, SHADER_UNIFORM_VEC4);
  60. // Create lights
  61. Light lights[MAX_LIGHTS] = { 0 };
  62. lights[0] = CreateLight(LIGHT_POINT, (Vector3){ -2, 1, -2 }, Vector3Zero(), YELLOW, shader);
  63. lights[1] = CreateLight(LIGHT_POINT, (Vector3){ 2, 1, 2 }, Vector3Zero(), RED, shader);
  64. lights[2] = CreateLight(LIGHT_POINT, (Vector3){ -2, 1, 2 }, Vector3Zero(), GREEN, shader);
  65. lights[3] = CreateLight(LIGHT_POINT, (Vector3){ 2, 1, -2 }, Vector3Zero(), BLUE, shader);
  66. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  67. //--------------------------------------------------------------------------------------
  68. // Main game loop
  69. while (!WindowShouldClose()) // Detect window close button or ESC key
  70. {
  71. // Update
  72. //----------------------------------------------------------------------------------
  73. UpdateCamera(&camera, CAMERA_ORBITAL);
  74. // Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
  75. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  76. SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
  77. // Check key inputs to enable/disable lights
  78. if (IsKeyPressed(KEY_Y)) { lights[0].enabled = !lights[0].enabled; }
  79. if (IsKeyPressed(KEY_R)) { lights[1].enabled = !lights[1].enabled; }
  80. if (IsKeyPressed(KEY_G)) { lights[2].enabled = !lights[2].enabled; }
  81. if (IsKeyPressed(KEY_B)) { lights[3].enabled = !lights[3].enabled; }
  82. // Update light values (actually, only enable/disable them)
  83. for (int i = 0; i < MAX_LIGHTS; i++) UpdateLightValues(shader, lights[i]);
  84. //----------------------------------------------------------------------------------
  85. // Draw
  86. //----------------------------------------------------------------------------------
  87. BeginDrawing();
  88. ClearBackground(RAYWHITE);
  89. BeginMode3D(camera);
  90. BeginShaderMode(shader);
  91. DrawPlane(Vector3Zero(), (Vector2) { 10.0, 10.0 }, WHITE);
  92. DrawCube(Vector3Zero(), 2.0, 4.0, 2.0, WHITE);
  93. EndShaderMode();
  94. // Draw spheres to show where the lights are
  95. for (int i = 0; i < MAX_LIGHTS; i++)
  96. {
  97. if (lights[i].enabled) DrawSphereEx(lights[i].position, 0.2f, 8, 8, lights[i].color);
  98. else DrawSphereWires(lights[i].position, 0.2f, 8, 8, ColorAlpha(lights[i].color, 0.3f));
  99. }
  100. DrawGrid(10, 1.0f);
  101. EndMode3D();
  102. DrawFPS(10, 10);
  103. DrawText("Use keys [Y][R][G][B] to toggle lights", 10, 40, 20, DARKGRAY);
  104. EndDrawing();
  105. //----------------------------------------------------------------------------------
  106. }
  107. // De-Initialization
  108. //--------------------------------------------------------------------------------------
  109. UnloadShader(shader); // Unload shader
  110. CloseWindow(); // Close window and OpenGL context
  111. //--------------------------------------------------------------------------------------
  112. return 0;
  113. }