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.

148 lines
6.5 KiB

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