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 | |
33 | #ifndef RLIGHTS_H |
34 | #define RLIGHTS_H |
35 | |
36 | #include "raylib.h" |
37 | |
38 | //---------------------------------------------------------------------------------- |
39 | // Defines and Macros |
40 | //---------------------------------------------------------------------------------- |
41 | #define MAX_LIGHTS 4 // Max lights supported by shader |
42 | #define LIGHT_DISTANCE 3.5f // Light distance from world center |
43 | #define LIGHT_HEIGHT 1.0f // Light height position |
44 | |
45 | //---------------------------------------------------------------------------------- |
46 | // Types and Structures Definition |
47 | //---------------------------------------------------------------------------------- |
48 | typedef enum { |
49 | LIGHT_DIRECTIONAL, |
50 | LIGHT_POINT |
51 | } LightType; |
52 | |
53 | typedef struct { |
54 | bool enabled; |
55 | LightType type; |
56 | Vector3 position; |
57 | Vector3 target; |
58 | Color color; |
59 | int enabledLoc; |
60 | int typeLoc; |
61 | int posLoc; |
62 | int targetLoc; |
63 | int colorLoc; |
64 | } Light; |
65 | |
66 | #ifdef __cplusplus |
67 | extern "C" { // Prevents name mangling of functions |
68 | #endif |
69 | |
70 | //---------------------------------------------------------------------------------- |
71 | // Module Functions Declaration |
72 | //---------------------------------------------------------------------------------- |
73 | void CreateLight(int type, Vector3 pos, Vector3 targ, Color color, Shader shader); // Defines a light and get locations from PBR shader |
74 | void UpdateLightValues(Shader shader, Light light); // Send to PBR shader light values |
75 | |
76 | #ifdef __cplusplus |
77 | } |
78 | #endif |
79 | |
80 | #endif // RLIGHTS_H |
81 | |
82 | |
83 | /*********************************************************************************** |
84 | * |
85 | * RLIGHTS IMPLEMENTATION |
86 | * |
87 | ************************************************************************************/ |
88 | |
89 | #if defined(RLIGHTS_IMPLEMENTATION) |
90 | |
91 | #include "raylib.h" |
92 | |
93 | //---------------------------------------------------------------------------------- |
94 | // Defines and Macros |
95 | //---------------------------------------------------------------------------------- |
96 | // ... |
97 | |
98 | //---------------------------------------------------------------------------------- |
99 | // Types and Structures Definition |
100 | //---------------------------------------------------------------------------------- |
101 | // ... |
102 | |
103 | //---------------------------------------------------------------------------------- |
104 | // Global Variables Definition |
105 | //---------------------------------------------------------------------------------- |
106 | static Light lights[MAX_LIGHTS] = { 0 }; |
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 | // Defines a light and get locations from PBR shader |
119 | void CreateLight(int type, Vector3 pos, Vector3 targ, 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 = pos; |
128 | light.target = targ; |
129 | light.color = color; |
130 | |
131 | char enabledName[32] = "lights[x].enabled\0" ; |
132 | char typeName[32] = "lights[x].type\0" ; |
133 | char posName[32] = "lights[x].position\0" ; |
134 | char targetName[32] = "lights[x].target\0" ; |
135 | char colorName[32] = "lights[x].color\0" ; |
136 | enabledName[7] = '0' + lightsCount; |
137 | typeName[7] = '0' + lightsCount; |
138 | posName[7] = '0' + lightsCount; |
139 | targetName[7] = '0' + lightsCount; |
140 | colorName[7] = '0' + lightsCount; |
141 | |
142 | light.enabledLoc = GetShaderLocation(shader, enabledName); |
143 | light.typeLoc = GetShaderLocation(shader, typeName); |
144 | light.posLoc = GetShaderLocation(shader, posName); |
145 | light.targetLoc = GetShaderLocation(shader, targetName); |
146 | light.colorLoc = GetShaderLocation(shader, colorName); |
147 | |
148 | UpdateLightValues(shader, light); |
149 | |
150 | lights[lightsCount] = light; |
151 | lightsCount++; |
152 | } |
153 | } |
154 | |
155 | // Send to PBR shader light values |
156 | void UpdateLightValues(Shader shader, Light light) |
157 | { |
158 | // Send to shader light enabled state and type |
159 | SetShaderValue(shader, light.enabledLoc, &light.enabled, UNIFORM_INT); |
160 | SetShaderValue(shader, light.typeLoc, &light.type, UNIFORM_INT); |
161 | |
162 | // Send to shader light position values |
163 | float position[3] = { light.position.x, light.position.y, light.position.z }; |
164 | SetShaderValue(shader, light.posLoc, position, UNIFORM_VEC3); |
165 | |
166 | // Send to shader light target position values |
167 | float target[3] = { light.target.x, light.target.y, light.target.z }; |
168 | SetShaderValue(shader, light.targetLoc, target, UNIFORM_VEC3); |
169 | |
170 | // Send to shader light color values |
171 | 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 }; |
172 | SetShaderValue(shader, light.colorLoc, diff, UNIFORM_VEC4); |
173 | } |
174 | |
175 | #endif // RLIGHTS_IMPLEMENTATION |