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.

174 lines
6.6 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. #include "raylib.h"
  35. //----------------------------------------------------------------------------------
  36. // Defines and Macros
  37. //----------------------------------------------------------------------------------
  38. #define MAX_LIGHTS 4 // Max lights supported by shader
  39. #define LIGHT_DISTANCE 3.5f // Light distance from world center
  40. #define LIGHT_HEIGHT 1.0f // Light height position
  41. //----------------------------------------------------------------------------------
  42. // Types and Structures Definition
  43. //----------------------------------------------------------------------------------
  44. typedef enum {
  45. LIGHT_DIRECTIONAL,
  46. LIGHT_POINT
  47. } LightType;
  48. typedef struct {
  49. bool enabled;
  50. LightType type;
  51. Vector3 position;
  52. Vector3 target;
  53. Color color;
  54. int enabledLoc;
  55. int typeLoc;
  56. int posLoc;
  57. int targetLoc;
  58. int colorLoc;
  59. } Light;
  60. #ifdef __cplusplus
  61. extern "C" { // Prevents name mangling of functions
  62. #endif
  63. //----------------------------------------------------------------------------------
  64. // Module Functions Declaration
  65. //----------------------------------------------------------------------------------
  66. void CreateLight(int type, Vector3 pos, Vector3 targ, Color color, Shader shader); // Defines a light and get locations from PBR shader
  67. void UpdateLightValues(Shader shader, Light light); // Send to PBR shader light values
  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 Light lights[MAX_LIGHTS] = { 0 };
  91. static int lightsCount = 0; // Current amount of created lights
  92. //----------------------------------------------------------------------------------
  93. // Module specific Functions Declaration
  94. //----------------------------------------------------------------------------------
  95. // ...
  96. //----------------------------------------------------------------------------------
  97. // Module Functions Definition
  98. //----------------------------------------------------------------------------------
  99. // Defines a light and get locations from PBR shader
  100. void CreateLight(int type, Vector3 pos, Vector3 targ, Color color, Shader shader)
  101. {
  102. Light light = { 0 };
  103. if (lightsCount < MAX_LIGHTS)
  104. {
  105. light.enabled = true;
  106. light.type = type;
  107. light.position = pos;
  108. light.target = targ;
  109. light.color = color;
  110. char enabledName[32] = "lights[x].enabled\0";
  111. char typeName[32] = "lights[x].type\0";
  112. char posName[32] = "lights[x].position\0";
  113. char targetName[32] = "lights[x].target\0";
  114. char colorName[32] = "lights[x].color\0";
  115. enabledName[7] = '0' + lightsCount;
  116. typeName[7] = '0' + lightsCount;
  117. posName[7] = '0' + lightsCount;
  118. targetName[7] = '0' + lightsCount;
  119. colorName[7] = '0' + lightsCount;
  120. light.enabledLoc = GetShaderLocation(shader, enabledName);
  121. light.typeLoc = GetShaderLocation(shader, typeName);
  122. light.posLoc = GetShaderLocation(shader, posName);
  123. light.targetLoc = GetShaderLocation(shader, targetName);
  124. light.colorLoc = GetShaderLocation(shader, colorName);
  125. UpdateLightValues(shader, light);
  126. lights[lightsCount] = light;
  127. lightsCount++;
  128. }
  129. }
  130. // Send to PBR shader light values
  131. void UpdateLightValues(Shader shader, Light light)
  132. {
  133. // Send to shader light enabled state and type
  134. SetShaderValue(shader, light.enabledLoc, &light.enabled, UNIFORM_INT);
  135. SetShaderValue(shader, light.typeLoc, &light.type, UNIFORM_INT);
  136. // Send to shader light position values
  137. float position[3] = { light.position.x, light.position.y, light.position.z };
  138. SetShaderValue(shader, light.posLoc, position, UNIFORM_VEC3);
  139. // Send to shader light target position values
  140. float target[3] = { light.target.x, light.target.y, light.target.z };
  141. SetShaderValue(shader, light.targetLoc, target, UNIFORM_VEC3);
  142. // Send to shader light color values
  143. 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 };
  144. SetShaderValue(shader, light.colorLoc, diff, UNIFORM_VEC4);
  145. }
  146. #endif // RLIGHTS_IMPLEMENTATION