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