Skip to content

Commit 344ba04

Browse files
committed
rlights
1 parent c626bb1 commit 344ba04

1 file changed

Lines changed: 170 additions & 0 deletions

File tree

vendor/rlights.h

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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-2024 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+
bool enabled;
49+
Vector3 position;
50+
Vector3 target;
51+
Color color;
52+
float attenuation;
53+
54+
// Shader locations
55+
int enabledLoc;
56+
int typeLoc;
57+
int positionLoc;
58+
int targetLoc;
59+
int colorLoc;
60+
int attenuationLoc;
61+
} Light;
62+
63+
// Light type
64+
typedef enum {
65+
LIGHT_DIRECTIONAL = 0,
66+
LIGHT_POINT
67+
} LightType;
68+
69+
#ifdef __cplusplus
70+
extern "C" { // Prevents name mangling of functions
71+
#endif
72+
73+
//----------------------------------------------------------------------------------
74+
// Module Functions Declaration
75+
//----------------------------------------------------------------------------------
76+
Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader); // Create a light and get shader locations
77+
void UpdateLightValues(Shader shader, Light light); // Send light properties to shader
78+
79+
#ifdef __cplusplus
80+
}
81+
#endif
82+
83+
#endif // RLIGHTS_H
84+
85+
86+
/***********************************************************************************
87+
*
88+
* RLIGHTS IMPLEMENTATION
89+
*
90+
************************************************************************************/
91+
92+
#if defined(RLIGHTS_IMPLEMENTATION)
93+
94+
#include "raylib.h"
95+
96+
//----------------------------------------------------------------------------------
97+
// Defines and Macros
98+
//----------------------------------------------------------------------------------
99+
// ...
100+
101+
//----------------------------------------------------------------------------------
102+
// Types and Structures Definition
103+
//----------------------------------------------------------------------------------
104+
// ...
105+
106+
//----------------------------------------------------------------------------------
107+
// Global Variables Definition
108+
//----------------------------------------------------------------------------------
109+
static int lightsCount = 0; // Current amount of created lights
110+
111+
//----------------------------------------------------------------------------------
112+
// Module Internal Functions Declaration
113+
//----------------------------------------------------------------------------------
114+
// ...
115+
116+
//----------------------------------------------------------------------------------
117+
// Module Functions Definition
118+
//----------------------------------------------------------------------------------
119+
120+
// Create a light and get shader locations
121+
Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader)
122+
{
123+
Light light = { 0 };
124+
125+
if (lightsCount < MAX_LIGHTS)
126+
{
127+
light.enabled = true;
128+
light.type = type;
129+
light.position = position;
130+
light.target = target;
131+
light.color = color;
132+
133+
// NOTE: Lighting shader naming must be the provided ones
134+
light.enabledLoc = GetShaderLocation(shader, TextFormat("lights[%i].enabled", lightsCount));
135+
light.typeLoc = GetShaderLocation(shader, TextFormat("lights[%i].type", lightsCount));
136+
light.positionLoc = GetShaderLocation(shader, TextFormat("lights[%i].position", lightsCount));
137+
light.targetLoc = GetShaderLocation(shader, TextFormat("lights[%i].target", lightsCount));
138+
light.colorLoc = GetShaderLocation(shader, TextFormat("lights[%i].color", lightsCount));
139+
140+
UpdateLightValues(shader, light);
141+
142+
lightsCount++;
143+
}
144+
145+
return light;
146+
}
147+
148+
// Send light properties to shader
149+
// NOTE: Light shader locations should be available
150+
void UpdateLightValues(Shader shader, Light light)
151+
{
152+
// Send to shader light enabled state and type
153+
SetShaderValue(shader, light.enabledLoc, &light.enabled, SHADER_UNIFORM_INT);
154+
SetShaderValue(shader, light.typeLoc, &light.type, SHADER_UNIFORM_INT);
155+
156+
// Send to shader light position values
157+
float position[3] = { light.position.x, light.position.y, light.position.z };
158+
SetShaderValue(shader, light.positionLoc, position, SHADER_UNIFORM_VEC3);
159+
160+
// Send to shader light target position values
161+
float target[3] = { light.target.x, light.target.y, light.target.z };
162+
SetShaderValue(shader, light.targetLoc, target, SHADER_UNIFORM_VEC3);
163+
164+
// Send to shader light color values
165+
float color[4] = { (float)light.color.r/(float)255, (float)light.color.g/(float)255,
166+
(float)light.color.b/(float)255, (float)light.color.a/(float)255 };
167+
SetShaderValue(shader, light.colorLoc, color, SHADER_UNIFORM_VEC4);
168+
}
169+
170+
#endif // RLIGHTS_IMPLEMENTATION

0 commit comments

Comments
 (0)