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.

182 lines
6.8 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-2020 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. Vector3 position;
  45. Vector3 target;
  46. Color color;
  47. bool enabled;
  48. // Shader locations
  49. int enabledLoc;
  50. int typeLoc;
  51. int posLoc;
  52. int targetLoc;
  53. int colorLoc;
  54. } Light;
  55. // Light type
  56. typedef enum {
  57. LIGHT_DIRECTIONAL,
  58. LIGHT_POINT
  59. } LightType;
  60. #ifdef __cplusplus
  61. extern "C" { // Prevents name mangling of functions
  62. #endif
  63. //----------------------------------------------------------------------------------
  64. // Module Functions Declaration
  65. //----------------------------------------------------------------------------------
  66. Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader); // Create a light and get shader locations
  67. void UpdateLightValues(Shader shader, Light light); // Send light properties to shader
  68. #ifdef __cplusplus
  69. }
  70. #endif
  71. #endif // RLIGHTS_H
  72. /***********************************************************************************
  73. *
  74. * RLIGHTS IMPLEMENTATION
  75. *
  76. ************************************************************************************/
  77. #if defined(RLIGHTS_IMPLEMENTATION)
  78. #include "raylib.h"
  79. //----------------------------------------------------------------------------------
  80. // Defines and Macros
  81. //----------------------------------------------------------------------------------
  82. // ...
  83. //----------------------------------------------------------------------------------
  84. // Types and Structures Definition
  85. //----------------------------------------------------------------------------------
  86. // ...
  87. //----------------------------------------------------------------------------------
  88. // Global Variables Definition
  89. //----------------------------------------------------------------------------------
  90. static int lightsCount = 0; // Current amount of created lights
  91. //----------------------------------------------------------------------------------
  92. // Module specific Functions Declaration
  93. //----------------------------------------------------------------------------------
  94. // ...
  95. //----------------------------------------------------------------------------------
  96. // Module Functions Definition
  97. //----------------------------------------------------------------------------------
  98. // Create a light and get shader locations
  99. Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader)
  100. {
  101. Light light = { 0 };
  102. if (lightsCount < MAX_LIGHTS)
  103. {
  104. light.enabled = true;
  105. light.type = type;
  106. light.position = position;
  107. light.target = target;
  108. light.color = color;
  109. // TODO: Below code doesn't look good to me,
  110. // it assumes a specific shader naming and structure
  111. // Probably this implementation could be improved
  112. char enabledName[32] = "lights[x].enabled\0";
  113. char typeName[32] = "lights[x].type\0";
  114. char posName[32] = "lights[x].position\0";
  115. char targetName[32] = "lights[x].target\0";
  116. char colorName[32] = "lights[x].color\0";
  117. // Set location name [x] depending on lights count
  118. enabledName[7] = '0' + lightsCount;
  119. typeName[7] = '0' + lightsCount;
  120. posName[7] = '0' + lightsCount;
  121. targetName[7] = '0' + lightsCount;
  122. colorName[7] = '0' + lightsCount;
  123. light.enabledLoc = GetShaderLocation(shader, enabledName);
  124. light.typeLoc = GetShaderLocation(shader, typeName);
  125. light.posLoc = GetShaderLocation(shader, posName);
  126. light.targetLoc = GetShaderLocation(shader, targetName);
  127. light.colorLoc = GetShaderLocation(shader, colorName);
  128. UpdateLightValues(shader, light);
  129. lightsCount++;
  130. }
  131. return light;
  132. }
  133. // Send light properties to shader
  134. // NOTE: Light shader locations should be available
  135. void UpdateLightValues(Shader shader, Light light)
  136. {
  137. // Send to shader light enabled state and type
  138. SetShaderValue(shader, light.enabledLoc, &light.enabled, SHADER_UNIFORM_INT);
  139. SetShaderValue(shader, light.typeLoc, &light.type, SHADER_UNIFORM_INT);
  140. // Send to shader light position values
  141. float position[3] = { light.position.x, light.position.y, light.position.z };
  142. SetShaderValue(shader, light.posLoc, position, SHADER_UNIFORM_VEC3);
  143. // Send to shader light target position values
  144. float target[3] = { light.target.x, light.target.y, light.target.z };
  145. SetShaderValue(shader, light.targetLoc, target, SHADER_UNIFORM_VEC3);
  146. // Send to shader light color values
  147. float color[4] = { (float)light.color.r/(float)255, (float)light.color.g/(float)255,
  148. (float)light.color.b/(float)255, (float)light.color.a/(float)255 };
  149. SetShaderValue(shader, light.colorLoc, color, SHADER_UNIFORM_VEC4);
  150. }
  151. #endif // RLIGHTS_IMPLEMENTATION