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.

176 lines
6.7 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 Victor Fisac and Ramon Santamaria
  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 lights supported by shader
  38. #define LIGHT_DISTANCE 3.5f // Light distance from world center
  39. #define LIGHT_HEIGHT 1.0f // Light height position
  40. //----------------------------------------------------------------------------------
  41. // Types and Structures Definition
  42. //----------------------------------------------------------------------------------
  43. typedef enum {
  44. LIGHT_DIRECTIONAL,
  45. LIGHT_POINT
  46. } LightType;
  47. typedef struct {
  48. bool enabled;
  49. LightType type;
  50. Vector3 position;
  51. Vector3 target;
  52. Color color;
  53. int enabledLoc;
  54. int typeLoc;
  55. int posLoc;
  56. int targetLoc;
  57. int colorLoc;
  58. } Light;
  59. #ifdef __cplusplus
  60. extern "C" { // Prevents name mangling of functions
  61. #endif
  62. //----------------------------------------------------------------------------------
  63. // Global Variables Definition
  64. //----------------------------------------------------------------------------------
  65. int lightsCount = 0; // Current amount of created lights
  66. //----------------------------------------------------------------------------------
  67. // Module Functions Declaration
  68. //----------------------------------------------------------------------------------
  69. Light CreateLight(int type, Vector3 pos, Vector3 targ, Color color, Shader shader); // Defines a light and get locations from PBR shader
  70. void UpdateLightValues(Shader shader, Light light); // Send to PBR shader light values
  71. #ifdef __cplusplus
  72. }
  73. #endif
  74. #endif // RLIGHTS_H
  75. /***********************************************************************************
  76. *
  77. * RLIGHTS IMPLEMENTATION
  78. *
  79. ************************************************************************************/
  80. #if defined(RLIGHTS_IMPLEMENTATION)
  81. #include "raylib.h"
  82. //----------------------------------------------------------------------------------
  83. // Defines and Macros
  84. //----------------------------------------------------------------------------------
  85. // ...
  86. //----------------------------------------------------------------------------------
  87. // Types and Structures Definition
  88. //----------------------------------------------------------------------------------
  89. // ...
  90. //----------------------------------------------------------------------------------
  91. // Global Variables Definition
  92. //----------------------------------------------------------------------------------
  93. // ...
  94. //----------------------------------------------------------------------------------
  95. // Module specific Functions Declaration
  96. //----------------------------------------------------------------------------------
  97. // ...
  98. //----------------------------------------------------------------------------------
  99. // Module Functions Definition
  100. //----------------------------------------------------------------------------------
  101. // Defines a light and get locations from PBR shader
  102. Light CreateLight(int type, Vector3 pos, Vector3 targ, Color color, Shader shader)
  103. {
  104. Light light = { 0 };
  105. if (lightsCount < MAX_LIGHTS)
  106. {
  107. light.enabled = true;
  108. light.type = type;
  109. light.position = pos;
  110. light.target = targ;
  111. light.color = color;
  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. enabledName[7] = '0' + lightsCount;
  118. typeName[7] = '0' + lightsCount;
  119. posName[7] = '0' + lightsCount;
  120. targetName[7] = '0' + lightsCount;
  121. colorName[7] = '0' + lightsCount;
  122. light.enabledLoc = GetShaderLocation(shader, enabledName);
  123. light.typeLoc = GetShaderLocation(shader, typeName);
  124. light.posLoc = GetShaderLocation(shader, posName);
  125. light.targetLoc = GetShaderLocation(shader, targetName);
  126. light.colorLoc = GetShaderLocation(shader, colorName);
  127. UpdateLightValues(shader, light);
  128. lightsCount++;
  129. }
  130. return light;
  131. }
  132. // Send to PBR shader light values
  133. void UpdateLightValues(Shader shader, Light light)
  134. {
  135. // Send to shader light enabled state and type
  136. SetShaderValue(shader, light.enabledLoc, &light.enabled, UNIFORM_INT);
  137. SetShaderValue(shader, light.typeLoc, &light.type, UNIFORM_INT);
  138. // Send to shader light position values
  139. float position[3] = { light.position.x, light.position.y, light.position.z };
  140. SetShaderValue(shader, light.posLoc, position, UNIFORM_VEC3);
  141. // Send to shader light target position values
  142. float target[3] = { light.target.x, light.target.y, light.target.z };
  143. SetShaderValue(shader, light.targetLoc, target, UNIFORM_VEC3);
  144. // Send to shader light color values
  145. float diff[4] = { (float)light.color.r/(float)255, (float)light.color.g/(float)255, (float)light.color.b/(float)255, (float)light.color.a/(float)255 };
  146. SetShaderValue(shader, light.colorLoc, diff, UNIFORM_VEC4);
  147. }
  148. #endif // RLIGHTS_IMPLEMENTATION