shaders_basic_lighting.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * This example has been created using raylib 3.8 (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 (@codifies, http://bedroomcoders.co.uk/) and
  14. * reviewed by Ramon Santamaria (@raysan5)
  15. *
  16. * Copyright (c) 2019-2021 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
  17. *
  18. ********************************************************************************************/
  19. #include "raylib.h"
  20. #include "raymath.h"
  21. #define RLIGHTS_IMPLEMENTATION
  22. #include "rlights.h"
  23. #if defined(PLATFORM_DESKTOP)
  24. #define GLSL_VERSION 330
  25. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  26. #define GLSL_VERSION 100
  27. #endif
  28. int main(void)
  29. {
  30. // Initialization
  31. //--------------------------------------------------------------------------------------
  32. const int screenWidth = 800;
  33. const int screenHeight = 450;
  34. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  35. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic lighting");
  36. // Define the camera to look into our 3d world
  37. Camera camera = { 0 };
  38. camera.position = (Vector3){ 2.0f, 4.0f, 6.0f }; // Camera position
  39. camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point
  40. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  41. camera.fovy = 45.0f; // Camera field-of-view Y
  42. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  43. // Load plane model from a generated mesh
  44. Model model = LoadModelFromMesh(GenMeshPlane(10.0f, 10.0f, 3, 3));
  45. Model cube = LoadModelFromMesh(GenMeshCube(2.0f, 4.0f, 2.0f));
  46. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/base_lighting.vs", GLSL_VERSION),
  47. TextFormat("resources/shaders/glsl%i/lighting.fs", GLSL_VERSION));
  48. // Get some required shader loactions
  49. shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
  50. // NOTE: "matModel" location name is automatically assigned on shader loading,
  51. // no need to get the location again if using that uniform name
  52. //shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel");
  53. // Ambient light level (some basic lighting)
  54. int ambientLoc = GetShaderLocation(shader, "ambient");
  55. SetShaderValue(shader, ambientLoc, (float[4]){ 0.1f, 0.1f, 0.1f, 1.0f }, SHADER_UNIFORM_VEC4);
  56. // Assign out lighting shader to model
  57. model.materials[0].shader = shader;
  58. cube.materials[0].shader = shader;
  59. // Using 4 point lights: gold, red, green and blue
  60. Light lights[MAX_LIGHTS] = { 0 };
  61. lights[0] = CreateLight(LIGHT_POINT, (Vector3){ -2, 1, -2 }, Vector3Zero(), YELLOW, shader);
  62. lights[1] = CreateLight(LIGHT_POINT, (Vector3){ 2, 1, 2 }, Vector3Zero(), RED, shader);
  63. lights[2] = CreateLight(LIGHT_POINT, (Vector3){ -2, 1, 2 }, Vector3Zero(), GREEN, shader);
  64. lights[3] = CreateLight(LIGHT_POINT, (Vector3){ 2, 1, -2 }, Vector3Zero(), BLUE, shader);
  65. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  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); // Update camera
  74. // Check key inputs to enable/disable lights
  75. if (IsKeyPressed(KEY_Y)) { lights[0].enabled = !lights[0].enabled; }
  76. if (IsKeyPressed(KEY_R)) { lights[1].enabled = !lights[1].enabled; }
  77. if (IsKeyPressed(KEY_G)) { lights[2].enabled = !lights[2].enabled; }
  78. if (IsKeyPressed(KEY_B)) { lights[3].enabled = !lights[3].enabled; }
  79. // Update light values (actually, only enable/disable them)
  80. UpdateLightValues(shader, lights[0]);
  81. UpdateLightValues(shader, lights[1]);
  82. UpdateLightValues(shader, lights[2]);
  83. UpdateLightValues(shader, lights[3]);
  84. // Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
  85. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  86. SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
  87. //----------------------------------------------------------------------------------
  88. // Draw
  89. //----------------------------------------------------------------------------------
  90. BeginDrawing();
  91. ClearBackground(RAYWHITE);
  92. BeginMode3D(camera);
  93. DrawModel(model, Vector3Zero(), 1.0f, WHITE);
  94. DrawModel(cube, Vector3Zero(), 1.0f, WHITE);
  95. // Draw markers to show where the lights are
  96. if (lights[0].enabled) DrawSphereEx(lights[0].position, 0.2f, 8, 8, YELLOW);
  97. else DrawSphereWires(lights[0].position, 0.2f, 8, 8, ColorAlpha(YELLOW, 0.3f));
  98. if (lights[1].enabled) DrawSphereEx(lights[1].position, 0.2f, 8, 8, RED);
  99. else DrawSphereWires(lights[1].position, 0.2f, 8, 8, ColorAlpha(RED, 0.3f));
  100. if (lights[2].enabled) DrawSphereEx(lights[2].position, 0.2f, 8, 8, GREEN);
  101. else DrawSphereWires(lights[2].position, 0.2f, 8, 8, ColorAlpha(GREEN, 0.3f));
  102. if (lights[3].enabled) DrawSphereEx(lights[3].position, 0.2f, 8, 8, BLUE);
  103. else DrawSphereWires(lights[3].position, 0.2f, 8, 8, ColorAlpha(BLUE, 0.3f));
  104. DrawGrid(10, 1.0f);
  105. EndMode3D();
  106. DrawFPS(10, 10);
  107. DrawText("Use keys [Y][R][G][B] to toggle lights", 10, 40, 20, DARKGRAY);
  108. EndDrawing();
  109. //----------------------------------------------------------------------------------
  110. }
  111. // De-Initialization
  112. //--------------------------------------------------------------------------------------
  113. UnloadModel(model); // Unload the model
  114. UnloadModel(cube); // Unload the model
  115. UnloadShader(shader); // Unload shader
  116. CloseWindow(); // Close window and OpenGL context
  117. //--------------------------------------------------------------------------------------
  118. return 0;
  119. }