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.

186 lines
7.0 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-2019 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. // Global Variables Definition
  65. //----------------------------------------------------------------------------------
  66. int lightsCount = 0; // Current amount of created lights
  67. //----------------------------------------------------------------------------------
  68. // Module Functions Declaration
  69. //----------------------------------------------------------------------------------
  70. Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader); // Create a light and get shader locations
  71. void UpdateLightValues(Shader shader, Light light); // Send light properties to shader
  72. //void InitLightLocations(Shader shader, Light *light); // Init light shader locations
  73. #ifdef __cplusplus
  74. }
  75. #endif
  76. #endif // RLIGHTS_H
  77. /***********************************************************************************
  78. *
  79. * RLIGHTS IMPLEMENTATION
  80. *
  81. ************************************************************************************/
  82. #if defined(RLIGHTS_IMPLEMENTATION)
  83. #include "raylib.h"
  84. //----------------------------------------------------------------------------------
  85. // Defines and Macros
  86. //----------------------------------------------------------------------------------
  87. // ...
  88. //----------------------------------------------------------------------------------
  89. // Types and Structures Definition
  90. //----------------------------------------------------------------------------------
  91. // ...
  92. //----------------------------------------------------------------------------------
  93. // Global Variables Definition
  94. //----------------------------------------------------------------------------------
  95. // ...
  96. //----------------------------------------------------------------------------------
  97. // Module specific Functions Declaration
  98. //----------------------------------------------------------------------------------
  99. // ...
  100. //----------------------------------------------------------------------------------
  101. // Module Functions Definition
  102. //----------------------------------------------------------------------------------
  103. // Create a light and get shader locations
  104. Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader)
  105. {
  106. Light light = { 0 };
  107. if (lightsCount < MAX_LIGHTS)
  108. {
  109. light.enabled = true;
  110. light.type = type;
  111. light.position = position;
  112. light.target = target;
  113. light.color = color;
  114. // TODO: Below code doesn't look good to me,
  115. // it assumes a specific shader naming and structure
  116. // Probably this implementation could be improved
  117. char enabledName[32] = "lights[x].enabled\0";
  118. char typeName[32] = "lights[x].type\0";
  119. char posName[32] = "lights[x].position\0";
  120. char targetName[32] = "lights[x].target\0";
  121. char colorName[32] = "lights[x].color\0";
  122. enabledName[7] = '0' + lightsCount;
  123. typeName[7] = '0' + lightsCount;
  124. posName[7] = '0' + lightsCount;
  125. targetName[7] = '0' + lightsCount;
  126. colorName[7] = '0' + lightsCount;
  127. light.enabledLoc = GetShaderLocation(shader, enabledName);
  128. light.typeLoc = GetShaderLocation(shader, typeName);
  129. light.posLoc = GetShaderLocation(shader, posName);
  130. light.targetLoc = GetShaderLocation(shader, targetName);
  131. light.colorLoc = GetShaderLocation(shader, colorName);
  132. UpdateLightValues(shader, light);
  133. lightsCount++;
  134. }
  135. return light;
  136. }
  137. // Send light properties to shader
  138. // NOTE: Light shader locations should be available
  139. void UpdateLightValues(Shader shader, Light light)
  140. {
  141. // Send to shader light enabled state and type
  142. SetShaderValue(shader, light.enabledLoc, &light.enabled, UNIFORM_INT);
  143. SetShaderValue(shader, light.typeLoc, &light.type, UNIFORM_INT);
  144. // Send to shader light position values
  145. float position[3] = { light.position.x, light.position.y, light.position.z };
  146. SetShaderValue(shader, light.posLoc, position, UNIFORM_VEC3);
  147. // Send to shader light target position values
  148. float target[3] = { light.target.x, light.target.y, light.target.z };
  149. SetShaderValue(shader, light.targetLoc, target, UNIFORM_VEC3);
  150. // Send to shader light color values
  151. float color[4] = { (float)light.color.r/(float)255, (float)light.color.g/(float)255,
  152. (float)light.color.b/(float)255, (float)light.color.a/(float)255 };
  153. SetShaderValue(shader, light.colorLoc, color, UNIFORM_VEC4);
  154. }
  155. #endif // RLIGHTS_IMPLEMENTATION