2019-06-14 13:59:40 +03:00
|
|
|
/**********************************************************************************************
|
|
|
|
*
|
|
|
|
* raylib.lights - Some useful functions to deal with lights data
|
|
|
|
*
|
|
|
|
* CONFIGURATION:
|
|
|
|
*
|
|
|
|
* #define RLIGHTS_IMPLEMENTATION
|
|
|
|
* Generates the implementation of the library into the included file.
|
|
|
|
* If not defined, the library is in header only mode and can be included in other headers
|
|
|
|
* or source files without problems. But only ONE file should hold the implementation.
|
|
|
|
*
|
|
|
|
* LICENSE: zlib/libpng
|
|
|
|
*
|
2023-01-01 18:00:56 +03:00
|
|
|
* Copyright (c) 2017-2023 Victor Fisac (@victorfisac) and Ramon Santamaria (@raysan5)
|
2019-06-14 13:59:40 +03:00
|
|
|
*
|
|
|
|
* This software is provided "as-is", without any express or implied warranty. In no event
|
|
|
|
* will the authors be held liable for any damages arising from the use of this software.
|
|
|
|
*
|
|
|
|
* Permission is granted to anyone to use this software for any purpose, including commercial
|
|
|
|
* applications, and to alter it and redistribute it freely, subject to the following restrictions:
|
|
|
|
*
|
|
|
|
* 1. The origin of this software must not be misrepresented; you must not claim that you
|
|
|
|
* wrote the original software. If you use this software in a product, an acknowledgment
|
|
|
|
* in the product documentation would be appreciated but is not required.
|
|
|
|
*
|
|
|
|
* 2. Altered source versions must be plainly marked as such, and must not be misrepresented
|
|
|
|
* as being the original software.
|
|
|
|
*
|
|
|
|
* 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*
|
|
|
|
**********************************************************************************************/
|
|
|
|
|
|
|
|
#ifndef RLIGHTS_H
|
|
|
|
#define RLIGHTS_H
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Defines and Macros
|
|
|
|
//----------------------------------------------------------------------------------
|
2022-06-21 22:06:03 +03:00
|
|
|
#define MAX_LIGHTS 4 // Max dynamic lights supported by shader
|
2019-06-14 13:59:40 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Types and Structures Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Light data
|
|
|
|
typedef struct {
|
|
|
|
int type;
|
2022-06-21 22:06:03 +03:00
|
|
|
bool enabled;
|
2019-06-14 13:59:40 +03:00
|
|
|
Vector3 position;
|
|
|
|
Vector3 target;
|
|
|
|
Color color;
|
2022-06-22 01:35:06 +03:00
|
|
|
float attenuation;
|
2019-06-14 13:59:40 +03:00
|
|
|
|
|
|
|
// Shader locations
|
|
|
|
int enabledLoc;
|
|
|
|
int typeLoc;
|
2022-06-21 22:06:03 +03:00
|
|
|
int positionLoc;
|
2019-06-14 13:59:40 +03:00
|
|
|
int targetLoc;
|
|
|
|
int colorLoc;
|
2022-06-22 01:35:06 +03:00
|
|
|
int attenuationLoc;
|
2019-06-14 13:59:40 +03:00
|
|
|
} Light;
|
|
|
|
|
|
|
|
// Light type
|
|
|
|
typedef enum {
|
2022-06-21 22:06:03 +03:00
|
|
|
LIGHT_DIRECTIONAL = 0,
|
2019-06-14 13:59:40 +03:00
|
|
|
LIGHT_POINT
|
|
|
|
} LightType;
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" { // Prevents name mangling of functions
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module Functions Declaration
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader); // Create a light and get shader locations
|
|
|
|
void UpdateLightValues(Shader shader, Light light); // Send light properties to shader
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // RLIGHTS_H
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************************
|
|
|
|
*
|
|
|
|
* RLIGHTS IMPLEMENTATION
|
|
|
|
*
|
|
|
|
************************************************************************************/
|
|
|
|
|
|
|
|
#if defined(RLIGHTS_IMPLEMENTATION)
|
|
|
|
|
|
|
|
#include "raylib.h"
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Defines and Macros
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// ...
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Types and Structures Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// ...
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Global Variables Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
2020-12-30 00:01:48 +03:00
|
|
|
static int lightsCount = 0; // Current amount of created lights
|
2019-06-14 13:59:40 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module specific Functions Declaration
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// ...
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
// Module Functions Definition
|
|
|
|
//----------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Create a light and get shader locations
|
|
|
|
Light CreateLight(int type, Vector3 position, Vector3 target, Color color, Shader shader)
|
|
|
|
{
|
|
|
|
Light light = { 0 };
|
|
|
|
|
|
|
|
if (lightsCount < MAX_LIGHTS)
|
|
|
|
{
|
|
|
|
light.enabled = true;
|
|
|
|
light.type = type;
|
|
|
|
light.position = position;
|
|
|
|
light.target = target;
|
|
|
|
light.color = color;
|
|
|
|
|
2022-06-21 22:06:03 +03:00
|
|
|
// NOTE: Lighting shader naming must be the provided ones
|
|
|
|
light.enabledLoc = GetShaderLocation(shader, TextFormat("lights[%i].enabled", lightsCount));
|
|
|
|
light.typeLoc = GetShaderLocation(shader, TextFormat("lights[%i].type", lightsCount));
|
|
|
|
light.positionLoc = GetShaderLocation(shader, TextFormat("lights[%i].position", lightsCount));
|
|
|
|
light.targetLoc = GetShaderLocation(shader, TextFormat("lights[%i].target", lightsCount));
|
|
|
|
light.colorLoc = GetShaderLocation(shader, TextFormat("lights[%i].color", lightsCount));
|
2019-06-14 13:59:40 +03:00
|
|
|
|
|
|
|
UpdateLightValues(shader, light);
|
|
|
|
|
|
|
|
lightsCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return light;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send light properties to shader
|
|
|
|
// NOTE: Light shader locations should be available
|
|
|
|
void UpdateLightValues(Shader shader, Light light)
|
|
|
|
{
|
|
|
|
// Send to shader light enabled state and type
|
2021-03-14 13:05:51 +03:00
|
|
|
SetShaderValue(shader, light.enabledLoc, &light.enabled, SHADER_UNIFORM_INT);
|
|
|
|
SetShaderValue(shader, light.typeLoc, &light.type, SHADER_UNIFORM_INT);
|
2019-06-14 13:59:40 +03:00
|
|
|
|
|
|
|
// Send to shader light position values
|
|
|
|
float position[3] = { light.position.x, light.position.y, light.position.z };
|
2022-06-21 22:06:03 +03:00
|
|
|
SetShaderValue(shader, light.positionLoc, position, SHADER_UNIFORM_VEC3);
|
2019-06-14 13:59:40 +03:00
|
|
|
|
|
|
|
// Send to shader light target position values
|
|
|
|
float target[3] = { light.target.x, light.target.y, light.target.z };
|
2021-03-14 13:05:51 +03:00
|
|
|
SetShaderValue(shader, light.targetLoc, target, SHADER_UNIFORM_VEC3);
|
2019-06-14 13:59:40 +03:00
|
|
|
|
|
|
|
// Send to shader light color values
|
|
|
|
float color[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 };
|
2021-03-14 13:05:51 +03:00
|
|
|
SetShaderValue(shader, light.colorLoc, color, SHADER_UNIFORM_VEC4);
|
2019-06-14 13:59:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // RLIGHTS_IMPLEMENTATION
|