Skip to content

Commit

Permalink
refactor rlights into main lighting class
Browse files Browse the repository at this point in the history
  • Loading branch information
redthing1 committed Jul 30, 2020
1 parent f0de688 commit 5d6228a
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 140 deletions.
113 changes: 104 additions & 9 deletions source/re/gfx/lighting/scene_lights.d → source/re/gfx/lighting.d
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module re.gfx.lighting.scene_lights;
module re.gfx.lighting;

import re.core;
import re.ecs;
Expand All @@ -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",
Expand Down Expand Up @@ -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]);
}
}

Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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;
Expand Down
3 changes: 0 additions & 3 deletions source/re/gfx/lighting/package.d

This file was deleted.

128 changes: 0 additions & 128 deletions source/re/gfx/lighting/rlights.d

This file was deleted.

0 comments on commit 5d6228a

Please sign in to comment.