diff --git a/source/re/gfx/lighting/scene_lights.d b/source/re/gfx/lighting.d similarity index 54% rename from source/re/gfx/lighting/scene_lights.d rename to source/re/gfx/lighting.d index 1d112b6e..bc5ac9eb 100644 --- a/source/re/gfx/lighting/scene_lights.d +++ b/source/re/gfx/lighting.d @@ -1,4 +1,4 @@ -module re.gfx.lighting.scene_lights; +module re.gfx.lighting; import re.core; import re.ecs; @@ -9,16 +9,38 @@ import re.math; import std.algorithm; import std.container.array; static import raylib; -import rlights = re.gfx.lighting.rlights; /// acts as a manager for Light3D components class SceneLightManager : Manager, Updatable { - alias max_lights = rlights.MAX_LIGHTS; - private Array!(rlights.Light) _lights; + /// max lights supported by shader + private enum max_lights = 4; + private Array!(ShaderLight) _lights; private Array!Light3D _comps; private int light_count; + + /// the lighting shader public Shader shader; + private enum ShaderLightType { + LIGHT_DIRECTIONAL, + LIGHT_POINT + } + + private struct ShaderLight { + int type; + Vector3 position; + Vector3 target; + Color color; + bool enabled; + + // Shader locations + int enabledLoc; + int typeLoc; + int posLoc; + int targetLoc; + int colorLoc; + } + this() { // load the shader shader = Core.content.load_shader("shader/basic_lighting.vert", @@ -57,7 +79,7 @@ class SceneLightManager : Manager, Updatable { _lights[i].enabled = _comps[i].light_enabled; // update shader values - rlights.UpdateLightValues(shader, _lights[i]); + update_shader_lights(shader, _lights[i]); } } @@ -70,7 +92,7 @@ class SceneLightManager : Manager, Updatable { private void register(Light3D light_comp) { assert(light_count < max_lights, "maximum light count exceeded."); // add a light - _lights.insertBack(rlights.set_light(light_count, rlights.LightType.LIGHT_POINT, + _lights.insertBack(set_light(light_count, ShaderLightType.LIGHT_POINT, light_comp.transform.position, Vector3Zero, light_comp.color, shader)); _comps.insertBack(light_comp); // set internal light reference @@ -84,7 +106,7 @@ class SceneLightManager : Manager, Updatable { auto removed_index = cast(int) _comps[].countUntil(light_comp); // clear all lights for (int i = 0; i < light_count; i++) { - rlights.clear_light(i, shader); + clear_light(i, shader); } _comps.linearRemove(_comps[].dropExactly(removed_index).takeOne); _lights.linearRemove(_lights[].dropExactly(removed_index).takeOne); @@ -95,19 +117,92 @@ class SceneLightManager : Manager, Updatable { // reactivate the lights for (int i = 0; i < light_count; i++) { // update shader - _lights[i] = rlights.set_light(i, rlights.LightType.LIGHT_POINT, + _lights[i] = set_light(i, ShaderLightType.LIGHT_POINT, _comps[i].transform.position, Vector3Zero, _comps[i].color, shader); // set associated light _comps[i]._light = _lights[i]; } } + + // - ported from rlights + + private static ShaderLight set_light(int index, ShaderLightType type, + Vector3 pos, Vector3 target, Color color, Shader shader, bool enabled = true) { + ShaderLight light; + + light.enabled = enabled; + light.type = type; + light.position = pos; + light.target = target; + light.color = color; + + char[32] enabledName = "lights[x].enabled\0"; + char[32] typeName = "lights[x].type\0"; + char[32] posName = "lights[x].position\0"; + char[32] targetName = "lights[x].target\0"; + char[32] colorName = "lights[x].color\0"; + + // Set location name [x] depending on lights count + enabledName[7] = cast(char)('0' + index); + typeName[7] = cast(char)('0' + index); + posName[7] = cast(char)('0' + index); + targetName[7] = cast(char)('0' + index); + colorName[7] = cast(char)('0' + index); + + light.enabledLoc = raylib.GetShaderLocation(shader, cast(char*) enabledName); + light.typeLoc = raylib.GetShaderLocation(shader, cast(char*) typeName); + light.posLoc = raylib.GetShaderLocation(shader, cast(char*) posName); + light.targetLoc = raylib.GetShaderLocation(shader, cast(char*) targetName); + light.colorLoc = raylib.GetShaderLocation(shader, cast(char*) colorName); + + update_shader_lights(shader, light); + + return light; + } + + private static void clear_light(int index, Shader shader) { + // reset the light + set_light(index, ShaderLightType.LIGHT_POINT, Vector3Zero, Vector3Zero, + Colors.BLANK, shader, false); + } + + // Send light properties to shader + // NOTE: ShaderLight shader locations should be available + private static void update_shader_lights(Shader shader, ShaderLight light) { + // Send to shader light enabled state and type + raylib.SetShaderValue(shader, light.enabledLoc, &light.enabled, + raylib.ShaderUniformDataType.UNIFORM_INT); + raylib.SetShaderValue(shader, light.typeLoc, &light.type, + raylib.ShaderUniformDataType.UNIFORM_INT); + + // Send to shader light position values + float[3] position = [ + light.position.x, light.position.y, light.position.z + ]; + raylib.SetShaderValue(shader, light.posLoc, &position, + raylib.ShaderUniformDataType.UNIFORM_VEC3); + + // Send to shader light target position values + float[3] target = [light.target.x, light.target.y, light.target.z]; + raylib.SetShaderValue(shader, light.targetLoc, &target, + raylib.ShaderUniformDataType.UNIFORM_VEC3); + + // Send to shader light color values + float[4] color = [ + cast(float) light.color.r / cast(float) 255, + cast(float) light.color.g / cast(float) 255, + cast(float) light.color.b / cast(float) 255, + cast(float) light.color.a / cast(float) 255 + ]; + raylib.SetShaderValue(shader, light.colorLoc, &color, raylib.ShaderUniformDataType.UNIFORM_VEC4); + } } /// represents a 3D light class Light3D : Component, Renderable3D { private SceneLightManager _mgr; private enum phys_size = 0.2; - private rlights.Light _light; + private SceneLightManager.ShaderLight _light; /// the color of the light public Color color; diff --git a/source/re/gfx/lighting/package.d b/source/re/gfx/lighting/package.d deleted file mode 100644 index 817cdcad..00000000 --- a/source/re/gfx/lighting/package.d +++ /dev/null @@ -1,3 +0,0 @@ -module re.gfx.lighting; - -public import re.gfx.lighting.scene_lights; \ No newline at end of file diff --git a/source/re/gfx/lighting/rlights.d b/source/re/gfx/lighting/rlights.d deleted file mode 100644 index eda8427c..00000000 --- a/source/re/gfx/lighting/rlights.d +++ /dev/null @@ -1,128 +0,0 @@ -module re.gfx.lighting.rlights; - -import raylib; - -/********************************************************************************************** -* -* raylib.lights - Some useful functions to deal with lights data -* -* CONFIGURATION: -* -* enum 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 -* -* Copyright (c) 2017 Victor Fisac and Ramon Santamaria -* -* 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. -* -**********************************************************************************************/ - -//---------------------------------------------------------------------------------- -// Defines and Macros -//---------------------------------------------------------------------------------- -enum MAX_LIGHTS = 4; // max lights supported by shader - -//---------------------------------------------------------------------------------- -// Types and Structures Definition -//---------------------------------------------------------------------------------- -enum LightType { - LIGHT_DIRECTIONAL, - LIGHT_POINT -} - -struct Light { - int type; - Vector3 position; - Vector3 target; - Color color; - bool enabled; - - // Shader locations - int enabledLoc; - int typeLoc; - int posLoc; - int targetLoc; - int colorLoc; -} - -static Light set_light(int index, LightType type, Vector3 pos, Vector3 target, - Color color, Shader shader, bool enabled = true) { - Light light; - - light.enabled = enabled; - light.type = type; - light.position = pos; - light.target = target; - light.color = color; - - char[32] enabledName = "lights[x].enabled\0"; - char[32] typeName = "lights[x].type\0"; - char[32] posName = "lights[x].position\0"; - char[32] targetName = "lights[x].target\0"; - char[32] colorName = "lights[x].color\0"; - - // Set location name [x] depending on lights count - enabledName[7] = cast(char)('0' + index); - typeName[7] = cast(char)('0' + index); - posName[7] = cast(char)('0' + index); - targetName[7] = cast(char)('0' + index); - colorName[7] = cast(char)('0' + index); - - light.enabledLoc = GetShaderLocation(shader, cast(char*) enabledName); - light.typeLoc = GetShaderLocation(shader, cast(char*) typeName); - light.posLoc = GetShaderLocation(shader, cast(char*) posName); - light.targetLoc = GetShaderLocation(shader, cast(char*) targetName); - light.colorLoc = GetShaderLocation(shader, cast(char*) colorName); - - UpdateLightValues(shader, light); - - return light; -} - -static void clear_light(int index, Shader shader) { - // reset the light - set_light(index, LightType.LIGHT_POINT, Vector3Zero, Vector3Zero, Colors.BLANK, shader, false); -} - -// 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 - SetShaderValue(shader, light.enabledLoc, &light.enabled, - raylib.ShaderUniformDataType.UNIFORM_INT); - SetShaderValue(shader, light.typeLoc, &light.type, raylib.ShaderUniformDataType.UNIFORM_INT); - - // Send to shader light position values - float[3] position = [light.position.x, light.position.y, light.position.z]; - SetShaderValue(shader, light.posLoc, &position, raylib.ShaderUniformDataType.UNIFORM_VEC3); - - // Send to shader light target position values - float[3] target = [light.target.x, light.target.y, light.target.z]; - SetShaderValue(shader, light.targetLoc, &target, raylib.ShaderUniformDataType.UNIFORM_VEC3); - - // Send to shader light color values - float[4] color = [ - cast(float) light.color.r / cast(float) 255, - cast(float) light.color.g / cast(float) 255, - cast(float) light.color.b / cast(float) 255, - cast(float) light.color.a / cast(float) 255 - ]; - SetShaderValue(shader, light.colorLoc, &color, raylib.ShaderUniformDataType.UNIFORM_VEC4); -}