選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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