From d141ea7054f275b7a0fb59ea6b4e7982054dce2a Mon Sep 17 00:00:00 2001 From: Steven Gann Date: Mon, 13 Apr 2020 14:42:21 -0700 Subject: [PATCH] Implemented Rlights Ported functionality of rlights.h into Rlights.cs, based on API cues in shaders_basic_lighting.cs from Raylib-cs-examples. Most significant differences from Raylib's API are - MAX_LIGHTS is a static field in the Rlights class - CreateLight returns the Light struct instead of void --- Raylib-cs/Raylib-cs.csproj | 1 + Raylib-cs/Rlights.cs | 94 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 Raylib-cs/Rlights.cs diff --git a/Raylib-cs/Raylib-cs.csproj b/Raylib-cs/Raylib-cs.csproj index abcc37d..d7468e7 100644 --- a/Raylib-cs/Raylib-cs.csproj +++ b/Raylib-cs/Raylib-cs.csproj @@ -19,6 +19,7 @@ + diff --git a/Raylib-cs/Rlights.cs b/Raylib-cs/Rlights.cs new file mode 100644 index 0000000..67ab4c1 --- /dev/null +++ b/Raylib-cs/Rlights.cs @@ -0,0 +1,94 @@ +using System; +using static Raylib_cs.Raylib; +using static Raylib_cs.ShaderUniformDataType; +using System.Runtime.InteropServices; + +namespace Raylib_cs +{ + public static class Rlights + { + public static int MAX_LIGHTS = 4; + private static int lightsCount = 0; + + public static Light CreateLight(LightType type, Vector3 pos, Vector3 targ, Color color, Shader shader) + { + Light light = new Light(); + + if (lightsCount < MAX_LIGHTS) + { + light.enabled = true; + light.type = type; + light.position = pos; + light.target = targ; + light.color = color; + + string enabledName = "lights[" + lightsCount + "].enabled"; + string typeName = "lights[" + lightsCount + "].type"; + string posName = "lights[" + lightsCount + "].position"; + string targetName = "lights[" + lightsCount + "].target"; + string colorName = "lights[" + lightsCount + "].color"; + + light.enabledLoc = GetShaderLocation(shader, enabledName); + light.typeLoc = GetShaderLocation(shader, typeName); + light.posLoc = GetShaderLocation(shader, posName); + light.targetLoc = GetShaderLocation(shader, targetName); + light.colorLoc = GetShaderLocation(shader, colorName); + + UpdateLightValues(shader, light); + + //lights[lightsCount] = light; + lightsCount++; + return light; + } + + return default; + } + + public static void UpdateLightValues(Shader shader, Light light) + { + // Send to shader light enabled state and type + IntPtr enabledPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Int32))); + Marshal.WriteInt32(enabledPtr, light.enabled ? ((Int32)1) : ((Int32)0)); + SetShaderValue(shader, light.enabledLoc, enabledPtr, UNIFORM_INT); + + IntPtr lightPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Int32))); + Marshal.WriteInt32(lightPtr, (Int32)light.type); + SetShaderValue(shader, light.typeLoc, lightPtr, UNIFORM_INT); + + // Send to shader light position values + float[] position = new[] { light.position.x, light.position.y, light.position.z }; + + // Send to shader light target position values + float[] target = new[] { light.target.x, light.target.y, light.target.z }; + IntPtr targetPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * target.Length); + Marshal.Copy(target, 0, targetPtr, target.Length); + SetShaderValue(shader, light.targetLoc, targetPtr, UNIFORM_VEC3); + + // Send to shader light color values + float[] diff = new[] { (float)light.color.r / (float)255, (float)light.color.g / (float)255, (float)light.color.b / (float)255, (float)light.color.a / (float)255 }; + IntPtr diffPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(float)) * diff.Length); + Marshal.Copy(diff, 0, diffPtr, diff.Length); + SetShaderValue(shader, light.colorLoc, diffPtr, UNIFORM_VEC4); + } + } + + public enum LightType + { + LIGHT_DIRECTIONAL, + LIGHT_POINT + } + + public struct Light + { + public bool enabled; + public LightType type; + public Vector3 position; + public Vector3 target; + public Color color; + public int enabledLoc; + public int typeLoc; + public int posLoc; + public int targetLoc; + public int colorLoc; + } +} \ No newline at end of file