25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
6.4 KiB

  1. /**********************************************************************************************
  2. *
  3. * raylib.lights - Some useful functions to deal with lights data
  4. *
  5. * CONFIGURATION:
  6. *
  7. * #define RLIGHTS_IMPLEMENTATION
  8. * Generates the implementation of the library into the included file.
  9. * If not defined, the library is in header only mode and can be included in other headers
  10. * or source files without problems. But only ONE file should hold the implementation.
  11. *
  12. * LICENSE: zlib/libpng
  13. *
  14. * Copyright (c) 2017-2024 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
  15. *
  16. * This software is provided "as-is", without any express or implied warranty. In no event
  17. * will the authors be held liable for any damages arising from the use of this software.
  18. *
  19. * Permission is granted to anyone to use this software for any purpose, including commercial
  20. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  21. *
  22. * 1. The origin of this software must not be misrepresented; you must not claim that you
  23. * wrote the original software. If you use this software in a product, an acknowledgment
  24. * in the product documentation would be appreciated but is not required.
  25. *
  26. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  27. * as being the original software.
  28. *
  29. * 3. This notice may not be removed or altered from any source distribution.
  30. *
  31. **********************************************************************************************/
  32. #ifndef RLIGHTS_H
  33. #define RLIGHTS_H
  34. //----------------------------------------------------------------------------------
  35. // Defines and Macros
  36. //----------------------------------------------------------------------------------
  37. #define MAX_LIGHTS 4 // Max dynamic lights supported by shader
  38. //----------------------------------------------------------------------------------
  39. // Types and Structures Definition
  40. //----------------------------------------------------------------------------------
  41. // Light data
  42. typedef struct {
  43. int type;
  44. bool enabled;
  45. Vector3 position;
  46. Vector3 target;
  47. Color color;
  48. float attenuation;
  49. // Shader locations
  50. int enabledLoc;
  51. int typeLoc;
  52. int positionLoc;
  53. int targetLoc;
  54. int colorLoc;
  55. int attenuationLoc;
  56. } Light;
  57. // Light type
  58. typedef enum {
  59. LIGHT_DIRECTIONAL = 0,
  60. LIGHT_POINT
  61. } LightType;
  62. #ifdef __cplusplus
  63. extern "C" { // Prevents name mangling of functions
  64. #endif
  65. //----------------------------------------------------------------------------------
  66. // Module Functions Declaration
  67. //----------------------------------------------------------------------------------
  68. Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader); // Create a light and get shader locations
  69. void UpdateLightValues(Shader shader, Light light); // Send light properties to shader
  70. #ifdef __cplusplus
  71. }
  72. #endif
  73. #endif // RLIGHTS_H
  74. /***********************************************************************************
  75. *
  76. * RLIGHTS IMPLEMENTATION
  77. *
  78. ************************************************************************************/
  79. #if defined(RLIGHTS_IMPLEMENTATION)
  80. #include "raylib.h"
  81. //----------------------------------------------------------------------------------
  82. // Defines and Macros
  83. //----------------------------------------------------------------------------------
  84. // ...
  85. //----------------------------------------------------------------------------------
  86. // Types and Structures Definition
  87. //----------------------------------------------------------------------------------
  88. // ...
  89. //----------------------------------------------------------------------------------
  90. // Global Variables Definition
  91. //----------------------------------------------------------------------------------
  92. static int lightsCount = 0; // Current amount of created lights
  93. //----------------------------------------------------------------------------------
  94. // Module specific Functions Declaration
  95. //----------------------------------------------------------------------------------
  96. // ...
  97. //----------------------------------------------------------------------------------
  98. // Module Functions Definition
  99. //----------------------------------------------------------------------------------
  100. // Create a light and get shader locations
  101. Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader)
  102. {
  103. Light light = { 0 };
  104. if (lightsCount < MAX_LIGHTS)
  105. {
  106. light.enabled = true;
  107. light.type = type;
  108. light.position = position;
  109. light.target = target;
  110. light.color = color;
  111. // NOTE: Lighting shader naming must be the provided ones
  112. light.enabledLoc = GetShaderLocation(shader, TextFormat("lights[%i].enabled", lightsCount));
  113. light.typeLoc = GetShaderLocation(shader, TextFormat("lights[%i].type", lightsCount));
  114. light.positionLoc = GetShaderLocation(shader, TextFormat("lights[%i].position", lightsCount));
  115. light.targetLoc = GetShaderLocation(shader, TextFormat("lights[%i].target", lightsCount));
  116. light.colorLoc = GetShaderLocation(shader, TextFormat("lights[%i].color", lightsCount));
  117. UpdateLightValues(shader, light);
  118. lightsCount++;
  119. }
  120. return light;
  121. }
  122. // Send light properties to shader
  123. // NOTE: Light shader locations should be available
  124. void UpdateLightValues(Shader shader, Light light)
  125. {
  126. // Send to shader light enabled state and type
  127. SetShaderValue(shader, light.enabledLoc, &light.enabled, SHADER_UNIFORM_INT);
  128. SetShaderValue(shader, light.typeLoc, &light.type, SHADER_UNIFORM_INT);
  129. // Send to shader light position values
  130. float position[3] = { light.position.x, light.position.y, light.position.z };
  131. SetShaderValue(shader, light.positionLoc, position, SHADER_UNIFORM_VEC3);
  132. // Send to shader light target position values
  133. float target[3] = { light.target.x, light.target.y, light.target.z };
  134. SetShaderValue(shader, light.targetLoc, target, SHADER_UNIFORM_VEC3);
  135. // Send to shader light color values
  136. float color[4] = { (float)light.color.r/(float)255, (float)light.color.g/(float)255,
  137. (float)light.color.b/(float)255, (float)light.color.a/(float)255 };
  138. SetShaderValue(shader, light.colorLoc, color, SHADER_UNIFORM_VEC4);
  139. }
  140. #endif // RLIGHTS_IMPLEMENTATION