Updated lighting system...

...to avoid dynamic conditions on for loop (lightsCount) on standard
shader, it seems GLSL 100 doesn't support that feature... on some GPUs
like RaspberryPi...
This commit is contained in:
raysan5 2016-07-06 20:33:46 +02:00
parent e2a3a52ad6
commit 7cefbd8a94
4 changed files with 109 additions and 96 deletions

View file

@ -431,8 +431,8 @@ typedef struct Model {
// Light type
typedef struct LightData {
unsigned int id; // Light unique id
int type; // Light type: LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT
bool enabled; // Light enabled
int type; // Light type: LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT
Vector3 position; // Light position
Vector3 target; // Light target: LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction target)

View file

@ -260,7 +260,7 @@ static bool texCompASTCSupported = false; // ASTC texture compression support
// Lighting data
static Light lights[MAX_LIGHTS]; // Lights pool
static int lightsCount; // Enabled lights counter
static int lightsCount = 0; // Enabled lights counter
#endif
#if defined(RLGL_OCULUS_SUPPORT)
@ -2454,6 +2454,8 @@ Light CreateLight(int type, Vector3 position, Color diffuse)
Light light = NULL;
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if (lightsCount < MAX_LIGHTS)
{
// Allocate dynamic memory
light = (Light)malloc(sizeof(LightData));
@ -2472,6 +2474,8 @@ Light CreateLight(int type, Vector3 position, Color diffuse)
// Increase enabled lights count
lightsCount++;
}
else TraceLog(WARNING, "Too many lights, only supported up to %i lights", MAX_LIGHTS);
#else
// TODO: Support OpenGL 1.1 lighting system
TraceLog(WARNING, "Lighting currently not supported on OpenGL 1.1");
@ -2484,6 +2488,8 @@ Light CreateLight(int type, Vector3 position, Color diffuse)
void DestroyLight(Light light)
{
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
if (light != NULL)
{
// Free dynamic memory allocation
free(lights[light->id]);
@ -2501,6 +2507,7 @@ void DestroyLight(Light light)
// Decrease enabled physic objects count
lightsCount--;
}
#endif
}
@ -3625,15 +3632,15 @@ static void UnloadDefaultBuffers(void)
// NOTE: It would be far easier with shader UBOs but are not supported on OpenGL ES 2.0f
static void SetShaderLights(Shader shader)
{
int locPoint = glGetUniformLocation(shader.id, "lightsCount");
glUniform1i(locPoint, lightsCount);
int locPoint = -1;
char locName[32] = "lights[x].position\0";
for (int i = 0; i < lightsCount; i++)
for (int i = 0; i < MAX_LIGHTS; i++)
{
locName[7] = '0' + i;
if (lights[i] != NULL) // Only upload registered lights data
{
memcpy(&locName[10], "enabled\0", strlen("enabled\0") + 1);
locPoint = GetShaderLocation(shader, locName);
glUniform1i(locPoint, lights[i]->enabled);
@ -3692,6 +3699,13 @@ static void SetShaderLights(Shader shader)
// TODO: Pass to the shader any other required data from LightData struct
}
else // Not enabled lights
{
memcpy(&locName[10], "enabled\0", strlen("enabled\0") + 1);
locPoint = GetShaderLocation(shader, locName);
glUniform1i(locPoint, 0);
}
}
}
// Read text data from file

View file

@ -218,8 +218,8 @@ typedef enum { OPENGL_11 = 1, OPENGL_21, OPENGL_33, OPENGL_ES_20 } GlVersion;
// Light type
typedef struct LightData {
unsigned int id; // Light unique id
int type; // Light type: LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT
bool enabled; // Light enabled
int type; // Light type: LIGHT_POINT, LIGHT_DIRECTIONAL, LIGHT_SPOT
Vector3 position; // Light position
Vector3 target; // Light target: LIGHT_DIRECTIONAL and LIGHT_SPOT (cone direction target)

View file

@ -78,7 +78,6 @@ static const char fStandardShaderStr[] =
" float radius; \n"
" float coneAngle; }; \n"
"const int maxLights = 8; \n"
"uniform int lightsCount; \n"
"uniform Light lights[maxLights]; \n"
"\n"
"vec3 CalcPointLight(Light l, vec3 n, vec3 v, float s) \n"
@ -157,7 +156,7 @@ static const char fStandardShaderStr[] =
#elif defined(GRAPHICS_API_OPENGL_33)
" if (useSpecular == 1) spec *= normalize(texture(texture2, fragTexCoord).r);\n"
#endif
" for (int i = 0; i < lightsCount; i++)\n"
" for (int i = 0; i < maxLights; i++)\n"
" {\n"
" if (lights[i].enabled == 1)\n"
" {\n"